Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
listbox.qc File Reference
#include "listbox.qh"
Include dependency graph for listbox.qc:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

 AUTOCVAR (menu_scroll_averaging_time, float, 0.16, "smooth scroll averaging time")
 AUTOCVAR (menu_scroll_averaging_time_pressed, float, 0.06, "smooth scroll averaging time when dragging the scrollbar")
void ListBox_clickListBoxItem (entity me, float i, vector where)
void ListBox_configureListBox (entity me, float theScrollbarWidth, float theItemHeight)
void ListBox_doubleClickListBoxItem (entity me, float i, vector where)
void ListBox_draw (entity me)
void ListBox_drawListBoxItem (entity me, int i, vector absSize, bool isSelected, bool isFocused)
void ListBox_focusedItemChangeNotify (entity me)
void ListBox_focusLeave (entity me)
float ListBox_getFirstFullyVisibleItemAtScrollPos (entity me, float pos)
float ListBox_getItemAtPos (entity me, float pos)
float ListBox_getItemHeight (entity me, float i)
float ListBox_getItemStart (entity me, float i)
float ListBox_getLastFullyVisibleItemAtScrollPos (entity me, float pos)
float ListBox_getTotalHeight (entity me)
bool ListBox_isScrolling (entity me)
float ListBox_keyDown (entity me, float key, float ascii, float shift)
float ListBox_mouseDrag (entity me, vector pos)
float ListBox_mouseMove (entity me, vector pos)
float ListBox_mouseRelease (entity me, vector pos)
void ListBox_resizeNotify (entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
void ListBox_scrollToItem (entity me, int i)
void ListBox_setFocusedItem (entity me, int item)
void ListBox_setSelected (entity me, float i)
void ListBox_updateControlTopBottom (entity me)

Function Documentation

◆ AUTOCVAR() [1/2]

AUTOCVAR ( menu_scroll_averaging_time ,
float ,
0. 16,
"smooth scroll averaging time"  )

◆ AUTOCVAR() [2/2]

AUTOCVAR ( menu_scroll_averaging_time_pressed ,
float ,
0. 06,
"smooth scroll averaging time when dragging the scrollbar"  )

◆ ListBox_clickListBoxItem()

void ListBox_clickListBoxItem ( entity me,
float i,
vector where )

Definition at line 397 of file listbox.qc.

398 {
399 // template method
400 }

References entity(), and vector.

◆ ListBox_configureListBox()

void ListBox_configureListBox ( entity me,
float theScrollbarWidth,
float theItemHeight )

Definition at line 48 of file listbox.qc.

49 {
50 me.scrollbarWidth = theScrollbarWidth;
51 me.itemHeight = theItemHeight;
52 }

References entity().

◆ ListBox_doubleClickListBoxItem()

void ListBox_doubleClickListBoxItem ( entity me,
float i,
vector where )

Definition at line 402 of file listbox.qc.

403 {
404 // template method
405 }

References entity(), and vector.

◆ ListBox_draw()

void ListBox_draw ( entity me)

Definition at line 325 of file listbox.qc.

326 {
327 vector fillSize = '0 0 0';
328
329 // we can't do this in mouseMove as the list can scroll without moving the cursor
330 if (me.mouseMoveOffset != -1)
331 me.setFocusedItem(me, me.getItemAtPos(me, me.scrollPos + me.mouseMoveOffset));
332
333 if (me.needScrollToItem >= 0)
334 {
335 me.scrollToItem(me, me.needScrollToItem);
336 me.needScrollToItem = -1;
337 }
338 if (me.scrollPos != me.scrollPosTarget)
339 {
340 float averaging_time = (me.pressed == 1)
341 ? autocvar_menu_scroll_averaging_time_pressed
342 : autocvar_menu_scroll_averaging_time;
343 // this formula works with whatever framerate
344 float f = averaging_time ? exp(-frametime / averaging_time) : 0;
345 me.scrollPos = me.scrollPos * f + me.scrollPosTarget * (1 - f);
346 if (fabs(me.scrollPos - me.scrollPosTarget) < 0.001)
347 me.scrollPos = me.scrollPosTarget;
348 }
349
350 if (me.pressed == 2)
351 me.mouseDrag(me, me.dragScrollPos); // simulate mouseDrag event
352 me.updateControlTopBottom(me);
353 fillSize.x = (1 - me.controlWidth);
354 if (me.alphaBG)
355 draw_Fill('0 0 0', '0 1 0' + fillSize, me.colorBG, me.alphaBG);
356 if (me.controlWidth)
357 {
358 draw_VertButtonPicture(eX * (1 - me.controlWidth), strcat(me.src, "_s"), eX * me.controlWidth + eY, me.color2, 1);
359 if (me.getTotalHeight(me) > 1)
360 {
361 vector o, s;
362 o = eX * (1 - me.controlWidth) + eY * me.controlTop;
363 s = eX * me.controlWidth + eY * (me.controlBottom - me.controlTop);
364 if (me.pressed == 1)
365 draw_VertButtonPicture(o, strcat(me.src, "_c"), s, me.colorC, 1);
366 else if (me.focused)
367 draw_VertButtonPicture(o, strcat(me.src, "_f"), s, me.colorF, 1);
368 else
369 draw_VertButtonPicture(o, strcat(me.src, "_n"), s, me.color, 1);
370 }
371 }
372 draw_SetClip();
373 vector oldshift = draw_shift;
374 vector oldscale = draw_scale;
375
376 int i = me.getItemAtPos(me, me.scrollPos);
377 float y = me.getItemStart(me, i) - me.scrollPos;
378 for ( ; i < me.nItems && y < 1; ++i)
379 {
380 draw_shift = boxToGlobal(eY * y, oldshift, oldscale);
381 vector relSize = eX * (1 - me.controlWidth) + eY * me.getItemHeight(me, i);
382 vector absSize = boxToGlobalSize(relSize, me.size);
383 draw_scale = boxToGlobalSize(relSize, oldscale);
384 me.drawListBoxItem(me, i, absSize, (me.selectedItem == i), (me.focusedItem == i));
385 y += relSize.y;
386 }
388
389 draw_shift = oldshift;
390 draw_scale = oldscale;
391 SUPER(ListBox).draw(me);
392 }
float frametime
v y
Definition ent_cs.qc:146
float exp(float e)
Definition mathlib.qc:70
void draw_VertButtonPicture(vector theOrigin, string pic, vector theSize, vector theColor, float theAlpha)
Definition draw.qc:143
void draw_SetClip()
Definition draw.qc:345
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:355
vector draw_shift
Definition draw.qh:7
vector draw_scale
Definition draw.qh:8
float fabs(float f)
strcat(_("^F4Countdown stopped!"), "\n^BG", _("Teams are too unbalanced."))
#define SUPER(cname)
Definition oo.qh:236
vector
Definition self.qh:96
const vector eY
Definition vector.qh:44
const vector eX
Definition vector.qh:43

References boxToGlobal(), boxToGlobalSize(), draw_ClearClip(), draw_Fill(), draw_scale, draw_SetClip(), draw_shift, draw_VertButtonPicture(), entity(), eX, exp(), eY, fabs(), frametime, strcat(), SUPER, vector, and y.

◆ ListBox_drawListBoxItem()

void ListBox_drawListBoxItem ( entity me,
int i,
vector absSize,
bool isSelected,
bool isFocused )

Definition at line 407 of file listbox.qc.

408 {
409 // should be overridden
410 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);
411 }
void draw_Text(vector theOrigin, string theText, vector theSize, vector theColor, float theAlpha, bool allowColorCodes)
Definition draw.qc:281

References draw_Text(), entity(), eX, eY, and vector.

◆ ListBox_focusedItemChangeNotify()

void ListBox_focusedItemChangeNotify ( entity me)

Definition at line 394 of file listbox.qc.

395 {}

References entity().

◆ ListBox_focusLeave()

void ListBox_focusLeave ( entity me)

Definition at line 281 of file listbox.qc.

282 {
283 // Reset the var pressed in case listbox loses focus
284 // by a mouse click on an item of the list
285 // for example showing a dialog on right click
286 me.pressed = 0;
287 me.setFocusedItem(me, -1);
288 me.mouseMoveOffset = -1;
289 }

References entity().

◆ ListBox_getFirstFullyVisibleItemAtScrollPos()

float ListBox_getFirstFullyVisibleItemAtScrollPos ( entity me,
float pos )

Definition at line 75 of file listbox.qc.

76 {
77 return me.getItemAtPos(me, pos + 0.001) + 1;
78 }

References entity().

◆ ListBox_getItemAtPos()

float ListBox_getItemAtPos ( entity me,
float pos )

Definition at line 58 of file listbox.qc.

59 {
60 return floor(pos / me.itemHeight);
61 }
float floor(float f)

References entity(), and floor().

◆ ListBox_getItemHeight()

float ListBox_getItemHeight ( entity me,
float i )

Definition at line 66 of file listbox.qc.

67 {
68 return me.itemHeight;
69 }

References entity().

◆ ListBox_getItemStart()

float ListBox_getItemStart ( entity me,
float i )

Definition at line 62 of file listbox.qc.

63 {
64 return me.itemHeight * i;
65 }

References entity().

◆ ListBox_getLastFullyVisibleItemAtScrollPos()

float ListBox_getLastFullyVisibleItemAtScrollPos ( entity me,
float pos )

Definition at line 71 of file listbox.qc.

72 {
73 return me.getItemAtPos(me, pos + 0.999) - 1;
74 }

References entity().

◆ ListBox_getTotalHeight()

float ListBox_getTotalHeight ( entity me)

Definition at line 54 of file listbox.qc.

55 {
56 return me.nItems * me.itemHeight;
57 }

References entity().

◆ ListBox_isScrolling()

bool ListBox_isScrolling ( entity me)

Definition at line 3 of file listbox.qc.

4 {
5 return me.scrollPos != me.scrollPosTarget;
6 }

References entity().

◆ ListBox_keyDown()

float ListBox_keyDown ( entity me,
float key,
float ascii,
float shift )

Definition at line 80 of file listbox.qc.

81 {
82 if (key == K_MWHEELUP)
83 me.scrollPosTarget = max(me.scrollPosTarget - 0.5, 0);
84 else if (key == K_MWHEELDOWN)
85 me.scrollPosTarget = min(me.scrollPosTarget + 0.5, max(0, me.getTotalHeight(me) - 1));
86 else if (key == K_PGUP || key == K_KP_PGUP)
87 {
88 if (me.selectionDoesntMatter)
89 {
90 me.scrollPosTarget = max(me.scrollPosTarget - 0.5, 0);
91 return 1;
92 }
93
94 int i = me.selectedItem;
95 float a = me.getItemHeight(me, i);
96 for ( ; ; )
97 {
98 --i;
99 if (i < 0)
100 break;
101 a += me.getItemHeight(me, i);
102 if (a >= 1)
103 break;
104 }
105 me.setSelected(me, i + 1);
106 }
107 else if (key == K_PGDN || key == K_KP_PGDN)
108 {
109 if (me.selectionDoesntMatter)
110 {
111 me.scrollPosTarget = min(me.scrollPosTarget + 0.5, me.nItems * me.itemHeight - 1);
112 return 1;
113 }
114
115 int i = me.selectedItem;
116 float a = me.getItemHeight(me, i);
117 for ( ; ; )
118 {
119 ++i;
120 if (i >= me.nItems)
121 break;
122 a += me.getItemHeight(me, i);
123 if (a >= 1)
124 break;
125 }
126 me.setSelected(me, i - 1);
127 }
128 else if (key == K_UPARROW || key == K_KP_UPARROW)
129 {
130 if (me.selectionDoesntMatter)
131 {
132 me.scrollPosTarget = max(me.scrollPosTarget - me.itemHeight, 0);
133 return 1;
134 }
135
136 me.setSelected(me, me.selectedItem - 1);
137 }
138 else if (key == K_DOWNARROW || key == K_KP_DOWNARROW)
139 {
140 if (me.selectionDoesntMatter)
141 {
142 me.scrollPosTarget = min(me.scrollPosTarget + me.itemHeight, me.nItems * me.itemHeight - 1);
143 return 1;
144 }
145
146 me.setSelected(me, me.selectedItem + 1);
147 }
148 else if (key == K_HOME || key == K_KP_HOME)
149 me.setSelected(me, 0);
150 else if (key == K_END || key == K_KP_END)
151 me.setSelected(me, me.nItems - 1);
152 else
153 return 0;
154 return 1;
155 }
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 min(float f,...)
float max(float f,...)

References entity(), K_DOWNARROW, K_END, K_HOME, K_KP_DOWNARROW, K_KP_END, K_KP_HOME, K_KP_PGDN, K_KP_PGUP, K_KP_UPARROW, K_MWHEELDOWN, K_MWHEELUP, K_PGDN, K_PGUP, K_UPARROW, max(), and min().

◆ ListBox_mouseDrag()

float ListBox_mouseDrag ( entity me,
vector pos )

Definition at line 171 of file listbox.qc.

172 {
173 me.updateControlTopBottom(me);
174 me.dragScrollPos = pos;
175 if (me.pressed == 1 && me.getTotalHeight(me) > 1)
176 {
177 if (pos.x < 1 - me.controlWidth - me.tolerance.x * me.controlWidth
178 || pos.y < 0 - me.tolerance.y
179 || pos.x >= 1 + me.tolerance.x * me.controlWidth
180 || pos.y >= 1 + me.tolerance.y)
181 me.scrollPosTarget = me.previousValue;
182 else // hit
183 {
184 // calculate new pos to v
185 float d = (pos.y - me.pressOffset) / (1 - (me.controlBottom - me.controlTop)) * (me.getTotalHeight(me) - 1);
186 me.scrollPosTarget = me.previousValue + d;
187 }
188 me.scrollPosTarget = min(me.scrollPosTarget, me.getTotalHeight(me) - 1);
189 me.scrollPosTarget = max(me.scrollPosTarget, 0);
190 }
191 else if (me.pressed == 2)
192 {
193 int clickeditem = me.getItemAtPos(me, me.scrollPos + pos.y);
194 me.setSelected(me, clickeditem);
195 me.setFocusedItem(me, clickeditem);
196 me.mouseMoveOffset = -1;
197 }
198 return 1;
199 }

References entity(), max(), min(), and vector.

◆ ListBox_mouseMove()

float ListBox_mouseMove ( entity me,
vector pos )

Definition at line 156 of file listbox.qc.

157 {
158 me.mouseMoveOffset = -1;
159 if (pos.x < 0 || pos.y < 0 || pos.x >= 1 || pos.y >= 1)
160 return 0;
161
162 if (pos.x < 1 - me.controlWidth)
163 me.mouseMoveOffset = pos.y;
164 else
165 {
166 me.setFocusedItem(me, -1);
167 me.mouseMoveOffset = -1;
168 }
169 return 1;
170 }

References entity(), and vector.

◆ ListBox_mouseRelease()

float ListBox_mouseRelease ( entity me,
vector pos )

Definition at line 249 of file listbox.qc.

250 {
251 if (me.pressed == 1)
252 {
253 // slider dragging mode
254 // in that case, nothing happens on releasing
255 }
256 else if (me.pressed == 2)
257 {
258 me.pressed = 3; // do that here, so setSelected can know the mouse has been released
259 // item dragging mode
260 // select current one one last time...
261 int clickeditem = me.getItemAtPos(me, me.scrollPos + pos.y);
262 me.setSelected(me, clickeditem);
263 me.setFocusedItem(me, clickeditem);
264 // and give it a nice click event
265 if (me.nItems > 0)
266 {
267 vector where = globalToBox(pos, eY * (me.getItemStart(me, me.selectedItem) - me.scrollPos), eX * (1 - me.controlWidth) + eY * me.getItemHeight(me, me.selectedItem));
268
269 if ((me.selectedItem == me.lastClickedItem && clickeditem == me.selectedItem) && (time < me.lastClickedTime + 0.3))
270 me.doubleClickListBoxItem(me, me.selectedItem, where);
271 else
272 me.clickListBoxItem(me, me.selectedItem, where);
273
274 me.lastClickedItem = me.selectedItem;
275 me.lastClickedTime = time;
276 }
277 }
278 me.pressed = 0;
279 return 1;
280 }
float time
vector globalToBox(vector v, vector theOrigin, vector theScale)
Definition draw.qc:30

References entity(), eX, eY, globalToBox(), time, and vector.

◆ ListBox_resizeNotify()

void ListBox_resizeNotify ( entity me,
vector relOrigin,
vector relSize,
vector absOrigin,
vector absSize )

Definition at line 43 of file listbox.qc.

44 {
45 SUPER(ListBox).resizeNotify(me, relOrigin, relSize, absOrigin, absSize);
46 me.controlWidth = me.scrollbarWidth / absSize.x;
47 }

References entity(), SUPER, and vector.

◆ ListBox_scrollToItem()

void ListBox_scrollToItem ( entity me,
int i )

Definition at line 8 of file listbox.qc.

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)
31 me.scrollPosTarget = me.getTotalHeight(me) - 1;
32 else
33 me.scrollPosTarget = me.getItemStart(me, i + 1) - 1;
34 }
35 }
float bound(float min, float value, float max)

References bound(), and entity().

◆ ListBox_setFocusedItem()

void ListBox_setFocusedItem ( entity me,
int item )

Definition at line 238 of file listbox.qc.

239 {
240 float focusedItem_save = me.focusedItem;
241 me.focusedItem = (item >= 0 && item < me.nItems) ? item : -1;
242 if (focusedItem_save != me.focusedItem)
243 {
244 me.focusedItemChangeNotify(me);
245 if (me.focusedItem >= 0)
246 me.focusedItemAlpha = SKINALPHA_LISTBOX_FOCUSED;
247 }
248 }

References entity().

◆ ListBox_setSelected()

void ListBox_setSelected ( entity me,
float i )

Definition at line 37 of file listbox.qc.

38 {
39 i = bound(0, i, me.nItems - 1);
40 me.scrollToItem(me, i);
41 me.selectedItem = i;
42 }

References bound(), and entity().

◆ ListBox_updateControlTopBottom()

void ListBox_updateControlTopBottom ( entity me)

Definition at line 290 of file listbox.qc.

291 {
292 // scrollPos is in 0..1 and indicates where the "page" currently shown starts.
293 if (me.getTotalHeight(me) <= 1)
294 {
295 // we don't need no stinkin' scrollbar, we don't need no view control...
296 me.controlTop = 0;
297 me.controlBottom = 1;
298 me.scrollPos = 0;
299 }
300 else
301 {
302 // if scroll pos is above beginning or below end of list, fix it
303 me.scrollPos = bound(0, me.scrollPos, me.getTotalHeight(me) - 1);
304 // now that we know where the list is scrolled to, find out where to draw the control
305 me.controlTop = max(0, me.scrollPos / me.getTotalHeight(me));
306 me.controlBottom = min((me.scrollPos + 1) / me.getTotalHeight(me), 1);
307
308 const float minfactor = 2 * me.controlWidth / me.size.y * me.size.x;
309 float f = me.controlBottom - me.controlTop;
310 if (f < minfactor) // FIXME good default?
311 {
312 // f * X + 1 * (1-X) = minfactor
313 // (f - 1) * X + 1 = minfactor
314 // (f - 1) * X = minfactor - 1
315 // X = (minfactor - 1) / (f - 1)
316 f = (minfactor - 1) / (f - 1);
317 me.controlTop = me.controlTop * f + 0 * (1 - f);
318 me.controlBottom = me.controlBottom * f + 1 * (1 - f);
319 }
320 }
321 }

References bound(), entity(), max(), and min().