-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrollpane.hpp
executable file
·213 lines (184 loc) · 5.07 KB
/
scrollpane.hpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef SCROLLPANE_HPP
#define SCROLLPANE_HPP
#include "main.hpp"
const int scrollPaneTopMargin = 0,
scrollPaneLeftMargin = 5;
const int scrollPaneScrollbarWidth = 20;
const int scrollButtonHeight = 20;
template<class T>
class ScrollPane : public Widget
{
public:
ScrollPane(int x, int y, int width, int height, const std::vector<T> &elements, const TextParams *font=&fontDefault);
bool mouseDownSelf(float x, float y, int button, int mod);
bool containsPoint(float x, float y);
void setElements(const std::vector<T> &e);
void clearSelection();
bool haveSelection();
int getSelectedIndex();
T getSelectedElement();
protected:
void redrawSelf();
bool keyDownSelf(SDL_keysym sym);
bool haveScrollbar();
private:
void updateList();
std::vector<T> elements;
int x, y, width, height;
int scrollPos, maxScroll;
const TextParams *font;
int lineHeight;
int lines;
int selection;
double lastClickTime;
int lastClickIndex;
};
template<class T>
ScrollPane<T>::ScrollPane(int x, int y, int width, int height, const std::vector<T> &e, const TextParams *font)
: x(x), y(y), width(width), height(height), scrollPos(0), font(font)
{
selection = -1;
TTF_SizeText(font->font, "Test", NULL, &lineHeight);
lines = height/lineHeight;
setElements(e);
lastClickTime = 0;
lastClickIndex = -1;
}
template<class T>
void ScrollPane<T>::updateList()
{
maxScroll = elements.size() - height/lineHeight;
}
template<class T>
void ScrollPane<T>::setElements(const std::vector<T> &e)
{
elements.clear();
if(e.size())
elements.insert(elements.begin(), e.begin(), e.end());
updateList();
}
template<class T>
void ScrollPane<T>::clearSelection()
{
selection = -1;
}
template<class T>
void ScrollPane<T>::redrawSelf()
{
int scrollbarX = haveScrollbar() ? (x+width-scrollPaneScrollbarWidth) : (x+width);
glLineWidth(1.0);
glBindTexture(GL_TEXTURE_2D, 0);
glColor4ub(30, 30, 30, 100);
glBegin(GL_QUADS);
glVertex2f(x, y);
glVertex2f(x+width, y);
glVertex2f(x+width, y+height);
glVertex2f(x, y+height);
glEnd();
int scrollbarWidth = haveScrollbar()?scrollPaneScrollbarWidth:0;
if(selection != -1 && selection>=scrollPos && selection<scrollPos+lines)
{
glColor3ub(80, 80, 80);
glBegin(GL_QUADS);
glVertex2f(x, y + lineHeight* selection -scrollPos*lineHeight+scrollPaneTopMargin);
glVertex2f(scrollbarX, y + lineHeight* selection -scrollPos*lineHeight+scrollPaneTopMargin);
glVertex2f(scrollbarX, y + lineHeight*(selection+1) -scrollPos*lineHeight+scrollPaneTopMargin);
glVertex2f(x, y + lineHeight*(selection+1) -scrollPos*lineHeight+scrollPaneTopMargin);
glEnd();
}
for(unsigned ii=scrollPos; ii<elements.size() && ii-scrollPos<lines; ii++)
{
screenPrintf(x+scrollPaneLeftMargin,
y+lineHeight*ii-scrollPos*lineHeight+scrollPaneTopMargin,
*font, "%s", elements[ii].toString().c_str());
}
// Draw border
glColor3ub(80,80,80);
glBindTexture(GL_TEXTURE_2D, 0);
glBegin(GL_LINE_LOOP);
glVertex2f(x, y);
glVertex2f(x+width, y);
glVertex2f(x+width, y+height);
glVertex2f(x, y+height);
glEnd();
if(haveScrollbar()) {
glBegin(GL_LINES);
glVertex2f(x+width-scrollbarWidth, y);
glVertex2f(x+width-scrollbarWidth, y+height);
glVertex2f(scrollbarX, y+scrollButtonHeight);
glVertex2f(x+width, y+scrollButtonHeight);
glVertex2f(scrollbarX, y+height-scrollButtonHeight);
glVertex2f(x+width, y+height-scrollButtonHeight);
glEnd();
}
}
template<class T>
bool ScrollPane<T>::mouseDownSelf(float mx, float my, int button, int mod)
{
if(!containsPoint(mx,my))
return false;
// Click on scrollbar
if(haveScrollbar() && mx>x+width-scrollPaneScrollbarWidth && mx<x+width)
{
// Top scroll button?
if(my < y+scrollButtonHeight) {
if(scrollPos>0)
scrollPos--;
}
// Bottom scroll button?
else if(my > y+height-scrollButtonHeight) {
if(scrollPos<maxScroll)
scrollPos++;
}
// Elsewhere on scrollbar?
else {
// TODO
}
}
else
{
int index = (my-y)/lineHeight + scrollPos;
if(index == lastClickIndex && lastClickTime+doubleClickDelay>getTime())
activate();
lastClickTime = getTime();
lastClickIndex = index;
if(index>=0 && index<elements.size())
selection = index;
}
return true;
}
template<class T>
bool ScrollPane<T>::containsPoint(float px, float py)
{
return px>=x && py>=y && px<x+width && py<y+height;
}
template<class T>
bool ScrollPane<T>::keyDownSelf(SDL_keysym sym)
{
if(sym.sym==SDLK_RETURN) {
activate();
return true;
}
return false;
}
template<class T>
bool ScrollPane<T>::haveScrollbar()
{
return elements.size()*lineHeight >= height;
}
template<class T>
int ScrollPane<T>::getSelectedIndex()
{
return selection;
}
template<class T>
T ScrollPane<T>::getSelectedElement()
{
return elements[selection];
}
template<class T>
bool ScrollPane<T>::haveSelection()
{
return selection>=0 && selection<elements.size();
}
#endif