Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
listbox.qc
Go to the documentation of this file.
1#include "listbox.qh"
2
4 {
5 return me.scrollPos != me.scrollPosTarget;
6 }
7
9 {
10 // scroll doesn't work properly until itemHeight is set to the correct value
11 // at the first resizeNotify call
12 if (me.itemHeight == 1) // initial temporary value of itemHeight is 1
13 {
14 me.needScrollToItem = i;
15 return;
16 }
17
18 i = bound(0, i, me.nItems - 1);
19
20 // scroll the list to make sure the selected item is visible
21 // (even if the selected item doesn't change).
22 if (i < me.getFirstFullyVisibleItemAtScrollPos(me, me.scrollPos))
23 {
24 // above visible area
25 me.scrollPosTarget = me.getItemStart(me, i);
26 }
27 else if (i > me.getLastFullyVisibleItemAtScrollPos(me, me.scrollPos))
28 {
29 // below visible area
30 if (i == me.nItems - 1) me.scrollPosTarget = me.getTotalHeight(me) - 1;
31 else me.scrollPosTarget = me.getItemStart(me, i + 1) - 1;
32 }
33 }
34
35 void ListBox_setSelected(entity me, float i)
36 {
37 i = bound(0, i, me.nItems - 1);
38 me.scrollToItem(me, i);
39 me.selectedItem = i;
40 }
41 void ListBox_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
42 {
43 SUPER(ListBox).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
44 me.controlWidth = me.scrollbarWidth / absSize.x;
45 }
46 void ListBox_configureListBox(entity me, float theScrollbarWidth, float theItemHeight)
47 {
48 me.scrollbarWidth = theScrollbarWidth;
49 me.itemHeight = theItemHeight;
50 }
51
53 {
54 return me.nItems * me.itemHeight;
55 }
56 float ListBox_getItemAtPos(entity me, float pos)
57 {
58 return floor(pos / me.itemHeight);
59 }
60 float ListBox_getItemStart(entity me, float i)
61 {
62 return me.itemHeight * i;
63 }
64 float ListBox_getItemHeight(entity me, float i)
65 {
66 return me.itemHeight;
67 }
68
70 {
71 return me.getItemAtPos(me, pos + 0.999) - 1;
72 }
74 {
75 return me.getItemAtPos(me, pos + 0.001) + 1;
76 }
77 float ListBox_keyDown(entity me, float key, float ascii, float shift)
78 {
79 if (key == K_MWHEELUP)
80 {
81 me.scrollPosTarget = max(me.scrollPosTarget - 0.5, 0);
82 }
83 else if (key == K_MWHEELDOWN)
84 {
85 me.scrollPosTarget = min(me.scrollPosTarget + 0.5, max(0, me.getTotalHeight(me) - 1));
86 }
87 else if (key == K_PGUP || key == K_KP_PGUP)
88 {
89 if (me.selectionDoesntMatter)
90 {
91 me.scrollPosTarget = max(me.scrollPosTarget - 0.5, 0);
92 return 1;
93 }
94
95 float i = me.selectedItem;
96 float a = me.getItemHeight(me, i);
97 for ( ; ; )
98 {
99 --i;
100 if (i < 0) break;
101 a += me.getItemHeight(me, i);
102 if (a >= 1) break;
103 }
104 me.setSelected(me, i + 1);
105 }
106 else if (key == K_PGDN || key == K_KP_PGDN)
107 {
108 if (me.selectionDoesntMatter)
109 {
110 me.scrollPosTarget = min(me.scrollPosTarget + 0.5, me.nItems * me.itemHeight - 1);
111 return 1;
112 }
113
114 float i = me.selectedItem;
115 float a = me.getItemHeight(me, i);
116 for ( ; ; )
117 {
118 ++i;
119 if (i >= me.nItems) break;
120 a += me.getItemHeight(me, i);
121 if (a >= 1) break;
122 }
123 me.setSelected(me, i - 1);
124 }
125 else if (key == K_UPARROW || key == K_KP_UPARROW)
126 {
127 if (me.selectionDoesntMatter)
128 {
129 me.scrollPosTarget = max(me.scrollPosTarget - me.itemHeight, 0);
130 return 1;
131 }
132
133 me.setSelected(me, me.selectedItem - 1);
134 }
135 else if (key == K_DOWNARROW || key == K_KP_DOWNARROW)
136 {
137 if (me.selectionDoesntMatter)
138 {
139 me.scrollPosTarget = min(me.scrollPosTarget + me.itemHeight, me.nItems * me.itemHeight - 1);
140 return 1;
141 }
142
143 me.setSelected(me, me.selectedItem + 1);
144 }
145 else if (key == K_HOME || key == K_KP_HOME)
146 {
147 me.setSelected(me, 0);
148 }
149 else if (key == K_END || key == K_KP_END)
150 {
151 me.setSelected(me, me.nItems - 1);
152 }
153 else
154 {
155 return 0;
156 }
157 return 1;
158 }
160 {
161 me.mouseMoveOffset = -1;
162 if (pos_x < 0) return 0;
163 if (pos_y < 0) return 0;
164 if (pos_x >= 1) return 0;
165 if (pos_y >= 1) return 0;
166 if (pos_x < 1 - me.controlWidth)
167 {
168 me.mouseMoveOffset = pos.y;
169 }
170 else
171 {
172 me.setFocusedItem(me, -1);
173 me.mouseMoveOffset = -1;
174 }
175 return 1;
176 }
178 {
179 float hit;
180 me.updateControlTopBottom(me);
181 me.dragScrollPos = pos;
182 if (me.pressed == 1 && me.getTotalHeight(me) > 1)
183 {
184 hit = 1;
185 if (pos.x < 1 - me.controlWidth - me.tolerance.x * me.controlWidth) hit = 0;
186 if (pos.y < 0 - me.tolerance.y) hit = 0;
187 if (pos.x >= 1 + me.tolerance.x * me.controlWidth) hit = 0;
188 if (pos.y >= 1 + me.tolerance.y) hit = 0;
189 if (hit)
190 {
191 // calculate new pos to v
192 float d;
193 d = (pos.y - me.pressOffset) / (1 - (me.controlBottom - me.controlTop)) * (me.getTotalHeight(me) - 1);
194 me.scrollPosTarget = me.previousValue + d;
195 }
196 else
197 {
198 me.scrollPosTarget = me.previousValue;
199 }
200 me.scrollPosTarget = min(me.scrollPosTarget, me.getTotalHeight(me) - 1);
201 me.scrollPosTarget = max(me.scrollPosTarget, 0);
202 }
203 else if (me.pressed == 2)
204 {
205 int clickeditem = me.getItemAtPos(me, me.scrollPos + pos.y);
206 me.setSelected(me, clickeditem);
207 me.setFocusedItem(me, clickeditem);
208 me.mouseMoveOffset = -1;
209 }
210 return 1;
211 }
213 {
214 if (pos.x < 0) return false;
215 if (pos.y < 0) return false;
216 if (pos.x >= 1) return false;
217 if (pos.y >= 1) return false;
218 this.dragScrollPos = pos;
219 this.updateControlTopBottom(this);
220 if (pos.x >= 1 - this.controlWidth)
221 {
222 // if hit, set this.pressed, otherwise scroll by one page
223 if (pos.y < this.controlTop)
224 {
225 // page up
226 this.scrollPosTarget = max(this.scrollPosTarget - 1, 0);
227 }
228 else if (pos.y > this.controlBottom)
229 {
230 // page down
231 this.scrollPosTarget = min(this.scrollPosTarget + 1, this.getTotalHeight(this) - 1);
232 }
233 else
234 {
235 this.pressed = 1;
236 this.pressOffset = pos.y;
237 this.previousValue = this.scrollPos;
238 }
239 }
240 else
241 {
242 // continue doing that while dragging (even when dragging outside). When releasing, forward the click to the then selected item.
243 this.pressed = 2;
244 // an item has been clicked. Select it, ...
245 int clickeditem = this.getItemAtPos(this, this.scrollPos + pos.y);
246 this.setSelected(this, clickeditem);
247 this.setFocusedItem(this, clickeditem);
248 }
249 return true;
250 }
252 {
253 float focusedItem_save = me.focusedItem;
254 me.focusedItem = (item >= 0 && item < me.nItems) ? item : -1;
255 if (focusedItem_save != me.focusedItem)
256 {
257 me.focusedItemChangeNotify(me);
258 if (me.focusedItem >= 0) me.focusedItemAlpha = SKINALPHA_LISTBOX_FOCUSED;
259 }
260 }
262 {
263 if (me.pressed == 1)
264 {
265 // slider dragging mode
266 // in that case, nothing happens on releasing
267 }
268 else if (me.pressed == 2)
269 {
270 me.pressed = 3; // do that here, so setSelected can know the mouse has been released
271 // item dragging mode
272 // select current one one last time...
273 int clickeditem = me.getItemAtPos(me, me.scrollPos + pos.y);
274 me.setSelected(me, clickeditem);
275 me.setFocusedItem(me, clickeditem);
276 // and give it a nice click event
277 if (me.nItems > 0)
278 {
279 vector where = globalToBox(pos, eY * (me.getItemStart(me, me.selectedItem) - me.scrollPos), eX * (1 - me.controlWidth) + eY * me.getItemHeight(me, me.selectedItem));
280
281 if ((me.selectedItem == me.lastClickedItem && clickeditem == me.selectedItem) && (time < me.lastClickedTime + 0.3))
282 me.doubleClickListBoxItem(me, me.selectedItem, where);
283 else
284 me.clickListBoxItem(me, me.selectedItem, where);
285
286 me.lastClickedItem = me.selectedItem;
287 me.lastClickedTime = time;
288 }
289 }
290 me.pressed = 0;
291 return 1;
292 }
294 {
295 // Reset the var pressed in case listbox loses focus
296 // by a mouse click on an item of the list
297 // for example showing a dialog on right click
298 me.pressed = 0;
299 me.setFocusedItem(me, -1);
300 me.mouseMoveOffset = -1;
301 }
303 {
304 // scrollPos is in 0..1 and indicates where the "page" currently shown starts.
305 if (me.getTotalHeight(me) <= 1)
306 {
307 // we don't need no stinkin' scrollbar, we don't need no view control...
308 me.controlTop = 0;
309 me.controlBottom = 1;
310 me.scrollPos = 0;
311 }
312 else
313 {
314 // if scroll pos is above beginning or below end of list, fix it
315 me.scrollPos = bound(0, me.scrollPos, me.getTotalHeight(me) - 1);
316 // now that we know where the list is scrolled to, find out where to draw the control
317 me.controlTop = max(0, me.scrollPos / me.getTotalHeight(me));
318 me.controlBottom = min((me.scrollPos + 1) / me.getTotalHeight(me), 1);
319
320 const float minfactor = 2 * me.controlWidth / me.size.y * me.size.x;
321 float f = me.controlBottom - me.controlTop;
322 if (f < minfactor) // FIXME good default?
323 {
324 // f * X + 1 * (1-X) = minfactor
325 // (f - 1) * X + 1 = minfactor
326 // (f - 1) * X = minfactor - 1
327 // X = (minfactor - 1) / (f - 1)
328 f = (minfactor - 1) / (f - 1);
329 me.controlTop = me.controlTop * f + 0 * (1 - f);
330 me.controlBottom = me.controlBottom * f + 1 * (1 - f);
331 }
332 }
333 }
334 AUTOCVAR(menu_scroll_averaging_time, float, 0.16, "smooth scroll averaging time");
335 // scroll faster while dragging the scrollbar
336 AUTOCVAR(menu_scroll_averaging_time_pressed, float, 0.06, "smooth scroll averaging time when dragging the scrollbar");
338 {
339 vector fillSize = '0 0 0';
340
341 // we can't do this in mouseMove as the list can scroll without moving the cursor
342 if (me.mouseMoveOffset != -1) me.setFocusedItem(me, me.getItemAtPos(me, me.scrollPos + me.mouseMoveOffset));
343
344 if (me.needScrollToItem >= 0)
345 {
346 me.scrollToItem(me, me.needScrollToItem);
347 me.needScrollToItem = -1;
348 }
349 if (me.scrollPos != me.scrollPosTarget)
350 {
351 float averaging_time = (me.pressed == 1)
352 ? autocvar_menu_scroll_averaging_time_pressed
353 : autocvar_menu_scroll_averaging_time;
354 // this formula works with whatever framerate
355 float f = averaging_time ? exp(-frametime / averaging_time) : 0;
356 me.scrollPos = me.scrollPos * f + me.scrollPosTarget * (1 - f);
357 if (fabs(me.scrollPos - me.scrollPosTarget) < 0.001) me.scrollPos = me.scrollPosTarget;
358 }
359
360 if (me.pressed == 2) me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
361 me.updateControlTopBottom(me);
362 fillSize.x = (1 - me.controlWidth);
363 if (me.alphaBG) draw_Fill('0 0 0', '0 1 0' + fillSize, me.colorBG, me.alphaBG);
364 if (me.controlWidth)
365 {
366 draw_VertButtonPicture(eX * (1 - me.controlWidth), strcat(me.src, "_s"), eX * me.controlWidth + eY, me.color2, 1);
367 if (me.getTotalHeight(me) > 1)
368 {
369 vector o, s;
370 o = eX * (1 - me.controlWidth) + eY * me.controlTop;
371 s = eX * me.controlWidth + eY * (me.controlBottom - me.controlTop);
372 if (me.pressed == 1) draw_VertButtonPicture(o, strcat(me.src, "_c"), s, me.colorC, 1);
373 else if (me.focused) draw_VertButtonPicture(o, strcat(me.src, "_f"), s, me.colorF, 1);
374 else draw_VertButtonPicture(o, strcat(me.src, "_n"), s, me.color, 1);
375 }
376 }
377 draw_SetClip();
378 vector oldshift = draw_shift;
379 vector oldscale = draw_scale;
380
381 int i = me.getItemAtPos(me, me.scrollPos);
382 float y = me.getItemStart(me, i) - me.scrollPos;
383 for ( ; i < me.nItems && y < 1; ++i)
384 {
385 draw_shift = boxToGlobal(eY * y, oldshift, oldscale);
386 vector relSize = eX * (1 - me.controlWidth) + eY * me.getItemHeight(me, i);
387 vector absSize = boxToGlobalSize(relSize, me.size);
388 draw_scale = boxToGlobalSize(relSize, oldscale);
389 me.drawListBoxItem(me, i, absSize, (me.selectedItem == i), (me.focusedItem == i));
390 y += relSize.y;
391 }
393
394 draw_shift = oldshift;
395 draw_scale = oldscale;
396 SUPER(ListBox).draw(me);
397 }
398
401
402 void ListBox_clickListBoxItem(entity me, float i, vector where)
403 {
404 // template method
405 }
406
408 {
409 // template method
410 }
411
412 void ListBox_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
413 {
414 // should be overridden
415 draw_Text('0 0 0', sprintf(_("Item %d"), i), eX * (8 / absSize.x) + eY * (8 / absSize.y), (isSelected ? '0 1 0' : '1 1 1'), 1, 0);
416 }
var entity(vector mins, vector maxs,.entity tofield) findbox_tofield_OrFallback
virtual void setSelected()
virtual void getTotalHeight()
virtual void mousePress()
Definition listbox.qc:212
vector dragScrollPos
Definition listbox.qh:36
float pressOffset
Definition listbox.qh:30
virtual void getItemAtPos()
virtual void updateControlTopBottom()
float pressed
Definition listbox.qh:29
float previousValue
Definition listbox.qh:28
float scrollPosTarget
Definition listbox.qh:24
virtual void setFocusedItem()
float scrollPos
Definition listbox.qh:23
float frametime
float time
#define AUTOCVAR(...)
Definition cvar.qh:161
v y
Definition ent_cs.qc:121
float ListBox_getLastFullyVisibleItemAtScrollPos(entity me, float pos)
Definition listbox.qc:69
void ListBox_draw(entity me)
Definition listbox.qc:337
float ListBox_mouseRelease(entity me, vector pos)
Definition listbox.qc:261
void ListBox_updateControlTopBottom(entity me)
Definition listbox.qc:302
void ListBox_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused)
Definition listbox.qc:412
void ListBox_resizeNotify(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
Definition listbox.qc:41
void ListBox_clickListBoxItem(entity me, float i, vector where)
Definition listbox.qc:402
float ListBox_getItemAtPos(entity me, float pos)
Definition listbox.qc:56
void ListBox_doubleClickListBoxItem(entity me, float i, vector where)
Definition listbox.qc:407
void ListBox_scrollToItem(entity me, int i)
Definition listbox.qc:8
void ListBox_setSelected(entity me, float i)
Definition listbox.qc:35
bool ListBox_isScrolling(entity me)
Definition listbox.qc:3
float ListBox_getFirstFullyVisibleItemAtScrollPos(entity me, float pos)
Definition listbox.qc:73
void ListBox_configureListBox(entity me, float theScrollbarWidth, float theItemHeight)
Definition listbox.qc:46
float ListBox_getItemHeight(entity me, float i)
Definition listbox.qc:64
void ListBox_focusedItemChangeNotify(entity me)
Definition listbox.qc:399
float ListBox_mouseDrag(entity me, vector pos)
Definition listbox.qc:177
void ListBox_setFocusedItem(entity me, int item)
Definition listbox.qc:251
float ListBox_getItemStart(entity me, float i)
Definition listbox.qc:60
float ListBox_keyDown(entity me, float key, float ascii, float shift)
Definition listbox.qc:77
void ListBox_focusLeave(entity me)
Definition listbox.qc:293
float ListBox_mouseMove(entity me, vector pos)
Definition listbox.qc:159
float ListBox_getTotalHeight(entity me)
Definition listbox.qc:52
float K_UPARROW
Definition keycodes.qc:15
float K_PGDN
Definition keycodes.qc:39
float K_DOWNARROW
Definition keycodes.qc:16
float K_MWHEELDOWN
Definition keycodes.qc:133
float K_KP_UPARROW
Definition keycodes.qc:64
float K_PGUP
Definition keycodes.qc:40
float K_KP_HOME
Definition keycodes.qc:62
float K_KP_DOWNARROW
Definition keycodes.qc:53
float K_HOME
Definition keycodes.qc:41
float K_END
Definition keycodes.qc:42
float K_KP_PGDN
Definition keycodes.qc:55
float K_KP_PGUP
Definition keycodes.qc:66
float K_KP_END
Definition keycodes.qc:51
float K_MWHEELUP
Definition keycodes.qc:132
float exp(float e)
Definition mathlib.qc:73
vector globalToBox(vector v, vector theOrigin, vector theScale)
Definition draw.qc:30
void draw_VertButtonPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
Definition draw.qc:143
void draw_Text(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha, float ICanHasKallerz)
Definition draw.qc:282
void draw_SetClip()
Definition draw.qc:349
vector boxToGlobalSize(vector v, vector theScale)
Definition draw.qc:53
vector boxToGlobal(vector v, vector theOrigin, vector theScale)
Definition draw.qc:45
void draw_Fill(vector theOrigin, vector theSize, vector theColor, float theAlpha)
Definition draw.qc:97
void draw_ClearClip()
Definition draw.qc:362
vector draw_shift
Definition draw.qh:7
vector draw_scale
Definition draw.qh:8
float bound(float min, float value, float max)
float min(float f,...)
float fabs(float f)
float floor(float f)
float max(float f,...)
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))
#define SUPER(cname)
Definition oo.qh:231
#define METHOD(cname, name, prototype)
Definition oo.qh:269
vector
Definition self.qh:92
const vector eY
Definition vector.qh:45
const vector eX
Definition vector.qh:44