From e6e23669eea8768ec004c16437c67717b66fb1a0 Mon Sep 17 00:00:00 2001 From: Simon Robertshaw Date: Fri, 22 Jun 2012 15:13:24 +0100 Subject: [PATCH] Scrollable comments --- src/preview/Comment.h | 12 +++- src/preview/PreviewView.cpp | 138 +++++++++++++++++++++++++++++------- src/preview/PreviewView.h | 9 +++ 3 files changed, 132 insertions(+), 27 deletions(-) diff --git a/src/preview/Comment.h b/src/preview/Comment.h index c9a807c2f..bf4b8d441 100644 --- a/src/preview/Comment.h +++ b/src/preview/Comment.h @@ -8,6 +8,8 @@ #ifndef COMMENT_H_ #define COMMENT_H_ +#include + class SaveComment { public: @@ -15,7 +17,15 @@ public: std::string authorName; std::string comment; SaveComment(int userID, std::string username, std::string commentText): - authorID(userID), authorName(username), comment(commentText) + authorID(userID), authorName(username), comment(commentText) + { + } + SaveComment(const SaveComment & comment): + authorID(comment.authorID), authorName(comment.authorName), comment(comment.comment) + { + } + SaveComment(const SaveComment * comment): + authorID(comment->authorID), authorName(comment->authorName), comment(comment->comment) { } }; diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp index d0d0d21f8..cc49f91fc 100644 --- a/src/preview/PreviewView.cpp +++ b/src/preview/PreviewView.cpp @@ -6,6 +6,7 @@ */ #include +#include #include "PreviewView.h" #include "dialogues/TextPrompt.h" #include "simulation/SaveRenderer.h" @@ -17,7 +18,10 @@ PreviewView::PreviewView(): ui::Window(ui::Point(-1, -1), ui::Point((XRES/2)+200, (YRES/2)+150)), savePreview(NULL), - doOpen(false) + doOpen(false), + commentsOffset(0), + commentsVel(0), + maxOffset(0) { class OpenAction: public ui::ButtonAction { @@ -165,6 +169,31 @@ void PreviewView::OnDraw() void PreviewView::OnTick(float dt) { + if(commentsVel > 5.0f) commentsVel = 5.0f; + if(commentsVel < -5.0f) commentsVel = -5.0f; + if(commentsVel > -0.5f && commentsVel < 0.5) + commentsVel = 0; + + int oldOffset = commentsOffset; + commentsOffset += commentsVel; + + commentsVel*=0.99f; + + if(oldOffset!=int(commentsOffset)) + { + if(commentsOffset<0) + { + commentsOffset = 0; + commentsVel = 0; + } + if(commentsOffset>maxOffset) + { + commentsOffset = maxOffset; + commentsVel = 0; + } + displayComments(commentsOffset); + } + c->Update(); } @@ -221,7 +250,7 @@ void PreviewView::NotifySaveChanged(PreviewModel * sender) } } -void PreviewView::NotifyCommentsChanged(PreviewModel * sender) +void PreviewView::displayComments(int yOffset) { for(int i = 0; i < commentComponents.size(); i++) { @@ -231,40 +260,97 @@ void PreviewView::NotifyCommentsChanged(PreviewModel * sender) commentComponents.clear(); commentTextComponents.clear(); - int currentY = 0; + int currentY = -yOffset; ui::Label * tempUsername; ui::Textblock * tempComment; - std::vector * tempComments = sender->GetComments(); - if(tempComments) + for(int i = 0; i < comments.size(); i++) { - for(int i = 0; i < tempComments->size(); i++) - { - tempUsername = new ui::Label(ui::Point((XRES/2) + 5, currentY+5), ui::Point(Size.X-((XRES/2) + 10), 16), tempComments->at(i)->authorName); - tempUsername->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; tempUsername->Appearance.VerticalAlign = ui::Appearance::AlignBottom; - currentY += 16; - tempComment = new ui::Textblock(ui::Point((XRES/2) + 5, currentY+5), ui::Point(Size.X-((XRES/2) + 10), -1), tempComments->at(i)->comment); - tempComment->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; tempComment->Appearance.VerticalAlign = ui::Appearance::AlignTop; - tempComment->SetTextColour(ui::Colour(180, 180, 180)); - currentY += tempComment->Size.Y+4; + int usernameY = currentY+5, commentY; + tempUsername = new ui::Label(ui::Point((XRES/2) + 5, currentY+5), ui::Point(Size.X-((XRES/2) + 10), 16), comments[i].authorName); + tempUsername->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; tempUsername->Appearance.VerticalAlign = ui::Appearance::AlignBottom; + currentY += 16; + if(currentY > Size.Y || usernameY < 0) + { + delete tempUsername; if(currentY > Size.Y) - { - delete tempUsername; - delete tempComment; break; - } - else - { - commentComponents.push_back(tempComment); - AddComponent(tempComment); - commentComponents.push_back(tempUsername); - AddComponent(tempUsername); - commentTextComponents.push_back(tempComment); - } + } + else + { + commentComponents.push_back(tempUsername); + AddComponent(tempUsername); + } + + commentY = currentY+5; + tempComment = new ui::Textblock(ui::Point((XRES/2) + 5, currentY+5), ui::Point(Size.X-((XRES/2) + 10), -1), comments[i].comment); + tempComment->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; tempComment->Appearance.VerticalAlign = ui::Appearance::AlignTop; + tempComment->SetTextColour(ui::Colour(180, 180, 180)); + currentY += tempComment->Size.Y+4; + + if(currentY > Size.Y || commentY < 0) + { + delete tempComment; + if(currentY > Size.Y) + break; + } + else + { + commentComponents.push_back(tempComment); + AddComponent(tempComment); + commentTextComponents.push_back(tempComment); } } } +void PreviewView::NotifyCommentsChanged(PreviewModel * sender) +{ + if(sender->GetComments()) + { + comments = std::vector(sender->GetComments()->begin(), sender->GetComments()->end()); + } + else + { + comments.clear(); + } + + ui::Label * tempUsername; + ui::Textblock * tempComment; + int maxY = 0; + for(int i = 0; i < comments.size(); i++) + { + tempUsername = new ui::Label(ui::Point(0, 0), ui::Point(Size.X-((XRES/2) + 10), 16), comments[i].authorName); + tempUsername->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + tempUsername->Appearance.VerticalAlign = ui::Appearance::AlignBottom; + maxY += 16; + tempComment = new ui::Textblock(ui::Point(0, 0), ui::Point(Size.X-((XRES/2) + 10), -1), comments[i].comment); + tempComment->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; + tempComment->Appearance.VerticalAlign = ui::Appearance::AlignTop; + tempComment->SetTextColour(ui::Colour(180, 180, 180)); + maxY += tempComment->Size.Y+4; + + delete tempUsername; + delete tempComment; + } + + + maxOffset = (maxY-Size.Y)+16; + commentsOffset = 0; + commentsVel = 0; + displayComments(commentsOffset); +} + +void PreviewView::OnMouseWheel(int x, int y, int d) +{ + commentsVel-=d; + /*if(!d) + return; + if(d<0) + c->NextPage(); + else + c->PrevPage();*/ +} + /*void PreviewView::NotifyPreviewChanged(PreviewModel * sender) { savePreview = sender->GetGameSave(); diff --git a/src/preview/PreviewView.h b/src/preview/PreviewView.h index 011f86494..33510a2de 100644 --- a/src/preview/PreviewView.h +++ b/src/preview/PreviewView.h @@ -9,6 +9,7 @@ #define PREVIEWVIEW_H_ #include +#include "Comment.h" #include "interface/Window.h" #include "preview/PreviewController.h" #include "preview/PreviewModel.h" @@ -29,11 +30,18 @@ class PreviewView: public ui::Window { ui::Label * saveNameLabel; ui::Label * authorDateLabel; ui::Textblock * saveDescriptionTextblock; + std::vector comments; std::vector commentComponents; std::vector commentTextComponents; int votesUp; int votesDown; bool doOpen; + + int maxOffset; + float commentsOffset; + float commentsVel; + + void displayComments(int yOffset); public: void AttachController(PreviewController * controller) { c = controller;} PreviewView(); @@ -43,6 +51,7 @@ public: virtual void DoDraw(); virtual void OnTick(float dt); virtual void OnMouseDown(int x, int y, unsigned button); + virtual void OnMouseWheel(int x, int y, int d); virtual ~PreviewView(); };