Skip to content

Commit 98ccbdf

Browse files
committed
add ScrollWindow (makes ProfileViewer scrollable if necessary)
1 parent 4323a25 commit 98ccbdf

6 files changed

Lines changed: 87 additions & 21 deletions

File tree

src/gui/profile/ProfileViewer.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "json/json.h"
99

1010
ProfileViewer::ProfileViewer(std::string profileName):
11-
Window_(Point(CENTERED, CENTERED), Point(260, 350)),
11+
ScrollWindow(Point(CENTERED, CENTERED), Point(260, 350)),
1212
name(profileName),
1313
avatar(NULL),
1414
ageLabel(NULL),
@@ -81,6 +81,8 @@ void ProfileViewer::OnTick(float dt)
8181
this->AddComponent(locationLabel);
8282
this->AddComponent(websiteLabel);
8383
this->AddComponent(biographyLabel);
84+
if (biographyLabel->GetSize().Y+115 > this->GetSize().Y)
85+
this->SetScrollable(true, biographyLabel->GetSize().Y+117-this->GetSize().Y*2);
8486

8587
// If we don't do this average score will have a ton of decimal points, round to 2 here
8688
float average = root["User"]["Saves"]["AverageScore"].asFloat();
@@ -128,13 +130,13 @@ void ProfileViewer::OnTick(float dt)
128130
void ProfileViewer::OnDraw(VideoBuffer *buf)
129131
{
130132
if (avatar)
131-
buf->DrawImage(avatar, 210, 10, 40, 40);
132-
buf->DrawText(10, 22, "Age:", 175, 175, 175, 255);
133-
buf->DrawText(10, 34, "Location:", 175, 175, 175, 255);
134-
buf->DrawText(10, 46, "Website:", 175, 175, 175, 255);
135-
buf->DrawText(10, 58, "Saves:", 175, 175, 175, 255);
136-
buf->DrawText(15, 70, "Count:", 175, 175, 175, 255);
137-
buf->DrawText(15, 82, "Average Score:", 175, 175, 175, 255);
138-
buf->DrawText(15, 94, "Highest Score:", 175, 175, 175, 255);
139-
buf->DrawText(10, 106, "Biography:", 175, 175, 175, 255);
133+
buf->DrawImage(avatar, 210, 10-GetScrollPosition(), 40, 40);
134+
buf->DrawText(10, 22-GetScrollPosition(), "Age:", 175, 175, 175, 255);
135+
buf->DrawText(10, 34-GetScrollPosition(), "Location:", 175, 175, 175, 255);
136+
buf->DrawText(10, 46-GetScrollPosition(), "Website:", 175, 175, 175, 255);
137+
buf->DrawText(10, 58-GetScrollPosition(), "Saves:", 175, 175, 175, 255);
138+
buf->DrawText(15, 70-GetScrollPosition(), "Count:", 175, 175, 175, 255);
139+
buf->DrawText(15, 82-GetScrollPosition(), "Average Score:", 175, 175, 175, 255);
140+
buf->DrawText(15, 94-GetScrollPosition(), "Highest Score:", 175, 175, 175, 255);
141+
buf->DrawText(10, 106-GetScrollPosition(), "Biography:", 175, 175, 175, 255);
140142
}

src/gui/profile/ProfileViewer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
#define PROFILEVIEWER_H
33

44
#include <string>
5-
#include "interface/Window.h"
5+
#include "interface/ScrollWindow.h"
66
#include "graphics.h"
77

88
class Download;
99
class Label;
10-
class ProfileViewer : public Window_
10+
class ProfileViewer : public ScrollWindow
1111
{
1212
std::string name;
1313
Download *profileInfoDownload;

src/interface/Component.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class Component
3131
virtual void OnTick() { }
3232

3333
virtual void OnDefocus() { }
34+
35+
friend class ScrollWindow;
3436
};
3537

3638
#endif

src/interface/ScrollWindow.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "ScrollWindow.h"
2+
3+
ScrollWindow::ScrollWindow(Point position, Point size):
4+
Window_(position, size),
5+
scrollable(false),
6+
scrollSize(size.Y),
7+
scrolled(0)
8+
{
9+
}
10+
11+
void ScrollWindow::DoMouseWheel(int x, int y, int d)
12+
{
13+
if (scrollable)
14+
{
15+
int oldScrolled = scrolled;
16+
if (d > 0 && scrolled > 0)
17+
scrolled = std::max(scrolled-d*4, 0);
18+
else if (d < 0 && scrolled < size.Y + scrollSize)
19+
scrolled = std::min(scrolled-d*4, size.Y + scrollSize);
20+
21+
for (std::vector<Component*>::iterator iter = Components.begin(), end = Components.end(); iter != end; iter++)
22+
{
23+
(*iter)->position.Y -= (scrolled-oldScrolled);
24+
}
25+
}
26+
27+
/*for (std::vector<Component*>::iterator iter = Components.begin(), end = Components.end(); iter != end; iter++)
28+
{
29+
(*iter)->OnMouseWheel(x, y, d);
30+
}*/
31+
32+
OnMouseWheel(x, y, d);
33+
}
34+
35+
void ScrollWindow::SetScrollable(bool scroll, int maxScroll)
36+
{
37+
scrollable = scroll;
38+
scrollSize = maxScroll;
39+
}

src/interface/ScrollWindow.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef SCROLLWINDOW_H
2+
#define SCROLLWINDOW_H
3+
4+
#include "Window.h"
5+
6+
class ScrollWindow : public Window_
7+
{
8+
bool scrollable;
9+
int scrollSize, scrolled;
10+
11+
public:
12+
ScrollWindow(Point position, Point size);
13+
14+
void SetScrollable(bool scroll, int maxScroll);
15+
int GetScrollPosition() { return scrolled; }
16+
17+
protected:
18+
void DoMouseWheel(int x, int y, int d);
19+
};
20+
21+
#endif

src/interface/Window.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@
88
class VideoBuffer;
99
class Window_
1010
{
11-
private:
12-
Point position;
13-
Point size;
14-
VideoBuffer* videoBuffer;
15-
std::vector<Component*> Components;
16-
Component* focused;
17-
Component* clicked;
18-
1911
public:
2012
Window_(Point position, Point size);
2113
~Window_();
@@ -32,17 +24,22 @@ class Window_
3224
void DoMouseMove(int x, int y, int dx, int dy);
3325
void DoMouseDown(int x, int y, unsigned char button);
3426
void DoMouseUp(int x, int y, unsigned char button);
35-
void DoMouseWheel(int x, int y, int d);
27+
virtual void DoMouseWheel(int x, int y, int d);
3628
void DoKeyPress(int key, unsigned short character, unsigned char modifiers);
3729
void DoKeyRelease(int key, unsigned short character, unsigned char modifiers);
3830

31+
Point GetPosition() { return position; }
32+
Point GetSize() { return size; }
3933
VideoBuffer* GetVid() { return videoBuffer; }
4034

4135
bool toDelete;
4236

4337
static const int CENTERED = -1;
4438

4539
protected:
40+
Point position;
41+
Point size;
42+
std::vector<Component*> Components;
4643

4744
virtual void OnTick(float dt) { }
4845
virtual void OnDraw(VideoBuffer *buf) { }
@@ -52,6 +49,11 @@ class Window_
5249
virtual void OnMouseWheel(int x, int y, int d) { }
5350
virtual void OnKeyPress(int key, unsigned short character, unsigned char modifiers) { }
5451
virtual void OnKeyRelease(int key, unsigned short character, unsigned char modifiers) { }
52+
53+
private:
54+
VideoBuffer* videoBuffer;
55+
Component* focused;
56+
Component* clicked;
5557
};
5658

5759
#endif

0 commit comments

Comments
 (0)