From 05fc39e40faa817cdbf3e50de94d28a5ef9349dc Mon Sep 17 00:00:00 2001 From: jacob1 Date: Fri, 14 Dec 2012 13:34:00 -0500 Subject: [PATCH] a working scrollbar in the save preview. Also, fix the bug where you couldn't go back up a page when there weren't enough comments to fill a page --- src/interface/ScrollPanel.cpp | 62 ++++++++++++++++++++++++++++++++--- src/interface/ScrollPanel.h | 7 ++++ src/preview/PreviewModel.cpp | 1 - src/preview/PreviewView.cpp | 6 ++-- 4 files changed, 67 insertions(+), 9 deletions(-) diff --git a/src/interface/ScrollPanel.cpp b/src/interface/ScrollPanel.cpp index c2d4f66c3..74f6b62ab 100644 --- a/src/interface/ScrollPanel.cpp +++ b/src/interface/ScrollPanel.cpp @@ -10,17 +10,21 @@ ScrollPanel::ScrollPanel(Point position, Point size): offsetY(0), yScrollVel(0.0f), xScrollVel(0.0f), - scrollBarWidth(0) + scrollBarWidth(0), + isMouseInsideScrollbar(false), + scrollbarSelected(false), + scrollbarInitialYOffset(0), + scrollbarInitialYClick(0) { } int ScrollPanel::GetScrollLimit() { - if(maxOffset.Y == -ViewportPosition.Y) - return 1; - else if(ViewportPosition.Y == 0) + if(ViewportPosition.Y == 0) return -1; + else if(maxOffset.Y == -ViewportPosition.Y) + return 1; return 0; } @@ -52,6 +56,54 @@ void ScrollPanel::Draw(const Point& screenPos) } } +void ScrollPanel::XOnMouseClick(int x, int y, unsigned int button) +{ + if (isMouseInsideScrollbar) + { + scrollbarSelected = true; + scrollbarInitialYOffset = offsetY; + scrollbarInitialYClick = y; + } +} + +void ScrollPanel::XOnMouseUp(int x, int y, unsigned int button) +{ + scrollbarSelected = false; +} + +void ScrollPanel::XOnMouseMoved(int x, int y, int dx, int dy) +{ + if(maxOffset.Y>0 && InnerSize.Y>0) + { + float scrollHeight = float(Size.Y)*(float(Size.Y)/float(InnerSize.Y)); + float scrollPos = 0; + if(-ViewportPosition.Y>0) + { + scrollPos = float(Size.Y-scrollHeight)*(float(offsetY)/float(maxOffset.Y)); + } + + if (scrollbarSelected) + { + if (x > 0) + { + int scrollY = float(y-scrollbarInitialYClick)/float(Size.Y)*float(InnerSize.Y)+scrollbarInitialYOffset; + ViewportPosition.Y = -scrollY; + offsetY = scrollY; + } + else + { + ViewportPosition.Y = -scrollbarInitialYOffset; + offsetY = scrollbarInitialYOffset; + } + } + + if (x > (Size.X-scrollBarWidth) && x < (Size.X-scrollBarWidth)+scrollBarWidth && y > scrollPos && y < scrollPos+scrollHeight) + isMouseInsideScrollbar = true; + else + isMouseInsideScrollbar = false; + } +} + void ScrollPanel::XTick(float dt) { //if(yScrollVel > 7.0f) yScrollVel = 7.0f; @@ -116,6 +168,6 @@ void ScrollPanel::XTick(float dt) if(mouseInside && scrollBarWidth < 6) scrollBarWidth++; - else if(!mouseInside && scrollBarWidth > 0) + else if(!mouseInside && scrollBarWidth > 0 && !scrollbarSelected) scrollBarWidth--; } \ No newline at end of file diff --git a/src/interface/ScrollPanel.h b/src/interface/ScrollPanel.h index fc54b3135..c26c42084 100644 --- a/src/interface/ScrollPanel.h +++ b/src/interface/ScrollPanel.h @@ -13,6 +13,10 @@ namespace ui float offsetY; float yScrollVel; float xScrollVel; + bool isMouseInsideScrollbar; + bool scrollbarSelected; + int scrollbarInitialYOffset; + int scrollbarInitialYClick; public: ScrollPanel(Point position, Point size); @@ -21,5 +25,8 @@ namespace ui virtual void Draw(const Point& screenPos); virtual void XTick(float dt); virtual void XOnMouseWheelInside(int localx, int localy, int d); + virtual void XOnMouseClick(int localx, int localy, unsigned int button); + virtual void XOnMouseUp(int x, int y, unsigned int button); + virtual void XOnMouseMoved(int localx, int localy, int dx, int dy); }; } \ No newline at end of file diff --git a/src/preview/PreviewModel.cpp b/src/preview/PreviewModel.cpp index 20f6bfd30..399fedf68 100644 --- a/src/preview/PreviewModel.cpp +++ b/src/preview/PreviewModel.cpp @@ -63,7 +63,6 @@ void * PreviewModel::updateSaveDataT() void * PreviewModel::updateSaveCommentsT() { - //Haha, j/k std::vector * tempComments = Client::Ref().GetComments(tSaveID, (commentsPageNumber-1)*20, 20); updateSaveCommentsFinished = true; return tempComments; diff --git a/src/preview/PreviewView.cpp b/src/preview/PreviewView.cpp index 445142ae0..d6c444858 100644 --- a/src/preview/PreviewView.cpp +++ b/src/preview/PreviewView.cpp @@ -53,7 +53,7 @@ public: }; PreviewView::PreviewView(): - ui::Window(ui::Point(-1, -1), ui::Point((XRES/2)+200, (YRES/2)+150)), + ui::Window(ui::Point(-1, -1), ui::Point((XRES/2)+210, (YRES/2)+150)), savePreview(NULL), doOpen(false), addCommentBox(NULL), @@ -514,7 +514,7 @@ void PreviewView::NotifyCommentsChanged(PreviewModel * sender) for(int i = 0; i < comments.size(); i++) { int usernameY = currentY+5, commentY; - tempUsername = new ui::Label(ui::Point(5, currentY+5), ui::Point(Size.X-((XRES/2) + 10), 16), comments[i].authorName); + tempUsername = new ui::Label(ui::Point(5, currentY+5), ui::Point(Size.X-((XRES/2) + 13), 16), comments[i].authorName); tempUsername->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; tempUsername->Appearance.VerticalAlign = ui::Appearance::AlignBottom; currentY += 16; @@ -524,7 +524,7 @@ void PreviewView::NotifyCommentsChanged(PreviewModel * sender) commentY = currentY+5; - tempComment = new ui::Label(ui::Point(5, currentY+5), ui::Point(Size.X-((XRES/2) + 10), -1), comments[i].comment); + tempComment = new ui::Label(ui::Point(5, currentY+5), ui::Point(Size.X-((XRES/2) + 13), -1), comments[i].comment); tempComment->SetMultiline(true); tempComment->Appearance.HorizontalAlign = ui::Appearance::AlignLeft; tempComment->Appearance.VerticalAlign = ui::Appearance::AlignTop;