summaryrefslogtreecommitdiff
path: root/src/interface/ScrollPanel.cpp
blob: c2d4f66c3508913d50fda58eb4178397172a6ead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include "ScrollPanel.h"

using namespace ui;

ScrollPanel::ScrollPanel(Point position, Point size):
	Panel(position, size),
	maxOffset(0, 0),
	offsetX(0),
	offsetY(0),
	yScrollVel(0.0f),
	xScrollVel(0.0f),
	scrollBarWidth(0)
{

}

int ScrollPanel::GetScrollLimit()
{
	if(maxOffset.Y == -ViewportPosition.Y)
		return 1;
	else if(ViewportPosition.Y == 0)
		return -1;
	return 0;
}

void ScrollPanel::XOnMouseWheelInside(int localx, int localy, int d)
{
	if(!d)
		return;
	yScrollVel -= d*2;
}

void ScrollPanel::Draw(const Point& screenPos)
{
	Panel::Draw(screenPos);

	Graphics * g = ui::Engine::Ref().g;

	//Vertical scroll bar
	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));
		}

		g->fillrect(screenPos.X+(Size.X-scrollBarWidth), screenPos.Y, scrollBarWidth, Size.Y, 125, 125, 125, 100);
		g->fillrect(screenPos.X+(Size.X-scrollBarWidth), screenPos.Y+scrollPos, scrollBarWidth, scrollHeight, 255, 255, 255, 255);
	}
}

void ScrollPanel::XTick(float dt)
{
	//if(yScrollVel > 7.0f) yScrollVel = 7.0f;
	//if(yScrollVel < -7.0f) yScrollVel = -7.0f;
	if(yScrollVel > -0.5f && yScrollVel < 0.5)
		yScrollVel = 0;

	if(xScrollVel > 7.0f) xScrollVel = 7.0f;
	if(xScrollVel < -7.0f) xScrollVel = -7.0f;
	if(xScrollVel > -0.5f && xScrollVel < 0.5)
		xScrollVel = 0;

	maxOffset = InnerSize-Size;
	maxOffset.Y = std::max(0, maxOffset.Y);
	maxOffset.X = std::max(0, maxOffset.X);

	int oldOffsetY = offsetY;
	offsetY += yScrollVel;
	int oldOffsetX = offsetX;
	offsetX += xScrollVel;

	yScrollVel*=0.98f;
	xScrollVel*=0.98f;

	if(oldOffsetY!=int(offsetY))
	{
		if(offsetY<0)
		{
			offsetY = 0;
			yScrollVel = 0;
			//commentsBegin = true;
			//commentsEnd = false;
		}
		else if(offsetY>maxOffset.Y)
		{
			offsetY = maxOffset.Y;
			yScrollVel = 0;
			//commentsEnd = true;
			//commentsBegin = false;
		}
		else
		{
			//commentsEnd = false;
			//commentsBegin = false;
		}
		ViewportPosition.Y = -offsetY;
	}
	else
	{
		if(offsetY<0)
		{
			offsetY = 0;
			yScrollVel = 0;
			ViewportPosition.Y = -offsetY;
		}
		else if(offsetY>maxOffset.Y)
		{
			offsetY = maxOffset.Y;
			ViewportPosition.Y = -offsetY;
		}
	}

	if(mouseInside && scrollBarWidth < 6)
		scrollBarWidth++;
	else if(!mouseInside && scrollBarWidth > 0)
		scrollBarWidth--;
}