Debian Codegraph @

code/ui/ui_shared.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 //
23 // string allocation/management
24
25 #include "ui_shared.h"
26
27 #define SCROLL_TIME_START 500
28 #define SCROLL_TIME_ADJUST 150
29 #define SCROLL_TIME_ADJUSTOFFSET 40
30 #define SCROLL_TIME_FLOOR 20
31
32 typedef struct scrollInfo_s {
33 int nextScrollTime;
34 int nextAdjustTime;
35 int adjustValue;
36 int scrollKey;
37 float xStart;
38 float yStart;
39 itemDef_t *item;
40 qboolean scrollDir;
41 } scrollInfo_t;
42
43 static scrollInfo_t scrollInfo;
44
45 static void (*captureFunc) (void *p) = 0;
46 static void *captureData = NULL;
47 static itemDef_t *itemCapture = NULL; // item that has the mouse captured ( if any )
48
49 displayContextDef_t *DC = NULL;
50
51 static qboolean g_waitingForKey = qfalse;
52 static qboolean g_editingField = qfalse;
53
54 static itemDef_t *g_bindItem = NULL;
55 static itemDef_t *g_editItem = NULL;
56
57 menuDef_t Menus[MAX_MENUS]; // defined menus
58 int menuCount = 0; // how many
59
60 menuDef_t *menuStack[MAX_OPEN_MENUS];
61 int openMenuCount = 0;
62
63 static qboolean debugMode = qfalse;
64
65 #define DOUBLE_CLICK_DELAY 300
66 static int lastListBoxClickTime = 0;
67
68 void Item_RunScript(itemDef_t *item, const char *s);
69 void Item_SetupKeywordHash(void);
70 void Menu_SetupKeywordHash(void);
71 int BindingIDFromName(const char *name);
72 qboolean Item_Bind_HandleKey(itemDef_t *item, int key, qboolean down);
73 itemDef_t *Menu_SetPrevCursorItem(menuDef_t *menu);
74 itemDef_t *Menu_SetNextCursorItem(menuDef_t *menu);
75 static qboolean Menu_OverActiveItem(menuDef_t *menu, float x, float y);
76
77 #ifdef CGAME
78 #define MEM_POOL_SIZE 128 * 1024
79 #else
80 #define MEM_POOL_SIZE 1024 * 1024
81 #endif
82
83 static char memoryPool[MEM_POOL_SIZE];
84 static int allocPoint, outOfMemory;
85
86
87 /*
88 ===============
89 UI_Alloc
90 ===============
91 */
92 void *UI_Alloc( int size ) {
93 char *p;
94
95 if ( allocPoint + size > MEM_POOL_SIZE ) {
96 outOfMemory = qtrue;
97 if (DC->Print) {
98 DC->Print("UI_Alloc: Failure. Out of memory!\n");
99 }
100 //DC->trap_Print(S_COLOR_YELLOW"WARNING: UI Out of Memory!\n");
101 return NULL;
102 }
103
104 p = &memoryPool[allocPoint];
105
106 allocPoint += ( size + 15 ) & ~15;
107
108 return p;
109 }
110
111 /*
112 ===============
113 UI_InitMemory
114 ===============
115 */
116 void UI_InitMemory( void ) {
117 allocPoint = 0;
118 outOfMemory = qfalse;
119 }
120
121 qboolean UI_OutOfMemory( void ) {
122 return outOfMemory;
123 }
124
125
126
127
128
129 #define HASH_TABLE_SIZE 2048
130 /*
131 ================
132 return a hash value for the string
133 ================
134 */
135 static unsigned hashForString(const char *str) {
136 int i;
137 unsigned hash;
138 char letter;
139
140 hash = 0;
141 i = 0;
142 while (str[i] != '\0') {
143 letter = tolower(str[i]);
144 hash+=(unsigned)(letter)*(i+119);
145 i++;
146 }
147 hash &= (HASH_TABLE_SIZE-1);
148 return hash;
149 }
150
151 typedef struct stringDef_s {
152 struct stringDef_s *next;
153 const char *str;
154 } stringDef_t;
155
156 static int strPoolIndex = 0;
157 static char strPool[STRING_POOL_SIZE];
158
159 static int strHandleCount = 0;
160 static stringDef_t *strHandle[HASH_TABLE_SIZE];
161
162
163 const char *String_Alloc(const char *p) {
164 int len;
165 unsigned hash;
166 stringDef_t *str, *last;
167 static const char *staticNULL = "";
168
169 if (p == NULL) {
170 return NULL;
171 }
172
173 if (*p == 0) {
174 return staticNULL;
175 }
176
177 hash = hashForString(p);
178
179 str = strHandle[hash];
180 while (str) {
181 if (strcmp(p, str->str) == 0) {
182 return str->str;
183 }
184 str = str->next;
185 }
186
187 len = strlen(p);
188 if (len + strPoolIndex + 1 < STRING_POOL_SIZE) {
189 int ph = strPoolIndex;
190 strcpy(&strPool[strPoolIndex], p);
191 strPoolIndex += len + 1;
192
193 str = strHandle[hash];
194 last = str;
195 while (str && str->next) {
196 last = str;
197 str = str->next;
198 }
199
200 str = UI_Alloc(sizeof(stringDef_t));
201 if (!str) {
202 return NULL;
203 }
204 str->next = NULL;
205 str->str = &strPool[ph];
206 if (last) {
207 last->next = str;
208 } else {
209 strHandle[hash] = str;
210 }
211 return &strPool[ph];
212 }
213 return NULL;
214 }
215
216 void String_Report(void) {
217 float f;
218 Com_Printf("Memory/String Pool Info\n");
219 Com_Printf("----------------\n");
220 f = strPoolIndex;
221 f /= STRING_POOL_SIZE;
222 f *= 100;
223 Com_Printf("String Pool is %.1f%% full, %i bytes out of %i used.\n", f, strPoolIndex, STRING_POOL_SIZE);
224 f = allocPoint;
225 f /= MEM_POOL_SIZE;
226 f *= 100;
227 Com_Printf("Memory Pool is %.1f%% full, %i bytes out of %i used.\n", f, allocPoint, MEM_POOL_SIZE);
228 }
229
230 /*
231 =================
232 String_Init
233 =================
234 */
235 void String_Init(void) {
236 int i;
237 for (i = 0; i < HASH_TABLE_SIZE; i++) {
238 strHandle[i] = NULL;
239 }
240 strHandleCount = 0;
241 strPoolIndex = 0;
242 menuCount = 0;
243 openMenuCount = 0;
244 UI_InitMemory();
245 Item_SetupKeywordHash();
246 Menu_SetupKeywordHash();
247 if (DC && DC->getBindingBuf) {
248 Controls_GetConfig();
249 }
250 }
251
252 #if 0
253 /*
254 =================
255 PC_SourceWarning
256 =================
257 */
258 static Q_PRINTF_FUNC(2, 3) void PC_SourceWarning(int handle, char *format, ...) {
259 int line;
260 char filename[128];
261 va_list argptr;
262 static char string[4096];
263
264 va_start (argptr, format);
265 Q_vsnprintf (string, sizeof(string), format, argptr);
266 va_end (argptr);
267
268 filename[0] = '\0';
269 line = 0;
270 trap_PC_SourceFileAndLine(handle, filename, &line);
271
272 Com_Printf(S_COLOR_YELLOW "WARNING: %s, line %d: %s\n", filename, line, string);
273 }
274 #endif
275
276 /*
277 =================
278 PC_SourceError
279 =================
280 */
281 static Q_PRINTF_FUNC(2, 3) void PC_SourceError(int handle, char *format, ...) {
282 int line;
283 char filename[128];
284 va_list argptr;
285 static char string[4096];
286
287 va_start (argptr, format);
288 Q_vsnprintf (string, sizeof(string), format, argptr);
289 va_end (argptr);
290
291 filename[0] = '\0';
292 line = 0;
293 trap_PC_SourceFileAndLine(handle, filename, &line);
294
295 Com_Printf(S_COLOR_RED "ERROR: %s, line %d: %s\n", filename, line, string);
296 }
297
298 /*
299 =================
300 LerpColor
301 =================
302 */
303 void LerpColor(vec4_t a, vec4_t b, vec4_t c, float t)
304 {
305 int i;
306
307 // lerp and clamp each component
308 for (i=0; i<4; i++)
309 {
310 c[i] = a[i] + t*(b[i]-a[i]);
311 if (c[i] < 0)
312 c[i] = 0;
313 else if (c[i] > 1.0)
314 c[i] = 1.0;
315 }
316 }
317
318 /*
319 =================
320 Float_Parse
321 =================
322 */
323 qboolean Float_Parse(char **p, float *f) {
324 char *token;
325 token = COM_ParseExt(p, qfalse);
326 if (token && token[0] != 0) {
327 *f = atof(token);
328 return qtrue;
329 } else {
330 return qfalse;
331 }
332 }
333
334 /*
335 =================
336 PC_Float_Parse
337 =================
338 */
339 qboolean PC_Float_Parse(int handle, float *f) {
340 pc_token_t token;
341 int negative = qfalse;
342
343 if (!trap_PC_ReadToken(handle, &token))
344 return qfalse;
345 if (token.string[0] == '-') {
346 if (!trap_PC_ReadToken(handle, &token))
347 return qfalse;
348 negative = qtrue;
349 }
350 if (token.type != TT_NUMBER) {
351 PC_SourceError(handle, "expected float but found %s", token.string);
352 return qfalse;
353 }
354 if (negative)
355 *f = -token.floatvalue;
356 else
357 *f = token.floatvalue;
358 return qtrue;
359 }
360
361 /*
362 =================
363 Color_Parse
364 =================
365 */
366 qboolean Color_Parse(char **p, vec4_t *c) {
367 int i;
368 float f;
369
370 for (i = 0; i < 4; i++) {
371 if (!Float_Parse(p, &f)) {
372 return qfalse;
373 }
374 (*c)[i] = f;
375 }
376 return qtrue;
377 }
378
379 /*
380 =================
381 PC_Color_Parse
382 =================
383 */
384 qboolean PC_Color_Parse(int handle, vec4_t *c) {
385 int i;
386 float f;
387
388 for (i = 0; i < 4; i++) {
389 if (!PC_Float_Parse(handle, &f)) {
390 return qfalse;
391 }
392 (*c)[i] = f;
393 }
394 return qtrue;
395 }
396
397 /*
398 =================
399 Int_Parse
400 =================
401 */
402 qboolean Int_Parse(char **p, int *i) {
403 char *token;
404 token = COM_ParseExt(p, qfalse);
405
406 if (token && token[0] != 0) {
407 *i = atoi(token);
408 return qtrue;
409 } else {
410 return qfalse;
411 }
412 }
413
414 /*
415 =================
416 PC_Int_Parse
417 =================
418 */
419 qboolean PC_Int_Parse(int handle, int *i) {
420 pc_token_t token;
421 int negative = qfalse;
422
423 if (!i)
424 return qfalse;
425 if (!trap_PC_ReadToken(handle, &token))
426 return qfalse;
427 if (token.string[0] == '-') {
428 if (!trap_PC_ReadToken(handle, &token))
429 return qfalse;
430 negative = qtrue;
431 }
432 if (token.type != TT_NUMBER) {
433 PC_SourceError(handle, "expected integer but found %s", token.string);
434 return qfalse;
435 }
436 *i = token.intvalue;
437 if (negative)
438 *i = - *i;
439 return qtrue;
440 }
441
442 /*
443 =================
444 Rect_Parse
445 =================
446 */
447 qboolean Rect_Parse(char **p, rectDef_t *r) {
448 if (Float_Parse(p, &r->x)) {
449 if (Float_Parse(p, &r->y)) {
450 if (Float_Parse(p, &r->w)) {
451 if (Float_Parse(p, &r->h)) {
452 return qtrue;
453 }
454 }
455 }
456 }
457 return qfalse;
458 }
459
460 /*
461 =================
462 PC_Rect_Parse
463 =================
464 */
465 qboolean PC_Rect_Parse(int handle, rectDef_t *r) {
466 if (PC_Float_Parse(handle, &r->x)) {
467 if (PC_Float_Parse(handle, &r->y)) {
468 if (PC_Float_Parse(handle, &r->w)) {
469 if (PC_Float_Parse(handle, &r->h)) {
470 return qtrue;
471 }
472 }
473 }
474 }
475 return qfalse;
476 }
477
478 /*
479 =================
480 String_Parse
481 =================
482 */
483 qboolean String_Parse(char **p, const char **out) {
484 char *token;
485
486 token = COM_ParseExt(p, qfalse);
487 if (token && token[0] != 0) {
488 *(out) = String_Alloc(token);
489 return qtrue;
490 }
491 return qfalse;
492 }
493
494 /*
495 =================
496 PC_String_Parse
497 =================
498 */
499 qboolean PC_String_Parse(int handle, const char **out) {
500 pc_token_t token;
501
502 if (!trap_PC_ReadToken(handle, &token))
503 return qfalse;
504
505 *(out) = String_Alloc(token.string);
506 return qtrue;
507 }
508
509 /*
510 =================
511 PC_Script_Parse
512 =================
513 */
514 qboolean PC_Script_Parse(int handle, const char **out) {
515 char script[1024];
516 pc_token_t token;
517
518 memset(script, 0, sizeof(script));
519 // scripts start with { and have ; separated command lists.. commands are command, arg..
520 // basically we want everything between the { } as it will be interpreted at run time
521
522 if (!trap_PC_ReadToken(handle, &token))
523 return qfalse;
524 if (Q_stricmp(token.string, "{") != 0) {
525 return qfalse;
526 }
527
528 while ( 1 ) {
529 if (!trap_PC_ReadToken(handle, &token))
530 return qfalse;
531
532 if (Q_stricmp(token.string, "}") == 0) {
533 *out = String_Alloc(script);
534 return qtrue;
535 }
536
537 if (token.string[1] != '\0') {
538 Q_strcat(script, 1024, va("\"%s\"", token.string));
539 } else {
540 Q_strcat(script, 1024, token.string);
541 }
542 Q_strcat(script, 1024, " ");
543 }
544 return qfalse;
545 }
546
547 // display, window, menu, item code
548 //
549
550 /*
551 ==================
552 Init_Display
553
554 Initializes the display with a structure to all the drawing routines
555 ==================
556 */
557 void Init_Display(displayContextDef_t *dc) {
558 DC = dc;
559 }
560
561
562
563 // type and style painting
564
565 void GradientBar_Paint(rectDef_t *rect, vec4_t color) {
566 // gradient bar takes two paints
567 DC->setColor( color );
568 DC->drawHandlePic(rect->x, rect->y, rect->w, rect->h, DC->Assets.gradientBar);
569 DC->setColor( NULL );
570 }
571
572
573 /*
574 ==================
575 Window_Init
576
577 Initializes a window structure ( windowDef_t ) with defaults
578
579 ==================
580 */
581 void Window_Init(Window *w) {
582 memset(w, 0, sizeof(windowDef_t));
583 w->borderSize = 1;
584 w->foreColor[0] = w->foreColor[1] = w->foreColor[2] = w->foreColor[3] = 1.0;
585 w->cinematic = -1;
586 }
587
588 void Fade(int *flags, float *f, float clamp, int *nextTime, int offsetTime, qboolean bFlags, float fadeAmount) {
589 if (*flags & (WINDOW_FADINGOUT | WINDOW_FADINGIN)) {
590 if (DC->realTime > *nextTime) {
591 *nextTime = DC->realTime + offsetTime;
592 if (*flags & WINDOW_FADINGOUT) {
593 *f -= fadeAmount;
594 if (bFlags && *f <= 0.0) {
595 *flags &= ~(WINDOW_FADINGOUT | WINDOW_VISIBLE);
596 }
597 } else {
598 *f += fadeAmount;
599 if (*f >= clamp) {
600 *f = clamp;
601 if (bFlags) {
602 *flags &= ~WINDOW_FADINGIN;
603 }
604 }
605 }
606 }
607 }
608 }
609
610
611
612 void Window_Paint(Window *w, float fadeAmount, float fadeClamp, float fadeCycle) {
613 //float bordersize = 0;
614 vec4_t color = {0};
615 rectDef_t fillRect;
616
617 if ( w == NULL ) {
618 return;
619 }
620
621 if (debugMode) {
622 color[0] = color[1] = color[2] = color[3] = 1;
623 DC->drawRect(w->rect.x, w->rect.y, w->rect.w, w->rect.h, 1, color);
624 }
625
626 if (w->style == 0 && w->border == 0) {
627 return;
628 }
629
630 fillRect = w->rect;
631
632 if (w->border != 0) {
633 fillRect.x += w->borderSize;
634 fillRect.y += w->borderSize;
635 fillRect.w -= w->borderSize + 1;
636 fillRect.h -= w->borderSize + 1;
637 }
638
639 if (w->style == WINDOW_STYLE_FILLED) {
640 // box, but possible a shader that needs filled
641 if (w->background) {
642 Fade(&w->flags, &w->backColor[3], fadeClamp, &w->nextTime, fadeCycle, qtrue, fadeAmount);
643 DC->setColor(w->backColor);
644 DC->drawHandlePic(fillRect.x, fillRect.y, fillRect.w, fillRect.h, w->background);
645 DC->setColor(NULL);
646 } else {
647 DC->fillRect(fillRect.x, fillRect.y, fillRect.w, fillRect.h, w->backColor);
648 }
649 } else if (w->style == WINDOW_STYLE_GRADIENT) {
650 GradientBar_Paint(&fillRect, w->backColor);
651 // gradient bar
652 } else if (w->style == WINDOW_STYLE_SHADER) {
653 if (w->flags & WINDOW_FORECOLORSET) {
654 DC->setColor(w->foreColor);
655 }
656 DC->drawHandlePic(fillRect.x, fillRect.y, fillRect.w, fillRect.h, w->background);
657 DC->setColor(NULL);
658 } else if (w->style == WINDOW_STYLE_TEAMCOLOR) {
659 if (DC->getTeamColor) {
660 DC->getTeamColor(&color);
661 DC->fillRect(fillRect.x, fillRect.y, fillRect.w, fillRect.h, color);
662 }
663 } else if (w->style == WINDOW_STYLE_CINEMATIC) {
664 if (w->cinematic == -1) {
665 w->cinematic = DC->playCinematic(w->cinematicName, fillRect.x, fillRect.y, fillRect.w, fillRect.h);
666 if (w->cinematic == -1) {
667 w->cinematic = -2;
668 }
669 }
670 if (w->cinematic >= 0) {
671 DC->runCinematicFrame(w->cinematic);
672 DC->drawCinematic(w->cinematic, fillRect.x, fillRect.y, fillRect.w, fillRect.h);
673 }
674 }
675
676 if (w->border == WINDOW_BORDER_FULL) {
677 // full
678 // HACK HACK HACK
679 if (w->style == WINDOW_STYLE_TEAMCOLOR) {
680 if (color[0] > 0) {
681 // red
682 color[0] = 1;
683 color[1] = color[2] = .5;
684
685 } else {
686 color[2] = 1;
687 color[0] = color[1] = .5;
688 }
689 color[3] = 1;
690 DC->drawRect(w->rect.x, w->rect.y, w->rect.w, w->rect.h, w->borderSize, color);
691 } else {
692 DC->drawRect(w->rect.x, w->rect.y, w->rect.w, w->rect.h, w->borderSize, w->borderColor);
693 }
694 } else if (w->border == WINDOW_BORDER_HORZ) {
695 // top/bottom
696 DC->setColor(w->borderColor);
697 DC->drawTopBottom(w->rect.x, w->rect.y, w->rect.w, w->rect.h, w->borderSize);
698 DC->setColor( NULL );
699 } else if (w->border == WINDOW_BORDER_VERT) {
700 // left right
701 DC->setColor(w->borderColor);
702 DC->drawSides(w->rect.x, w->rect.y, w->rect.w, w->rect.h, w->borderSize);
703 DC->setColor( NULL );
704 } else if (w->border == WINDOW_BORDER_KCGRADIENT) {
705 // this is just two gradient bars along each horz edge
706 rectDef_t r = w->rect;
707 r.h = w->borderSize;
708 GradientBar_Paint(&r, w->borderColor);
709 r.y = w->rect.y + w->rect.h - 1;
710 GradientBar_Paint(&r, w->borderColor);
711 }
712
713 }
714
715
716 void Item_SetScreenCoords(itemDef_t *item, float x, float y) {
717
718 if (item == NULL) {
719 return;
720 }
721
722 if (item->window.border != 0) {
723 x += item->window.borderSize;
724 y += item->window.borderSize;
725 }
726
727 item->window.rect.x = x + item->window.rectClient.x;
728 item->window.rect.y = y + item->window.rectClient.y;
729 item->window.rect.w = item->window.rectClient.w;
730 item->window.rect.h = item->window.rectClient.h;
731
732 // force the text rects to recompute
733 item->textRect.w = 0;
734 item->textRect.h = 0;
735 }
736
737 // FIXME: consolidate this with nearby stuff
738 void Item_UpdatePosition(itemDef_t *item) {
739 float x, y;
740 menuDef_t *menu;
741
742 if (item == NULL || item->parent == NULL) {
743 return;
744 }
745
746 menu = item->parent;
747
748 x = menu->window.rect.x;
749 y = menu->window.rect.y;
750
751 if (menu->window.border != 0) {
752 x += menu->window.borderSize;
753 y += menu->window.borderSize;
754 }
755
756 Item_SetScreenCoords(item, x, y);
757
758 }
759
760 // menus
761 void Menu_UpdatePosition(menuDef_t *menu) {
762 int i;
763 float x, y;
764
765 if (menu == NULL) {
766 return;
767 }
768
769 x = menu->window.rect.x;
770 y = menu->window.rect.y;
771 if (menu->window.border != 0) {
772 x += menu->window.borderSize;
773 y += menu->window.borderSize;
774 }
775
776 for (i = 0; i < menu->itemCount; i++) {
777 Item_SetScreenCoords(menu->items[i], x, y);
778 }
779 }
780
781 void Menu_PostParse(menuDef_t *menu) {
782 if (menu == NULL) {
783 return;
784 }
785 if (menu->fullScreen) {
786 menu->window.rect.x = 0;
787 menu->window.rect.y = 0;
788 menu->window.rect.w = 640;
789 menu->window.rect.h = 480;
790 }
791 Menu_UpdatePosition(menu);
792 }
793
794 itemDef_t *Menu_ClearFocus(menuDef_t *menu) {
795 int i;
796 itemDef_t *ret = NULL;
797
798 if (menu == NULL) {
799 return NULL;
800 }
801
802 for (i = 0; i < menu->itemCount; i++) {
803 if (menu->items[i]->window.flags & WINDOW_HASFOCUS) {
804 ret = menu->items[i];
805 }
806 menu->items[i]->window.flags &= ~WINDOW_HASFOCUS;
807 if (menu->items[i]->leaveFocus) {
808 Item_RunScript(menu->items[i], menu->items[i]->leaveFocus);
809 }
810 }
811
812 return ret;
813 }
814
815 qboolean IsVisible(int flags) {
816 return (flags & WINDOW_VISIBLE && !(flags & WINDOW_FADINGOUT));
817 }
818
819 qboolean Rect_ContainsPoint(rectDef_t *rect, float x, float y) {
820 if (rect) {
821 if (x > rect->x && x < rect->x + rect->w && y > rect->y && y < rect->y + rect->h) {
822 return qtrue;
823 }
824 }
825 return qfalse;
826 }
827
828 int Menu_ItemsMatchingGroup(menuDef_t *menu, const char *name) {
829 int i;
830 int count = 0;
831 for (i = 0; i < menu->itemCount; i++) {
832 if (Q_stricmp(menu->items[i]->window.name, name) == 0 || (menu->items[i]->window.group && Q_stricmp(menu->items[i]->window.group, name) == 0)) {
833 count++;
834 }
835 }
836 return count;
837 }
838
839 itemDef_t *Menu_GetMatchingItemByNumber(menuDef_t *menu, int index, const char *name) {
840 int i;
841 int count = 0;
842 for (i = 0; i < menu->itemCount; i++) {
843 if (Q_stricmp(menu->items[i]->window.name, name) == 0 || (menu->items[i]->window.group && Q_stricmp(menu->items[i]->window.group, name) == 0)) {
844 if (count == index) {
845 return menu->items[i];
846 }
847 count++;
848 }
849 }
850 return NULL;
851 }
852
853
854
855 void Script_SetColor(itemDef_t *item, char **args) {
856 const char *name;
857 int i;
858 float f;
859 vec4_t *out;
860 // expecting type of color to set and 4 args for the color
861 if (String_Parse(args, &name)) {
862 out = NULL;
863 if (Q_stricmp(name, "backcolor") == 0) {
864 out = &item->window.backColor;
865 item->window.flags |= WINDOW_BACKCOLORSET;
866 } else if (Q_stricmp(name, "forecolor") == 0) {
867 out = &item->window.foreColor;
868 item->window.flags |= WINDOW_FORECOLORSET;
869 } else if (Q_stricmp(name, "bordercolor") == 0) {
870 out = &item->window.borderColor;
871 }
872
873 if (out) {
874 for (i = 0; i < 4; i++) {
875 if (!Float_Parse(args, &f)) {
876 return;
877 }
878 (*out)[i] = f;
879 }
880 }
881 }
882 }
883
884 void Script_SetAsset(itemDef_t *item, char **args) {
885 const char *name;
886 // expecting name to set asset to
887 if (String_Parse(args, &name)) {
888 // check for a model
889 if (item->type == ITEM_TYPE_MODEL) {
890 }
891 }
892 }
893
894 void Script_SetBackground(itemDef_t *item, char **args) {
895 const char *name;
896 // expecting name to set asset to
897 if (String_Parse(args, &name)) {
898 item->window.background = DC->registerShaderNoMip(name);
899 }
900 }
901
902
903
904
905 itemDef_t *Menu_FindItemByName(menuDef_t *menu, const char *p) {
906 int i;
907 if (menu == NULL || p == NULL) {
908 return NULL;
909 }
910
911 for (i = 0; i < menu->itemCount; i++) {
912 if (Q_stricmp(p, menu->items[i]->window.name) == 0) {
913 return menu->items[i];
914 }
915 }
916
917 return NULL;
918 }
919
920 void Script_SetTeamColor(itemDef_t *item, char **args) {
921 if (DC->getTeamColor) {
922 int i;
923 vec4_t color;
924 DC->getTeamColor(&color);
925 for (i = 0; i < 4; i++) {
926 item->window.backColor[i] = color[i];
927 }
928 }
929 }
930
931 void Script_SetItemColor(itemDef_t *item, char **args) {
932 const char *itemname;
933 const char *name;
934 vec4_t color;
935 int i;
936 vec4_t *out;
937 // expecting type of color to set and 4 args for the color
938 if (String_Parse(args, &itemname) && String_Parse(args, &name)) {
939 itemDef_t *item2;
940 int j;
941 int count = Menu_ItemsMatchingGroup(item->parent, itemname);
942
943 if (!Color_Parse(args, &color)) {
944 return;
945 }
946
947 for (j = 0; j < count; j++) {
948 item2 = Menu_GetMatchingItemByNumber(item->parent, j, itemname);
949 if (item2 != NULL) {
950 out = NULL;
951 if (Q_stricmp(name, "backcolor") == 0) {
952 out = &item2->window.backColor;
953 } else if (Q_stricmp(name, "forecolor") == 0) {
954 out = &item2->window.foreColor;
955 item2->window.flags |= WINDOW_FORECOLORSET;
956 } else if (Q_stricmp(name, "bordercolor") == 0) {
957 out = &item2->window.borderColor;
958 }
959
960 if (out) {
961 for (i = 0; i < 4; i++) {
962 (*out)[i] = color[i];
963 }
964 }
965 }
966 }
967 }
968 }
969
970
971 void Menu_ShowItemByName(menuDef_t *menu, const char *p, qboolean bShow) {
972 itemDef_t *item;
973 int i;
974 int count = Menu_ItemsMatchingGroup(menu, p);
975 for (i = 0; i < count; i++) {
976 item = Menu_GetMatchingItemByNumber(menu, i, p);
977 if (item != NULL) {
978 if (bShow) {
979 item->window.flags |= WINDOW_VISIBLE;
980 } else {
981 item->window.flags &= ~WINDOW_VISIBLE;
982 // stop cinematics playing in the window
983 if (item->window.cinematic >= 0) {
984 DC->stopCinematic(item->window.cinematic);
985 item->window.cinematic = -1;
986 }
987 }
988 }
989 }
990 }
991
992 void Menu_FadeItemByName(menuDef_t *menu, const char *p, qboolean fadeOut) {
993 itemDef_t *item;
994 int i;
995 int count = Menu_ItemsMatchingGroup(menu, p);
996 for (i = 0; i < count; i++) {
997 item = Menu_GetMatchingItemByNumber(menu, i, p);
998 if (item != NULL) {
999 if (fadeOut) {
1000 item->window.flags |= (WINDOW_FADINGOUT | WINDOW_VISIBLE);
1001 item->window.flags &= ~WINDOW_FADINGIN;
1002 } else {
1003 item->window.flags |= (WINDOW_VISIBLE | WINDOW_FADINGIN);
1004 item->window.flags &= ~WINDOW_FADINGOUT;
1005 }
1006 }
1007 }
1008 }
1009
1010 menuDef_t *Menus_FindByName(const char *p) {
1011 int i;
1012 for (i = 0; i < menuCount; i++) {
1013 if (Q_stricmp(Menus[i].window.name, p) == 0) {
1014 return &Menus[i];
1015 }
1016 }
1017 return NULL;
1018 }
1019
1020 void Menus_ShowByName(const char *p) {
1021 menuDef_t *menu = Menus_FindByName(p);
1022 if (menu) {
1023 Menus_Activate(menu);
1024 }
1025 }
1026
1027 void Menus_OpenByName(const char *p) {
1028 Menus_ActivateByName(p);
1029 }
1030
1031 static void Menu_RunCloseScript(menuDef_t *menu) {
1032 if (menu && menu->window.flags & WINDOW_VISIBLE && menu->onClose) {
1033 itemDef_t item;
1034 item.parent = menu;
1035 Item_RunScript(&item, menu->onClose);
1036 }
1037 }
1038
1039 void Menus_CloseByName(const char *p) {
1040 menuDef_t *menu = Menus_FindByName(p);
1041 if (menu != NULL) {
1042 Menu_RunCloseScript(menu);
1043 menu->window.flags &= ~(WINDOW_VISIBLE | WINDOW_HASFOCUS);
1044 }
1045 }
1046
1047 void Menus_CloseAll(void) {
1048 int i;
1049 for (i = 0; i < menuCount; i++) {
1050 Menu_RunCloseScript(&Menus[i]);
1051 Menus[i].window.flags &= ~(WINDOW_HASFOCUS | WINDOW_VISIBLE);
1052 }
1053 }
1054
1055
1056 void Script_Show(itemDef_t *item, char **args) {
1057 const char *name;
1058 if (String_Parse(args, &name)) {
1059 Menu_ShowItemByName(item->parent, name, qtrue);
1060 }
1061 }
1062
1063 void Script_Hide(itemDef_t *item, char **args) {
1064 const char *name;
1065 if (String_Parse(args, &name)) {
1066 Menu_ShowItemByName(item->parent, name, qfalse);
1067 }
1068 }
1069
1070 void Script_FadeIn(itemDef_t *item, char **args) {
1071 const char *name;
1072 if (String_Parse(args, &name)) {
1073 Menu_FadeItemByName(item->parent, name, qfalse);
1074 }
1075 }
1076
1077 void Script_FadeOut(itemDef_t *item, char **args) {
1078 const char *name;
1079 if (String_Parse(args, &name)) {
1080 Menu_FadeItemByName(item->parent, name, qtrue);
1081 }
1082 }
1083
1084
1085
1086 void Script_Open(itemDef_t *item, char **args) {
1087 const char *name;
1088 if (String_Parse(args, &name)) {
1089 Menus_OpenByName(name);
1090 }
1091 }
1092
1093 void Script_ConditionalOpen(itemDef_t *item, char **args) {
1094 const char *cvar;
1095 const char *name1;
1096 const char *name2;
1097 float val;
1098
1099 if ( String_Parse(args, &cvar) && String_Parse(args, &name1) && String_Parse(args, &name2) ) {
1100 val = DC->getCVarValue( cvar );
1101 if ( val == 0.f ) {
1102 Menus_OpenByName(name2);
1103 } else {
1104 Menus_OpenByName(name1);
1105 }
1106 }
1107 }
1108
1109 void Script_Close(itemDef_t *item, char **args) {
1110 const char *name;
1111 if (String_Parse(args, &name)) {
1112 Menus_CloseByName(name);
1113 }
1114 }
1115
1116 void Menu_TransitionItemByName(menuDef_t *menu, const char *p, rectDef_t rectFrom, rectDef_t rectTo, int time, float amt) {
1117 itemDef_t *item;
1118 int i;
1119 int count = Menu_ItemsMatchingGroup(menu, p);
1120 for (i = 0; i < count; i++) {
1121 item = Menu_GetMatchingItemByNumber(menu, i, p);
1122 if (item != NULL) {
1123 item->window.flags |= (WINDOW_INTRANSITION | WINDOW_VISIBLE);
1124 item->window.offsetTime = time;
1125 memcpy(&item->window.rectClient, &rectFrom, sizeof(rectDef_t));
1126 memcpy(&item->window.rectEffects, &rectTo, sizeof(rectDef_t));
1127 item->window.rectEffects2.x = fabs(rectTo.x - rectFrom.x) / amt;
1128 item->window.rectEffects2.y = fabs(rectTo.y - rectFrom.y) / amt;
1129 item->window.rectEffects2.w = fabs(rectTo.w - rectFrom.w) / amt;
1130 item->window.rectEffects2.h = fabs(rectTo.h - rectFrom.h) / amt;
1131 Item_UpdatePosition(item);
1132 }
1133 }
1134 }
1135
1136
1137 void Script_Transition(itemDef_t *item, char **args) {
1138 const char *name;
1139 rectDef_t rectFrom, rectTo;
1140 int time;
1141 float amt;
1142
1143 if (String_Parse(args, &name)) {
1144 if ( Rect_Parse(args, &rectFrom) && Rect_Parse(args, &rectTo) && Int_Parse(args, &time) && Float_Parse(args, &amt)) {
1145 Menu_TransitionItemByName(item->parent, name, rectFrom, rectTo, time, amt);
1146 }
1147 }
1148 }
1149
1150
1151 void Menu_OrbitItemByName(menuDef_t *menu, const char *p, float x, float y, float cx, float cy, int time) {
1152 itemDef_t *item;
1153 int i;
1154 int count = Menu_ItemsMatchingGroup(menu, p);
1155 for (i = 0; i < count; i++) {
1156 item = Menu_GetMatchingItemByNumber(menu, i, p);
1157 if (item != NULL) {
1158 item->window.flags |= (WINDOW_ORBITING | WINDOW_VISIBLE);
1159 item->window.offsetTime = time;
1160 item->window.rectEffects.x = cx;
1161 item->window.rectEffects.y = cy;
1162 item->window.rectClient.x = x;
1163 item->window.rectClient.y = y;
1164 Item_UpdatePosition(item);
1165 }
1166 }
1167 }
1168
1169
1170 void Script_Orbit(itemDef_t *item, char **args) {
1171 const char *name;
1172 float cx, cy, x, y;
1173 int time;
1174
1175 if (String_Parse(args, &name)) {
1176 if ( Float_Parse(args, &x) && Float_Parse(args, &y) && Float_Parse(args, &cx) && Float_Parse(args, &cy) && Int_Parse(args, &time) ) {
1177 Menu_OrbitItemByName(item->parent, name, x, y, cx, cy, time);
1178 }
1179 }
1180 }
1181
1182
1183
1184 void Script_SetFocus(itemDef_t *item, char **args) {
1185 const char *name;
1186 itemDef_t *focusItem;
1187
1188 if (String_Parse(args, &name)) {
1189 focusItem = Menu_FindItemByName(item->parent, name);
1190 if (focusItem && !(focusItem->window.flags & WINDOW_DECORATION) && !(focusItem->window.flags & WINDOW_HASFOCUS)) {
1191 Menu_ClearFocus(item->parent);
1192 focusItem->window.flags |= WINDOW_HASFOCUS;
1193 if (focusItem->onFocus) {
1194 Item_RunScript(focusItem, focusItem->onFocus);
1195 }
1196 if (DC->Assets.itemFocusSound) {
1197 DC->startLocalSound( DC->Assets.itemFocusSound, CHAN_LOCAL_SOUND );
1198 }
1199 }
1200 }
1201 }
1202
1203 void Script_SetPlayerModel(itemDef_t *item, char **args) {
1204 const char *name;
1205 if (String_Parse(args, &name)) {
1206 DC->setCVar("team_model", name);
1207 }
1208 }
1209
1210 void Script_SetPlayerHead(itemDef_t *item, char **args) {
1211 const char *name;
1212 if (String_Parse(args, &name)) {
1213 DC->setCVar("team_headmodel", name);
1214 }
1215 }
1216
1217 void Script_SetCvar(itemDef_t *item, char **args) {
1218 const char *cvar, *val;
1219 if (String_Parse(args, &cvar) && String_Parse(args, &val)) {
1220 DC->setCVar(cvar, val);
1221 }
1222
1223 }
1224
1225 void Script_Exec(itemDef_t *item, char **args) {
1226 const char *val;
1227 if (String_Parse(args, &val)) {
1228 DC->executeText(EXEC_APPEND, va("%s ; ", val));
1229 }
1230 }
1231
1232 void Script_Play(itemDef_t *item, char **args) {
1233 const char *val;
1234 if (String_Parse(args, &val)) {
1235 DC->startLocalSound(DC->registerSound(val, qfalse), CHAN_LOCAL_SOUND);
1236 }
1237 }
1238
1239 void Script_playLooped(itemDef_t *item, char **args) {
1240 const char *val;
1241 if (String_Parse(args, &val)) {
1242 DC->stopBackgroundTrack();
1243 DC->startBackgroundTrack(val, val);
1244 }
1245 }
1246
1247
1248 commandDef_t commandList[] =
1249 {
1250 {"fadein", &Script_FadeIn}, // group/name
1251 {"fadeout", &Script_FadeOut}, // group/name
1252 {"show", &Script_Show}, // group/name
1253 {"hide", &Script_Hide}, // group/name
1254 {"setcolor", &Script_SetColor}, // works on this
1255 {"open", &Script_Open}, // menu
1256 {"conditionalopen", &Script_ConditionalOpen}, // menu
1257 {"close", &Script_Close}, // menu
1258 {"setasset", &Script_SetAsset}, // works on this
1259 {"setbackground", &Script_SetBackground}, // works on this
1260 {"setitemcolor", &Script_SetItemColor}, // group/name
1261 {"setteamcolor", &Script_SetTeamColor}, // sets this background color to team color
1262 {"setfocus", &Script_SetFocus}, // sets this background color to team color
1263 {"setplayermodel", &Script_SetPlayerModel}, // sets this background color to team color
1264 {"setplayerhead", &Script_SetPlayerHead}, // sets this background color to team color
1265 {"transition", &Script_Transition}, // group/name
1266 {"setcvar", &Script_SetCvar}, // group/name
1267 {"exec", &Script_Exec}, // group/name
1268 {"play", &Script_Play}, // group/name
1269 {"playlooped", &Script_playLooped}, // group/name
1270 {"orbit", &Script_Orbit} // group/name
1271 };
1272
1273 int scriptCommandCount = ARRAY_LEN(commandList);
1274
1275
1276 void Item_RunScript(itemDef_t *item, const char *s) {
1277 char script[1024], *p;
1278 int i;
1279 qboolean bRan;
1280 memset(script, 0, sizeof(script));
1281 if (item && s && s[0]) {
1282 Q_strcat(script, 1024, s);
1283 p = script;
1284 while (1) {
1285 const char *command;
1286 // expect command then arguments, ; ends command, NULL ends script
1287 if (!String_Parse(&p, &command)) {
1288 return;
1289 }
1290
1291 if (command[0] == ';' && command[1] == '\0') {
1292 continue;
1293 }
1294
1295 bRan = qfalse;
1296 for (i = 0; i < scriptCommandCount; i++) {
1297 if (Q_stricmp(command, commandList[i].name) == 0) {
1298 (commandList[i].handler(item, &p));
1299 bRan = qtrue;
1300 break;
1301 }
1302 }
1303 // not in our auto list, pass to handler
1304 if (!bRan) {
1305 DC->runScript(&p);
1306 }
1307 }
1308 }
1309 }
1310
1311
1312 qboolean Item_EnableShowViaCvar(itemDef_t *item, int flag) {
1313 char script[1024], *p;
1314 memset(script, 0, sizeof(script));
1315 if (item && item->enableCvar && *item->enableCvar && item->cvarTest && *item->cvarTest) {
1316 char buff[1024];
1317 DC->getCVarString(item->cvarTest, buff, sizeof(buff));
1318
1319 Q_strcat(script, 1024, item->enableCvar);
1320 p = script;
1321 while (1) {
1322 const char *val;
1323 // expect value then ; or NULL, NULL ends list
1324 if (!String_Parse(&p, &val)) {
1325 return (item->cvarFlags & flag) ? qfalse : qtrue;
1326 }
1327
1328 if (val[0] == ';' && val[1] == '\0') {
1329 continue;
1330 }
1331
1332 // enable it if any of the values are true
1333 if (item->cvarFlags & flag) {
1334 if (Q_stricmp(buff, val) == 0) {
1335 return qtrue;
1336 }
1337 } else {
1338 // disable it if any of the values are true
1339 if (Q_stricmp(buff, val) == 0) {
1340 return qfalse;
1341 }
1342 }
1343
1344 }
1345 return (item->cvarFlags & flag) ? qfalse : qtrue;
1346 }
1347 return qtrue;
1348 }
1349
1350
1351 // will optionaly set focus to this item
1352 qboolean Item_SetFocus(itemDef_t *item, float x, float y) {
1353 int i;
1354 itemDef_t *oldFocus;
1355 sfxHandle_t *sfx = &DC->Assets.itemFocusSound;
1356 qboolean playSound = qfalse;
1357 menuDef_t *parent;
1358 // sanity check, non-null, not a decoration and does not already have the focus
1359 if (item == NULL || item->window.flags & WINDOW_DECORATION || item->window.flags & WINDOW_HASFOCUS || !(item->window.flags & WINDOW_VISIBLE)) {
1360 return qfalse;
1361 }
1362
1363 // this can be NULL
1364 parent = (menuDef_t*)item->parent;
1365
1366 // items can be enabled and disabled based on cvars
1367 if (item->cvarFlags & (CVAR_ENABLE | CVAR_DISABLE) && !Item_EnableShowViaCvar(item, CVAR_ENABLE)) {
1368 return qfalse;
1369 }
1370
1371 if (item->cvarFlags & (CVAR_SHOW | CVAR_HIDE) && !Item_EnableShowViaCvar(item, CVAR_SHOW)) {
1372 return qfalse;
1373 }
1374
1375 oldFocus = Menu_ClearFocus(item->parent);
1376
1377 if (item->type == ITEM_TYPE_TEXT) {
1378 rectDef_t r;
1379 r = item->textRect;
1380 r.y -= r.h;
1381 if (Rect_ContainsPoint(&r, x, y)) {
1382 item->window.flags |= WINDOW_HASFOCUS;
1383 if (item->focusSound) {
1384 sfx = &item->focusSound;
1385 }
1386 playSound = qtrue;
1387 } else {
1388 if (oldFocus) {
1389 oldFocus->window.flags |= WINDOW_HASFOCUS;
1390 if (oldFocus->onFocus) {
1391 Item_RunScript(oldFocus, oldFocus->onFocus);
1392 }
1393 }
1394 }
1395 } else {
1396 item->window.flags |= WINDOW_HASFOCUS;
1397 if (item->onFocus) {
1398 Item_RunScript(item, item->onFocus);
1399 }
1400 if (item->focusSound) {
1401 sfx = &item->focusSound;
1402 }
1403 playSound = qtrue;
1404 }
1405
1406 if (playSound && sfx) {
1407 DC->startLocalSound( *sfx, CHAN_LOCAL_SOUND );
1408 }
1409
1410 for (i = 0; i < parent->itemCount; i++) {
1411 if (parent->items[i] == item) {
1412 parent->cursorItem = i;
1413 break;
1414 }
1415 }
1416
1417 return qtrue;
1418 }
1419
1420 int Item_ListBox_MaxScroll(itemDef_t *item) {
1421 listBoxDef_t *listPtr = (listBoxDef_t*)item->typeData;
1422 int count = DC->feederCount(item->special);
1423 int max;
1424
1425 if (item->window.flags & WINDOW_HORIZONTAL) {
1426 max = count - (item->window.rect.w / listPtr->elementWidth) + 1;
1427 }
1428 else {
1429 max = count - (item->window.rect.h / listPtr->elementHeight) + 1;
1430 }
1431 if (max < 0) {
1432 return 0;
1433 }
1434 return max;
1435 }
1436
1437 int Item_ListBox_ThumbPosition(itemDef_t *item) {
1438 float max, pos, size;
1439 listBoxDef_t *listPtr = (listBoxDef_t*)item->typeData;
1440
1441 max = Item_ListBox_MaxScroll(item);
1442 if (item->window.flags & WINDOW_HORIZONTAL) {
1443 size = item->window.rect.w - (SCROLLBAR_SIZE * 2) - 2;
1444 if (max > 0) {
1445 pos = (size-SCROLLBAR_SIZE) / (float) max;
1446 } else {
1447 pos = 0;
1448 }
1449 pos *= listPtr->startPos;
1450 return item->window.rect.x + 1 + SCROLLBAR_SIZE + pos;
1451 }
1452 else {
1453 size = item->window.rect.h - (SCROLLBAR_SIZE * 2) - 2;
1454 if (max > 0) {
1455 pos = (size-SCROLLBAR_SIZE) / (float) max;
1456 } else {
1457 pos = 0;
1458 }
1459 pos *= listPtr->startPos;
1460 return item->window.rect.y + 1 + SCROLLBAR_SIZE + pos;
1461 }
1462 }
1463
1464 int Item_ListBox_ThumbDrawPosition(itemDef_t *item) {
1465 int min, max;
1466
1467 if (itemCapture == item) {
1468 if (item->window.flags & WINDOW_HORIZONTAL) {
1469 min = item->window.rect.x + SCROLLBAR_SIZE + 1;
1470 max = item->window.rect.x + item->window.rect.w - 2*SCROLLBAR_SIZE - 1;
1471 if (DC->cursorx >= min + SCROLLBAR_SIZE/2 && DC->cursorx <= max + SCROLLBAR_SIZE/2) {
1472 return DC->cursorx - SCROLLBAR_SIZE/2;
1473 }
1474 else {
1475 return Item_ListBox_ThumbPosition(item);
1476 }
1477 }
1478 else {
1479 min = item->window.rect.y + SCROLLBAR_SIZE + 1;
1480 max = item->window.rect.y + item->window.rect.h - 2*SCROLLBAR_SIZE - 1;
1481 if (DC->cursory >= min + SCROLLBAR_SIZE/2 && DC->cursory <= max + SCROLLBAR_SIZE/2) {
1482 return DC->cursory - SCROLLBAR_SIZE/2;
1483 }
1484 else {
1485 return Item_ListBox_ThumbPosition(item);
1486 }
1487 }
1488 }
1489 else {
1490 return Item_ListBox_ThumbPosition(item);
1491 }
1492 }
1493
1494 float Item_Slider_ThumbPosition(itemDef_t *item) {
1495 float value, range, x;
1496 editFieldDef_t *editDef = item->typeData;
1497
1498 if (item->text) {
1499 x = item->textRect.x + item->textRect.w + 8;
1500 } else {
1501 x = item->window.rect.x;
1502 }
1503
1504 if (!editDef || !item->cvar) {
1505 return x;
1506 }
1507
1508 value = DC->getCVarValue(item->cvar);
1509
1510 if (value < editDef->minVal) {
1511 value = editDef->minVal;
1512 } else if (value > editDef->maxVal) {
1513 value = editDef->maxVal;
1514 }
1515
1516 range = editDef->maxVal - editDef->minVal;
1517 value -= editDef->minVal;
1518 value /= range;
1519 //value /= (editDef->maxVal - editDef->minVal);
1520 value *= SLIDER_WIDTH;
1521 x += value;
1522 // vm fuckage
1523 //x = x + (((float)value / editDef->maxVal) * SLIDER_WIDTH);
1524 return x;
1525 }
1526
1527 int Item_Slider_OverSlider(itemDef_t *item, float x, float y) {
1528 rectDef_t r;
1529
1530 r.x = Item_Slider_ThumbPosition(item) - (SLIDER_THUMB_WIDTH / 2);
1531 r.y = item->window.rect.y - 2;
1532 r.w = SLIDER_THUMB_WIDTH;
1533 r.h = SLIDER_THUMB_HEIGHT;
1534
1535 if (Rect_ContainsPoint(&r, x, y)) {
1536 return WINDOW_LB_THUMB;
1537 }
1538 return 0;
1539 }
1540
1541 int Item_ListBox_OverLB(itemDef_t *item, float x, float y) {
1542 rectDef_t r;
1543 int thumbstart;
1544
1545 if (item->window.flags & WINDOW_HORIZONTAL) {
1546 // check if on left arrow
1547 r.x = item->window.rect.x;
1548 r.y = item->window.rect.y + item->window.rect.h - SCROLLBAR_SIZE;
1549 r.h = r.w = SCROLLBAR_SIZE;
1550 if (Rect_ContainsPoint(&r, x, y)) {
1551 return WINDOW_LB_LEFTARROW;
1552 }
1553 // check if on right arrow
1554 r.x = item->window.rect.x + item->window.rect.w - SCROLLBAR_SIZE;
1555 if (Rect_ContainsPoint(&r, x, y)) {
1556 return WINDOW_LB_RIGHTARROW;
1557 }
1558 // check if on thumb
1559 thumbstart = Item_ListBox_ThumbPosition(item);
1560 r.x = thumbstart;
1561 if (Rect_ContainsPoint(&r, x, y)) {
1562 return WINDOW_LB_THUMB;
1563 }
1564 r.x = item->window.rect.x + SCROLLBAR_SIZE;
1565 r.w = thumbstart - r.x;
1566 if (Rect_ContainsPoint(&r, x, y)) {
1567 return WINDOW_LB_PGUP;
1568 }
1569 r.x = thumbstart + SCROLLBAR_SIZE;
1570 r.w = item->window.rect.x + item->window.rect.w - SCROLLBAR_SIZE;
1571 if (Rect_ContainsPoint(&r, x, y)) {
1572 return WINDOW_LB_PGDN;
1573 }
1574 } else {
1575 r.x = item->window.rect.x + item->window.rect.w - SCROLLBAR_SIZE;
1576 r.y = item->window.rect.y;
1577 r.h = r.w = SCROLLBAR_SIZE;
1578 if (Rect_ContainsPoint(&r, x, y)) {
1579 return WINDOW_LB_LEFTARROW;
1580 }
1581 r.y = item->window.rect.y + item->window.rect.h - SCROLLBAR_SIZE;
1582 if (Rect_ContainsPoint(&r, x, y)) {
1583 return WINDOW_LB_RIGHTARROW;
1584 }
1585 thumbstart = Item_ListBox_ThumbPosition(item);
1586 r.y = thumbstart;
1587 if (Rect_ContainsPoint(&r, x, y)) {
1588 return WINDOW_LB_THUMB;
1589 }
1590 r.y = item->window.rect.y + SCROLLBAR_SIZE;
1591 r.h = thumbstart - r.y;
1592 if (Rect_ContainsPoint(&r, x, y)) {
1593 return WINDOW_LB_PGUP;
1594 }
1595 r.y = thumbstart + SCROLLBAR_SIZE;
1596 r.h = item->window.rect.y + item->window.rect.h - SCROLLBAR_SIZE;
1597 if (Rect_ContainsPoint(&r, x, y)) {
1598 return WINDOW_LB_PGDN;
1599 }
1600 }
1601 return 0;
1602 }
1603
1604
1605 void Item_ListBox_MouseEnter(itemDef_t *item, float x, float y)
1606 {
1607 rectDef_t r;
1608 listBoxDef_t *listPtr = (listBoxDef_t*)item->typeData;
1609
1610 item->window.flags &= ~(WINDOW_LB_LEFTARROW | WINDOW_LB_RIGHTARROW | WINDOW_LB_THUMB | WINDOW_LB_PGUP | WINDOW_LB_PGDN);
1611 item->window.flags |= Item_ListBox_OverLB(item, x, y);
1612
1613 if (item->window.flags & WINDOW_HORIZONTAL) {
1614 if (!(item->window.flags & (WINDOW_LB_LEFTARROW | WINDOW_LB_RIGHTARROW | WINDOW_LB_THUMB | WINDOW_LB_PGUP | WINDOW_LB_PGDN))) {
1615 // check for selection hit as we have exausted buttons and thumb
1616 if (listPtr->elementStyle == LISTBOX_IMAGE) {
1617 r.x = item->window.rect.x;
1618 r.y = item->window.rect.y;
1619 r.h = item->window.rect.h - SCROLLBAR_SIZE;
1620 r.w = item->window.rect.w - listPtr->drawPadding;
1621 if (Rect_ContainsPoint(&r, x, y)) {
1622 listPtr->cursorPos = (int)((x - r.x) / listPtr->elementWidth) + listPtr->startPos;
1623 if (listPtr->cursorPos >= listPtr->endPos) {
1624 listPtr->cursorPos = listPtr->endPos;
1625 }
1626 }
1627 } else {
1628 // text hit..
1629 }
1630 }
1631 } else if (!(item->window.flags & (WINDOW_LB_LEFTARROW | WINDOW_LB_RIGHTARROW | WINDOW_LB_THUMB | WINDOW_LB_PGUP | WINDOW_LB_PGDN))) {
1632 r.x = item->window.rect.x;
1633 r.y = item->window.rect.y;
1634 r.w = item->window.rect.w - SCROLLBAR_SIZE;
1635 r.h = item->window.rect.h - listPtr->drawPadding;
1636 if (Rect_ContainsPoint(&r, x, y)) {
1637 listPtr->cursorPos = (int)((y - 2 - r.y) / listPtr->elementHeight) + listPtr->startPos;
1638 if (listPtr->cursorPos > listPtr->endPos) {
1639 listPtr->cursorPos = listPtr->endPos;
1640 }
1641 }
1642 }
1643 }
1644
1645 void Item_MouseEnter(itemDef_t *item, float x, float y) {
1646 rectDef_t r;
1647 if (item) {
1648 r = item->textRect;
1649 r.y -= r.h;
1650 // in the text rect?
1651
1652 // items can be enabled and disabled based on cvars
1653 if (item->cvarFlags & (CVAR_ENABLE | CVAR_DISABLE) && !Item_EnableShowViaCvar(item, CVAR_ENABLE)) {
1654 return;
1655 }
1656
1657 if (item->cvarFlags & (CVAR_SHOW | CVAR_HIDE) && !Item_EnableShowViaCvar(item, CVAR_SHOW)) {
1658 return;
1659 }
1660
1661 if (Rect_ContainsPoint(&r, x, y)) {
1662 if (!(item->window.flags & WINDOW_MOUSEOVERTEXT)) {
1663 Item_RunScript(item, item->mouseEnterText);
1664 item->window.flags |= WINDOW_MOUSEOVERTEXT;
1665 }
1666 if (!(item->window.flags & WINDOW_MOUSEOVER)) {
1667 Item_RunScript(item, item->mouseEnter);
1668 item->window.flags |= WINDOW_MOUSEOVER;
1669 }
1670
1671 } else {
1672 // not in the text rect
1673 if (item->window.flags & WINDOW_MOUSEOVERTEXT) {
1674 // if we were
1675 Item_RunScript(item, item->mouseExitText);
1676 item->window.flags &= ~WINDOW_MOUSEOVERTEXT;
1677 }
1678 if (!(item->window.flags & WINDOW_MOUSEOVER)) {
1679 Item_RunScript(item, item->mouseEnter);
1680 item->window.flags |= WINDOW_MOUSEOVER;
1681 }
1682
1683 if (item->type == ITEM_TYPE_LISTBOX) {
1684 Item_ListBox_MouseEnter(item, x, y);
1685 }
1686 }
1687 }
1688 }
1689
1690 void Item_MouseLeave(itemDef_t *item) {
1691 if (item) {
1692 if (item->window.flags & WINDOW_MOUSEOVERTEXT) {
1693 Item_RunScript(item, item->mouseExitText);
1694 item->window.flags &= ~WINDOW_MOUSEOVERTEXT;
1695 }
1696 Item_RunScript(item, item->mouseExit);
1697 item->window.flags &= ~(WINDOW_LB_RIGHTARROW | WINDOW_LB_LEFTARROW);
1698 }
1699 }
1700
1701 itemDef_t *Menu_HitTest(menuDef_t *menu, float x, float y) {
1702 int i;
1703 for (i = 0; i < menu->itemCount; i++) {
1704 if (Rect_ContainsPoint(&menu->items[i]->window.rect, x, y)) {
1705 return menu->items[i];
1706 }
1707 }
1708 return NULL;
1709 }
1710
1711 void Item_SetMouseOver(itemDef_t *item, qboolean focus) {
1712 if (item) {
1713 if (focus) {
1714 item->window.flags |= WINDOW_MOUSEOVER;
1715 } else {
1716 item->window.flags &= ~WINDOW_MOUSEOVER;
1717 }
1718 }
1719 }
1720
1721
1722 qboolean Item_OwnerDraw_HandleKey(itemDef_t *item, int key) {
1723 if (item && DC->ownerDrawHandleKey) {
1724 return DC->ownerDrawHandleKey(item->window.ownerDraw, item->window.ownerDrawFlags, &item->special, key);
1725 }
1726 return qfalse;
1727 }
1728
1729 qboolean Item_ListBox_HandleKey(itemDef_t *item, int key, qboolean down, qboolean force) {
1730 listBoxDef_t *listPtr = (listBoxDef_t*)item->typeData;
1731 int count = DC->feederCount(item->special);
1732 int max, viewmax;
1733
1734 if (force || (Rect_ContainsPoint(&item->window.rect, DC->cursorx, DC->cursory) && item->window.flags & WINDOW_HASFOCUS)) {
1735 max = Item_ListBox_MaxScroll(item);
1736 if (item->window.flags & WINDOW_HORIZONTAL) {
1737 viewmax = (item->window.rect.w / listPtr->elementWidth);
1738 if ( key == K_LEFTARROW || key == K_KP_LEFTARROW )
1739 {
1740 if (!listPtr->notselectable) {
1741 listPtr->cursorPos--;
1742 if (listPtr->cursorPos < 0) {
1743 listPtr->cursorPos = 0;
1744 }
1745 if (listPtr->cursorPos < listPtr->startPos) {
1746 listPtr->startPos = listPtr->cursorPos;
1747 }
1748 if (listPtr->cursorPos >= listPtr->startPos + viewmax) {
1749 listPtr->startPos = listPtr->cursorPos - viewmax + 1;
1750 }
1751 item->cursorPos = listPtr->cursorPos;
1752 DC->feederSelection(item->special, item->cursorPos);
1753 }
1754 else {
1755 listPtr->startPos--;
1756 if (listPtr->startPos < 0)
1757 listPtr->startPos = 0;
1758 }
1759 return qtrue;
1760 }
1761 if ( key == K_RIGHTARROW || key == K_KP_RIGHTARROW )
1762 {
1763 if (!listPtr->notselectable) {
1764 listPtr->cursorPos++;
1765 if (listPtr->cursorPos < listPtr->startPos) {
1766 listPtr->startPos = listPtr->cursorPos;
1767 }
1768 if (listPtr->cursorPos >= count) {
1769 listPtr->cursorPos = count-1;
1770 }
1771 if (listPtr->cursorPos >= listPtr->startPos + viewmax) {
1772 listPtr->startPos = listPtr->cursorPos - viewmax + 1;
1773 }
1774 item->cursorPos = listPtr->cursorPos;
1775 DC->feederSelection(item->special, item->cursorPos);
1776 }
1777 else {
1778 listPtr->startPos++;
1779 if (listPtr->startPos >= count)
1780 listPtr->startPos = count-1;
1781 }
1782 return qtrue;
1783 }
1784 }
1785 else {
1786 viewmax = (item->window.rect.h / listPtr->elementHeight);
1787 if ( key == K_UPARROW || key == K_KP_UPARROW )
1788 {
1789 if (!listPtr->notselectable) {
1790 listPtr->cursorPos--;
1791 if (listPtr->cursorPos < 0) {
1792 listPtr->cursorPos = 0;
1793 }
1794 if (listPtr->cursorPos < listPtr->startPos) {
1795 listPtr->startPos = listPtr->cursorPos;
1796 }
1797 if (listPtr->cursorPos >= listPtr->startPos + viewmax) {
1798 listPtr->startPos = listPtr->cursorPos - viewmax + 1;
1799 }
1800 item->cursorPos = listPtr->cursorPos;
1801 DC->feederSelection(item->special, item->cursorPos);
1802 }
1803 else {
1804 listPtr->startPos--;
1805 if (listPtr->startPos < 0)
1806 listPtr->startPos = 0;
1807 }
1808 return qtrue;
1809 }
1810 if ( key == K_DOWNARROW || key == K_KP_DOWNARROW )
1811 {
1812 if (!listPtr->notselectable) {
1813 listPtr->cursorPos++;
1814 if (listPtr->cursorPos < listPtr->startPos) {
1815 listPtr->startPos = listPtr->cursorPos;
1816 }
1817 if (listPtr->cursorPos >= count) {
1818 listPtr->cursorPos = count-1;
1819 }
1820 if (listPtr->cursorPos >= listPtr->startPos + viewmax) {
1821 listPtr->startPos = listPtr->cursorPos - viewmax + 1;
1822 }
1823 item->cursorPos = listPtr->cursorPos;
1824 DC->feederSelection(item->special, item->cursorPos);
1825 }
1826 else {
1827 listPtr->startPos++;
1828 if (listPtr->startPos > max)
1829 listPtr->startPos = max;
1830 }
1831 return qtrue;
1832 }
1833 }
1834
1835 // Use mouse wheel in vertical and horizontal menus.
1836 // If scrolling 3 items would replace over half of the
1837 // displayed items, only scroll 1 item at a time.
1838 if ( key == K_MWHEELUP ) {
1839 int scroll = viewmax < 6 ? 1 : 3;
1840 listPtr->startPos -= scroll;
1841 if (listPtr->startPos < 0) {
1842 listPtr->startPos = 0;
1843 }
1844 return qtrue;
1845 }
1846 if ( key == K_MWHEELDOWN ) {
1847 int scroll = viewmax < 6 ? 1 : 3;
1848 listPtr->startPos += scroll;
1849 if (listPtr->startPos > max) {
1850 listPtr->startPos = max;
1851 }
1852 return qtrue;
1853 }
1854
1855 // mouse hit
1856 if (key == K_MOUSE1 || key == K_MOUSE2) {
1857 if (item->window.flags & WINDOW_LB_LEFTARROW) {
1858 listPtr->startPos--;
1859 if (listPtr->startPos < 0) {
1860 listPtr->startPos = 0;
1861 }
1862 } else if (item->window.flags & WINDOW_LB_RIGHTARROW) {
1863 // one down
1864 listPtr->startPos++;
1865 if (listPtr->startPos > max) {
1866 listPtr->startPos = max;
1867 }
1868 } else if (item->window.flags & WINDOW_LB_PGUP) {
1869 // page up
1870 listPtr->startPos -= viewmax;
1871 if (listPtr->startPos < 0) {
1872 listPtr->startPos = 0;
1873 }
1874 } else if (item->window.flags & WINDOW_LB_PGDN) {
1875 // page down
1876 listPtr->startPos += viewmax;
1877 if (listPtr->startPos > max) {
1878 listPtr->startPos = max;
1879 }
1880 } else if (item->window.flags & WINDOW_LB_THUMB) {
1881 // Display_SetCaptureItem(item);
1882 } else {
1883 // select an item
1884 if (DC->realTime < lastListBoxClickTime && listPtr->doubleClick) {
1885 Item_RunScript(item, listPtr->doubleClick);
1886 }
1887 lastListBoxClickTime = DC->realTime + DOUBLE_CLICK_DELAY;
1888 if (item->cursorPos != listPtr->cursorPos) {
1889 item->cursorPos = listPtr->cursorPos;
1890 DC->feederSelection(item->special, item->cursorPos);
1891 }
1892 }
1893 return qtrue;
1894 }
1895 if ( key == K_HOME || key == K_KP_HOME) {
1896 // home
1897 listPtr->startPos = 0;
1898 return qtrue;
1899 }
1900 if ( key == K_END || key == K_KP_END) {
1901 // end
1902 listPtr->startPos = max;
1903 return qtrue;
1904 }
1905 if (key == K_PGUP || key == K_KP_PGUP ) {
1906 // page up
1907 if (!listPtr->notselectable) {
1908 listPtr->cursorPos -= viewmax;
1909 if (listPtr->cursorPos < 0) {
1910 listPtr->cursorPos = 0;
1911 }
1912 if (listPtr->cursorPos < listPtr->startPos) {
1913 listPtr->startPos = listPtr->cursorPos;
1914 }
1915 if (listPtr->cursorPos >= listPtr->startPos + viewmax) {
1916 listPtr->startPos = listPtr->cursorPos - viewmax + 1;
1917 }
1918 item->cursorPos = listPtr->cursorPos;
1919 DC->feederSelection(item->special, item->cursorPos);
1920 }
1921 else {
1922 listPtr->startPos -= viewmax;
1923 if (listPtr->startPos < 0) {
1924 listPtr->startPos = 0;
1925 }
1926 }
1927 return qtrue;
1928 }
1929 if ( key == K_PGDN || key == K_KP_PGDN ) {
1930 // page down
1931 if (!listPtr->notselectable) {
1932 listPtr->cursorPos += viewmax;
1933 if (listPtr->cursorPos < listPtr->startPos) {
1934 listPtr->startPos = listPtr->cursorPos;
1935 }
1936 if (listPtr->cursorPos >= count) {
1937 listPtr->cursorPos = count-1;
1938 }
1939 if (listPtr->cursorPos >= listPtr->startPos + viewmax) {
1940 listPtr->startPos = listPtr->cursorPos - viewmax + 1;
1941 }
1942 item->cursorPos = listPtr->cursorPos;
1943 DC->feederSelection(item->special, item->cursorPos);
1944 }
1945 else {
1946 listPtr->startPos += viewmax;
1947 if (listPtr->startPos > max) {
1948 listPtr->startPos = max;
1949 }
1950 }
1951 return qtrue;
1952 }
1953 }
1954 return qfalse;
1955 }
1956
1957 qboolean Item_YesNo_HandleKey(itemDef_t *item, int key) {
1958
1959 if (item->cvar) {
1960 qboolean action = qfalse;
1961 if (key == K_MOUSE1 || key == K_MOUSE2 || key == K_MOUSE3) {
1962 if (Rect_ContainsPoint(&item->window.rect, DC->cursorx, DC->cursory) && item->window.flags & WINDOW_HASFOCUS) {
1963 action = qtrue;
1964 }
1965 } else if (UI_SelectForKey(key) != 0) {
1966 action = qtrue;
1967 }
1968 if (action) {
1969 DC->setCVar(item->cvar, va("%i", !DC->getCVarValue(item->cvar)));
1970 return qtrue;
1971 }
1972 }
1973
1974 return qfalse;
1975
1976 }
1977
1978 int Item_Multi_CountSettings(itemDef_t *item) {
1979 multiDef_t *multiPtr = (multiDef_t*)item->typeData;
1980 if (multiPtr == NULL) {
1981 return 0;
1982 }
1983 return multiPtr->count;
1984 }
1985
1986 int Item_Multi_FindCvarByValue(itemDef_t *item) {
1987 char buff[1024];
1988 float value = 0;
1989 int i;
1990 multiDef_t *multiPtr = (multiDef_t*)item->typeData;
1991 if (multiPtr) {
1992 if (multiPtr->strDef) {
1993 DC->getCVarString(item->cvar, buff, sizeof(buff));
1994 } else {
1995 value = DC->getCVarValue(item->cvar);
1996 }
1997 for (i = 0; i < multiPtr->count; i++) {
1998 if (multiPtr->strDef) {
1999 if (Q_stricmp(buff, multiPtr->cvarStr[i]) == 0) {
2000 return i;
2001 }
2002 } else {
2003 if (multiPtr->cvarValue[i] == value) {
2004 return i;
2005 }
2006 }
2007 }
2008 }
2009 return 0;
2010 }
2011
2012 const char *Item_Multi_Setting(itemDef_t *item) {
2013 char buff[1024];
2014 float value = 0;
2015 int i;
2016 multiDef_t *multiPtr = (multiDef_t*)item->typeData;
2017 if (multiPtr) {
2018 if (multiPtr->strDef) {
2019 DC->getCVarString(item->cvar, buff, sizeof(buff));
2020 } else {
2021 value = DC->getCVarValue(item->cvar);
2022 }
2023 for (i = 0; i < multiPtr->count; i++) {
2024 if (multiPtr->strDef) {
2025 if (Q_stricmp(buff, multiPtr->cvarStr[i]) == 0) {
2026 return multiPtr->cvarList[i];
2027 }
2028 } else {
2029 if (multiPtr->cvarValue[i] == value) {
2030 return multiPtr->cvarList[i];
2031 }
2032 }
2033 }
2034 }
2035 return "";
2036 }
2037
2038 qboolean Item_Multi_HandleKey(itemDef_t *item, int key) {
2039 multiDef_t *multiPtr = (multiDef_t*)item->typeData;
2040 if (multiPtr) {
2041 if (item->cvar) {
2042 int select = 0;
2043 if (key == K_MOUSE1 || key == K_MOUSE2 || key == K_MOUSE3) {
2044 if (Rect_ContainsPoint(&item->window.rect, DC->cursorx, DC->cursory) && item->window.flags & WINDOW_HASFOCUS) {
2045 select = (key == K_MOUSE2) ? -1 : 1;
2046 }
2047 } else {
2048 select = UI_SelectForKey(key);
2049 }
2050 if (select != 0) {
2051 int current = Item_Multi_FindCvarByValue(item) + select;
2052 int max = Item_Multi_CountSettings(item);
2053 if ( current < 0 ) {
2054 current = max-1;
2055 } else if ( current >= max ) {
2056 current = 0;
2057 }
2058
2059 if (multiPtr->videoMode) {
2060 if (multiPtr->cvarValue[current] != -1) {
2061 DC->setCVar("r_mode", va("%i", (int) multiPtr->cvarValue[current] ));
2062 } else {
2063 int w, h;
2064 char *x;
2065 char str[8];
2066
2067 x = strchr( multiPtr->cvarStr[current], 'x' ) + 1;
2068 Q_strncpyz( str, multiPtr->cvarStr[current], MIN( x-multiPtr->cvarStr[current], sizeof( str ) ) );
2069 w = atoi( str );
2070 h = atoi( x );
2071
2072 DC->setCVar("r_mode", "-1");
2073 DC->setCVar("r_customwidth", va("%i", w));
2074 DC->setCVar("r_customheight", va("%i", h));
2075 }
2076 }
2077
2078 if (multiPtr->strDef) {
2079 DC->setCVar(item->cvar, multiPtr->cvarStr[current]);
2080 } else {
2081 float value = multiPtr->cvarValue[current];
2082 if (((float)((int) value)) == value) {
2083 DC->setCVar(item->cvar, va("%i", (int) value ));
2084 }
2085 else {
2086 DC->setCVar(item->cvar, va("%f", value ));
2087 }
2088 }
2089 return qtrue;
2090 }
2091 }
2092 }
2093 return qfalse;
2094 }
2095
2096 qboolean Item_TextField_HandleKey(itemDef_t *item, int key) {
2097 char buff[1024];
2098 int len;
2099 itemDef_t *newItem = NULL;
2100 editFieldDef_t *editPtr = (editFieldDef_t*)item->typeData;
2101
2102 if (item->cvar) {
2103
2104 memset(buff, 0, sizeof(buff));
2105 DC->getCVarString(item->cvar, buff, sizeof(buff));
2106 len = strlen(buff);
2107 if (editPtr->maxChars && len > editPtr->maxChars) {
2108 len = editPtr->maxChars;
2109 }
2110 if ( key & K_CHAR_FLAG ) {
2111 key &= ~K_CHAR_FLAG;
2112
2113
2114 if (key == 'h' - 'a' + 1 ) { // ctrl-h is backspace
2115 if ( item->cursorPos > 0 ) {
2116 memmove( &buff[item->cursorPos - 1], &buff[item->cursorPos], len + 1 - item->cursorPos);
2117 item->cursorPos--;
2118 if (item->cursorPos < editPtr->paintOffset) {
2119 editPtr->paintOffset--;
2120 }
2121 }
2122 DC->setCVar(item->cvar, buff);
2123 return qtrue;
2124 }
2125
2126
2127 //
2128 // ignore any non printable chars
2129 //
2130 if ( key < 32 || !item->cvar) {
2131 return qtrue;
2132 }
2133
2134 if (item->type == ITEM_TYPE_NUMERICFIELD) {
2135 if (key < '0' || key > '9') {
2136 return qfalse;
2137 }
2138 }
2139
2140 if (!DC->getOverstrikeMode()) {
2141 if (( len == MAX_EDITFIELD - 1 ) || (editPtr->maxChars && len >= editPtr->maxChars)) {
2142 return qtrue;
2143 }
2144 memmove( &buff[item->cursorPos + 1], &buff[item->cursorPos], len + 1 - item->cursorPos );
2145 } else {
2146 if (editPtr->maxChars && item->cursorPos >= editPtr->maxChars) {
2147 return qtrue;
2148 }
2149 }
2150
2151 buff[item->cursorPos] = key;
2152
2153 DC->setCVar(item->cvar, buff);
2154
2155 if (item->cursorPos < len + 1) {
2156 item->cursorPos++;
2157 if (editPtr->maxPaintChars && item->cursorPos > editPtr->maxPaintChars) {
2158 editPtr->paintOffset++;
2159 }
2160 }
2161
2162 } else {
2163
2164 if ( key == K_DEL || key == K_KP_DEL ) {
2165 if ( item->cursorPos < len ) {
2166 memmove( buff + item->cursorPos, buff + item->cursorPos + 1, len - item->cursorPos);
2167 DC->setCVar(item->cvar, buff);
2168 }
2169 return qtrue;
2170 }
2171
2172 if ( key == K_RIGHTARROW || key == K_KP_RIGHTARROW )
2173 {
2174 if (editPtr->maxPaintChars && item->cursorPos >= editPtr->maxPaintChars && item->cursorPos < len) {
2175 item->cursorPos++;
2176 editPtr->paintOffset++;
2177 return qtrue;
2178 }
2179 if (item->cursorPos < len) {
2180 item->cursorPos++;
2181 }
2182 return qtrue;
2183 }
2184
2185 if ( key == K_LEFTARROW || key == K_KP_LEFTARROW )
2186 {
2187 if ( item->cursorPos > 0 ) {
2188 item->cursorPos--;
2189 }
2190 if (item->cursorPos < editPtr->paintOffset) {
2191 editPtr->paintOffset--;
2192 }
2193 return qtrue;
2194 }
2195
2196 if ( key == K_HOME || key == K_KP_HOME) {// || ( tolower(key) == 'a' && trap_Key_IsDown( K_CTRL ) ) ) {
2197 item->cursorPos = 0;
2198 editPtr->paintOffset = 0;
2199 return qtrue;
2200 }
2201
2202 if ( key == K_END || key == K_KP_END) {// ( tolower(key) == 'e' && trap_Key_IsDown( K_CTRL ) ) ) {
2203 item->cursorPos = len;
2204 if(item->cursorPos > editPtr->maxPaintChars) {
2205 editPtr->paintOffset = len - editPtr->maxPaintChars;
2206 }
2207 return qtrue;
2208 }
2209
2210 if ( key == K_INS || key == K_KP_INS ) {
2211 DC->setOverstrikeMode(!DC->getOverstrikeMode());
2212 return qtrue;
2213 }
2214 }
2215
2216 if (key == K_TAB || key == K_DOWNARROW || key == K_KP_DOWNARROW) {
2217 newItem = Menu_SetNextCursorItem(item->parent);
2218 if (newItem && (newItem->type == ITEM_TYPE_EDITFIELD || newItem->type == ITEM_TYPE_NUMERICFIELD)) {
2219 g_editItem = newItem;
2220 }
2221 }
2222
2223 if (key == K_UPARROW || key == K_KP_UPARROW) {
2224 newItem = Menu_SetPrevCursorItem(item->parent);
2225 if (newItem && (newItem->type == ITEM_TYPE_EDITFIELD || newItem->type == ITEM_TYPE_NUMERICFIELD)) {
2226 g_editItem = newItem;
2227 }
2228 }
2229
2230 if ( key == K_ENTER || key == K_KP_ENTER || key == K_ESCAPE) {
2231 return qfalse;
2232 }
2233
2234 return qtrue;
2235 }
2236 return qfalse;
2237
2238 }
2239
2240 static void Scroll_ListBox_AutoFunc(void *p) {
2241 scrollInfo_t *si = (scrollInfo_t*)p;
2242 if (DC->realTime > si->nextScrollTime) {
2243 // need to scroll which is done by simulating a click to the item
2244 // this is done a bit sideways as the autoscroll "knows" that the item is a listbox
2245 // so it calls it directly
2246 Item_ListBox_HandleKey(si->item, si->scrollKey, qtrue, qfalse);
2247 si->nextScrollTime = DC->realTime + si->adjustValue;
2248 }
2249
2250 if (DC->realTime > si->nextAdjustTime) {
2251 si->nextAdjustTime = DC->realTime + SCROLL_TIME_ADJUST;
2252 if (si->adjustValue > SCROLL_TIME_FLOOR) {
2253 si->adjustValue -= SCROLL_TIME_ADJUSTOFFSET;
2254 }
2255 }
2256 }
2257
2258 static void Scroll_ListBox_ThumbFunc(void *p) {
2259 scrollInfo_t *si = (scrollInfo_t*)p;
2260 rectDef_t r;
2261 int pos, max;
2262
2263 listBoxDef_t *listPtr = (listBoxDef_t*)si->item->typeData;
2264 if (si->item->window.flags & WINDOW_HORIZONTAL) {
2265 if (DC->cursorx == si->xStart) {
2266 return;
2267 }
2268 r.x = si->item->window.rect.x + SCROLLBAR_SIZE + 1;
2269 r.y = si->item->window.rect.y + si->item->window.rect.h - SCROLLBAR_SIZE - 1;
2270 r.h = SCROLLBAR_SIZE;
2271 r.w = si->item->window.rect.w - (SCROLLBAR_SIZE*2) - 2;
2272 max = Item_ListBox_MaxScroll(si->item);
2273 //
2274 pos = (DC->cursorx - r.x - SCROLLBAR_SIZE/2) * max / (r.w - SCROLLBAR_SIZE);
2275 if (pos < 0) {
2276 pos = 0;
2277 }
2278 else if (pos > max) {
2279 pos = max;
2280 }
2281 listPtr->startPos = pos;
2282 si->xStart = DC->cursorx;
2283 }
2284 else if (DC->cursory != si->yStart) {
2285
2286 r.x = si->item->window.rect.x + si->item->window.rect.w - SCROLLBAR_SIZE - 1;
2287 r.y = si->item->window.rect.y + SCROLLBAR_SIZE + 1;
2288 r.h = si->item->window.rect.h - (SCROLLBAR_SIZE*2) - 2;
2289 r.w = SCROLLBAR_SIZE;
2290 max = Item_ListBox_MaxScroll(si->item);
2291 //
2292 pos = (DC->cursory - r.y - SCROLLBAR_SIZE/2) * max / (r.h - SCROLLBAR_SIZE);
2293 if (pos < 0) {
2294 pos = 0;
2295 }
2296 else if (pos > max) {
2297 pos = max;
2298 }
2299 listPtr->startPos = pos;
2300 si->yStart = DC->cursory;
2301 }
2302
2303 if (DC->realTime > si->nextScrollTime) {
2304 // need to scroll which is done by simulating a click to the item
2305 // this is done a bit sideways as the autoscroll "knows" that the item is a listbox
2306 // so it calls it directly
2307 Item_ListBox_HandleKey(si->item, si->scrollKey, qtrue, qfalse);
2308 si->nextScrollTime = DC->realTime + si->adjustValue;
2309 }
2310
2311 if (DC->realTime > si->nextAdjustTime) {
2312 si->nextAdjustTime = DC->realTime + SCROLL_TIME_ADJUST;
2313 if (si->adjustValue > SCROLL_TIME_FLOOR) {
2314 si->adjustValue -= SCROLL_TIME_ADJUSTOFFSET;
2315 }
2316 }
2317 }
2318
2319 static void Scroll_Slider_ThumbFunc(void *p) {
2320 float x, value, cursorx;
2321 scrollInfo_t *si = (scrollInfo_t*)p;
2322 editFieldDef_t *editDef = si->item->typeData;
2323
2324 if (si->item->text) {
2325 x = si->item->textRect.x + si->item->textRect.w + 8;
2326 } else {
2327 x = si->item->window.rect.x;
2328 }
2329
2330 cursorx = DC->cursorx;
2331
2332 if (cursorx < x) {
2333 cursorx = x;
2334 } else if (cursorx > x + SLIDER_WIDTH) {
2335 cursorx = x + SLIDER_WIDTH;
2336 }
2337 value = cursorx - x;
2338 value /= SLIDER_WIDTH;
2339 value *= (editDef->maxVal - editDef->minVal);
2340 value += editDef->minVal;
2341 DC->setCVar(si->item->cvar, va("%f", value));
2342 }
2343
2344 void Item_StartCapture(itemDef_t *item, int key) {
2345 int flags;
2346 switch (item->type) {
2347 case ITEM_TYPE_EDITFIELD:
2348 case ITEM_TYPE_NUMERICFIELD:
2349
2350 case ITEM_TYPE_LISTBOX:
2351 {
2352 flags = Item_ListBox_OverLB(item, DC->cursorx, DC->cursory);
2353 if (flags & (WINDOW_LB_LEFTARROW | WINDOW_LB_RIGHTARROW)) {
2354 scrollInfo.nextScrollTime = DC->realTime + SCROLL_TIME_START;
2355 scrollInfo.nextAdjustTime = DC->realTime + SCROLL_TIME_ADJUST;
2356 scrollInfo.adjustValue = SCROLL_TIME_START;
2357 scrollInfo.scrollKey = key;
2358 scrollInfo.scrollDir = (flags & WINDOW_LB_LEFTARROW) ? qtrue : qfalse;
2359 scrollInfo.item = item;
2360 captureData = &scrollInfo;
2361 captureFunc = &Scroll_ListBox_AutoFunc;
2362 itemCapture = item;
2363 } else if (flags & WINDOW_LB_THUMB) {
2364 scrollInfo.scrollKey = key;
2365 scrollInfo.item = item;
2366 scrollInfo.xStart = DC->cursorx;
2367 scrollInfo.yStart = DC->cursory;
2368 captureData = &scrollInfo;
2369 captureFunc = &Scroll_ListBox_ThumbFunc;
2370 itemCapture = item;
2371 }
2372 break;
2373 }
2374 case ITEM_TYPE_SLIDER:
2375 {
2376 flags = Item_Slider_OverSlider(item, DC->cursorx, DC->cursory);
2377 if (flags & WINDOW_LB_THUMB) {
2378 scrollInfo.scrollKey = key;
2379 scrollInfo.item = item;
2380 scrollInfo.xStart = DC->cursorx;
2381 scrollInfo.yStart = DC->cursory;
2382 captureData = &scrollInfo;
2383 captureFunc = &Scroll_Slider_ThumbFunc;
2384 itemCapture = item;
2385 }
2386 break;
2387 }
2388 }
2389 }
2390
2391 void Item_StopCapture(itemDef_t *item) {
2392
2393 }
2394
2395 qboolean Item_Slider_HandleKey(itemDef_t *item, int key, qboolean down) {
2396 float x, value, width, work;
2397
2398 //DC->Print("slider handle key\n");
2399 if (item->cvar) {
2400 if (key == K_MOUSE1 || key == K_MOUSE2 || key == K_MOUSE3) {
2401 editFieldDef_t *editDef = item->typeData;
2402 if (editDef && Rect_ContainsPoint(&item->window.rect, DC->cursorx, DC->cursory) && item->window.flags & WINDOW_HASFOCUS) {
2403 rectDef_t testRect;
2404 width = SLIDER_WIDTH;
2405 if (item->text) {
2406 x = item->textRect.x + item->textRect.w + 8;
2407 } else {
2408 x = item->window.rect.x;
2409 }
2410
2411 testRect = item->window.rect;
2412 testRect.x = x;
2413 value = (float)SLIDER_THUMB_WIDTH / 2;
2414 testRect.x -= value;
2415 //DC->Print("slider x: %f\n", testRect.x);
2416 testRect.w = (SLIDER_WIDTH + (float)SLIDER_THUMB_WIDTH / 2);
2417 //DC->Print("slider w: %f\n", testRect.w);
2418 if (Rect_ContainsPoint(&testRect, DC->cursorx, DC->cursory)) {
2419 work = DC->cursorx - x;
2420 value = work / width;
2421 value *= (editDef->maxVal - editDef->minVal);
2422 // vm fuckage
2423 // value = (((float)(DC->cursorx - x)/ SLIDER_WIDTH) * (editDef->maxVal - editDef->minVal));
2424 value += editDef->minVal;
2425 DC->setCVar(item->cvar, va("%f", value));
2426 return qtrue;
2427 }
2428 }
2429 } else {
2430 int select = UI_SelectForKey(key);
2431 if (select != 0) {
2432 editFieldDef_t *editDef = item->typeData;
2433 if (editDef) {
2434 // 20 is number of steps
2435 value = DC->getCVarValue(item->cvar) + (((editDef->maxVal - editDef->minVal)/20) * select);
2436
2437 if (value < editDef->minVal)
2438 value = editDef->minVal;
2439 else if (value > editDef->maxVal)
2440 value = editDef->maxVal;
2441
2442 DC->setCVar(item->cvar, va("%f", value));
2443 return qtrue;
2444 }
2445 }
2446 }
2447 }
2448 DC->Print("slider handle key exit\n");
2449 return qfalse;
2450 }
2451
2452
2453 qboolean Item_HandleKey(itemDef_t *item, int key, qboolean down) {
2454
2455 if (itemCapture) {
2456 Item_StopCapture(itemCapture);
2457 itemCapture = NULL;
2458 captureFunc = 0;
2459 captureData = NULL;
2460 } else {
2461 if ( down && ( key == K_MOUSE1 || key == K_MOUSE2 || key == K_MOUSE3 ) ) {
2462 Item_StartCapture(item, key);
2463 }
2464 }
2465
2466 if (!down) {
2467 return qfalse;
2468 }
2469
2470 switch (item->type) {
2471 case ITEM_TYPE_BUTTON:
2472 return qfalse;
2473 break;
2474 case ITEM_TYPE_RADIOBUTTON:
2475 return qfalse;
2476 break;
2477 case ITEM_TYPE_CHECKBOX:
2478 return qfalse;
2479 break;
2480 case ITEM_TYPE_EDITFIELD:
2481 case ITEM_TYPE_NUMERICFIELD:
2482 //return Item_TextField_HandleKey(item, key);
2483 return qfalse;
2484 break;
2485 case ITEM_TYPE_COMBO:
2486 return qfalse;
2487 break;
2488 case ITEM_TYPE_LISTBOX:
2489 return Item_ListBox_HandleKey(item, key, down, qfalse);
2490 break;
2491 case ITEM_TYPE_YESNO:
2492 return Item_YesNo_HandleKey(item, key);
2493 break;
2494 case ITEM_TYPE_MULTI:
2495 return Item_Multi_HandleKey(item, key);
2496 break;
2497 case ITEM_TYPE_OWNERDRAW:
2498 return Item_OwnerDraw_HandleKey(item, key);
2499 break;
2500 case ITEM_TYPE_BIND:
2501 return Item_Bind_HandleKey(item, key, down);
2502 break;
2503 case ITEM_TYPE_SLIDER:
2504 return Item_Slider_HandleKey(item, key, down);
2505 break;
2506 //case ITEM_TYPE_IMAGE:
2507 // Item_Image_Paint(item);
2508 // break;
2509 default:
2510 return qfalse;
2511 break;
2512 }
2513
2514 //return qfalse;
2515 }
2516
2517 void Item_Action(itemDef_t *item) {
2518 if (item) {
2519 Item_RunScript(item, item->action);
2520 }
2521 }
2522
2523 itemDef_t *Menu_SetPrevCursorItem(menuDef_t *menu) {
2524 qboolean wrapped = qfalse;
2525 int oldCursor = menu->cursorItem;
2526
2527 if (menu->cursorItem < 0) {
2528 menu->cursorItem = menu->itemCount-1;
2529 wrapped = qtrue;
2530 }
2531
2532 while (menu->cursorItem > -1) {
2533
2534 menu->cursorItem--;
2535 if (menu->cursorItem < 0 && !wrapped) {
2536 wrapped = qtrue;
2537 menu->cursorItem = menu->itemCount -1;
2538 }
2539
2540 if (Item_SetFocus(menu->items[menu->cursorItem], DC->cursorx, DC->cursory)) {
2541 Menu_HandleMouseMove(menu, menu->items[menu->cursorItem]->window.rect.x + 1, menu->items[menu->cursorItem]->window.rect.y + 1);
2542 return menu->items[menu->cursorItem];
2543 }
2544 }
2545 menu->cursorItem = oldCursor;
2546 return NULL;
2547
2548 }
2549
2550 itemDef_t *Menu_SetNextCursorItem(menuDef_t *menu) {
2551
2552 qboolean wrapped = qfalse;
2553 int oldCursor = menu->cursorItem;
2554
2555
2556 if (menu->cursorItem == -1) {
2557 menu->cursorItem = 0;
2558 wrapped = qtrue;
2559 }
2560
2561 while (menu->cursorItem < menu->itemCount) {
2562
2563 menu->cursorItem++;
2564 if (menu->cursorItem >= menu->itemCount && !wrapped) {
2565 wrapped = qtrue;
2566 menu->cursorItem = 0;
2567 }
2568 if (Item_SetFocus(menu->items[menu->cursorItem], DC->cursorx, DC->cursory)) {
2569 Menu_HandleMouseMove(menu, menu->items[menu->cursorItem]->window.rect.x + 1, menu->items[menu->cursorItem]->window.rect.y + 1);
2570 return menu->items[menu->cursorItem];
2571 }
2572
2573 }
2574
2575 menu->cursorItem = oldCursor;
2576 return NULL;
2577 }
2578
2579 static void Window_CloseCinematic(windowDef_t *window) {
2580 if (window->style == WINDOW_STYLE_CINEMATIC && window->cinematic >= 0) {
2581 DC->stopCinematic(window->cinematic);
2582 window->cinematic = -1;
2583 }
2584 }
2585
2586 static void Menu_CloseCinematics(menuDef_t *menu) {
2587 if (menu) {
2588 int i;
2589 Window_CloseCinematic(&menu->window);
2590 for (i = 0; i < menu->itemCount; i++) {
2591 Window_CloseCinematic(&menu->items[i]->window);
2592 if (menu->items[i]->type == ITEM_TYPE_OWNERDRAW) {
2593 DC->stopCinematic(0-menu->items[i]->window.ownerDraw);
2594 }
2595 }
2596 }
2597 }
2598
2599 static void Display_CloseCinematics( void ) {
2600 int i;
2601 for (i = 0; i < menuCount; i++) {
2602 Menu_CloseCinematics(&Menus[i]);
2603 }
2604 }
2605
2606 void Menus_Activate(menuDef_t *menu) {
2607 menu->window.flags |= (WINDOW_HASFOCUS | WINDOW_VISIBLE);
2608 if (menu->onOpen) {
2609 itemDef_t item;
2610 item.parent = menu;
2611 Item_RunScript(&item, menu->onOpen);
2612 }
2613
2614 if (menu->soundName && *menu->soundName) {
2615 // DC->stopBackgroundTrack(); // you don't want to do this since it will reset s_rawend
2616 DC->startBackgroundTrack(menu->soundName, menu->soundName);
2617 }
2618
2619 Display_CloseCinematics();
2620
2621 }
2622
2623 int Display_VisibleMenuCount( void ) {
2624 int i, count;
2625 count = 0;
2626 for (i = 0; i < menuCount; i++) {
2627 if (Menus[i].window.flags & (WINDOW_FORCED | WINDOW_VISIBLE)) {
2628 count++;
2629 }
2630 }
2631 return count;
2632 }
2633
2634 void Menus_HandleOOBClick(menuDef_t *menu, int key, qboolean down) {
2635 if (menu) {
2636 int i;
2637 // basically the behaviour we are looking for is if there are windows in the stack.. see if
2638 // the cursor is within any of them.. if not close them otherwise activate them and pass the
2639 // key on.. force a mouse move to activate focus and script stuff
2640 if (down && menu->window.flags & WINDOW_OOB_CLICK) {
2641 Menu_RunCloseScript(menu);
2642 menu->window.flags &= ~(WINDOW_HASFOCUS | WINDOW_VISIBLE);
2643 }
2644
2645 for (i = 0; i < menuCount; i++) {
2646 if (Menu_OverActiveItem(&Menus[i], DC->cursorx, DC->cursory)) {
2647 Menu_RunCloseScript(menu);
2648 menu->window.flags &= ~(WINDOW_HASFOCUS | WINDOW_VISIBLE);
2649 Menus_Activate(&Menus[i]);
2650 Menu_HandleMouseMove(&Menus[i], DC->cursorx, DC->cursory);
2651 Menu_HandleKey(&Menus[i], key, down);
2652 }
2653 }
2654
2655 if (Display_VisibleMenuCount() == 0) {
2656 if (DC->Pause) {
2657 DC->Pause(qfalse);
2658 }
2659 }
2660 Display_CloseCinematics();
2661 }
2662 }
2663
2664 static rectDef_t *Item_CorrectedTextRect(itemDef_t *item) {
2665 static rectDef_t rect;
2666 memset(&rect, 0, sizeof(rectDef_t));
2667 if (item) {
2668 rect = item->textRect;
2669 if (rect.w) {
2670 rect.y -= rect.h;
2671 }
2672 }
2673 return &rect;
2674 }
2675
2676 // menu item key horizontal action: -1 = previous value, 1 = next value, 0 = no change
2677 int UI_SelectForKey(int key)
2678 {
2679 switch (key) {
2680 case K_MOUSE1:
2681 case K_MOUSE3:
2682 case K_ENTER:
2683 case K_KP_ENTER:
2684 case K_RIGHTARROW:
2685 case K_KP_RIGHTARROW:
2686 case K_JOY1:
2687 case K_JOY2:
2688 case K_JOY3:
2689 case K_JOY4:
2690 return 1; // next
2691
2692 case K_MOUSE2:
2693 case K_LEFTARROW:
2694 case K_KP_LEFTARROW:
2695 return -1; // previous
2696 }
2697
2698 // no change
2699 return 0;
2700 }
2701
2702 void Menu_HandleKey(menuDef_t *menu, int key, qboolean down) {
2703 int i;
2704 itemDef_t *item = NULL;
2705
2706 if (g_waitingForKey && down) {
2707 Item_Bind_HandleKey(g_bindItem, key, down);
2708 return;
2709 }
2710
2711 if (g_editingField && down) {
2712 if (!Item_TextField_HandleKey(g_editItem, key)) {
2713 g_editingField = qfalse;
2714 g_editItem = NULL;
2715 return;
2716 } else if (key == K_MOUSE1 || key == K_MOUSE2 || key == K_MOUSE3) {
2717 g_editingField = qfalse;
2718 g_editItem = NULL;
2719 Display_MouseMove(NULL, DC->cursorx, DC->cursory);
2720 } else if (key == K_TAB || key == K_UPARROW || key == K_DOWNARROW) {
2721 return;
2722 }
2723 }
2724
2725 if (menu == NULL) {
2726 return;
2727 }
2728
2729 // see if the mouse is within the window bounds and if so is this a mouse click
2730 if (down && !(menu->window.flags & WINDOW_POPUP) && !Rect_ContainsPoint(&menu->window.rect, DC->cursorx, DC->cursory)) {
2731 static qboolean inHandleKey = qfalse;
2732 if (!inHandleKey && ( key == K_MOUSE1 || key == K_MOUSE2 || key == K_MOUSE3 ) ) {
2733 inHandleKey = qtrue;
2734 Menus_HandleOOBClick(menu, key, down);
2735 inHandleKey = qfalse;
2736 return;
2737 }
2738 }
2739
2740 // get the item with focus
2741 for (i = 0; i < menu->itemCount; i++) {
2742 if (menu->items[i]->window.flags & WINDOW_HASFOCUS) {
2743 item = menu->items[i];
2744 }
2745 }
2746
2747 if (item != NULL) {
2748 if (Item_HandleKey(item, key, down)) {
2749 Item_Action(item);
2750 return;
2751 }
2752 }
2753
2754 if (!down) {
2755 return;
2756 }
2757
2758 // default handling
2759 switch ( key ) {
2760
2761 case K_F11:
2762 if (DC->getCVarValue("developer")) {
2763 debugMode ^= 1;
2764 }
2765 break;
2766
2767 case K_F12:
2768 if (DC->getCVarValue("developer")) {
2769 DC->executeText(EXEC_APPEND, "screenshot\n");
2770 }
2771 break;
2772 case K_KP_UPARROW:
2773 case K_UPARROW:
2774 Menu_SetPrevCursorItem(menu);
2775 break;
2776
2777 case K_ESCAPE:
2778 if (!g_waitingForKey && menu->onESC) {
2779 itemDef_t it;
2780 it.parent = menu;
2781 Item_RunScript(&it, menu->onESC);
2782 }
2783 break;
2784 case K_TAB:
2785 case K_KP_DOWNARROW:
2786 case K_DOWNARROW:
2787 Menu_SetNextCursorItem(menu);
2788 break;
2789
2790 case K_MOUSE1:
2791 case K_MOUSE2:
2792 if (item) {
2793 if (item->type == ITEM_TYPE_TEXT) {
2794 if (Rect_ContainsPoint(Item_CorrectedTextRect(item), DC->cursorx, DC->cursory)) {
2795 Item_Action(item);
2796 }
2797 } else if (item->type == ITEM_TYPE_EDITFIELD || item->type == ITEM_TYPE_NUMERICFIELD) {
2798 if (Rect_ContainsPoint(&item->window.rect, DC->cursorx, DC->cursory)) {
2799 item->cursorPos = 0;
2800 g_editingField = qtrue;
2801 g_editItem = item;
2802 }
2803 } else {
2804 if (Rect_ContainsPoint(&item->window.rect, DC->cursorx, DC->cursory)) {
2805 Item_Action(item);
2806 }
2807 }
2808 }
2809 break;
2810
2811 case K_JOY1:
2812 case K_JOY2:
2813 case K_JOY3:
2814 case K_JOY4:
2815 case K_AUX1:
2816 case K_AUX2:
2817 case K_AUX3:
2818 case K_AUX4:
2819 case K_AUX5:
2820 case K_AUX6:
2821 case K_AUX7:
2822 case K_AUX8:
2823 case K_AUX9:
2824 case K_AUX10:
2825 case K_AUX11:
2826 case K_AUX12:
2827 case K_AUX13:
2828 case K_AUX14:
2829 case K_AUX15:
2830 case K_AUX16:
2831 case K_KP_ENTER:
2832 case K_ENTER:
2833 if (item) {
2834 if (item->type == ITEM_TYPE_EDITFIELD || item->type == ITEM_TYPE_NUMERICFIELD) {
2835 item->cursorPos = 0;
2836 g_editingField = qtrue;
2837 g_editItem = item;
2838 } else {
2839 Item_Action(item);
2840 }
2841 }
2842 break;
2843 }
2844 }
2845
2846 void ToWindowCoords(float *x, float *y, windowDef_t *window) {
2847 if (window->border != 0) {
2848 *x += window->borderSize;
2849 *y += window->borderSize;
2850 }
2851 *x += window->rect.x;
2852 *y += window->rect.y;
2853 }
2854
2855 void Rect_ToWindowCoords(rectDef_t *rect, windowDef_t *window) {
2856 ToWindowCoords(&rect->x, &rect->y, window);
2857 }
2858
2859 void Item_SetTextExtents(itemDef_t *item, int *width, int *height, const char *text) {
2860 const char *textPtr = (text) ? text : item->text;
2861
2862 if (textPtr == NULL ) {
2863 return;
2864 }
2865
2866 *width = item->textRect.w;
2867 *height = item->textRect.h;
2868
2869 // keeps us from computing the widths and heights more than once
2870 if (*width == 0 || (item->type == ITEM_TYPE_OWNERDRAW && item->textalignment == ITEM_ALIGN_CENTER)) {
2871 int originalWidth = DC->textWidth(item->text, item->textscale, 0);
2872
2873 if (item->type == ITEM_TYPE_OWNERDRAW && (item->textalignment == ITEM_ALIGN_CENTER || item->textalignment == ITEM_ALIGN_RIGHT)) {
2874 originalWidth += DC->ownerDrawWidth(item->window.ownerDraw, item->textscale);
2875 } else if (item->type == ITEM_TYPE_EDITFIELD && item->textalignment == ITEM_ALIGN_CENTER && item->cvar) {
2876 char buff[256];
2877 DC->getCVarString(item->cvar, buff, 256);
2878 originalWidth += DC->textWidth(buff, item->textscale, 0);
2879 }
2880
2881 *width = DC->textWidth(textPtr, item->textscale, 0);
2882 *height = DC->textHeight(textPtr, item->textscale, 0);
2883 item->textRect.w = *width;
2884 item->textRect.h = *height;
2885 item->textRect.x = item->textalignx;
2886 item->textRect.y = item->textaligny;
2887 if (item->textalignment == ITEM_ALIGN_RIGHT) {
2888 item->textRect.x = item->textalignx - originalWidth;
2889 } else if (item->textalignment == ITEM_ALIGN_CENTER) {
2890 item->textRect.x = item->textalignx - originalWidth / 2;
2891 }
2892
2893 ToWindowCoords(&item->textRect.x, &item->textRect.y, &item->window);
2894 }
2895 }
2896
2897 void Item_TextColor(itemDef_t *item, vec4_t *newColor) {
2898 vec4_t lowLight;
2899 menuDef_t *parent = (menuDef_t*)item->parent;
2900
2901 Fade(&item->window.flags, &item->window.foreColor[3], parent->fadeClamp, &item->window.nextTime, parent->fadeCycle, qtrue, parent->fadeAmount);
2902
2903 if (item->window.flags & WINDOW_HASFOCUS) {
2904 lowLight[0] = 0.8 * parent->focusColor[0];
2905 lowLight[1] = 0.8 * parent->focusColor[1];
2906 lowLight[2] = 0.8 * parent->focusColor[2];
2907 lowLight[3] = 0.8 * parent->focusColor[3];
2908 LerpColor(parent->focusColor,lowLight,*newColor,0.5+0.5*sin(DC->realTime / PULSE_DIVISOR));
2909 } else if (item->textStyle == ITEM_TEXTSTYLE_BLINK && !((DC->realTime/BLINK_DIVISOR) & 1)) {
2910 lowLight[0] = 0.8 * item->window.foreColor[0];
2911 lowLight[1] = 0.8 * item->window.foreColor[1];
2912 lowLight[2] = 0.8 * item->window.foreColor[2];
2913 lowLight[3] = 0.8 * item->window.foreColor[3];
2914 LerpColor(item->window.foreColor,lowLight,*newColor,0.5+0.5*sin(DC->realTime / PULSE_DIVISOR));
2915 } else {
2916 memcpy(newColor, &item->window.foreColor, sizeof(vec4_t));
2917 // items can be enabled and disabled based on cvars
2918 }
2919
2920 if (item->enableCvar && *item->enableCvar && item->cvarTest && *item->cvarTest) {
2921 if (item->cvarFlags & (CVAR_ENABLE | CVAR_DISABLE) && !Item_EnableShowViaCvar(item, CVAR_ENABLE)) {
2922 memcpy(newColor, &parent->disableColor, sizeof(vec4_t));
2923 }
2924 }
2925 }
2926
2927 void Item_Text_AutoWrapped_Paint(itemDef_t *item) {
2928 char text[1024];
2929 const char *p, *textPtr, *newLinePtr;
2930 char buff[1024];
2931 int width, height, len, textWidth, newLine, newLineWidth;
2932 float y;
2933 vec4_t color;
2934
2935 textWidth = 0;
2936 newLinePtr = NULL;
2937
2938 if (item->text == NULL) {
2939 if (item->cvar == NULL) {
2940 return;
2941 }
2942 else {
2943 DC->getCVarString(item->cvar, text, sizeof(text));
2944 textPtr = text;
2945 }
2946 }
2947 else {
2948 textPtr = item->text;
2949 }
2950 if (*textPtr == '\0') {
2951 return;
2952 }
2953 Item_TextColor(item, &color);
2954 Item_SetTextExtents(item, &width, &height, textPtr);
2955
2956 y = item->textaligny;
2957 len = 0;
2958 buff[0] = '\0';
2959 newLine = 0;
2960 newLineWidth = 0;
2961 p = textPtr;
2962 while (p) {
2963 if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\0') {
2964 newLine = len;
2965 newLinePtr = p+1;
2966 newLineWidth = textWidth;
2967 }
2968 textWidth = DC->textWidth(buff, item->textscale, 0);
2969 if ( (newLine && textWidth > item->window.rect.w) || *p == '\n' || *p == '\0') {
2970 if (len) {
2971 if (item->textalignment == ITEM_ALIGN_LEFT) {
2972 item->textRect.x = item->textalignx;
2973 } else if (item->textalignment == ITEM_ALIGN_RIGHT) {
2974 item->textRect.x = item->textalignx - newLineWidth;
2975 } else if (item->textalignment == ITEM_ALIGN_CENTER) {
2976 item->textRect.x = item->textalignx - newLineWidth / 2;
2977 }
2978 item->textRect.y = y;
2979 ToWindowCoords(&item->textRect.x, &item->textRect.y, &item->window);
2980 //
2981 buff[newLine] = '\0';
2982 DC->drawText(item->textRect.x, item->textRect.y, item->textscale, color, buff, 0, 0, item->textStyle);
2983 }
2984 if (*p == '\0') {
2985 break;
2986 }
2987 //
2988 y += height + 5;
2989 p = newLinePtr;
2990 len = 0;
2991 newLine = 0;
2992 newLineWidth = 0;
2993 continue;
2994 }
2995 buff[len++] = *p++;
2996 buff[len] = '\0';
2997 }
2998 }
2999
3000 void Item_Text_Wrapped_Paint(itemDef_t *item) {
3001 char text[1024];
3002 const char *p, *start, *textPtr;
3003 char buff[1024];
3004 int length;
3005 int width, height;
3006 float x, y;
3007 vec4_t color;
3008
3009 // now paint the text and/or any optional images
3010 // default to left
3011
3012 if (item->text == NULL) {
3013 if (item->cvar == NULL) {
3014 return;
3015 }
3016 else {
3017 DC->getCVarString(item->cvar, text, sizeof(text));
3018 textPtr = text;
3019 }
3020 }
3021 else {
3022 textPtr = item->text;
3023 }
3024 if (*textPtr == '\0') {
3025 return;
3026 }
3027
3028 Item_TextColor(item, &color);
3029 Item_SetTextExtents(item, &width, &height, textPtr);
3030
3031 x = item->textRect.x;
3032 y = item->textRect.y;
3033 start = textPtr;
3034 p = strchr(textPtr, '\r');
3035 while (p && *p) {
3036 length = p-start+1;
3037 if (length > sizeof(buff)) {
3038 length = sizeof(buff);
3039 }
3040 Q_strncpyz(buff, start, length);
3041 DC->drawText(x, y, item->textscale, color, buff, 0, 0, item->textStyle);
3042 y += height + 5;
3043 start += length;
3044 p = strchr(p+1, '\r');
3045 }
3046 DC->drawText(x, y, item->textscale, color, start, 0, 0, item->textStyle);
3047 }
3048
3049 void Item_Text_Paint(itemDef_t *item) {
3050 char text[1024];
3051 const char *textPtr;
3052 int height, width;
3053 vec4_t color;
3054
3055 if (item->window.flags & WINDOW_WRAPPED) {
3056 Item_Text_Wrapped_Paint(item);
3057 return;
3058 }
3059 if (item->window.flags & WINDOW_AUTOWRAPPED) {
3060 Item_Text_AutoWrapped_Paint(item);
3061 return;
3062 }
3063
3064 if (item->text == NULL) {
3065 if (item->cvar == NULL) {
3066 return;
3067 }
3068 else {
3069 DC->getCVarString(item->cvar, text, sizeof(text));
3070 textPtr = text;
3071 }
3072 }
3073 else {
3074 textPtr = item->text;
3075 }
3076
3077 // this needs to go here as it sets extents for cvar types as well
3078 Item_SetTextExtents(item, &width, &height, textPtr);
3079
3080 if (*textPtr == '\0') {
3081 return;
3082 }
3083
3084
3085 Item_TextColor(item, &color);
3086
3087 //FIXME: this is a fucking mess
3088 /*
3089 adjust = 0;
3090 if (item->textStyle == ITEM_TEXTSTYLE_OUTLINED || item->textStyle == ITEM_TEXTSTYLE_OUTLINESHADOWED) {
3091 adjust = 0.5;
3092 }
3093
3094 if (item->textStyle == ITEM_TEXTSTYLE_SHADOWED || item->textStyle == ITEM_TEXTSTYLE_OUTLINESHADOWED) {
3095 Fade(&item->window.flags, &DC->Assets.shadowColor[3], DC->Assets.fadeClamp, &item->window.nextTime, DC->Assets.fadeCycle, qfalse);
3096 DC->drawText(item->textRect.x + DC->Assets.shadowX, item->textRect.y + DC->Assets.shadowY, item->textscale, DC->Assets.shadowColor, textPtr, adjust);
3097 }
3098 */
3099
3100
3101 // if (item->textStyle == ITEM_TEXTSTYLE_OUTLINED || item->textStyle == ITEM_TEXTSTYLE_OUTLINESHADOWED) {
3102 // Fade(&item->window.flags, &item->window.outlineColor[3], DC->Assets.fadeClamp, &item->window.nextTime, DC->Assets.fadeCycle, qfalse);
3103 // /*
3104 // Text_Paint(item->textRect.x-1, item->textRect.y-1, item->textscale, item->window.foreColor, textPtr, adjust);
3105 // Text_Paint(item->textRect.x, item->textRect.y-1, item->textscale, item->window.foreColor, textPtr, adjust);
3106 // Text_Paint(item->textRect.x+1, item->textRect.y-1, item->textscale, item->window.foreColor, textPtr, adjust);
3107 // Text_Paint(item->textRect.x-1, item->textRect.y, item->textscale, item->window.foreColor, textPtr, adjust);
3108 // Text_Paint(item->textRect.x+1, item->textRect.y, item->textscale, item->window.foreColor, textPtr, adjust);
3109 // Text_Paint(item->textRect.x-1, item->textRect.y+1, item->textscale, item->window.foreColor, textPtr, adjust);
3110 // Text_Paint(item->textRect.x, item->textRect.y+1, item->textscale, item->window.foreColor, textPtr, adjust);
3111 // Text_Paint(item->textRect.x+1, item->textRect.y+1, item->textscale, item->window.foreColor, textPtr, adjust);
3112 // */
3113 // DC->drawText(item->textRect.x - 1, item->textRect.y + 1, item->textscale * 1.02, item->window.outlineColor, textPtr, adjust);
3114 // }
3115
3116 DC->drawText(item->textRect.x, item->textRect.y, item->textscale, color, textPtr, 0, 0, item->textStyle);
3117 }
3118
3119
3120
3121 //float trap_Cvar_VariableValue( const char *var_name );
3122 //void trap_Cvar_VariableStringBuffer( const char *var_name, char *buffer, int bufsize );
3123
3124 void Item_TextField_Paint(itemDef_t *item) {
3125 char buff[1024];
3126 vec4_t newColor, lowLight;
3127 int offset;
3128 menuDef_t *parent = (menuDef_t*)item->parent;
3129 editFieldDef_t *editPtr = (editFieldDef_t*)item->typeData;
3130
3131 Item_Text_Paint(item);
3132
3133 buff[0] = '\0';
3134
3135 if (item->cvar) {
3136 DC->getCVarString(item->cvar, buff, sizeof(buff));
3137 }
3138
3139 if (item->window.flags & WINDOW_HASFOCUS) {
3140 lowLight[0] = 0.8 * parent->focusColor[0];
3141 lowLight[1] = 0.8 * parent->focusColor[1];
3142 lowLight[2] = 0.8 * parent->focusColor[2];
3143 lowLight[3] = 0.8 * parent->focusColor[3];
3144 LerpColor(parent->focusColor,lowLight,newColor,0.5+0.5*sin(DC->realTime / PULSE_DIVISOR));
3145 } else {
3146 memcpy(&newColor, &item->window.foreColor, sizeof(vec4_t));
3147 }
3148
3149 offset = (item->text && *item->text) ? 8 : 0;
3150 if (item->window.flags & WINDOW_HASFOCUS && g_editingField) {
3151 char cursor = DC->getOverstrikeMode() ? '_' : '|';
3152 DC->drawTextWithCursor(item->textRect.x + item->textRect.w + offset, item->textRect.y, item->textscale, newColor, buff + editPtr->paintOffset, item->cursorPos - editPtr->paintOffset , cursor, editPtr->maxPaintChars, item->textStyle);
3153 } else {
3154 DC->drawText(item->textRect.x + item->textRect.w + offset, item->textRect.y, item->textscale, newColor, buff + editPtr->paintOffset, 0, editPtr->maxPaintChars, item->textStyle);
3155 }
3156
3157 }
3158
3159 void Item_YesNo_Paint(itemDef_t *item) {
3160 vec4_t newColor, lowLight;
3161 float value;
3162 menuDef_t *parent = (menuDef_t*)item->parent;
3163
3164 value = (item->cvar) ? DC->getCVarValue(item->cvar) : 0;
3165
3166 if (item->window.flags & WINDOW_HASFOCUS) {
3167 lowLight[0] = 0.8 * parent->focusColor[0];
3168 lowLight[1] = 0.8 * parent->focusColor[1];
3169 lowLight[2] = 0.8 * parent->focusColor[2];
3170 lowLight[3] = 0.8 * parent->focusColor[3];
3171 LerpColor(parent->focusColor,lowLight,newColor,0.5+0.5*sin(DC->realTime / PULSE_DIVISOR));
3172 } else {
3173 memcpy(&newColor, &item->window.foreColor, sizeof(vec4_t));
3174 }
3175
3176 if (item->text) {
3177 Item_Text_Paint(item);
3178 DC->drawText(item->textRect.x + item->textRect.w + 8, item->textRect.y, item->textscale, newColor, (value != 0) ? "Yes" : "No", 0, 0, item->textStyle);
3179 } else {
3180 DC->drawText(item->textRect.x, item->textRect.y, item->textscale, newColor, (value != 0) ? "Yes" : "No", 0, 0, item->textStyle);
3181 }
3182 }
3183
3184 void Item_Multi_Paint(itemDef_t *item) {
3185 vec4_t newColor, lowLight;
3186 const char *text = "";
3187 menuDef_t *parent = (menuDef_t*)item->parent;
3188
3189 if (item->window.flags & WINDOW_HASFOCUS) {
3190 lowLight[0] = 0.8 * parent->focusColor[0];
3191 lowLight[1] = 0.8 * parent->focusColor[1];
3192 lowLight[2] = 0.8 * parent->focusColor[2];
3193 lowLight[3] = 0.8 * parent->focusColor[3];
3194 LerpColor(parent->focusColor,lowLight,newColor,0.5+0.5*sin(DC->realTime / PULSE_DIVISOR));
3195 } else {
3196 memcpy(&newColor, &item->window.foreColor, sizeof(vec4_t));
3197 }
3198
3199 text = Item_Multi_Setting(item);
3200
3201 if (item->text) {
3202 Item_Text_Paint(item);
3203 DC->drawText(item->textRect.x + item->textRect.w + 8, item->textRect.y, item->textscale, newColor, text, 0, 0, item->textStyle);
3204 } else {
3205 DC->drawText(item->textRect.x, item->textRect.y, item->textscale, newColor, text, 0, 0, item->textStyle);
3206 }
3207 }
3208
3209
3210 typedef struct {
3211 char *command;
3212 int defaultbind1;
3213 int defaultbind2;
3214 int bind1;
3215 int bind2;
3216 } bind_t;
3217
3218 typedef struct
3219 {
3220 char* name;
3221 float defaultvalue;
3222 float value;
3223 } configcvar_t;
3224
3225
3226 static bind_t g_bindings[] =
3227 {
3228 {"+scores", K_TAB, -1, -1, -1},
3229 {"+button2", K_ENTER, -1, -1, -1},
3230 {"+speed", K_SHIFT, -1, -1, -1},
3231 {"+forward", K_UPARROW, -1, -1, -1},
3232 {"+back", K_DOWNARROW, -1, -1, -1},
3233 {"+moveleft", ',', -1, -1, -1},
3234 {"+moveright", '.', -1, -1, -1},
3235 {"+moveup", K_SPACE, -1, -1, -1},
3236 {"+movedown", 'c', -1, -1, -1},
3237 {"+left", K_LEFTARROW, -1, -1, -1},
3238 {"+right", K_RIGHTARROW, -1, -1, -1},
3239 {"+strafe", K_ALT, -1, -1, -1},
3240 {"+lookup", K_PGDN, -1, -1, -1},
3241 {"+lookdown", K_DEL, -1, -1, -1},
3242 {"+mlook", '/', -1, -1, -1},
3243 {"centerview", K_END, -1, -1, -1},
3244 {"+zoom", -1, -1, -1, -1},
3245 {"weapon 1", '1', -1, -1, -1},
3246 {"weapon 2", '2', -1, -1, -1},
3247 {"weapon 3", '3', -1, -1, -1},
3248 {"weapon 4", '4', -1, -1, -1},
3249 {"weapon 5", '5', -1, -1, -1},
3250 {"weapon 6", '6', -1, -1, -1},
3251 {"weapon 7", '7', -1, -1, -1},
3252 {"weapon 8", '8', -1, -1, -1},
3253 {"weapon 9", '9', -1, -1, -1},
3254 {"weapon 10", '0', -1, -1, -1},
3255 {"weapon 11", -1, -1, -1, -1},
3256 {"weapon 12", -1, -1, -1, -1},
3257 {"weapon 13", -1, -1, -1, -1},
3258 {"+attack", K_CTRL, -1, -1, -1},
3259 {"weapprev", '[', -1, -1, -1},
3260 {"weapnext", ']', -1, -1, -1},
3261 {"+button3", K_MOUSE3, -1, -1, -1},
3262 {"+button4", K_MOUSE4, -1, -1, -1},
3263 {"prevTeamMember", 'w', -1, -1, -1},
3264 {"nextTeamMember", 'r', -1, -1, -1},
3265 {"nextOrder", 't', -1, -1, -1},
3266 {"confirmOrder", 'y', -1, -1, -1},
3267 {"denyOrder", 'n', -1, -1, -1},
3268 {"taskOffense", 'o', -1, -1, -1},
3269 {"taskDefense", 'd', -1, -1, -1},
3270 {"taskPatrol", 'p', -1, -1, -1},
3271 {"taskCamp", 'c', -1, -1, -1},
3272 {"taskFollow", 'f', -1, -1, -1},
3273 {"taskRetrieve", 'v', -1, -1, -1},
3274 {"taskEscort", 'e', -1, -1, -1},
3275 {"taskOwnFlag", 'i', -1, -1, -1},
3276 {"taskSuicide", 'k', -1, -1, -1},
3277 {"tauntKillInsult", K_F1, -1, -1, -1},
3278 {"tauntPraise", K_F2, -1, -1, -1},
3279 {"tauntTaunt", K_F3, -1, -1, -1},
3280 {"tauntDeathInsult", K_F4, -1, -1, -1},
3281 {"tauntGauntlet", K_F5, -1, -1, -1},
3282 {"scoresUp", K_KP_PGUP, -1, -1, -1},
3283 {"scoresDown", K_KP_PGDN, -1, -1, -1},
3284 {"messagemode", -1, -1, -1, -1},
3285 {"messagemode2", -1, -1, -1, -1},
3286 {"messagemode3", -1, -1, -1, -1},
3287 {"messagemode4", -1, -1, -1, -1}
3288 };
3289
3290
3291 static const int g_bindCount = ARRAY_LEN(g_bindings);
3292
3293 #ifndef MISSIONPACK
3294 static configcvar_t g_configcvars[] =
3295 {
3296 {"cl_run", 0, 0},
3297 {"m_pitch", 0, 0},
3298 {"cg_autoswitch", 0, 0},
3299 {"sensitivity", 0, 0},
3300 {"in_joystick", 0, 0},
3301 {"joy_threshold", 0, 0},
3302 {"m_filter", 0, 0},
3303 {"cl_freelook", 0, 0},
3304 {NULL, 0, 0}
3305 };
3306 #endif
3307
3308 /*
3309 =================
3310 Controls_GetKeyAssignment
3311 =================
3312 */
3313 static void Controls_GetKeyAssignment (char *command, int *twokeys)
3314 {
3315 int count;
3316 int j;
3317 char b[256];
3318
3319 twokeys[0] = twokeys[1] = -1;
3320 count = 0;
3321
3322 for ( j = 0; j < 256; j++ )
3323 {
3324 DC->getBindingBuf( j, b, 256 );
3325 if ( *b == 0 ) {
3326 continue;
3327 }
3328 if ( !Q_stricmp( b, command ) ) {
3329 twokeys[count] = j;
3330 count++;
3331 if (count == 2) {
3332 break;
3333 }
3334 }
3335 }
3336 }
3337
3338 /*
3339 =================
3340 Controls_GetConfig
3341 =================
3342 */
3343 void Controls_GetConfig( void )
3344 {
3345 int i;
3346 int twokeys[2];
3347
3348 // iterate each command, get its numeric binding
3349 for (i=0; i < g_bindCount; i++)
3350 {
3351
3352 Controls_GetKeyAssignment(g_bindings[i].command, twokeys);
3353
3354 g_bindings[i].bind1 = twokeys[0];
3355 g_bindings[i].bind2 = twokeys[1];
3356 }
3357
3358 //s_controls.invertmouse.curvalue = DC->getCVarValue( "m_pitch" ) < 0;
3359 //s_controls.smoothmouse.curvalue = UI_ClampCvar( 0, 1, Controls_GetCvarValue( "m_filter" ) );
3360 //s_controls.alwaysrun.curvalue = UI_ClampCvar( 0, 1, Controls_GetCvarValue( "cl_run" ) );
3361 //s_controls.autoswitch.curvalue = UI_ClampCvar( 0, 1, Controls_GetCvarValue( "cg_autoswitch" ) );
3362 //s_controls.sensitivity.curvalue = UI_ClampCvar( 2, 30, Controls_GetCvarValue( "sensitivity" ) );
3363 //s_controls.joyenable.curvalue = UI_ClampCvar( 0, 1, Controls_GetCvarValue( "in_joystick" ) );
3364 //s_controls.joythreshold.curvalue = UI_ClampCvar( 0.05, 0.75, Controls_GetCvarValue( "joy_threshold" ) );
3365 //s_controls.freelook.curvalue = UI_ClampCvar( 0, 1, Controls_GetCvarValue( "cl_freelook" ) );
3366 }
3367
3368 /*
3369 =================
3370 Controls_SetConfig
3371 =================
3372 */
3373 void Controls_SetConfig(qboolean restart)
3374 {
3375 int i;
3376
3377 // iterate each command, get its numeric binding
3378 for (i=0; i < g_bindCount; i++)
3379 {
3380
3381 if (g_bindings[i].bind1 != -1)
3382 {
3383 DC->setBinding( g_bindings[i].bind1, g_bindings[i].command );
3384
3385 if (g_bindings[i].bind2 != -1)
3386 DC->setBinding( g_bindings[i].bind2, g_bindings[i].command );
3387 }
3388 }
3389
3390 //if ( s_controls.invertmouse.curvalue )
3391 // DC->setCVar("m_pitch", va("%f),-fabs( DC->getCVarValue( "m_pitch" ) ) );
3392 //else
3393 // trap_Cvar_SetValue( "m_pitch", fabs( trap_Cvar_VariableValue( "m_pitch" ) ) );
3394
3395 //trap_Cvar_SetValue( "m_filter", s_controls.smoothmouse.curvalue );
3396 //trap_Cvar_SetValue( "cl_run", s_controls.alwaysrun.curvalue );
3397 //trap_Cvar_SetValue( "cg_autoswitch", s_controls.autoswitch.curvalue );
3398 //trap_Cvar_SetValue( "sensitivity", s_controls.sensitivity.curvalue );
3399 //trap_Cvar_SetValue( "in_joystick", s_controls.joyenable.curvalue );
3400 //trap_Cvar_SetValue( "joy_threshold", s_controls.joythreshold.curvalue );
3401 //trap_Cvar_SetValue( "cl_freelook", s_controls.freelook.curvalue );
3402 DC->executeText(EXEC_APPEND, "in_restart\n");
3403 //trap_Cmd_ExecuteText( EXEC_APPEND, "in_restart\n" );
3404 }
3405
3406 /*
3407 =================
3408 Controls_SetDefaults
3409 =================
3410 */
3411 void Controls_SetDefaults( void )
3412 {
3413 int i;
3414
3415 // iterate each command, set its default binding
3416 for (i=0; i < g_bindCount; i++)
3417 {
3418 g_bindings[i].bind1 = g_bindings[i].defaultbind1;
3419 g_bindings[i].bind2 = g_bindings[i].defaultbind2;
3420 }
3421
3422 //s_controls.invertmouse.curvalue = Controls_GetCvarDefault( "m_pitch" ) < 0;
3423 //s_controls.smoothmouse.curvalue = Controls_GetCvarDefault( "m_filter" );
3424 //s_controls.alwaysrun.curvalue = Controls_GetCvarDefault( "cl_run" );
3425 //s_controls.autoswitch.curvalue = Controls_GetCvarDefault( "cg_autoswitch" );
3426 //s_controls.sensitivity.curvalue = Controls_GetCvarDefault( "sensitivity" );
3427 //s_controls.joyenable.curvalue = Controls_GetCvarDefault( "in_joystick" );
3428 //s_controls.joythreshold.curvalue = Controls_GetCvarDefault( "joy_threshold" );
3429 //s_controls.freelook.curvalue = Controls_GetCvarDefault( "cl_freelook" );
3430 }
3431
3432 int BindingIDFromName(const char *name) {
3433 int i;
3434 for (i=0; i < g_bindCount; i++)
3435 {
3436 if (Q_stricmp(name, g_bindings[i].command) == 0) {
3437 return i;
3438 }
3439 }
3440 return -1;
3441 }
3442
3443 char g_nameBind1[32];
3444 char g_nameBind2[32];
3445
3446 void BindingFromName(const char *cvar) {
3447 int i, b1, b2;
3448
3449 // iterate each command, set its default binding
3450 for (i=0; i < g_bindCount; i++)
3451 {
3452 if (Q_stricmp(cvar, g_bindings[i].command) == 0) {
3453 b1 = g_bindings[i].bind1;
3454 if (b1 == -1) {
3455 break;
3456 }
3457 DC->keynumToStringBuf( b1, g_nameBind1, 32 );
3458 Q_strupr(g_nameBind1);
3459
3460 b2 = g_bindings[i].bind2;
3461 if (b2 != -1)
3462 {
3463 DC->keynumToStringBuf( b2, g_nameBind2, 32 );
3464 Q_strupr(g_nameBind2);
3465 strcat( g_nameBind1, " or " );
3466 strcat( g_nameBind1, g_nameBind2 );
3467 }
3468 return;
3469 }
3470 }
3471 strcpy(g_nameBind1, "???");
3472 }
3473
3474 void Item_Slider_Paint(itemDef_t *item) {
3475 vec4_t newColor, lowLight;
3476 float x, y;
3477 menuDef_t *parent = (menuDef_t*)item->parent;
3478
3479 if (item->window.flags & WINDOW_HASFOCUS) {
3480 lowLight[0] = 0.8 * parent->focusColor[0];
3481 lowLight[1] = 0.8 * parent->focusColor[1];
3482 lowLight[2] = 0.8 * parent->focusColor[2];
3483 lowLight[3] = 0.8 * parent->focusColor[3];
3484 LerpColor(parent->focusColor,lowLight,newColor,0.5+0.5*sin(DC->realTime / PULSE_DIVISOR));
3485 } else {
3486 memcpy(&newColor, &item->window.foreColor, sizeof(vec4_t));
3487 }
3488
3489 y = item->window.rect.y;
3490 if (item->text) {
3491 Item_Text_Paint(item);
3492 x = item->textRect.x + item->textRect.w + 8;
3493 } else {
3494 x = item->window.rect.x;
3495 }
3496 DC->setColor(newColor);
3497 DC->drawHandlePic( x, y, SLIDER_WIDTH, SLIDER_HEIGHT, DC->Assets.sliderBar );
3498
3499 x = Item_Slider_ThumbPosition(item);
3500 DC->drawHandlePic( x - (SLIDER_THUMB_WIDTH / 2), y - 2, SLIDER_THUMB_WIDTH, SLIDER_THUMB_HEIGHT, DC->Assets.sliderThumb );
3501 }
3502
3503 void Item_Bind_Paint(itemDef_t *item) {
3504 vec4_t newColor, lowLight;
3505 int maxChars = 0;
3506 menuDef_t *parent = (menuDef_t*)item->parent;
3507 editFieldDef_t *editPtr = (editFieldDef_t*)item->typeData;
3508 if (editPtr) {
3509 maxChars = editPtr->maxPaintChars;
3510 }
3511
3512 if (item->window.flags & WINDOW_HASFOCUS) {
3513 if (g_bindItem == item) {
3514 lowLight[0] = 0.8f * 1.0f;
3515 lowLight[1] = 0.8f * 0.0f;
3516 lowLight[2] = 0.8f * 0.0f;
3517 lowLight[3] = 0.8f * 1.0f;
3518 } else {
3519 lowLight[0] = 0.8f * parent->focusColor[0];
3520 lowLight[1] = 0.8f * parent->focusColor[1];
3521 lowLight[2] = 0.8f * parent->focusColor[2];
3522 lowLight[3] = 0.8f * parent->focusColor[3];
3523 }
3524 LerpColor(parent->focusColor,lowLight,newColor,0.5+0.5*sin(DC->realTime / PULSE_DIVISOR));
3525 } else {
3526 memcpy(&newColor, &item->window.foreColor, sizeof(vec4_t));
3527 }
3528
3529 if (item->text) {
3530 Item_Text_Paint(item);
3531 BindingFromName(item->cvar);
3532 DC->drawText(item->textRect.x + item->textRect.w + 8, item->textRect.y, item->textscale, newColor, g_nameBind1, 0, maxChars, item->textStyle);
3533 } else {
3534 DC->drawText(item->textRect.x, item->textRect.y, item->textscale, newColor, "FIXME", 0, maxChars, item->textStyle);
3535 }
3536 }
3537
3538 qboolean Display_KeyBindPending(void) {
3539 return g_waitingForKey;
3540 }
3541
3542 qboolean Item_Bind_HandleKey(itemDef_t *item, int key, qboolean down) {
3543 int id;
3544 int i;
3545
3546 if (!g_waitingForKey)
3547 {
3548 if (down && ((key == K_MOUSE1 && Rect_ContainsPoint(&item->window.rect, DC->cursorx, DC->cursory))
3549 || key == K_ENTER || key == K_KP_ENTER || key == K_JOY1 || key == K_JOY2 || key == K_JOY3 || key == K_JOY4)) {
3550 g_waitingForKey = qtrue;
3551 g_bindItem = item;
3552 }
3553 return qtrue;
3554 }
3555 else
3556 {
3557 if (g_bindItem == NULL) {
3558 return qtrue;
3559 }
3560
3561 if (key & K_CHAR_FLAG) {
3562 return qtrue;
3563 }
3564
3565 switch (key)
3566 {
3567 case K_ESCAPE:
3568 g_waitingForKey = qfalse;
3569 return qtrue;
3570
3571 case K_BACKSPACE:
3572 id = BindingIDFromName(item->cvar);
3573 if (id != -1) {
3574 if( g_bindings[id].bind1 != -1 ) {
3575 DC->setBinding( g_bindings[id].bind1, "" );
3576 g_bindings[id].bind1 = -1;
3577 }
3578 if( g_bindings[id].bind2 != -1 ) {
3579 DC->setBinding( g_bindings[id].bind2, "" );
3580 g_bindings[id].bind2 = -1;
3581 }
3582 }
3583 Controls_SetConfig(qtrue);
3584 g_waitingForKey = qfalse;
3585 g_bindItem = NULL;
3586 return qtrue;
3587
3588 case '`':
3589 return qtrue;
3590 }
3591 }
3592
3593 if (key != -1)
3594 {
3595
3596 for (i=0; i < g_bindCount; i++)
3597 {
3598
3599 if (g_bindings[i].bind2 == key) {
3600 g_bindings[i].bind2 = -1;
3601 }
3602
3603 if (g_bindings[i].bind1 == key)
3604 {
3605 g_bindings[i].bind1 = g_bindings[i].bind2;
3606 g_bindings[i].bind2 = -1;
3607 }
3608 }
3609 }
3610
3611
3612 id = BindingIDFromName(item->cvar);
3613
3614 if (id != -1) {
3615 if (key == -1) {
3616 if( g_bindings[id].bind1 != -1 ) {
3617 DC->setBinding( g_bindings[id].bind1, "" );
3618 g_bindings[id].bind1 = -1;
3619 }
3620 if( g_bindings[id].bind2 != -1 ) {
3621 DC->setBinding( g_bindings[id].bind2, "" );
3622 g_bindings[id].bind2 = -1;
3623 }
3624 }
3625 else if (g_bindings[id].bind1 == -1) {
3626 g_bindings[id].bind1 = key;
3627 }
3628 else if (g_bindings[id].bind1 != key && g_bindings[id].bind2 == -1) {
3629 g_bindings[id].bind2 = key;
3630 }
3631 else {
3632 DC->setBinding( g_bindings[id].bind1, "" );
3633 DC->setBinding( g_bindings[id].bind2, "" );
3634 g_bindings[id].bind1 = key;
3635 g_bindings[id].bind2 = -1;
3636 }
3637 }
3638
3639 Controls_SetConfig(qtrue);
3640 g_waitingForKey = qfalse;
3641
3642 return qtrue;
3643 }
3644
3645
3646
3647 void AdjustFrom640(float *x, float *y, float *w, float *h) {
3648 //*x = *x * DC->scale + DC->bias;
3649 *x *= DC->xscale;
3650 *y *= DC->yscale;
3651 *w *= DC->xscale;
3652 *h *= DC->yscale;
3653 }
3654
3655 void Item_Model_Paint(itemDef_t *item) {
3656 float x, y, w, h;
3657 refdef_t refdef;
3658 refEntity_t ent;
3659 vec3_t mins, maxs, origin;
3660 vec3_t angles;
3661 modelDef_t *modelPtr = (modelDef_t*)item->typeData;
3662
3663 if (modelPtr == NULL) {
3664 return;
3665 }
3666
3667 // setup the refdef
3668 memset( &refdef, 0, sizeof( refdef ) );
3669 refdef.rdflags = RDF_NOWORLDMODEL;
3670 AxisClear( refdef.viewaxis );
3671 x = item->window.rect.x+1;
3672 y = item->window.rect.y+1;
3673 w = item->window.rect.w-2;
3674 h = item->window.rect.h-2;
3675
3676 AdjustFrom640( &x, &y, &w, &h );
3677
3678 refdef.x = x;
3679 refdef.y = y;
3680 refdef.width = w;
3681 refdef.height = h;
3682
3683 DC->modelBounds( item->asset, mins, maxs );
3684
3685 origin[2] = -0.5 * ( mins[2] + maxs[2] );
3686 origin[1] = 0.5 * ( mins[1] + maxs[1] );
3687
3688 // calculate distance so the model nearly fills the box
3689 if (qtrue) {
3690 float len = 0.5 * ( maxs[2] - mins[2] );
3691 origin[0] = len / 0.268; // len / tan( fov/2 )
3692 //origin[0] = len / tan(w/2);
3693 } else {
3694 origin[0] = item->textscale;
3695 }
3696 refdef.fov_x = (modelPtr->fov_x) ? modelPtr->fov_x : w;
3697 refdef.fov_y = (modelPtr->fov_y) ? modelPtr->fov_y : h;
3698
3699 //refdef.fov_x = (int)((float)refdef.width / 640.0f * 90.0f);
3700 //xx = refdef.width / tan( refdef.fov_x / 360 * M_PI );
3701 //refdef.fov_y = atan2( refdef.height, xx );
3702 //refdef.fov_y *= ( 360 / M_PI );
3703
3704 DC->clearScene();
3705
3706 refdef.time = DC->realTime;
3707
3708 // add the model
3709
3710 memset( &ent, 0, sizeof(ent) );
3711
3712 //adjust = 5.0 * sin( (float)uis.realtime / 500 );
3713 //adjust = 360 % (int)((float)uis.realtime / 1000);
3714 //VectorSet( angles, 0, 0, 1 );
3715
3716 // use item storage to track
3717 if (modelPtr->rotationSpeed) {
3718 if (DC->realTime > item->window.nextTime) {
3719 item->window.nextTime = DC->realTime + modelPtr->rotationSpeed;
3720 modelPtr->angle = (int)(modelPtr->angle + 1) % 360;
3721 }
3722 }
3723 VectorSet( angles, 0, modelPtr->angle, 0 );
3724 AnglesToAxis( angles, ent.axis );
3725
3726 ent.hModel = item->asset;
3727 VectorCopy( origin, ent.origin );
3728 VectorCopy( origin, ent.lightingOrigin );
3729 ent.renderfx = RF_LIGHTING_ORIGIN | RF_NOSHADOW;
3730 VectorCopy( ent.origin, ent.oldorigin );
3731
3732 DC->addRefEntityToScene( &ent );
3733 DC->renderScene( &refdef );
3734
3735 }
3736
3737
3738 void Item_Image_Paint(itemDef_t *item) {
3739 if (item == NULL) {
3740 return;
3741 }
3742 DC->drawHandlePic(item->window.rect.x+1, item->window.rect.y+1, item->window.rect.w-2, item->window.rect.h-2, item->asset);
3743 }
3744
3745 void Item_ListBox_Paint(itemDef_t *item) {
3746 float x, y, size, thumb;
3747 int count, i;
3748 qhandle_t image;
3749 qhandle_t optionalImage;
3750 listBoxDef_t *listPtr = (listBoxDef_t*)item->typeData;
3751
3752 // the listbox is horizontal or vertical and has a fixed size scroll bar going either direction
3753 // elements are enumerated from the DC and either text or image handles are acquired from the DC as well
3754 // textscale is used to size the text, textalignx and textaligny are used to size image elements
3755 // there is no clipping available so only the last completely visible item is painted
3756 count = DC->feederCount(item->special);
3757 // default is vertical if horizontal flag is not here
3758 if (item->window.flags & WINDOW_HORIZONTAL) {
3759 // draw scrollbar in bottom of the window
3760 // bar
3761 x = item->window.rect.x + 1;
3762 y = item->window.rect.y + item->window.rect.h - SCROLLBAR_SIZE - 1;
3763 DC->drawHandlePic(x, y, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarArrowLeft);
3764 x += SCROLLBAR_SIZE - 1;
3765 size = item->window.rect.w - (SCROLLBAR_SIZE * 2);
3766 DC->drawHandlePic(x, y, size+1, SCROLLBAR_SIZE, DC->Assets.scrollBar);
3767 x += size - 1;
3768 DC->drawHandlePic(x, y, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarArrowRight);
3769 // thumb
3770 thumb = Item_ListBox_ThumbDrawPosition(item);//Item_ListBox_ThumbPosition(item);
3771 if (thumb > x - SCROLLBAR_SIZE - 1) {
3772 thumb = x - SCROLLBAR_SIZE - 1;
3773 }
3774 DC->drawHandlePic(thumb, y, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarThumb);
3775 //
3776 listPtr->endPos = listPtr->startPos;
3777 size = item->window.rect.w - 2;
3778 // items
3779 // size contains max available space
3780 if (listPtr->elementStyle == LISTBOX_IMAGE) {
3781 // fit = 0;
3782 x = item->window.rect.x + 1;
3783 y = item->window.rect.y + 1;
3784 for (i = listPtr->startPos; i < count; i++) {
3785 // always draw at least one
3786 // which may overdraw the box if it is too small for the element
3787 image = DC->feederItemImage(item->special, i);
3788 if (image) {
3789 DC->drawHandlePic(x+1, y+1, listPtr->elementWidth - 2, listPtr->elementHeight - 2, image);
3790 }
3791
3792 if (i == item->cursorPos) {
3793 DC->drawRect(x, y, listPtr->elementWidth-1, listPtr->elementHeight-1, item->window.borderSize, item->window.borderColor);
3794 }
3795
3796 size -= listPtr->elementWidth;
3797 if (size < listPtr->elementWidth) {
3798 listPtr->drawPadding = size; //listPtr->elementWidth - size;
3799 break;
3800 }
3801 x += listPtr->elementWidth;
3802 listPtr->endPos++;
3803 // fit++;
3804 }
3805 } else {
3806 //
3807 }
3808 } else {
3809 // draw scrollbar to right side of the window
3810 x = item->window.rect.x + item->window.rect.w - SCROLLBAR_SIZE - 1;
3811 y = item->window.rect.y + 1;
3812 DC->drawHandlePic(x, y, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarArrowUp);
3813 y += SCROLLBAR_SIZE - 1;
3814
3815 listPtr->endPos = listPtr->startPos;
3816 size = item->window.rect.h - (SCROLLBAR_SIZE * 2);
3817 DC->drawHandlePic(x, y, SCROLLBAR_SIZE, size+1, DC->Assets.scrollBar);
3818 y += size - 1;
3819 DC->drawHandlePic(x, y, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarArrowDown);
3820 // thumb
3821 thumb = Item_ListBox_ThumbDrawPosition(item);//Item_ListBox_ThumbPosition(item);
3822 if (thumb > y - SCROLLBAR_SIZE - 1) {
3823 thumb = y - SCROLLBAR_SIZE - 1;
3824 }
3825 DC->drawHandlePic(x, thumb, SCROLLBAR_SIZE, SCROLLBAR_SIZE, DC->Assets.scrollBarThumb);
3826
3827 // adjust size for item painting
3828 size = item->window.rect.h - 2;
3829 if (listPtr->elementStyle == LISTBOX_IMAGE) {
3830 // fit = 0;
3831 x = item->window.rect.x + 1;
3832 y = item->window.rect.y + 1;
3833 for (i = listPtr->startPos; i < count; i++) {
3834 // always draw at least one
3835 // which may overdraw the box if it is too small for the element
3836 image = DC->feederItemImage(item->special, i);
3837 if (image) {
3838 DC->drawHandlePic(x+1, y+1, listPtr->elementWidth - 2, listPtr->elementHeight - 2, image);
3839 }
3840
3841 if (i == item->cursorPos) {
3842 DC->drawRect(x, y, listPtr->elementWidth - 1, listPtr->elementHeight - 1, item->window.borderSize, item->window.borderColor);
3843 }
3844
3845 listPtr->endPos++;
3846 size -= listPtr->elementWidth;
3847 if (size < listPtr->elementHeight) {
3848 listPtr->drawPadding = listPtr->elementHeight - size;
3849 break;
3850 }
3851 y += listPtr->elementHeight;
3852 // fit++;
3853 }
3854 } else {
3855 x = item->window.rect.x + 1;
3856 y = item->window.rect.y + 1;
3857 for (i = listPtr->startPos; i < count; i++) {
3858 const char *text;
3859 // always draw at least one
3860 // which may overdraw the box if it is too small for the element
3861
3862 if (listPtr->numColumns > 0) {
3863 int j;
3864 for (j = 0; j < listPtr->numColumns; j++) {
3865 text = DC->feederItemText(item->special, i, j, &optionalImage);
3866 if (optionalImage >= 0) {
3867 DC->drawHandlePic(x + 4 + listPtr->columnInfo[j].pos, y - 1 + listPtr->elementHeight / 2, listPtr->columnInfo[j].width, listPtr->columnInfo[j].width, optionalImage);
3868 } else if (text) {
3869 DC->drawText(x + 4 + listPtr->columnInfo[j].pos, y + listPtr->elementHeight, item->textscale, item->window.foreColor, text, 0, listPtr->columnInfo[j].maxChars, item->textStyle);
3870 }
3871 }
3872 } else {
3873 text = DC->feederItemText(item->special, i, 0, &optionalImage);
3874 if (optionalImage >= 0) {
3875 //DC->drawHandlePic(x + 4 + listPtr->elementHeight, y, listPtr->columnInfo[j].width, listPtr->columnInfo[j].width, optionalImage);
3876 } else if (text) {
3877 DC->drawText(x + 4, y + listPtr->elementHeight, item->textscale, item->window.foreColor, text, 0, 0, item->textStyle);
3878 }
3879 }
3880
3881 if (i == item->cursorPos) {
3882 DC->fillRect(x + 2, y + 2, item->window.rect.w - SCROLLBAR_SIZE - 4, listPtr->elementHeight, item->window.outlineColor);
3883 }
3884
3885 size -= listPtr->elementHeight;
3886 if (size < listPtr->elementHeight) {
3887 listPtr->drawPadding = listPtr->elementHeight - size;
3888 break;
3889 }
3890 listPtr->endPos++;
3891 y += listPtr->elementHeight;
3892 // fit++;
3893 }
3894 }
3895 }
3896 }
3897
3898
3899 void Item_OwnerDraw_Paint(itemDef_t *item) {
3900 if (item == NULL) {
3901 return;
3902 }
3903
3904 if (DC->ownerDrawItem) {
3905 vec4_t color, lowLight;
3906 menuDef_t *parent = (menuDef_t*)item->parent;
3907 Fade(&item->window.flags, &item->window.foreColor[3], parent->fadeClamp, &item->window.nextTime, parent->fadeCycle, qtrue, parent->fadeAmount);
3908 memcpy(&color, &item->window.foreColor, sizeof(color));
3909 if (item->numColors > 0 && DC->getValue) {
3910 // if the value is within one of the ranges then set color to that, otherwise leave at default
3911 int i;
3912 float f = DC->getValue(item->window.ownerDraw);
3913 for (i = 0; i < item->numColors; i++) {
3914 if (f >= item->colorRanges[i].low && f <= item->colorRanges[i].high) {
3915 memcpy(&color, &item->colorRanges[i].color, sizeof(color));
3916 break;
3917 }
3918 }
3919 }
3920
3921 if (item->window.flags & WINDOW_HASFOCUS) {
3922 lowLight[0] = 0.8 * parent->focusColor[0];
3923 lowLight[1] = 0.8 * parent->focusColor[1];
3924 lowLight[2] = 0.8 * parent->focusColor[2];
3925 lowLight[3] = 0.8 * parent->focusColor[3];
3926 LerpColor(parent->focusColor,lowLight,color,0.5+0.5*sin(DC->realTime / PULSE_DIVISOR));
3927 } else if (item->textStyle == ITEM_TEXTSTYLE_BLINK && !((DC->realTime/BLINK_DIVISOR) & 1)) {
3928 lowLight[0] = 0.8 * item->window.foreColor[0];
3929 lowLight[1] = 0.8 * item->window.foreColor[1];
3930 lowLight[2] = 0.8 * item->window.foreColor[2];
3931 lowLight[3] = 0.8 * item->window.foreColor[3];
3932 LerpColor(item->window.foreColor,lowLight,color,0.5+0.5*sin(DC->realTime / PULSE_DIVISOR));
3933 }
3934
3935 if (item->cvarFlags & (CVAR_ENABLE | CVAR_DISABLE) && !Item_EnableShowViaCvar(item, CVAR_ENABLE)) {
3936 Com_Memcpy(color, parent->disableColor, sizeof(vec4_t));
3937 }
3938
3939 if (item->text) {
3940 Item_Text_Paint(item);
3941 if (item->text[0]) {
3942 // +8 is an offset kludge to properly align owner draw items that have text combined with them
3943 DC->ownerDrawItem(item->textRect.x + item->textRect.w + 8, item->window.rect.y, item->window.rect.w, item->window.rect.h, 0, item->textaligny, item->window.ownerDraw, item->window.ownerDrawFlags, item->alignment, item->special, item->textscale, color, item->window.background, item->textStyle );
3944 } else {
3945 DC->ownerDrawItem(item->textRect.x + item->textRect.w, item->window.rect.y, item->window.rect.w, item->window.rect.h, 0, item->textaligny, item->window.ownerDraw, item->window.ownerDrawFlags, item->alignment, item->special, item->textscale, color, item->window.background, item->textStyle );
3946 }
3947 } else {
3948 DC->ownerDrawItem(item->window.rect.x, item->window.rect.y, item->window.rect.w, item->window.rect.h, item->textalignx, item->textaligny, item->window.ownerDraw, item->window.ownerDrawFlags, item->alignment, item->special, item->textscale, color, item->window.background, item->textStyle );
3949 }
3950 }
3951 }
3952
3953
3954 void Item_Paint(itemDef_t *item) {
3955 vec4_t red;
3956 menuDef_t *parent;
3957
3958 red[0] = red[3] = 1;
3959 red[1] = red[2] = 0;
3960
3961 if (item == NULL) {
3962 return;
3963 }
3964
3965 parent = (menuDef_t*)item->parent;
3966
3967 if (item->window.flags & WINDOW_ORBITING) {
3968 if (DC->realTime > item->window.nextTime) {
3969 float rx, ry, a, c, s, w, h;
3970
3971 item->window.nextTime = DC->realTime + item->window.offsetTime;
3972 // translate
3973 w = item->window.rectClient.w / 2;
3974 h = item->window.rectClient.h / 2;
3975 rx = item->window.rectClient.x + w - item->window.rectEffects.x;
3976 ry = item->window.rectClient.y + h - item->window.rectEffects.y;
3977 a = 3 * M_PI / 180;
3978 c = cos(a);
3979 s = sin(a);
3980 item->window.rectClient.x = (rx * c - ry * s) + item->window.rectEffects.x - w;
3981 item->window.rectClient.y = (rx * s + ry * c) + item->window.rectEffects.y - h;
3982 Item_UpdatePosition(item);
3983
3984 }
3985 }
3986
3987
3988 if (item->window.flags & WINDOW_INTRANSITION) {
3989 if (DC->realTime > item->window.nextTime) {
3990 int done = 0;
3991 item->window.nextTime = DC->realTime + item->window.offsetTime;
3992 // transition the x,y
3993 if (item->window.rectClient.x == item->window.rectEffects.x) {
3994 done++;
3995 } else {
3996 if (item->window.rectClient.x < item->window.rectEffects.x) {
3997 item->window.rectClient.x += item->window.rectEffects2.x;
3998 if (item->window.rectClient.x > item->window.rectEffects.x) {
3999 item->window.rectClient.x = item->window.rectEffects.x;
4000 done++;
4001 }
4002 } else {
4003 item->window.rectClient.x -= item->window.rectEffects2.x;
4004 if (item->window.rectClient.x < item->window.rectEffects.x) {
4005 item->window.rectClient.x = item->window.rectEffects.x;
4006 done++;
4007 }
4008 }
4009 }
4010 if (item->window.rectClient.y == item->window.rectEffects.y) {
4011 done++;
4012 } else {
4013 if (item->window.rectClient.y < item->window.rectEffects.y) {
4014 item->window.rectClient.y += item->window.rectEffects2.y;
4015 if (item->window.rectClient.y > item->window.rectEffects.y) {
4016 item->window.rectClient.y = item->window.rectEffects.y;
4017 done++;
4018 }
4019 } else {
4020 item->window.rectClient.y -= item->window.rectEffects2.y;
4021 if (item->window.rectClient.y < item->window.rectEffects.y) {
4022 item->window.rectClient.y = item->window.rectEffects.y;
4023 done++;
4024 }
4025 }
4026 }
4027 if (item->window.rectClient.w == item->window.rectEffects.w) {
4028 done++;
4029 } else {
4030 if (item->window.rectClient.w < item->window.rectEffects.w) {
4031 item->window.rectClient.w += item->window.rectEffects2.w;
4032 if (item->window.rectClient.w > item->window.rectEffects.w) {
4033 item->window.rectClient.w = item->window.rectEffects.w;
4034 done++;
4035 }
4036 } else {
4037 item->window.rectClient.w -= item->window.rectEffects2.w;
4038 if (item->window.rectClient.w < item->window.rectEffects.w) {
4039 item->window.rectClient.w = item->window.rectEffects.w;
4040 done++;
4041 }
4042 }
4043 }
4044 if (item->window.rectClient.h == item->window.rectEffects.h) {
4045 done++;
4046 } else {
4047 if (item->window.rectClient.h < item->window.rectEffects.h) {
4048 item->window.rectClient.h += item->window.rectEffects2.h;
4049 if (item->window.rectClient.h > item->window.rectEffects.h) {
4050 item->window.rectClient.h = item->window.rectEffects.h;
4051 done++;
4052 }
4053 } else {
4054 item->window.rectClient.h -= item->window.rectEffects2.h;
4055 if (item->window.rectClient.h < item->window.rectEffects.h) {
4056 item->window.rectClient.h = item->window.rectEffects.h;
4057 done++;
4058 }
4059 }
4060 }
4061
4062 Item_UpdatePosition(item);
4063
4064 if (done == 4) {
4065 item->window.flags &= ~WINDOW_INTRANSITION;
4066 }
4067
4068 }
4069 }
4070
4071 if (item->window.ownerDrawFlags && DC->ownerDrawVisible) {
4072 if (!DC->ownerDrawVisible(item->window.ownerDrawFlags)) {
4073 item->window.flags &= ~WINDOW_VISIBLE;
4074 } else {
4075 item->window.flags |= WINDOW_VISIBLE;
4076 }
4077 }
4078
4079 if (item->cvarFlags & (CVAR_SHOW | CVAR_HIDE)) {
4080 if (!Item_EnableShowViaCvar(item, CVAR_SHOW)) {
4081 return;
4082 }
4083 }
4084
4085 if (item->window.flags & WINDOW_TIMEDVISIBLE) {
4086
4087 }
4088
4089 if (!(item->window.flags & WINDOW_VISIBLE)) {
4090 return;
4091 }
4092
4093 // paint the rect first..
4094 Window_Paint(&item->window, parent->fadeAmount , parent->fadeClamp, parent->fadeCycle);
4095
4096 if (debugMode) {
4097 vec4_t color;
4098 rectDef_t *r = Item_CorrectedTextRect(item);
4099 color[1] = color[3] = 1;
4100 color[0] = color[2] = 0;
4101 DC->drawRect(r->x, r->y, r->w, r->h, 1, color);
4102 }
4103
4104 //DC->drawRect(item->window.rect.x, item->window.rect.y, item->window.rect.w, item->window.rect.h, 1, red);
4105
4106 switch (item->type) {
4107 case ITEM_TYPE_OWNERDRAW:
4108 Item_OwnerDraw_Paint(item);
4109 break;
4110 case ITEM_TYPE_TEXT:
4111 case ITEM_TYPE_BUTTON:
4112 Item_Text_Paint(item);
4113 break;
4114 case ITEM_TYPE_RADIOBUTTON:
4115 break;
4116 case ITEM_TYPE_CHECKBOX:
4117 break;
4118 case ITEM_TYPE_EDITFIELD:
4119 case ITEM_TYPE_NUMERICFIELD:
4120 Item_TextField_Paint(item);
4121 break;
4122 case ITEM_TYPE_COMBO:
4123 break;
4124 case ITEM_TYPE_LISTBOX:
4125 Item_ListBox_Paint(item);
4126 break;
4127 //case ITEM_TYPE_IMAGE:
4128 // Item_Image_Paint(item);
4129 // break;
4130 case ITEM_TYPE_MODEL:
4131 Item_Model_Paint(item);
4132 break;
4133 case ITEM_TYPE_YESNO:
4134 Item_YesNo_Paint(item);
4135 break;
4136 case ITEM_TYPE_MULTI:
4137 Item_Multi_Paint(item);
4138 break;
4139 case ITEM_TYPE_BIND:
4140 Item_Bind_Paint(item);
4141 break;
4142 case ITEM_TYPE_SLIDER:
4143 Item_Slider_Paint(item);
4144 break;
4145 default:
4146 break;
4147 }
4148
4149 }
4150
4151 void Menu_Init(menuDef_t *menu) {
4152 memset(menu, 0, sizeof(menuDef_t));
4153 menu->cursorItem = -1;
4154 menu->fadeAmount = DC->Assets.fadeAmount;
4155 menu->fadeClamp = DC->Assets.fadeClamp;
4156 menu->fadeCycle = DC->Assets.fadeCycle;
4157 Window_Init(&menu->window);
4158 }
4159
4160 itemDef_t *Menu_GetFocusedItem(menuDef_t *menu) {
4161 int i;
4162 if (menu) {
4163 for (i = 0; i < menu->itemCount; i++) {
4164 if (menu->items[i]->window.flags & WINDOW_HASFOCUS) {
4165 return menu->items[i];
4166 }
4167 }
4168 }
4169 return NULL;
4170 }
4171
4172 menuDef_t *Menu_GetFocused(void) {
4173 int i;
4174 for (i = 0; i < menuCount; i++) {
4175 if (Menus[i].window.flags & WINDOW_HASFOCUS && Menus[i].window.flags & WINDOW_VISIBLE) {
4176 return &Menus[i];
4177 }
4178 }
4179 return NULL;
4180 }
4181
4182 void Menu_ScrollFeeder(menuDef_t *menu, int feeder, qboolean down) {
4183 if (menu) {
4184 int i;
4185 for (i = 0; i < menu->itemCount; i++) {
4186 if (menu->items[i]->special == feeder) {
4187 Item_ListBox_HandleKey(menu->items[i], (down) ? K_DOWNARROW : K_UPARROW, qtrue, qtrue);
4188 return;
4189 }
4190 }
4191 }
4192 }
4193
4194
4195
4196 void Menu_SetFeederSelection(menuDef_t *menu, int feeder, int index, const char *name) {
4197 if (menu == NULL) {
4198 if (name == NULL) {
4199 menu = Menu_GetFocused();
4200 } else {
4201 menu = Menus_FindByName(name);
4202 }
4203 }
4204
4205 if (menu) {
4206 int i;
4207 for (i = 0; i < menu->itemCount; i++) {
4208 if (menu->items[i]->special == feeder) {
4209 if (index == 0) {
4210 listBoxDef_t *listPtr = (listBoxDef_t*)menu->items[i]->typeData;
4211 listPtr->cursorPos = 0;
4212 listPtr->startPos = 0;
4213 }
4214 menu->items[i]->cursorPos = index;
4215 DC->feederSelection(menu->items[i]->special, menu->items[i]->cursorPos);
4216 return;
4217 }
4218 }
4219 }
4220 }
4221
4222 qboolean Menus_AnyFullScreenVisible(void) {
4223 int i;
4224 for (i = 0; i < menuCount; i++) {
4225 if (Menus[i].window.flags & WINDOW_VISIBLE && Menus[i].fullScreen) {
4226 return qtrue;
4227 }
4228 }
4229 return qfalse;
4230 }
4231
4232 menuDef_t *Menus_ActivateByName(const char *p) {
4233 int i;
4234 menuDef_t *m = NULL;
4235 menuDef_t *focus = Menu_GetFocused();
4236 for (i = 0; i < menuCount; i++) {
4237 if (Q_stricmp(Menus[i].window.name, p) == 0) {
4238 m = &Menus[i];
4239 Menus_Activate(m);
4240 if (openMenuCount < MAX_OPEN_MENUS && focus != NULL) {
4241 menuStack[openMenuCount++] = focus;
4242 }
4243 } else {
4244 Menus[i].window.flags &= ~WINDOW_HASFOCUS;
4245 }
4246 }
4247 Display_CloseCinematics();
4248 return m;
4249 }
4250
4251
4252 void Item_Init(itemDef_t *item) {
4253 if (item == NULL) {
4254 return;
4255 }
4256
4257 memset(item, 0, sizeof(itemDef_t));
4258 item->textscale = 0.55f;
4259 Window_Init(&item->window);
4260 }
4261
4262 void Menu_HandleMouseMove(menuDef_t *menu, float x, float y) {
4263 int i, pass;
4264 qboolean focusSet = qfalse;
4265
4266 itemDef_t *overItem;
4267 if (menu == NULL) {
4268 return;
4269 }
4270
4271 if (!(menu->window.flags & (WINDOW_VISIBLE | WINDOW_FORCED))) {
4272 return;
4273 }
4274
4275 if (itemCapture) {
4276 //Item_MouseMove(itemCapture, x, y);
4277 return;
4278 }
4279
4280 if (g_waitingForKey || g_editingField) {
4281 return;
4282 }
4283
4284 // FIXME: this is the whole issue of focus vs. mouse over..
4285 // need a better overall solution as i don't like going through everything twice
4286 for (pass = 0; pass < 2; pass++) {
4287 for (i = 0; i < menu->itemCount; i++) {
4288 // turn off focus each item
4289 // menu->items[i].window.flags &= ~WINDOW_HASFOCUS;
4290
4291 if (!(menu->items[i]->window.flags & (WINDOW_VISIBLE | WINDOW_FORCED))) {
4292 continue;
4293 }
4294
4295 // items can be enabled and disabled based on cvars
4296 if (menu->items[i]->cvarFlags & (CVAR_ENABLE | CVAR_DISABLE) && !Item_EnableShowViaCvar(menu->items[i], CVAR_ENABLE)) {
4297 continue;
4298 }
4299
4300 if (menu->items[i]->cvarFlags & (CVAR_SHOW | CVAR_HIDE) && !Item_EnableShowViaCvar(menu->items[i], CVAR_SHOW)) {
4301 continue;
4302 }
4303
4304
4305
4306 if (Rect_ContainsPoint(&menu->items[i]->window.rect, x, y)) {
4307 if (pass == 1) {
4308 overItem = menu->items[i];
4309 if (overItem->type == ITEM_TYPE_TEXT && overItem->text) {
4310 if (!Rect_ContainsPoint(Item_CorrectedTextRect(overItem), x, y)) {
4311 continue;
4312 }
4313 }
4314 // if we are over an item
4315 if (IsVisible(overItem->window.flags)) {
4316 // different one
4317 Item_MouseEnter(overItem, x, y);
4318 // Item_SetMouseOver(overItem, qtrue);
4319
4320 // if item is not a decoration see if it can take focus
4321 if (!focusSet) {
4322 focusSet = Item_SetFocus(overItem, x, y);
4323 }
4324 }
4325 }
4326 } else if (menu->items[i] && menu->items[i]->window.flags & WINDOW_MOUSEOVER) {
4327 Item_MouseLeave(menu->items[i]);
4328 Item_SetMouseOver(menu->items[i], qfalse);
4329 }
4330 }
4331 }
4332
4333 }
4334
4335 void Menu_Paint(menuDef_t *menu, qboolean forcePaint) {
4336 int i;
4337
4338 if (menu == NULL) {
4339 return;
4340 }
4341
4342 if (!(menu->window.flags & WINDOW_VISIBLE) && !forcePaint) {
4343 return;
4344 }
4345
4346 if (menu->window.ownerDrawFlags && DC->ownerDrawVisible && !DC->ownerDrawVisible(menu->window.ownerDrawFlags)) {
4347 return;
4348 }
4349
4350 if (forcePaint) {
4351 menu->window.flags |= WINDOW_FORCED;
4352 }
4353
4354 // draw the background if necessary
4355 if (menu->fullScreen) {
4356 // implies a background shader
4357 // FIXME: make sure we have a default shader if fullscreen is set with no background
4358 DC->drawHandlePic( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, menu->window.background );
4359 } else if (menu->window.background) {
4360 // this allows a background shader without being full screen
4361 //UI_DrawHandlePic(menu->window.rect.x, menu->window.rect.y, menu->window.rect.w, menu->window.rect.h, menu->backgroundShader);
4362 }
4363
4364 // paint the background and or border
4365 Window_Paint(&menu->window, menu->fadeAmount, menu->fadeClamp, menu->fadeCycle );
4366
4367 for (i = 0; i < menu->itemCount; i++) {
4368 Item_Paint(menu->items[i]);
4369 }
4370
4371 if (debugMode) {
4372 vec4_t color;
4373 color[0] = color[2] = color[3] = 1;
4374 color[1] = 0;
4375 DC->drawRect(menu->window.rect.x, menu->window.rect.y, menu->window.rect.w, menu->window.rect.h, 1, color);
4376 }
4377 }
4378
4379 /*
4380 ===============
4381 Item_ValidateTypeData
4382 ===============
4383 */
4384 void Item_ValidateTypeData(itemDef_t *item) {
4385 if (item->typeData) {
4386 return;
4387 }
4388
4389 if (item->type == ITEM_TYPE_LISTBOX) {
4390 item->typeData = UI_Alloc(sizeof(listBoxDef_t));
4391 memset(item->typeData, 0, sizeof(listBoxDef_t));
4392 } else if (item->type == ITEM_TYPE_EDITFIELD || item->type == ITEM_TYPE_NUMERICFIELD || item->type == ITEM_TYPE_YESNO || item->type == ITEM_TYPE_BIND || item->type == ITEM_TYPE_SLIDER || item->type == ITEM_TYPE_TEXT) {
4393 item->typeData = UI_Alloc(sizeof(editFieldDef_t));
4394 memset(item->typeData, 0, sizeof(editFieldDef_t));
4395 if (item->type == ITEM_TYPE_EDITFIELD) {
4396 if (!((editFieldDef_t *) item->typeData)->maxPaintChars) {
4397 ((editFieldDef_t *) item->typeData)->maxPaintChars = MAX_EDITFIELD;
4398 }
4399 }
4400 } else if (item->type == ITEM_TYPE_MULTI) {
4401 item->typeData = UI_Alloc(sizeof(multiDef_t));
4402 } else if (item->type == ITEM_TYPE_MODEL) {
4403 item->typeData = UI_Alloc(sizeof(modelDef_t));
4404 }
4405 }
4406
4407 /*
4408 ===============
4409 Keyword Hash
4410 ===============
4411 */
4412
4413 #define KEYWORDHASH_SIZE 512
4414
4415 typedef struct keywordHash_s
4416 {
4417 char *keyword;
4418 qboolean (*func)(itemDef_t *item, int handle);
4419 struct keywordHash_s *next;
4420 } keywordHash_t;
4421
4422 int KeywordHash_Key(char *keyword) {
4423 int hash, i;
4424
4425 hash = 0;
4426 for (i = 0; keyword[i] != '\0'; i++) {
4427 if (keyword[i] >= 'A' && keyword[i] <= 'Z')
4428 hash += (keyword[i] + ('a' - 'A')) * (119 + i);
4429 else
4430 hash += keyword[i] * (119 + i);
4431 }
4432 hash = (hash ^ (hash >> 10) ^ (hash >> 20)) & (KEYWORDHASH_SIZE-1);
4433 return hash;
4434 }
4435
4436 void KeywordHash_Add(keywordHash_t *table[], keywordHash_t *key) {
4437 int hash;
4438
4439 hash = KeywordHash_Key(key->keyword);
4440 /*
4441 if (table[hash]) {
4442 int collision = qtrue;
4443 }
4444 */
4445 key->next = table[hash];
4446 table[hash] = key;
4447 }
4448
4449 keywordHash_t *KeywordHash_Find(keywordHash_t *table[], char *keyword)
4450 {
4451 keywordHash_t *key;
4452 int hash;
4453
4454 hash = KeywordHash_Key(keyword);
4455 for (key = table[hash]; key; key = key->next) {
4456 if (!Q_stricmp(key->keyword, keyword))
4457 return key;
4458 }
4459 return NULL;
4460 }
4461
4462 /*
4463 ===============
4464 Item Keyword Parse functions
4465 ===============
4466 */
4467
4468 // name <string>
4469 qboolean ItemParse_name( itemDef_t *item, int handle ) {
4470 if (!PC_String_Parse(handle, &item->window.name)) {
4471 return qfalse;
4472 }
4473 return qtrue;
4474 }
4475
4476 // name <string>
4477 qboolean ItemParse_focusSound( itemDef_t *item, int handle ) {
4478 const char *temp;
4479 if (!PC_String_Parse(handle, &temp)) {
4480 return qfalse;
4481 }
4482 item->focusSound = DC->registerSound(temp, qfalse);
4483 return qtrue;
4484 }
4485
4486
4487 // text <string>
4488 qboolean ItemParse_text( itemDef_t *item, int handle ) {
4489 if (!PC_String_Parse(handle, &item->text)) {
4490 return qfalse;
4491 }
4492 return qtrue;
4493 }
4494
4495 // group <string>
4496 qboolean ItemParse_group( itemDef_t *item, int handle ) {
4497 if (!PC_String_Parse(handle, &item->window.group)) {
4498 return qfalse;
4499 }
4500 return qtrue;
4501 }
4502
4503 // asset_model <string>
4504 qboolean ItemParse_asset_model( itemDef_t *item, int handle ) {
4505 const char *temp;
4506 modelDef_t *modelPtr;
4507 Item_ValidateTypeData(item);
4508 modelPtr = (modelDef_t*)item->typeData;
4509
4510 if (!PC_String_Parse(handle, &temp)) {
4511 return qfalse;
4512 }
4513 item->asset = DC->registerModel(temp);
4514 modelPtr->angle = rand() % 360;
4515 return qtrue;
4516 }
4517
4518 // asset_shader <string>
4519 qboolean ItemParse_asset_shader( itemDef_t *item, int handle ) {
4520 const char *temp;
4521
4522 if (!PC_String_Parse(handle, &temp)) {
4523 return qfalse;
4524 }
4525 item->asset = DC->registerShaderNoMip(temp);
4526 return qtrue;
4527 }
4528
4529 // model_origin <number> <number> <number>
4530 qboolean ItemParse_model_origin( itemDef_t *item, int handle ) {
4531 modelDef_t *modelPtr;
4532 Item_ValidateTypeData(item);
4533 modelPtr = (modelDef_t*)item->typeData;
4534
4535 if (PC_Float_Parse(handle, &modelPtr->origin[0])) {
4536 if (PC_Float_Parse(handle, &modelPtr->origin[1])) {
4537 if (PC_Float_Parse(handle, &modelPtr->origin[2])) {
4538 return qtrue;
4539 }
4540 }
4541 }
4542 return qfalse;
4543 }
4544
4545 // model_fovx <number>
4546 qboolean ItemParse_model_fovx( itemDef_t *item, int handle ) {
4547 modelDef_t *modelPtr;
4548 Item_ValidateTypeData(item);
4549 modelPtr = (modelDef_t*)item->typeData;
4550
4551 if (!PC_Float_Parse(handle, &modelPtr->fov_x)) {
4552 return qfalse;
4553 }
4554 return qtrue;
4555 }
4556
4557 // model_fovy <number>
4558 qboolean ItemParse_model_fovy( itemDef_t *item, int handle ) {
4559 modelDef_t *modelPtr;
4560 Item_ValidateTypeData(item);
4561 modelPtr = (modelDef_t*)item->typeData;
4562
4563 if (!PC_Float_Parse(handle, &modelPtr->fov_y)) {
4564 return qfalse;
4565 }
4566 return qtrue;
4567 }
4568
4569 // model_rotation <integer>
4570 qboolean ItemParse_model_rotation( itemDef_t *item, int handle ) {
4571 modelDef_t *modelPtr;
4572 Item_ValidateTypeData(item);
4573 modelPtr = (modelDef_t*)item->typeData;
4574
4575 if (!PC_Int_Parse(handle, &modelPtr->rotationSpeed)) {
4576 return qfalse;
4577 }
4578 return qtrue;
4579 }
4580
4581 // model_angle <integer>
4582 qboolean ItemParse_model_angle( itemDef_t *item, int handle ) {
4583 modelDef_t *modelPtr;
4584 Item_ValidateTypeData(item);
4585 modelPtr = (modelDef_t*)item->typeData;
4586
4587 if (!PC_Int_Parse(handle, &modelPtr->angle)) {
4588 return qfalse;
4589 }
4590 return qtrue;
4591 }
4592
4593 // rect <rectangle>
4594 qboolean ItemParse_rect( itemDef_t *item, int handle ) {
4595 if (!PC_Rect_Parse(handle, &item->window.rectClient)) {
4596 return qfalse;
4597 }
4598 return qtrue;
4599 }
4600
4601 // style <integer>
4602 qboolean ItemParse_style( itemDef_t *item, int handle ) {
4603 if (!PC_Int_Parse(handle, &item->window.style)) {
4604 return qfalse;
4605 }
4606 return qtrue;
4607 }
4608
4609 // decoration
4610 qboolean ItemParse_decoration( itemDef_t *item, int handle ) {
4611 item->window.flags |= WINDOW_DECORATION;
4612 return qtrue;
4613 }
4614
4615 // notselectable
4616 qboolean ItemParse_notselectable( itemDef_t *item, int handle ) {
4617 listBoxDef_t *listPtr;
4618 Item_ValidateTypeData(item);
4619 listPtr = (listBoxDef_t*)item->typeData;
4620 if (item->type == ITEM_TYPE_LISTBOX && listPtr) {
4621 listPtr->notselectable = qtrue;
4622 }
4623 return qtrue;
4624 }
4625
4626 // manually wrapped
4627 qboolean ItemParse_wrapped( itemDef_t *item, int handle ) {
4628 item->window.flags |= WINDOW_WRAPPED;
4629 return qtrue;
4630 }
4631
4632 // auto wrapped
4633 qboolean ItemParse_autowrapped( itemDef_t *item, int handle ) {
4634 item->window.flags |= WINDOW_AUTOWRAPPED;
4635 return qtrue;
4636 }
4637
4638
4639 // horizontalscroll
4640 qboolean ItemParse_horizontalscroll( itemDef_t *item, int handle ) {
4641 item->window.flags |= WINDOW_HORIZONTAL;
4642 return qtrue;
4643 }
4644
4645 // type <integer>
4646 qboolean ItemParse_type( itemDef_t *item, int handle ) {
4647 if (!PC_Int_Parse(handle, &item->type)) {
4648 return qfalse;
4649 }
4650 Item_ValidateTypeData(item);
4651 return qtrue;
4652 }
4653
4654 // elementwidth, used for listbox image elements
4655 // uses textalignx for storage
4656 qboolean ItemParse_elementwidth( itemDef_t *item, int handle ) {
4657 listBoxDef_t *listPtr;
4658
4659 Item_ValidateTypeData(item);
4660 listPtr = (listBoxDef_t*)item->typeData;
4661 if (!PC_Float_Parse(handle, &listPtr->elementWidth)) {
4662 return qfalse;
4663 }
4664 return qtrue;
4665 }
4666
4667 // elementheight, used for listbox image elements
4668 // uses textaligny for storage
4669 qboolean ItemParse_elementheight( itemDef_t *item, int handle ) {
4670 listBoxDef_t *listPtr;
4671
4672 Item_ValidateTypeData(item);
4673 listPtr = (listBoxDef_t*)item->typeData;
4674 if (!PC_Float_Parse(handle, &listPtr->elementHeight)) {
4675 return qfalse;
4676 }
4677 return qtrue;
4678 }
4679
4680 // feeder <float>
4681 qboolean ItemParse_feeder( itemDef_t *item, int handle ) {
4682 if (!PC_Float_Parse(handle, &item->special)) {
4683 return qfalse;
4684 }
4685 return qtrue;
4686 }
4687
4688 // elementtype, used to specify what type of elements a listbox contains
4689 // uses textstyle for storage
4690 qboolean ItemParse_elementtype( itemDef_t *item, int handle ) {
4691 listBoxDef_t *listPtr;
4692
4693 Item_ValidateTypeData(item);
4694 if (!item->typeData)
4695 return qfalse;
4696 listPtr = (listBoxDef_t*)item->typeData;
4697 if (!PC_Int_Parse(handle, &listPtr->elementStyle)) {
4698 return qfalse;
4699 }
4700 return qtrue;
4701 }
4702
4703 // columns sets a number of columns and an x pos and width per..
4704 qboolean ItemParse_columns( itemDef_t *item, int handle ) {
4705 int num, i;
4706 listBoxDef_t *listPtr;
4707
4708 Item_ValidateTypeData(item);
4709 if (!item->typeData)
4710 return qfalse;
4711 listPtr = (listBoxDef_t*)item->typeData;
4712 if (PC_Int_Parse(handle, &num)) {
4713 if (num > MAX_LB_COLUMNS) {
4714 num = MAX_LB_COLUMNS;
4715 }
4716 listPtr->numColumns = num;
4717 for (i = 0; i < num; i++) {
4718 int pos, width, maxChars;
4719
4720 if (PC_Int_Parse(handle, &pos) && PC_Int_Parse(handle, &width) && PC_Int_Parse(handle, &maxChars)) {
4721 listPtr->columnInfo[i].pos = pos;
4722 listPtr->columnInfo[i].width = width;
4723 listPtr->columnInfo[i].maxChars = maxChars;
4724 } else {
4725 return qfalse;
4726 }
4727 }
4728 } else {
4729 return qfalse;
4730 }
4731 return qtrue;
4732 }
4733
4734 qboolean ItemParse_border( itemDef_t *item, int handle ) {
4735 if (!PC_Int_Parse(handle, &item->window.border)) {
4736 return qfalse;
4737 }
4738 return qtrue;
4739 }
4740
4741 qboolean ItemParse_bordersize( itemDef_t *item, int handle ) {
4742 if (!PC_Float_Parse(handle, &item->window.borderSize)) {
4743 return qfalse;
4744 }
4745 return qtrue;
4746 }
4747
4748 qboolean ItemParse_visible( itemDef_t *item, int handle ) {
4749 int i;
4750
4751 if (!PC_Int_Parse(handle, &i)) {
4752 return qfalse;
4753 }
4754 if (i) {
4755 item->window.flags |= WINDOW_VISIBLE;
4756 }
4757 return qtrue;
4758 }
4759
4760 qboolean ItemParse_ownerdraw( itemDef_t *item, int handle ) {
4761 if (!PC_Int_Parse(handle, &item->window.ownerDraw)) {
4762 return qfalse;
4763 }
4764 item->type = ITEM_TYPE_OWNERDRAW;
4765 return qtrue;
4766 }
4767
4768 qboolean ItemParse_align( itemDef_t *item, int handle ) {
4769 if (!PC_Int_Parse(handle, &item->alignment)) {
4770 return qfalse;
4771 }
4772 return qtrue;
4773 }
4774
4775 qboolean ItemParse_textalign( itemDef_t *item, int handle ) {
4776 if (!PC_Int_Parse(handle, &item->textalignment)) {
4777 return qfalse;
4778 }
4779 return qtrue;
4780 }
4781
4782 qboolean ItemParse_textalignx( itemDef_t *item, int handle ) {
4783 if (!PC_Float_Parse(handle, &item->textalignx)) {
4784 return qfalse;
4785 }
4786 return qtrue;
4787 }
4788
4789 qboolean ItemParse_textaligny( itemDef_t *item, int handle ) {
4790 if (!PC_Float_Parse(handle, &item->textaligny)) {
4791 return qfalse;
4792 }
4793 return qtrue;
4794 }
4795
4796 qboolean ItemParse_textscale( itemDef_t *item, int handle ) {
4797 if (!PC_Float_Parse(handle, &item->textscale)) {
4798 return qfalse;
4799 }
4800 return qtrue;
4801 }
4802
4803 qboolean ItemParse_textstyle( itemDef_t *item, int handle ) {
4804 if (!PC_Int_Parse(handle, &item->textStyle)) {
4805 return qfalse;
4806 }
4807 return qtrue;
4808 }
4809
4810 qboolean ItemParse_backcolor( itemDef_t *item, int handle ) {
4811 int i;
4812 float f;
4813
4814 for (i = 0; i < 4; i++) {
4815 if (!PC_Float_Parse(handle, &f)) {
4816 return qfalse;
4817 }
4818 item->window.backColor[i] = f;
4819 }
4820 return qtrue;
4821 }
4822
4823 qboolean ItemParse_forecolor( itemDef_t *item, int handle ) {
4824 int i;
4825 float f;
4826
4827 for (i = 0; i < 4; i++) {
4828 if (!PC_Float_Parse(handle, &f)) {
4829 return qfalse;
4830 }
4831 item->window.foreColor[i] = f;
4832 item->window.flags |= WINDOW_FORECOLORSET;
4833 }
4834 return qtrue;
4835 }
4836
4837 qboolean ItemParse_bordercolor( itemDef_t *item, int handle ) {
4838 int i;
4839 float f;
4840
4841 for (i = 0; i < 4; i++) {
4842 if (!PC_Float_Parse(handle, &f)) {
4843 return qfalse;
4844 }
4845 item->window.borderColor[i] = f;
4846 }
4847 return qtrue;
4848 }
4849
4850 qboolean ItemParse_outlinecolor( itemDef_t *item, int handle ) {
4851 if (!PC_Color_Parse(handle, &item->window.outlineColor)){
4852 return qfalse;
4853 }
4854 return qtrue;
4855 }
4856
4857 qboolean ItemParse_background( itemDef_t *item, int handle ) {
4858 const char *temp;
4859
4860 if (!PC_String_Parse(handle, &temp)) {
4861 return qfalse;
4862 }
4863 item->window.background = DC->registerShaderNoMip(temp);
4864 return qtrue;
4865 }
4866
4867 qboolean ItemParse_cinematic( itemDef_t *item, int handle ) {
4868 if (!PC_String_Parse(handle, &item->window.cinematicName)) {
4869 return qfalse;
4870 }
4871 return qtrue;
4872 }
4873
4874 qboolean ItemParse_doubleClick( itemDef_t *item, int handle ) {
4875 listBoxDef_t *listPtr;
4876
4877 Item_ValidateTypeData(item);
4878 if (!item->typeData) {
4879 return qfalse;
4880 }
4881
4882 listPtr = (listBoxDef_t*)item->typeData;
4883
4884 if (!PC_Script_Parse(handle, &listPtr->doubleClick)) {
4885 return qfalse;
4886 }
4887 return qtrue;
4888 }
4889
4890 qboolean ItemParse_onFocus( itemDef_t *item, int handle ) {
4891 if (!PC_Script_Parse(handle, &item->onFocus)) {
4892 return qfalse;
4893 }
4894 return qtrue;
4895 }
4896
4897 qboolean ItemParse_leaveFocus( itemDef_t *item, int handle ) {
4898 if (!PC_Script_Parse(handle, &item->leaveFocus)) {
4899 return qfalse;
4900 }
4901 return qtrue;
4902 }
4903
4904 qboolean ItemParse_mouseEnter( itemDef_t *item, int handle ) {
4905 if (!PC_Script_Parse(handle, &item->mouseEnter)) {
4906 return qfalse;
4907 }
4908 return qtrue;
4909 }
4910
4911 qboolean ItemParse_mouseExit( itemDef_t *item, int handle ) {
4912 if (!PC_Script_Parse(handle, &item->mouseExit)) {
4913 return qfalse;
4914 }
4915 return qtrue;
4916 }
4917
4918 qboolean ItemParse_mouseEnterText( itemDef_t *item, int handle ) {
4919 if (!PC_Script_Parse(handle, &item->mouseEnterText)) {
4920 return qfalse;
4921 }
4922 return qtrue;
4923 }
4924
4925 qboolean ItemParse_mouseExitText( itemDef_t *item, int handle ) {
4926 if (!PC_Script_Parse(handle, &item->mouseExitText)) {
4927 return qfalse;
4928 }
4929 return qtrue;
4930 }
4931
4932 qboolean ItemParse_action( itemDef_t *item, int handle ) {
4933 if (!PC_Script_Parse(handle, &item->action)) {
4934 return qfalse;
4935 }
4936 return qtrue;
4937 }
4938
4939 qboolean ItemParse_special( itemDef_t *item, int handle ) {
4940 if (!PC_Float_Parse(handle, &item->special)) {
4941 return qfalse;
4942 }
4943 return qtrue;
4944 }
4945
4946 qboolean ItemParse_cvarTest( itemDef_t *item, int handle ) {
4947 if (!PC_String_Parse(handle, &item->cvarTest)) {
4948 return qfalse;
4949 }
4950 return qtrue;
4951 }
4952
4953 qboolean ItemParse_cvar( itemDef_t *item, int handle ) {
4954 editFieldDef_t *editPtr;
4955
4956 Item_ValidateTypeData(item);
4957 if (!PC_String_Parse(handle, &item->cvar)) {
4958 return qfalse;
4959 }
4960 if (item->typeData) {
4961 editPtr = (editFieldDef_t*)item->typeData;
4962 editPtr->minVal = -1;
4963 editPtr->maxVal = -1;
4964 editPtr->defVal = -1;
4965 }
4966 return qtrue;
4967 }
4968
4969 qboolean ItemParse_maxChars( itemDef_t *item, int handle ) {
4970 editFieldDef_t *editPtr;
4971 int maxChars;
4972
4973 Item_ValidateTypeData(item);
4974 if (!item->typeData)
4975 return qfalse;
4976
4977 if (!PC_Int_Parse(handle, &maxChars)) {
4978 return qfalse;
4979 }
4980 editPtr = (editFieldDef_t*)item->typeData;
4981 editPtr->maxChars = maxChars;
4982 return qtrue;
4983 }
4984
4985 qboolean ItemParse_maxPaintChars( itemDef_t *item, int handle ) {
4986 editFieldDef_t *editPtr;
4987 int maxChars;
4988
4989 Item_ValidateTypeData(item);
4990 if (!item->typeData)
4991 return qfalse;
4992
4993 if (!PC_Int_Parse(handle, &maxChars)) {
4994 return qfalse;
4995 }
4996 editPtr = (editFieldDef_t*)item->typeData;
4997 editPtr->maxPaintChars = maxChars;
4998 return qtrue;
4999 }
5000
5001
5002
5003 qboolean ItemParse_cvarFloat( itemDef_t *item, int handle ) {
5004 editFieldDef_t *editPtr;
5005
5006 Item_ValidateTypeData(item);
5007 if (!item->typeData)
5008 return qfalse;
5009 editPtr = (editFieldDef_t*)item->typeData;
5010 if (PC_String_Parse(handle, &item->cvar) &&
5011 PC_Float_Parse(handle, &editPtr->defVal) &&
5012 PC_Float_Parse(handle, &editPtr->minVal) &&
5013 PC_Float_Parse(handle, &editPtr->maxVal)) {
5014 return qtrue;
5015 }
5016 return qfalse;
5017 }
5018
5019 qboolean ItemParse_cvarStrList( itemDef_t *item, int handle ) {
5020 pc_token_t token;
5021 multiDef_t *multiPtr;
5022 int pass;
5023
5024 Item_ValidateTypeData(item);
5025 if (!item->typeData)
5026 return qfalse;
5027 multiPtr = (multiDef_t*)item->typeData;
5028 multiPtr->count = 0;
5029 multiPtr->strDef = qtrue;
5030 multiPtr->videoMode = qfalse;
5031
5032 if (!trap_PC_ReadToken(handle, &token))
5033 return qfalse;
5034 if (*token.string != '{') {
5035 return qfalse;
5036 }
5037
5038 pass = 0;
5039 while ( 1 ) {
5040 if (!trap_PC_ReadToken(handle, &token)) {
5041 PC_SourceError(handle, "end of file inside menu item");
5042 return qfalse;
5043 }
5044
5045 if (*token.string == '}') {
5046 return qtrue;
5047 }
5048
5049 if (*token.string == ',' || *token.string == ';') {
5050 continue;
5051 }
5052
5053 if (pass == 0) {
5054 multiPtr->cvarList[multiPtr->count] = String_Alloc(token.string);
5055 pass = 1;
5056 } else {
5057 multiPtr->cvarStr[multiPtr->count] = String_Alloc(token.string);
5058 pass = 0;
5059 multiPtr->count++;
5060 if (multiPtr->count >= MAX_MULTI_CVARS) {
5061 return qfalse;
5062 }
5063 }
5064
5065 }
5066 return qfalse;
5067 }
5068
5069 qboolean ItemParse_cvarFloatList( itemDef_t *item, int handle ) {
5070 pc_token_t token;
5071 multiDef_t *multiPtr;
5072
5073 Item_ValidateTypeData(item);
5074 if (!item->typeData)
5075 return qfalse;
5076 multiPtr = (multiDef_t*)item->typeData;
5077 multiPtr->count = 0;
5078 multiPtr->strDef = qfalse;
5079 multiPtr->videoMode = qfalse;
5080
5081 if (!trap_PC_ReadToken(handle, &token))
5082 return qfalse;
5083 if (*token.string != '{') {
5084 return qfalse;
5085 }
5086
5087 while ( 1 ) {
5088 if (!trap_PC_ReadToken(handle, &token)) {
5089 PC_SourceError(handle, "end of file inside menu item");
5090 return qfalse;
5091 }
5092
5093 if (*token.string == '}') {
5094 return qtrue;
5095 }
5096
5097 if (*token.string == ',' || *token.string == ';') {
5098 continue;
5099 }
5100
5101 multiPtr->cvarList[multiPtr->count] = String_Alloc(token.string);
5102 if (!PC_Float_Parse(handle, &multiPtr->cvarValue[multiPtr->count])) {
5103 return qfalse;
5104 }
5105
5106 multiPtr->count++;
5107 if (multiPtr->count >= MAX_MULTI_CVARS) {
5108 return qfalse;
5109 }
5110
5111 }
5112 return qfalse;
5113 }
5114
5115
5116
5117 qboolean ItemParse_addColorRange( itemDef_t *item, int handle ) {
5118 colorRangeDef_t color;
5119
5120 if (PC_Float_Parse(handle, &color.low) &&
5121 PC_Float_Parse(handle, &color.high) &&
5122 PC_Color_Parse(handle, &color.color) ) {
5123 if (item->numColors < MAX_COLOR_RANGES) {
5124 memcpy(&item->colorRanges[item->numColors], &color, sizeof(color));
5125 item->numColors++;
5126 }
5127 return qtrue;
5128 }
5129 return qfalse;
5130 }
5131
5132 qboolean ItemParse_ownerdrawFlag( itemDef_t *item, int handle ) {
5133 int i;
5134 if (!PC_Int_Parse(handle, &i)) {
5135 return qfalse;
5136 }
5137 item->window.ownerDrawFlags |= i;
5138 return qtrue;
5139 }
5140
5141 qboolean ItemParse_enableCvar( itemDef_t *item, int handle ) {
5142 if (PC_Script_Parse(handle, &item->enableCvar)) {
5143 item->cvarFlags = CVAR_ENABLE;
5144 return qtrue;
5145 }
5146 return qfalse;
5147 }
5148
5149 qboolean ItemParse_disableCvar( itemDef_t *item, int handle ) {
5150 if (PC_Script_Parse(handle, &item->enableCvar)) {
5151 item->cvarFlags = CVAR_DISABLE;
5152 return qtrue;
5153 }
5154 return qfalse;
5155 }
5156
5157 qboolean ItemParse_showCvar( itemDef_t *item, int handle ) {
5158 if (PC_Script_Parse(handle, &item->enableCvar)) {
5159 item->cvarFlags = CVAR_SHOW;
5160 return qtrue;
5161 }
5162 return qfalse;
5163 }
5164
5165 qboolean ItemParse_hideCvar( itemDef_t *item, int handle ) {
5166 if (PC_Script_Parse(handle, &item->enableCvar)) {
5167 item->cvarFlags = CVAR_HIDE;
5168 return qtrue;
5169 }
5170 return qfalse;
5171 }
5172
5173
5174 keywordHash_t itemParseKeywords[] = {
5175 {"name", ItemParse_name, NULL},
5176 {"text", ItemParse_text, NULL},
5177 {"group", ItemParse_group, NULL},
5178 {"asset_model", ItemParse_asset_model, NULL},
5179 {"asset_shader", ItemParse_asset_shader, NULL},
5180 {"model_origin", ItemParse_model_origin, NULL},
5181 {"model_fovx", ItemParse_model_fovx, NULL},
5182 {"model_fovy", ItemParse_model_fovy, NULL},
5183 {"model_rotation", ItemParse_model_rotation, NULL},
5184 {"model_angle", ItemParse_model_angle, NULL},
5185 {"rect", ItemParse_rect, NULL},
5186 {"style", ItemParse_style, NULL},
5187 {"decoration", ItemParse_decoration, NULL},
5188 {"notselectable", ItemParse_notselectable, NULL},
5189 {"wrapped", ItemParse_wrapped, NULL},
5190 {"autowrapped", ItemParse_autowrapped, NULL},
5191 {"horizontalscroll", ItemParse_horizontalscroll, NULL},
5192 {"type", ItemParse_type, NULL},
5193 {"elementwidth", ItemParse_elementwidth, NULL},
5194 {"elementheight", ItemParse_elementheight, NULL},
5195 {"feeder", ItemParse_feeder, NULL},
5196 {"elementtype", ItemParse_elementtype, NULL},
5197 {"columns", ItemParse_columns, NULL},
5198 {"border", ItemParse_border, NULL},
5199 {"bordersize", ItemParse_bordersize, NULL},
5200 {"visible", ItemParse_visible, NULL},
5201 {"ownerdraw", ItemParse_ownerdraw, NULL},
5202 {"align", ItemParse_align, NULL},
5203 {"textalign", ItemParse_textalign, NULL},
5204 {"textalignx", ItemParse_textalignx, NULL},
5205 {"textaligny", ItemParse_textaligny, NULL},
5206 {"textscale", ItemParse_textscale, NULL},
5207 {"textstyle", ItemParse_textstyle, NULL},
5208 {"backcolor", ItemParse_backcolor, NULL},
5209 {"forecolor", ItemParse_forecolor, NULL},
5210 {"bordercolor", ItemParse_bordercolor, NULL},
5211 {"outlinecolor", ItemParse_outlinecolor, NULL},
5212 {"background", ItemParse_background, NULL},
5213 {"onFocus", ItemParse_onFocus, NULL},
5214 {"leaveFocus", ItemParse_leaveFocus, NULL},
5215 {"mouseEnter", ItemParse_mouseEnter, NULL},
5216 {"mouseExit", ItemParse_mouseExit, NULL},
5217 {"mouseEnterText", ItemParse_mouseEnterText, NULL},
5218 {"mouseExitText", ItemParse_mouseExitText, NULL},
5219 {"action", ItemParse_action, NULL},
5220 {"special", ItemParse_special, NULL},
5221 {"cvar", ItemParse_cvar, NULL},
5222 {"maxChars", ItemParse_maxChars, NULL},
5223 {"maxPaintChars", ItemParse_maxPaintChars, NULL},
5224 {"focusSound", ItemParse_focusSound, NULL},
5225 {"cvarFloat", ItemParse_cvarFloat, NULL},
5226 {"cvarStrList", ItemParse_cvarStrList, NULL},
5227 {"cvarFloatList", ItemParse_cvarFloatList, NULL},
5228 {"addColorRange", ItemParse_addColorRange, NULL},
5229 {"ownerdrawFlag", ItemParse_ownerdrawFlag, NULL},
5230 {"enableCvar", ItemParse_enableCvar, NULL},
5231 {"cvarTest", ItemParse_cvarTest, NULL},
5232 {"disableCvar", ItemParse_disableCvar, NULL},
5233 {"showCvar", ItemParse_showCvar, NULL},
5234 {"hideCvar", ItemParse_hideCvar, NULL},
5235 {"cinematic", ItemParse_cinematic, NULL},
5236 {"doubleclick", ItemParse_doubleClick, NULL},
5237 {NULL, 0, NULL}
5238 };
5239
5240 keywordHash_t *itemParseKeywordHash[KEYWORDHASH_SIZE];
5241
5242 /*
5243 ===============
5244 Item_SetupKeywordHash
5245 ===============
5246 */
5247 void Item_SetupKeywordHash(void) {
5248 int i;
5249
5250 memset(itemParseKeywordHash, 0, sizeof(itemParseKeywordHash));
5251 for (i = 0; itemParseKeywords[i].keyword; i++) {
5252 KeywordHash_Add(itemParseKeywordHash, &itemParseKeywords[i]);
5253 }
5254 }
5255
5256 static const char *builtinResolutions[ ] =
5257 {
5258 "320x240",
5259 "400x300",
5260 "512x384",
5261 "640x480",
5262 "800x600",
5263 "960x720",
5264 "1024x768",
5265 "1152x864",
5266 "1280x1024",
5267 "1600x1200",
5268 "2048x1536",
5269 "856x480",
5270 NULL
5271 };
5272
5273 static const char *knownRatios[ ][2] =
5274 {
5275 { "1.25:1", "5:4" },
5276 { "1.33:1", "4:3" },
5277 { "1.50:1", "3:2" },
5278 { "1.56:1", "14:9" },
5279 { "1.60:1", "16:10" },
5280 { "1.67:1", "5:3" },
5281 { "1.78:1", "16:9" },
5282 { NULL , NULL }
5283 };
5284
5285 /*
5286 ===============
5287 UI_ResolutionToAspect
5288 ===============
5289 */
5290 static void UI_ResolutionToAspect( const char *resolution, char *aspect, size_t aspectLength ) {
5291 int i, w, h;
5292 char *x;
5293 char str[8];
5294
5295 // calculate resolution's aspect ratio
5296 x = strchr( resolution, 'x' ) + 1;
5297 Q_strncpyz( str, resolution, MIN( x-resolution, sizeof( str ) ) );
5298 w = atoi( str );
5299 h = atoi( x );
5300 Com_sprintf( aspect, aspectLength, "%.2f:1", (float)w / (float)h );
5301
5302 // rename common ratios ("1.33:1" -> "4:3")
5303 for( i = 0; knownRatios[i][0]; i++ ) {
5304 if( !Q_stricmp( aspect, knownRatios[i][0] ) ) {
5305 Q_strncpyz( aspect, knownRatios[i][1], aspectLength );
5306 break;
5307 }
5308 }
5309 }
5310
5311 /*
5312 ===============
5313 Item_ApplyHacks
5314
5315 Hacks to fix issues with Team Arena menu scripts
5316 ===============
5317 */
5318 static void Item_ApplyHacks( itemDef_t *item ) {
5319
5320 // Fix length of favorite address in createfavorite.menu
5321 if ( item->type == ITEM_TYPE_EDITFIELD && item->cvar && !Q_stricmp( item->cvar, "ui_favoriteAddress" ) ) {
5322 editFieldDef_t *editField = (editFieldDef_t *)item->typeData;
5323
5324 // enough to hold an IPv6 address plus null
5325 if ( editField->maxChars < 48 ) {
5326 Com_Printf( "Extended create favorite address edit field length to hold an IPv6 address\n" );
5327 editField->maxChars = 48;
5328 }
5329 }
5330
5331 if ( item->type == ITEM_TYPE_EDITFIELD && item->cvar && ( !Q_stricmp( item->cvar, "ui_Name" ) || !Q_stricmp( item->cvar, "ui_findplayer" ) ) ) {
5332 editFieldDef_t *editField = (editFieldDef_t *)item->typeData;
5333
5334 // enough to hold a full player name
5335 if ( editField->maxChars < MAX_NAME_LENGTH ) {
5336 if ( editField->maxPaintChars > editField->maxChars ) {
5337 editField->maxPaintChars = editField->maxChars;
5338 }
5339
5340 Com_Printf( "Extended player name field using cvar %s to %d characters\n", item->cvar, MAX_NAME_LENGTH );
5341 editField->maxChars = MAX_NAME_LENGTH;
5342 }
5343 }
5344
5345 // Replace mode list and use a temporary ui_videomode cvar for handling custom modes
5346 if ( item->type == ITEM_TYPE_MULTI && item->cvar && !Q_stricmp( item->cvar, "r_mode" ) ) {
5347 multiDef_t *multiPtr = (multiDef_t*)item->typeData;
5348 int i, oldCount;
5349 char resbuf[MAX_STRING_CHARS];
5350 char modeName[32], aspect[8];
5351
5352 item->cvar = "ui_videomode";
5353 multiPtr->strDef = qtrue;
5354 multiPtr->videoMode = qtrue;
5355
5356 oldCount = multiPtr->count;
5357 multiPtr->count = 0;
5358
5359 DC->getCVarString( "r_availableModes", resbuf, sizeof( resbuf ) );
5360
5361 if ( *resbuf ) {
5362 char *s = resbuf, *mode;
5363
5364 while ( s && multiPtr->count < MAX_MULTI_CVARS ) {
5365 mode = s;
5366
5367 s = strchr(s, ' ');
5368 if( s )
5369 *s++ = '\0';
5370
5371 UI_ResolutionToAspect( mode, aspect, sizeof( aspect ) );
5372 Com_sprintf( modeName, sizeof( modeName ), "%s (%s)", mode, aspect );
5373
5374 multiPtr->cvarList[multiPtr->count] = String_Alloc( modeName );
5375
5376 for ( i = 0; builtinResolutions[i]; i++ ) {
5377 if( !Q_stricmp( builtinResolutions[i], mode ) ) {
5378 multiPtr->cvarStr[multiPtr->count] = builtinResolutions[i];
5379 multiPtr->cvarValue[multiPtr->count] = i;
5380 break;
5381 }
5382 }
5383
5384 if ( builtinResolutions[i] == NULL ) {
5385 multiPtr->cvarStr[multiPtr->count] = String_Alloc( mode );
5386 multiPtr->cvarValue[multiPtr->count] = -1;
5387 }
5388
5389 multiPtr->count++;
5390 }
5391 } else {
5392 for ( i = 0; builtinResolutions[i] && multiPtr->count < MAX_MULTI_CVARS; i++ ) {
5393 UI_ResolutionToAspect( builtinResolutions[i], aspect, sizeof( aspect ) );
5394 Com_sprintf( modeName, sizeof( modeName ), "%s (%s)", builtinResolutions[i], aspect );
5395
5396 multiPtr->cvarList[multiPtr->count] = String_Alloc( modeName );
5397 multiPtr->cvarStr[multiPtr->count] = builtinResolutions[i];
5398 multiPtr->cvarValue[multiPtr->count] = i;
5399 multiPtr->count++;
5400 }
5401 }
5402
5403 // Add custom resolution if not in mode list
5404 if ( multiPtr->count < MAX_MULTI_CVARS ) {
5405 char currentResolution[20];
5406
5407 Com_sprintf( currentResolution, sizeof ( currentResolution ), "%dx%d", DC->glconfig.vidWidth, DC->glconfig.vidHeight );
5408 for ( i = 0; i < multiPtr->count; i++ ) {
5409 if ( !Q_stricmp( multiPtr->cvarStr[i], currentResolution ) ) {
5410 break;
5411 }
5412 }
5413
5414 if ( i == multiPtr->count ) {
5415 UI_ResolutionToAspect( currentResolution, aspect, sizeof( aspect ) );
5416 Com_sprintf( modeName, sizeof( modeName ), "%s (%s)", currentResolution, aspect );
5417
5418 multiPtr->cvarList[multiPtr->count] = String_Alloc( modeName );
5419 multiPtr->cvarStr[multiPtr->count] = String_Alloc( currentResolution );
5420 multiPtr->cvarValue[multiPtr->count] = -1;
5421 multiPtr->count++;
5422 }
5423 }
5424
5425 Com_Printf( "Found video mode list with %d modes, replaced list with %d modes\n", oldCount, multiPtr->count );
5426 }
5427
5428 }
5429
5430 /*
5431 ===============
5432 Item_Parse
5433 ===============
5434 */
5435 qboolean Item_Parse(int handle, itemDef_t *item) {
5436 pc_token_t token;
5437 keywordHash_t *key;
5438
5439
5440 if (!trap_PC_ReadToken(handle, &token))
5441 return qfalse;
5442 if (*token.string != '{') {
5443 return qfalse;
5444 }
5445 while ( 1 ) {
5446 if (!trap_PC_ReadToken(handle, &token)) {
5447 PC_SourceError(handle, "end of file inside menu item");
5448 return qfalse;
5449 }
5450
5451 if (*token.string == '}') {
5452 Item_ApplyHacks( item );
5453 return qtrue;
5454 }
5455
5456 key = KeywordHash_Find(itemParseKeywordHash, token.string);
5457 if (!key) {
5458 PC_SourceError(handle, "unknown menu item keyword %s", token.string);
5459 continue;
5460 }
5461 if ( !key->func(item, handle) ) {
5462 PC_SourceError(handle, "couldn't parse menu item keyword %s", token.string);
5463 return qfalse;
5464 }
5465 }
5466 return qfalse;
5467 }
5468
5469
5470 // Item_InitControls
5471 // init's special control types
5472 void Item_InitControls(itemDef_t *item) {
5473 if (item == NULL) {
5474 return;
5475 }
5476 if (item->type == ITEM_TYPE_LISTBOX) {
5477 listBoxDef_t *listPtr = (listBoxDef_t*)item->typeData;
5478 item->cursorPos = 0;
5479 if (listPtr) {
5480 listPtr->cursorPos = 0;
5481 listPtr->startPos = 0;
5482 listPtr->endPos = 0;
5483 listPtr->cursorPos = 0;
5484 }
5485 }
5486 }
5487
5488 /*
5489 ===============
5490 Menu Keyword Parse functions
5491 ===============
5492 */
5493
5494 qboolean MenuParse_font( itemDef_t *item, int handle ) {
5495 menuDef_t *menu = (menuDef_t*)item;
5496 if (!PC_String_Parse(handle, &menu->font)) {
5497 return qfalse;
5498 }
5499 if (!DC->Assets.fontRegistered) {
5500 DC->registerFont(menu->font, 48, &DC->Assets.textFont);
5501 DC->Assets.fontRegistered = qtrue;
5502 }
5503 return qtrue;
5504 }
5505
5506 qboolean MenuParse_name( itemDef_t *item, int handle ) {
5507 menuDef_t *menu = (menuDef_t*)item;
5508 if (!PC_String_Parse(handle, &menu->window.name)) {
5509 return qfalse;
5510 }
5511 if (Q_stricmp(menu->window.name, "main") == 0) {
5512 // default main as having focus
5513 //menu->window.flags |= WINDOW_HASFOCUS;
5514 }
5515 return qtrue;
5516 }
5517
5518 qboolean MenuParse_fullscreen( itemDef_t *item, int handle ) {
5519 menuDef_t *menu = (menuDef_t*)item;
5520 union
5521 {
5522 qboolean b;
5523 int i;
5524 } fullScreen;
5525
5526 if (!PC_Int_Parse(handle, &fullScreen.i)) {
5527 return qfalse;
5528 }
5529 menu->fullScreen = fullScreen.b;
5530 return qtrue;
5531 }
5532
5533 qboolean MenuParse_rect( itemDef_t *item, int handle ) {
5534 menuDef_t *menu = (menuDef_t*)item;
5535 if (!PC_Rect_Parse(handle, &menu->window.rect)) {
5536 return qfalse;
5537 }
5538 return qtrue;
5539 }
5540
5541 qboolean MenuParse_style( itemDef_t *item, int handle ) {
5542 menuDef_t *menu = (menuDef_t*)item;
5543 if (!PC_Int_Parse(handle, &menu->window.style)) {
5544 return qfalse;
5545 }
5546 return qtrue;
5547 }
5548
5549 qboolean MenuParse_visible( itemDef_t *item, int handle ) {
5550 int i;
5551 menuDef_t *menu = (menuDef_t*)item;
5552
5553 if (!PC_Int_Parse(handle, &i)) {
5554 return qfalse;
5555 }
5556 if (i) {
5557 menu->window.flags |= WINDOW_VISIBLE;
5558 }
5559 return qtrue;
5560 }
5561
5562 qboolean MenuParse_onOpen( itemDef_t *item, int handle ) {
5563 menuDef_t *menu = (menuDef_t*)item;
5564 if (!PC_Script_Parse(handle, &menu->onOpen)) {
5565 return qfalse;
5566 }
5567 return qtrue;
5568 }
5569
5570 qboolean MenuParse_onClose( itemDef_t *item, int handle ) {
5571 menuDef_t *menu = (menuDef_t*)item;
5572 if (!PC_Script_Parse(handle, &menu->onClose)) {
5573 return qfalse;
5574 }
5575 return qtrue;
5576 }
5577
5578 qboolean MenuParse_onESC( itemDef_t *item, int handle ) {
5579 menuDef_t *menu = (menuDef_t*)item;
5580 if (!PC_Script_Parse(handle, &menu->onESC)) {
5581 return qfalse;
5582 }
5583 return qtrue;
5584 }
5585
5586
5587
5588 qboolean MenuParse_border( itemDef_t *item, int handle ) {
5589 menuDef_t *menu = (menuDef_t*)item;
5590 if (!PC_Int_Parse(handle, &menu->window.border)) {
5591 return qfalse;
5592 }
5593 return qtrue;
5594 }
5595
5596 qboolean MenuParse_borderSize( itemDef_t *item, int handle ) {
5597 menuDef_t *menu = (menuDef_t*)item;
5598 if (!PC_Float_Parse(handle, &menu->window.borderSize)) {
5599 return qfalse;
5600 }
5601 return qtrue;
5602 }
5603
5604 qboolean MenuParse_backcolor( itemDef_t *item, int handle ) {
5605 int i;
5606 float f;
5607 menuDef_t *menu = (menuDef_t*)item;
5608
5609 for (i = 0; i < 4; i++) {
5610 if (!PC_Float_Parse(handle, &f)) {
5611 return qfalse;
5612 }
5613 menu->window.backColor[i] = f;
5614 }
5615 return qtrue;
5616 }
5617
5618 qboolean MenuParse_forecolor( itemDef_t *item, int handle ) {
5619 int i;
5620 float f;
5621 menuDef_t *menu = (menuDef_t*)item;
5622
5623 for (i = 0; i < 4; i++) {
5624 if (!PC_Float_Parse(handle, &f)) {
5625 return qfalse;
5626 }
5627 menu->window.foreColor[i] = f;
5628 menu->window.flags |= WINDOW_FORECOLORSET;
5629 }
5630 return qtrue;
5631 }
5632
5633 qboolean MenuParse_bordercolor( itemDef_t *item, int handle ) {
5634 int i;
5635 float f;
5636 menuDef_t *menu = (menuDef_t*)item;
5637
5638 for (i = 0; i < 4; i++) {
5639 if (!PC_Float_Parse(handle, &f)) {
5640 return qfalse;
5641 }
5642 menu->window.borderColor[i] = f;
5643 }
5644 return qtrue;
5645 }
5646
5647 qboolean MenuParse_focuscolor( itemDef_t *item, int handle ) {
5648 int i;
5649 float f;
5650 menuDef_t *menu = (menuDef_t*)item;
5651
5652 for (i = 0; i < 4; i++) {
5653 if (!PC_Float_Parse(handle, &f)) {
5654 return qfalse;
5655 }
5656 menu->focusColor[i] = f;
5657 }
5658 return qtrue;
5659 }
5660
5661 qboolean MenuParse_disablecolor( itemDef_t *item, int handle ) {
5662 int i;
5663 float f;
5664 menuDef_t *menu = (menuDef_t*)item;
5665 for (i = 0; i < 4; i++) {
5666 if (!PC_Float_Parse(handle, &f)) {
5667 return qfalse;
5668 }
5669 menu->disableColor[i] = f;
5670 }
5671 return qtrue;
5672 }
5673
5674
5675 qboolean MenuParse_outlinecolor( itemDef_t *item, int handle ) {
5676 menuDef_t *menu = (menuDef_t*)item;
5677 if (!PC_Color_Parse(handle, &menu->window.outlineColor)){
5678 return qfalse;
5679 }
5680 return qtrue;
5681 }
5682
5683 qboolean MenuParse_background( itemDef_t *item, int handle ) {
5684 const char *buff;
5685 menuDef_t *menu = (menuDef_t*)item;
5686
5687 if (!PC_String_Parse(handle, &buff)) {
5688 return qfalse;
5689 }
5690 menu->window.background = DC->registerShaderNoMip(buff);
5691 return qtrue;
5692 }
5693
5694 qboolean MenuParse_cinematic( itemDef_t *item, int handle ) {
5695 menuDef_t *menu = (menuDef_t*)item;
5696
5697 if (!PC_String_Parse(handle, &menu->window.cinematicName)) {
5698 return qfalse;
5699 }
5700 return qtrue;
5701 }
5702
5703 qboolean MenuParse_ownerdrawFlag( itemDef_t *item, int handle ) {
5704 int i;
5705 menuDef_t *menu = (menuDef_t*)item;
5706
5707 if (!PC_Int_Parse(handle, &i)) {
5708 return qfalse;
5709 }
5710 menu->window.ownerDrawFlags |= i;
5711 return qtrue;
5712 }
5713
5714 qboolean MenuParse_ownerdraw( itemDef_t *item, int handle ) {
5715 menuDef_t *menu = (menuDef_t*)item;
5716
5717 if (!PC_Int_Parse(handle, &menu->window.ownerDraw)) {
5718 return qfalse;
5719 }
5720 return qtrue;
5721 }
5722
5723
5724 // decoration
5725 qboolean MenuParse_popup( itemDef_t *item, int handle ) {
5726 menuDef_t *menu = (menuDef_t*)item;
5727 menu->window.flags |= WINDOW_POPUP;
5728 return qtrue;
5729 }
5730
5731
5732 qboolean MenuParse_outOfBounds( itemDef_t *item, int handle ) {
5733 menuDef_t *menu = (menuDef_t*)item;
5734
5735 menu->window.flags |= WINDOW_OOB_CLICK;
5736 return qtrue;
5737 }
5738
5739 qboolean MenuParse_soundLoop( itemDef_t *item, int handle ) {
5740 menuDef_t *menu = (menuDef_t*)item;
5741
5742 if (!PC_String_Parse(handle, &menu->soundName)) {
5743 return qfalse;
5744 }
5745 return qtrue;
5746 }
5747
5748 qboolean MenuParse_fadeClamp( itemDef_t *item, int handle ) {
5749 menuDef_t *menu = (menuDef_t*)item;
5750
5751 if (!PC_Float_Parse(handle, &menu->fadeClamp)) {
5752 return qfalse;
5753 }
5754 return qtrue;
5755 }
5756
5757 qboolean MenuParse_fadeAmount( itemDef_t *item, int handle ) {
5758 menuDef_t *menu = (menuDef_t*)item;
5759
5760 if (!PC_Float_Parse(handle, &menu->fadeAmount)) {
5761 return qfalse;
5762 }
5763 return qtrue;
5764 }
5765
5766
5767 qboolean MenuParse_fadeCycle( itemDef_t *item, int handle ) {
5768 menuDef_t *menu = (menuDef_t*)item;
5769
5770 if (!PC_Int_Parse(handle, &menu->fadeCycle)) {
5771 return qfalse;
5772 }
5773 return qtrue;
5774 }
5775
5776
5777 qboolean MenuParse_itemDef( itemDef_t *item, int handle ) {
5778 menuDef_t *menu = (menuDef_t*)item;
5779 if (menu->itemCount < MAX_MENUITEMS) {
5780 menu->items[menu->itemCount] = UI_Alloc(sizeof(itemDef_t));
5781 if (!menu->items[menu->itemCount]) {
5782 return qfalse;
5783 }
5784 Item_Init(menu->items[menu->itemCount]);
5785 if (!Item_Parse(handle, menu->items[menu->itemCount])) {
5786 return qfalse;
5787 }
5788 Item_InitControls(menu->items[menu->itemCount]);
5789 menu->items[menu->itemCount++]->parent = menu;
5790 }
5791 return qtrue;
5792 }
5793
5794 keywordHash_t menuParseKeywords[] = {
5795 {"font", MenuParse_font, NULL},
5796 {"name", MenuParse_name, NULL},
5797 {"fullscreen", MenuParse_fullscreen, NULL},
5798 {"rect", MenuParse_rect, NULL},
5799 {"style", MenuParse_style, NULL},
5800 {"visible", MenuParse_visible, NULL},
5801 {"onOpen", MenuParse_onOpen, NULL},
5802 {"onClose", MenuParse_onClose, NULL},
5803 {"onESC", MenuParse_onESC, NULL},
5804 {"border", MenuParse_border, NULL},
5805 {"borderSize", MenuParse_borderSize, NULL},
5806 {"backcolor", MenuParse_backcolor, NULL},
5807 {"forecolor", MenuParse_forecolor, NULL},
5808 {"bordercolor", MenuParse_bordercolor, NULL},
5809 {"focuscolor", MenuParse_focuscolor, NULL},
5810 {"disablecolor", MenuParse_disablecolor, NULL},
5811 {"outlinecolor", MenuParse_outlinecolor, NULL},
5812 {"background", MenuParse_background, NULL},
5813 {"ownerdraw", MenuParse_ownerdraw, NULL},
5814 {"ownerdrawFlag", MenuParse_ownerdrawFlag, NULL},
5815 {"outOfBoundsClick", MenuParse_outOfBounds, NULL},
5816 {"soundLoop", MenuParse_soundLoop, NULL},
5817 {"itemDef", MenuParse_itemDef, NULL},
5818 {"cinematic", MenuParse_cinematic, NULL},
5819 {"popup", MenuParse_popup, NULL},
5820 {"fadeClamp", MenuParse_fadeClamp, NULL},
5821 {"fadeCycle", MenuParse_fadeCycle, NULL},
5822 {"fadeAmount", MenuParse_fadeAmount, NULL},
5823 {NULL, 0, NULL}
5824 };
5825
5826 keywordHash_t *menuParseKeywordHash[KEYWORDHASH_SIZE];
5827
5828 /*
5829 ===============
5830 Menu_SetupKeywordHash
5831 ===============
5832 */
5833 void Menu_SetupKeywordHash(void) {
5834 int i;
5835
5836 memset(menuParseKeywordHash, 0, sizeof(menuParseKeywordHash));
5837 for (i = 0; menuParseKeywords[i].keyword; i++) {
5838 KeywordHash_Add(menuParseKeywordHash, &menuParseKeywords[i]);
5839 }
5840 }
5841
5842 /*
5843 ===============
5844 Menu_Parse
5845 ===============
5846 */
5847 qboolean Menu_Parse(int handle, menuDef_t *menu) {
5848 pc_token_t token;
5849 keywordHash_t *key;
5850
5851 if (!trap_PC_ReadToken(handle, &token))
5852 return qfalse;
5853 if (*token.string != '{') {
5854 return qfalse;
5855 }
5856
5857 while ( 1 ) {
5858
5859 memset(&token, 0, sizeof(pc_token_t));
5860 if (!trap_PC_ReadToken(handle, &token)) {
5861 PC_SourceError(handle, "end of file inside menu");
5862 return qfalse;
5863 }
5864
5865 if (*token.string == '}') {
5866 return qtrue;
5867 }
5868
5869 key = KeywordHash_Find(menuParseKeywordHash, token.string);
5870 if (!key) {
5871 PC_SourceError(handle, "unknown menu keyword %s", token.string);
5872 continue;
5873 }
5874 if ( !key->func((itemDef_t*)menu, handle) ) {
5875 PC_SourceError(handle, "couldn't parse menu keyword %s", token.string);
5876 return qfalse;
5877 }
5878 }
5879 return qfalse;
5880 }
5881
5882 /*
5883 ===============
5884 Menu_New
5885 ===============
5886 */
5887 void Menu_New(int handle) {
5888 menuDef_t *menu = &Menus[menuCount];
5889
5890 if (menuCount < MAX_MENUS) {
5891 Menu_Init(menu);
5892 if (Menu_Parse(handle, menu)) {
5893 Menu_PostParse(menu);
5894 menuCount++;
5895 }
5896 }
5897 }
5898
5899 int Menu_Count(void) {
5900 return menuCount;
5901 }
5902
5903 void Menu_PaintAll(void) {
5904 int i;
5905 if (captureFunc) {
5906 captureFunc(captureData);
5907 }
5908
5909 for (i = 0; i < Menu_Count(); i++) {
5910 Menu_Paint(&Menus[i], qfalse);
5911 }
5912
5913 if (debugMode) {
5914 vec4_t v = {1, 1, 1, 1};
5915 DC->drawText(5, 25, .5, v, va("fps: %f", DC->FPS), 0, 0, 0);
5916 }
5917 }
5918
5919 void Menu_Reset(void) {
5920 menuCount = 0;
5921 }
5922
5923 displayContextDef_t *Display_GetContext(void) {
5924 return DC;
5925 }
5926
5927 #ifndef MISSIONPACK
5928 static float captureX;
5929 static float captureY;
5930 #endif
5931
5932 void *Display_CaptureItem(int x, int y) {
5933 int i;
5934
5935 for (i = 0; i < menuCount; i++) {
5936 // turn off focus each item
5937 // menu->items[i].window.flags &= ~WINDOW_HASFOCUS;
5938 if (Rect_ContainsPoint(&Menus[i].window.rect, x, y)) {
5939 return &Menus[i];
5940 }
5941 }
5942 return NULL;
5943 }
5944
5945
5946 // FIXME:
5947 qboolean Display_MouseMove(void *p, int x, int y) {
5948 int i;
5949 menuDef_t *menu = p;
5950
5951 if (menu == NULL) {
5952 menu = Menu_GetFocused();
5953 if (menu) {
5954 if (menu->window.flags & WINDOW_POPUP) {
5955 Menu_HandleMouseMove(menu, x, y);
5956 return qtrue;
5957 }
5958 }
5959 for (i = 0; i < menuCount; i++) {
5960 Menu_HandleMouseMove(&Menus[i], x, y);
5961 }
5962 } else {
5963 menu->window.rect.x += x;
5964 menu->window.rect.y += y;
5965 Menu_UpdatePosition(menu);
5966 }
5967 return qtrue;
5968
5969 }
5970
5971 int Display_CursorType(int x, int y) {
5972 int i;
5973 for (i = 0; i < menuCount; i++) {
5974 rectDef_t r2;
5975 r2.x = Menus[i].window.rect.x - 3;
5976 r2.y = Menus[i].window.rect.y - 3;
5977 r2.w = r2.h = 7;
5978 if (Rect_ContainsPoint(&r2, x, y)) {
5979 return CURSOR_SIZER;
5980 }
5981 }
5982 return CURSOR_ARROW;
5983 }
5984
5985
5986 void Display_HandleKey(int key, qboolean down, int x, int y) {
5987 menuDef_t *menu = Display_CaptureItem(x, y);
5988 if (menu == NULL) {
5989 menu = Menu_GetFocused();
5990 }
5991 if (menu) {
5992 Menu_HandleKey(menu, key, down );
5993 }
5994 }
5995
5996 static void Window_CacheContents(windowDef_t *window) {
5997 if (window) {
5998 if (window->cinematicName) {
5999 int cin = DC->playCinematic(window->cinematicName, 0, 0, 0, 0);
6000 DC->stopCinematic(cin);
6001 }
6002 }
6003 }
6004
6005
6006 static void Item_CacheContents(itemDef_t *item) {
6007 if (item) {
6008 Window_CacheContents(&item->window);
6009 }
6010
6011 }
6012
6013 static void Menu_CacheContents(menuDef_t *menu) {
6014 if (menu) {
6015 int i;
6016 Window_CacheContents(&menu->window);
6017 for (i = 0; i < menu->itemCount; i++) {
6018 Item_CacheContents(menu->items[i]);
6019 }
6020
6021 if (menu->soundName && *menu->soundName) {
6022 DC->registerSound(menu->soundName, qfalse);
6023 }
6024 }
6025
6026 }
6027
6028 void Display_CacheAll(void) {
6029 int i;
6030 for (i = 0; i < menuCount; i++) {
6031 Menu_CacheContents(&Menus[i]);
6032 }
6033 }
6034
6035
6036 static qboolean Menu_OverActiveItem(menuDef_t *menu, float x, float y) {
6037 if (menu && menu->window.flags & (WINDOW_VISIBLE | WINDOW_FORCED)) {
6038 if (Rect_ContainsPoint(&menu->window.rect, x, y)) {
6039 int i;
6040 for (i = 0; i < menu->itemCount; i++) {
6041 // turn off focus each item
6042 // menu->items[i].window.flags &= ~WINDOW_HASFOCUS;
6043
6044 if (!(menu->items[i]->window.flags & (WINDOW_VISIBLE | WINDOW_FORCED))) {
6045 continue;
6046 }
6047
6048 if (menu->items[i]->window.flags & WINDOW_DECORATION) {
6049 continue;
6050 }
6051
6052 if (Rect_ContainsPoint(&menu->items[i]->window.rect, x, y)) {
6053 itemDef_t *overItem = menu->items[i];
6054 if (overItem->type == ITEM_TYPE_TEXT && overItem->text) {
6055 if (Rect_ContainsPoint(Item_CorrectedTextRect(overItem), x, y)) {
6056 return qtrue;
6057 } else {
6058 continue;
6059 }
6060 } else {
6061 return qtrue;
6062 }
6063 }
6064 }
6065
6066 }
6067 }
6068 return qfalse;
6069 }
6070