Debian Codegraph @

code/q3_ui/ui_atoms.c

raw
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 /**********************************************************************
24 UI_ATOMS.C
25
26 User interface building blocks and support functions.
27 **********************************************************************/
28 #include "ui_local.h"
29
30 uiStatic_t uis;
31 qboolean m_entersound; // after a frame, so caching won't disrupt the sound
32
33 void QDECL Com_Error( int level, const char *error, ... ) {
34 va_list argptr;
35 char text[1024];
36
37 va_start (argptr, error);
38 Q_vsnprintf (text, sizeof(text), error, argptr);
39 va_end (argptr);
40
41 trap_Error( va("%s", text) );
42 }
43
44 void QDECL Com_Printf( const char *msg, ... ) {
45 va_list argptr;
46 char text[1024];
47
48 va_start (argptr, msg);
49 Q_vsnprintf (text, sizeof(text), msg, argptr);
50 va_end (argptr);
51
52 trap_Print( va("%s", text) );
53 }
54
55 /*
56 =================
57 UI_ClampCvar
58 =================
59 */
60 float UI_ClampCvar( float min, float max, float value )
61 {
62 if ( value < min ) return min;
63 if ( value > max ) return max;
64 return value;
65 }
66
67 /*
68 =================
69 UI_StartDemoLoop
70 =================
71 */
72 void UI_StartDemoLoop( void ) {
73 trap_Cmd_ExecuteText( EXEC_APPEND, "d1\n" );
74 }
75
76 /*
77 =================
78 UI_PushMenu
79 =================
80 */
81 void UI_PushMenu( menuframework_s *menu )
82 {
83 int i;
84 menucommon_s* item;
85
86 // avoid stacking menus invoked by hotkeys
87 for (i=0 ; i<uis.menusp ; i++)
88 {
89 if (uis.stack[i] == menu)
90 {
91 uis.menusp = i;
92 break;
93 }
94 }
95
96 if (i == uis.menusp)
97 {
98 if (uis.menusp >= MAX_MENUDEPTH)
99 trap_Error("UI_PushMenu: menu stack overflow");
100
101 uis.stack[uis.menusp++] = menu;
102 }
103
104 uis.activemenu = menu;
105
106 // default cursor position
107 menu->cursor = 0;
108 menu->cursor_prev = 0;
109
110 m_entersound = qtrue;
111
112 trap_Key_SetCatcher( KEYCATCH_UI );
113
114 // force first available item to have focus
115 for (i=0; i<menu->nitems; i++)
116 {
117 item = (menucommon_s *)menu->items[i];
118 if (!(item->flags & (QMF_GRAYED|QMF_MOUSEONLY|QMF_INACTIVE)))
119 {
120 menu->cursor_prev = -1;
121 Menu_SetCursor( menu, i );
122 break;
123 }
124 }
125
126 uis.firstdraw = qtrue;
127 }
128
129 /*
130 =================
131 UI_PopMenu
132 =================
133 */
134 void UI_PopMenu (void)
135 {
136 trap_S_StartLocalSound( menu_out_sound, CHAN_LOCAL_SOUND );
137
138 uis.menusp--;
139
140 if (uis.menusp < 0)
141 trap_Error ("UI_PopMenu: menu stack underflow");
142
143 if (uis.menusp) {
144 uis.activemenu = uis.stack[uis.menusp-1];
145 uis.firstdraw = qtrue;
146 }
147 else {
148 UI_ForceMenuOff ();
149 }
150 }
151
152 void UI_ForceMenuOff (void)
153 {
154 uis.menusp = 0;
155 uis.activemenu = NULL;
156
157 trap_Key_SetCatcher( trap_Key_GetCatcher() & ~KEYCATCH_UI );
158 trap_Key_ClearStates();
159 trap_Cvar_Set( "cl_paused", "0" );
160 }
161
162 /*
163 =================
164 UI_LerpColor
165 =================
166 */
167 void UI_LerpColor(vec4_t a, vec4_t b, vec4_t c, float t)
168 {
169 int i;
170
171 // lerp and clamp each component
172 for (i=0; i<4; i++)
173 {
174 c[i] = a[i] + t*(b[i]-a[i]);
175 if (c[i] < 0)
176 c[i] = 0;
177 else if (c[i] > 1.0)
178 c[i] = 1.0;
179 }
180 }
181
182 /*
183 =================
184 UI_DrawProportionalString2
185 =================
186 */
187 static int propMap[128][3] = {
188 {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1},
189 {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1},
190
191 {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1},
192 {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1}, {0, 0, -1},
193
194 {0, 0, PROP_SPACE_WIDTH}, // SPACE
195 {11, 122, 7}, // !
196 {154, 181, 14}, // "
197 {55, 122, 17}, // #
198 {79, 122, 18}, // $
199 {101, 122, 23}, // %
200 {153, 122, 18}, // &
201 {9, 93, 7}, // '
202 {207, 122, 8}, // (
203 {230, 122, 9}, // )
204 {177, 122, 18}, // *
205 {30, 152, 18}, // +
206 {85, 181, 7}, // ,
207 {34, 93, 11}, // -
208 {110, 181, 6}, // .
209 {130, 152, 14}, // /
210
211 {22, 64, 17}, // 0
212 {41, 64, 12}, // 1
213 {58, 64, 17}, // 2
214 {78, 64, 18}, // 3
215 {98, 64, 19}, // 4
216 {120, 64, 18}, // 5
217 {141, 64, 18}, // 6
218 {204, 64, 16}, // 7
219 {162, 64, 17}, // 8
220 {182, 64, 18}, // 9
221 {59, 181, 7}, // :
222 {35,181, 7}, // ;
223 {203, 152, 14}, // <
224 {56, 93, 14}, // =
225 {228, 152, 14}, // >
226 {177, 181, 18}, // ?
227
228 {28, 122, 22}, // @
229 {5, 4, 18}, // A
230 {27, 4, 18}, // B
231 {48, 4, 18}, // C
232 {69, 4, 17}, // D
233 {90, 4, 13}, // E
234 {106, 4, 13}, // F
235 {121, 4, 18}, // G
236 {143, 4, 17}, // H
237 {164, 4, 8}, // I
238 {175, 4, 16}, // J
239 {195, 4, 18}, // K
240 {216, 4, 12}, // L
241 {230, 4, 23}, // M
242 {6, 34, 18}, // N
243 {27, 34, 18}, // O
244
245 {48, 34, 18}, // P
246 {68, 34, 18}, // Q
247 {90, 34, 17}, // R
248 {110, 34, 18}, // S
249 {130, 34, 14}, // T
250 {146, 34, 18}, // U
251 {166, 34, 19}, // V
252 {185, 34, 29}, // W
253 {215, 34, 18}, // X
254 {234, 34, 18}, // Y
255 {5, 64, 14}, // Z
256 {60, 152, 7}, // [
257 {106, 151, 13}, // '\'
258 {83, 152, 7}, // ]
259 {128, 122, 17}, // ^
260 {4, 152, 21}, // _
261
262 {134, 181, 5}, // '
263 {5, 4, 18}, // A
264 {27, 4, 18}, // B
265 {48, 4, 18}, // C
266 {69, 4, 17}, // D
267 {90, 4, 13}, // E
268 {106, 4, 13}, // F
269 {121, 4, 18}, // G
270 {143, 4, 17}, // H
271 {164, 4, 8}, // I
272 {175, 4, 16}, // J
273 {195, 4, 18}, // K
274 {216, 4, 12}, // L
275 {230, 4, 23}, // M
276 {6, 34, 18}, // N
277 {27, 34, 18}, // O
278
279 {48, 34, 18}, // P
280 {68, 34, 18}, // Q
281 {90, 34, 17}, // R
282 {110, 34, 18}, // S
283 {130, 34, 14}, // T
284 {146, 34, 18}, // U
285 {166, 34, 19}, // V
286 {185, 34, 29}, // W
287 {215, 34, 18}, // X
288 {234, 34, 18}, // Y
289 {5, 64, 14}, // Z
290 {153, 152, 13}, // {
291 {11, 181, 5}, // |
292 {180, 152, 13}, // }
293 {79, 93, 17}, // ~
294 {0, 0, -1} // DEL
295 };
296
297 static int propMapB[26][3] = {
298 {11, 12, 33},
299 {49, 12, 31},
300 {85, 12, 31},
301 {120, 12, 30},
302 {156, 12, 21},
303 {183, 12, 21},
304 {207, 12, 32},
305
306 {13, 55, 30},
307 {49, 55, 13},
308 {66, 55, 29},
309 {101, 55, 31},
310 {135, 55, 21},
311 {158, 55, 40},
312 {204, 55, 32},
313
314 {12, 97, 31},
315 {48, 97, 31},
316 {82, 97, 30},
317 {118, 97, 30},
318 {153, 97, 30},
319 {185, 97, 25},
320 {213, 97, 30},
321
322 {11, 139, 32},
323 {42, 139, 51},
324 {93, 139, 32},
325 {126, 139, 31},
326 {158, 139, 25},
327 };
328
329 #define PROPB_GAP_WIDTH 4
330 #define PROPB_SPACE_WIDTH 12
331 #define PROPB_HEIGHT 36
332
333 // bk001205 - code below duplicated in cgame/cg_drawtools.c
334 // bk001205 - FIXME: does this belong in ui_shared.c?
335 /*
336 =================
337 UI_DrawBannerString
338 =================
339 */
340 static void UI_DrawBannerString2( int x, int y, const char* str, vec4_t color )
341 {
342 const char* s;
343 unsigned char ch; // bk001204 - unsigned
344 float ax;
345 float ay;
346 float aw;
347 float ah;
348 float frow;
349 float fcol;
350 float fwidth;
351 float fheight;
352
353 // draw the colored text
354 trap_R_SetColor( color );
355
356 ax = x * uis.xscale + uis.bias;
357 ay = y * uis.yscale;
358
359 s = str;
360 while ( *s )
361 {
362 ch = *s & 127;
363 if ( ch == ' ' ) {
364 ax += ((float)PROPB_SPACE_WIDTH + (float)PROPB_GAP_WIDTH)* uis.xscale;
365 }
366 else if ( ch >= 'A' && ch <= 'Z' ) {
367 ch -= 'A';
368 fcol = (float)propMapB[ch][0] / 256.0f;
369 frow = (float)propMapB[ch][1] / 256.0f;
370 fwidth = (float)propMapB[ch][2] / 256.0f;
371 fheight = (float)PROPB_HEIGHT / 256.0f;
372 aw = (float)propMapB[ch][2] * uis.xscale;
373 ah = (float)PROPB_HEIGHT * uis.yscale;
374 trap_R_DrawStretchPic( ax, ay, aw, ah, fcol, frow, fcol+fwidth, frow+fheight, uis.charsetPropB );
375 ax += (aw + (float)PROPB_GAP_WIDTH * uis.xscale);
376 }
377 s++;
378 }
379
380 trap_R_SetColor( NULL );
381 }
382
383 void UI_DrawBannerString( int x, int y, const char* str, int style, vec4_t color ) {
384 const char * s;
385 int ch;
386 int width;
387 vec4_t drawcolor;
388
389 // find the width of the drawn text
390 s = str;
391 width = 0;
392 while ( *s ) {
393 ch = *s;
394 if ( ch == ' ' ) {
395 width += PROPB_SPACE_WIDTH;
396 }
397 else if ( ch >= 'A' && ch <= 'Z' ) {
398 width += propMapB[ch - 'A'][2] + PROPB_GAP_WIDTH;
399 }
400 s++;
401 }
402 width -= PROPB_GAP_WIDTH;
403
404 switch( style & UI_FORMATMASK ) {
405 case UI_CENTER:
406 x -= width / 2;
407 break;
408
409 case UI_RIGHT:
410 x -= width;
411 break;
412
413 case UI_LEFT:
414 default:
415 break;
416 }
417
418 if ( style & UI_DROPSHADOW ) {
419 drawcolor[0] = drawcolor[1] = drawcolor[2] = 0;
420 drawcolor[3] = color[3];
421 UI_DrawBannerString2( x+2, y+2, str, drawcolor );
422 }
423
424 UI_DrawBannerString2( x, y, str, color );
425 }
426
427
428 int UI_ProportionalStringWidth( const char* str ) {
429 const char * s;
430 int ch;
431 int charWidth;
432 int width;
433
434 s = str;
435 width = 0;
436 while ( *s ) {
437 ch = *s & 127;
438 charWidth = propMap[ch][2];
439 if ( charWidth != -1 ) {
440 width += charWidth;
441 width += PROP_GAP_WIDTH;
442 }
443 s++;
444 }
445
446 width -= PROP_GAP_WIDTH;
447 return width;
448 }
449
450 static void UI_DrawProportionalString2( int x, int y, const char* str, vec4_t color, float sizeScale, qhandle_t charset )
451 {
452 const char* s;
453 unsigned char ch; // bk001204 - unsigned
454 float ax;
455 float ay;
456 float aw = 0; // bk001204 - init
457 float ah;
458 float frow;
459 float fcol;
460 float fwidth;
461 float fheight;
462
463 // draw the colored text
464 trap_R_SetColor( color );
465
466 ax = x * uis.xscale + uis.bias;
467 ay = y * uis.yscale;
468
469 s = str;
470 while ( *s )
471 {
472 ch = *s & 127;
473 if ( ch == ' ' ) {
474 aw = (float)PROP_SPACE_WIDTH * uis.xscale * sizeScale;
475 }
476 else if ( propMap[ch][2] != -1 ) {
477 fcol = (float)propMap[ch][0] / 256.0f;
478 frow = (float)propMap[ch][1] / 256.0f;
479 fwidth = (float)propMap[ch][2] / 256.0f;
480 fheight = (float)PROP_HEIGHT / 256.0f;
481 aw = (float)propMap[ch][2] * uis.xscale * sizeScale;
482 ah = (float)PROP_HEIGHT * uis.yscale * sizeScale;
483 trap_R_DrawStretchPic( ax, ay, aw, ah, fcol, frow, fcol+fwidth, frow+fheight, charset );
484 }
485
486 ax += (aw + (float)PROP_GAP_WIDTH * uis.xscale * sizeScale);
487 s++;
488 }
489
490 trap_R_SetColor( NULL );
491 }
492
493 /*
494 =================
495 UI_ProportionalSizeScale
496 =================
497 */
498 float UI_ProportionalSizeScale( int style ) {
499 if( style & UI_SMALLFONT ) {
500 return PROP_SMALL_SIZE_SCALE;
501 }
502
503 return 1.00;
504 }
505
506
507 /*
508 =================
509 UI_DrawProportionalString
510 =================
511 */
512 void UI_DrawProportionalString( int x, int y, const char* str, int style, vec4_t color ) {
513 vec4_t drawcolor;
514 int width;
515 float sizeScale;
516
517 sizeScale = UI_ProportionalSizeScale( style );
518
519 switch( style & UI_FORMATMASK ) {
520 case UI_CENTER:
521 width = UI_ProportionalStringWidth( str ) * sizeScale;
522 x -= width / 2;
523 break;
524
525 case UI_RIGHT:
526 width = UI_ProportionalStringWidth( str ) * sizeScale;
527 x -= width;
528 break;
529
530 case UI_LEFT:
531 default:
532 break;
533 }
534
535 if ( style & UI_DROPSHADOW ) {
536 drawcolor[0] = drawcolor[1] = drawcolor[2] = 0;
537 drawcolor[3] = color[3];
538 UI_DrawProportionalString2( x+2, y+2, str, drawcolor, sizeScale, uis.charsetProp );
539 }
540
541 if ( style & UI_INVERSE ) {
542 drawcolor[0] = color[0] * 0.7;
543 drawcolor[1] = color[1] * 0.7;
544 drawcolor[2] = color[2] * 0.7;
545 drawcolor[3] = color[3];
546 UI_DrawProportionalString2( x, y, str, drawcolor, sizeScale, uis.charsetProp );
547 return;
548 }
549
550 if ( style & UI_PULSE ) {
551 drawcolor[0] = color[0] * 0.7;
552 drawcolor[1] = color[1] * 0.7;
553 drawcolor[2] = color[2] * 0.7;
554 drawcolor[3] = color[3];
555 UI_DrawProportionalString2( x, y, str, color, sizeScale, uis.charsetProp );
556
557 drawcolor[0] = color[0];
558 drawcolor[1] = color[1];
559 drawcolor[2] = color[2];
560 drawcolor[3] = 0.5 + 0.5 * sin( uis.realtime / PULSE_DIVISOR );
561 UI_DrawProportionalString2( x, y, str, drawcolor, sizeScale, uis.charsetPropGlow );
562 return;
563 }
564
565 UI_DrawProportionalString2( x, y, str, color, sizeScale, uis.charsetProp );
566 }
567
568 /*
569 =================
570 UI_DrawProportionalString_Wrapped
571 =================
572 */
573 void UI_DrawProportionalString_AutoWrapped( int x, int y, int xmax, int ystep, const char* str, int style, vec4_t color ) {
574 int width;
575 char *s1,*s2,*s3;
576 char c_bcp;
577 char buf[1024];
578 float sizeScale;
579
580 if (!str || str[0]=='\0')
581 return;
582
583 sizeScale = UI_ProportionalSizeScale( style );
584
585 Q_strncpyz(buf, str, sizeof(buf));
586 s1 = s2 = s3 = buf;
587
588 while (1) {
589 do {
590 s3++;
591 } while (*s3!=' ' && *s3!='\0');
592 c_bcp = *s3;
593 *s3 = '\0';
594 width = UI_ProportionalStringWidth(s1) * sizeScale;
595 *s3 = c_bcp;
596 if (width > xmax) {
597 if (s1==s2)
598 {
599 // fuck, don't have a clean cut, we'll overflow
600 s2 = s3;
601 }
602 *s2 = '\0';
603 UI_DrawProportionalString(x, y, s1, style, color);
604 y += ystep;
605 if (c_bcp == '\0')
606 {
607 // that was the last word
608 // we could start a new loop, but that wouldn't be much use
609 // even if the word is too long, we would overflow it (see above)
610 // so just print it now if needed
611 s2++;
612 if (*s2 != '\0') // if we are printing an overflowing line we have s2 == s3
613 UI_DrawProportionalString(x, y, s2, style, color);
614 break;
615 }
616 s2++;
617 s1 = s2;
618 s3 = s2;
619 }
620 else
621 {
622 s2 = s3;
623 if (c_bcp == '\0') // we reached the end
624 {
625 UI_DrawProportionalString(x, y, s1, style, color);
626 break;
627 }
628 }
629 }
630 }
631
632 /*
633 =================
634 UI_DrawString2
635 =================
636 */
637 static void UI_DrawString2( int x, int y, const char* str, vec4_t color, int charw, int charh )
638 {
639 const char* s;
640 char ch;
641 int forceColor = qfalse; //APSFIXME;
642 vec4_t tempcolor;
643 float ax;
644 float ay;
645 float aw;
646 float ah;
647 float frow;
648 float fcol;
649
650 if (y < -charh)
651 // offscreen
652 return;
653
654 // draw the colored text
655 trap_R_SetColor( color );
656
657 ax = x * uis.xscale + uis.bias;
658 ay = y * uis.yscale;
659 aw = charw * uis.xscale;
660 ah = charh * uis.yscale;
661
662 s = str;
663 while ( *s )
664 {
665 if ( Q_IsColorString( s ) )
666 {
667 if ( !forceColor )
668 {
669 memcpy( tempcolor, g_color_table[ColorIndex(s[1])], sizeof( tempcolor ) );
670 tempcolor[3] = color[3];
671 trap_R_SetColor( tempcolor );
672 }
673 s += 2;
674 continue;
675 }
676
677 ch = *s & 255;
678 if (ch != ' ')
679 {
680 frow = (ch>>4)*0.0625;
681 fcol = (ch&15)*0.0625;
682 trap_R_DrawStretchPic( ax, ay, aw, ah, fcol, frow, fcol + 0.0625, frow + 0.0625, uis.charset );
683 }
684
685 ax += aw;
686 s++;
687 }
688
689 trap_R_SetColor( NULL );
690 }
691
692 /*
693 =================
694 UI_DrawString
695 =================
696 */
697 void UI_DrawString( int x, int y, const char* str, int style, vec4_t color )
698 {
699 int len;
700 int charw;
701 int charh;
702 vec4_t newcolor;
703 vec4_t lowlight;
704 float *drawcolor;
705 vec4_t dropcolor;
706
707 if( !str ) {
708 return;
709 }
710
711 if ((style & UI_BLINK) && ((uis.realtime/BLINK_DIVISOR) & 1))
712 return;
713
714 if (style & UI_SMALLFONT)
715 {
716 charw = SMALLCHAR_WIDTH;
717 charh = SMALLCHAR_HEIGHT;
718 }
719 else if (style & UI_GIANTFONT)
720 {
721 charw = GIANTCHAR_WIDTH;
722 charh = GIANTCHAR_HEIGHT;
723 }
724 else
725 {
726 charw = BIGCHAR_WIDTH;
727 charh = BIGCHAR_HEIGHT;
728 }
729
730 if (style & UI_PULSE)
731 {
732 lowlight[0] = 0.8*color[0];
733 lowlight[1] = 0.8*color[1];
734 lowlight[2] = 0.8*color[2];
735 lowlight[3] = 0.8*color[3];
736 UI_LerpColor(color,lowlight,newcolor,0.5+0.5*sin(uis.realtime/PULSE_DIVISOR));
737 drawcolor = newcolor;
738 }
739 else
740 drawcolor = color;
741
742 switch (style & UI_FORMATMASK)
743 {
744 case UI_CENTER:
745 // center justify at x
746 len = strlen(str);
747 x = x - len*charw/2;
748 break;
749
750 case UI_RIGHT:
751 // right justify at x
752 len = strlen(str);
753 x = x - len*charw;
754 break;
755
756 default:
757 // left justify at x
758 break;
759 }
760
761 if ( style & UI_DROPSHADOW )
762 {
763 dropcolor[0] = dropcolor[1] = dropcolor[2] = 0;
764 dropcolor[3] = drawcolor[3];
765 UI_DrawString2(x+2,y+2,str,dropcolor,charw,charh);
766 }
767
768 UI_DrawString2(x,y,str,drawcolor,charw,charh);
769 }
770
771 /*
772 =================
773 UI_DrawChar
774 =================
775 */
776 void UI_DrawChar( int x, int y, int ch, int style, vec4_t color )
777 {
778 char buff[2];
779
780 buff[0] = ch;
781 buff[1] = '\0';
782
783 UI_DrawString( x, y, buff, style, color );
784 }
785
786 qboolean UI_IsFullscreen( void ) {
787 if ( uis.activemenu && ( trap_Key_GetCatcher() & KEYCATCH_UI ) ) {
788 return uis.activemenu->fullscreen;
789 }
790
791 return qfalse;
792 }
793
794 static void NeedCDAction( qboolean result ) {
795 if ( !result ) {
796 trap_Cmd_ExecuteText( EXEC_APPEND, "quit\n" );
797 }
798 }
799
800 static void NeedCDKeyAction( qboolean result ) {
801 if ( !result ) {
802 trap_Cmd_ExecuteText( EXEC_APPEND, "quit\n" );
803 }
804 }
805
806 void UI_SetActiveMenu( uiMenuCommand_t menu ) {
807 // this should be the ONLY way the menu system is brought up
808 // enusure minumum menu data is cached
809 Menu_Cache();
810
811 switch ( menu ) {
812 case UIMENU_NONE:
813 UI_ForceMenuOff();
814 return;
815 case UIMENU_MAIN:
816 UI_MainMenu();
817 return;
818 case UIMENU_NEED_CD:
819 UI_ConfirmMenu( "Insert the CD", 0, NeedCDAction );
820 return;
821 case UIMENU_BAD_CD_KEY:
822 UI_ConfirmMenu( "Bad CD Key", 0, NeedCDKeyAction );
823 return;
824 case UIMENU_INGAME:
825 /*
826 //GRank
827 UI_RankingsMenu();
828 return;
829 */
830 trap_Cvar_Set( "cl_paused", "1" );
831 UI_InGameMenu();
832 return;
833
834 // bk001204
835 case UIMENU_TEAM:
836 case UIMENU_POSTGAME:
837 default:
838 #ifndef NDEBUG
839 Com_Printf("UI_SetActiveMenu: bad enum %d\n", menu );
840 #endif
841 break;
842 }
843 }
844
845 /*
846 =================
847 UI_KeyEvent
848 =================
849 */
850 void UI_KeyEvent( int key, int down ) {
851 sfxHandle_t s;
852
853 if (!uis.activemenu) {
854 return;
855 }
856
857 if (!down) {
858 return;
859 }
860
861 if (uis.activemenu->key)
862 s = uis.activemenu->key( key );
863 else
864 s = Menu_DefaultKey( uis.activemenu, key );
865
866 if ((s > 0) && (s != menu_null_sound))
867 trap_S_StartLocalSound( s, CHAN_LOCAL_SOUND );
868 }
869
870 /*
871 =================
872 UI_MouseEvent
873 =================
874 */
875 void UI_MouseEvent( int dx, int dy )
876 {
877 int i;
878 menucommon_s* m;
879
880 if (!uis.activemenu)
881 return;
882
883 // update mouse screen position
884 uis.cursorx += dx;
885 if (uis.cursorx < -uis.bias)
886 uis.cursorx = -uis.bias;
887 else if (uis.cursorx > SCREEN_WIDTH+uis.bias)
888 uis.cursorx = SCREEN_WIDTH+uis.bias;
889
890 uis.cursory += dy;
891 if (uis.cursory < 0)
892 uis.cursory = 0;
893 else if (uis.cursory > SCREEN_HEIGHT)
894 uis.cursory = SCREEN_HEIGHT;
895
896 // region test the active menu items
897 for (i=0; i<uis.activemenu->nitems; i++)
898 {
899 m = (menucommon_s*)uis.activemenu->items[i];
900
901 if (m->flags & (QMF_GRAYED|QMF_INACTIVE))
902 continue;
903
904 if ((uis.cursorx < m->left) ||
905 (uis.cursorx > m->right) ||
906 (uis.cursory < m->top) ||
907 (uis.cursory > m->bottom))
908 {
909 // cursor out of item bounds
910 continue;
911 }
912
913 // set focus to item at cursor
914 if (uis.activemenu->cursor != i)
915 {
916 Menu_SetCursor( uis.activemenu, i );
917 ((menucommon_s*)(uis.activemenu->items[uis.activemenu->cursor_prev]))->flags &= ~QMF_HASMOUSEFOCUS;
918
919 if ( !(((menucommon_s*)(uis.activemenu->items[uis.activemenu->cursor]))->flags & QMF_SILENT ) ) {
920 trap_S_StartLocalSound( menu_move_sound, CHAN_LOCAL_SOUND );
921 }
922 }
923
924 ((menucommon_s*)(uis.activemenu->items[uis.activemenu->cursor]))->flags |= QMF_HASMOUSEFOCUS;
925 return;
926 }
927
928 if (uis.activemenu->nitems > 0) {
929 // out of any region
930 ((menucommon_s*)(uis.activemenu->items[uis.activemenu->cursor]))->flags &= ~QMF_HASMOUSEFOCUS;
931 }
932 }
933
934 char *UI_Argv( int arg ) {
935 static char buffer[MAX_STRING_CHARS];
936
937 trap_Argv( arg, buffer, sizeof( buffer ) );
938
939 return buffer;
940 }
941
942
943 char *UI_Cvar_VariableString( const char *var_name ) {
944 static char buffer[MAX_STRING_CHARS];
945
946 trap_Cvar_VariableStringBuffer( var_name, buffer, sizeof( buffer ) );
947
948 return buffer;
949 }
950
951
952 /*
953 =================
954 UI_Cache
955 =================
956 */
957 void UI_Cache_f( void ) {
958 MainMenu_Cache();
959 InGame_Cache();
960 ConfirmMenu_Cache();
961 PlayerModel_Cache();
962 PlayerSettings_Cache();
963 Controls_Cache();
964 Demos_Cache();
965 UI_CinematicsMenu_Cache();
966 Preferences_Cache();
967 ServerInfo_Cache();
968 SpecifyServer_Cache();
969 ArenaServers_Cache();
970 StartServer_Cache();
971 ServerOptions_Cache();
972 DriverInfo_Cache();
973 GraphicsOptions_Cache();
974 UI_DisplayOptionsMenu_Cache();
975 UI_SoundOptionsMenu_Cache();
976 UI_NetworkOptionsMenu_Cache();
977 UI_SPLevelMenu_Cache();
978 UI_SPSkillMenu_Cache();
979 UI_SPPostgameMenu_Cache();
980 TeamMain_Cache();
981 UI_AddBots_Cache();
982 UI_RemoveBots_Cache();
983 UI_SetupMenu_Cache();
984 // UI_LoadConfig_Cache();
985 // UI_SaveConfigMenu_Cache();
986 UI_BotSelectMenu_Cache();
987 UI_CDKeyMenu_Cache();
988 UI_ModsMenu_Cache();
989
990 }
991
992
993 /*
994 =================
995 UI_ConsoleCommand
996 =================
997 */
998 qboolean UI_ConsoleCommand( int realTime ) {
999 char *cmd;
1000
1001 uis.frametime = realTime - uis.realtime;
1002 uis.realtime = realTime;
1003
1004 cmd = UI_Argv( 0 );
1005
1006 // ensure minimum menu data is available
1007 Menu_Cache();
1008
1009 if ( Q_stricmp (cmd, "levelselect") == 0 ) {
1010 UI_SPLevelMenu_f();
1011 return qtrue;
1012 }
1013
1014 if ( Q_stricmp (cmd, "postgame") == 0 ) {
1015 UI_SPPostgameMenu_f();
1016 return qtrue;
1017 }
1018
1019 if ( Q_stricmp (cmd, "ui_cache") == 0 ) {
1020 UI_Cache_f();
1021 return qtrue;
1022 }
1023
1024 if ( Q_stricmp (cmd, "ui_cinematics") == 0 ) {
1025 UI_CinematicsMenu_f();
1026 return qtrue;
1027 }
1028
1029 if ( Q_stricmp (cmd, "ui_teamOrders") == 0 ) {
1030 UI_TeamOrdersMenu_f();
1031 return qtrue;
1032 }
1033
1034 if ( Q_stricmp (cmd, "iamacheater") == 0 ) {
1035 UI_SPUnlock_f();
1036 return qtrue;
1037 }
1038
1039 if ( Q_stricmp (cmd, "iamamonkey") == 0 ) {
1040 UI_SPUnlockMedals_f();
1041 return qtrue;
1042 }
1043
1044 if ( Q_stricmp (cmd, "ui_cdkey") == 0 ) {
1045 UI_CDKeyMenu_f();
1046 return qtrue;
1047 }
1048
1049 if ( Q_stricmp (cmd, "ui_mappage") == 0 ) {
1050 mappage.pagenumber = atoi(UI_Argv( 1 ));
1051 Q_strncpyz(mappage.mapname[0],UI_Argv(2),32);
1052 Q_strncpyz(mappage.mapname[1],UI_Argv(3),32);
1053 Q_strncpyz(mappage.mapname[2],UI_Argv(4),32);
1054 Q_strncpyz(mappage.mapname[3],UI_Argv(5),32);
1055 Q_strncpyz(mappage.mapname[4],UI_Argv(6),32);
1056 Q_strncpyz(mappage.mapname[5],UI_Argv(7),32);
1057 Q_strncpyz(mappage.mapname[6],UI_Argv(8),32);
1058 Q_strncpyz(mappage.mapname[7],UI_Argv(9),32);
1059 Q_strncpyz(mappage.mapname[8],UI_Argv(10),32);
1060 Q_strncpyz(mappage.mapname[9],UI_Argv(11),32);
1061
1062 UI_VoteMapMenuInternal();
1063 return qtrue;
1064 }
1065
1066 return qfalse;
1067 }
1068
1069 /*
1070 =================
1071 UI_Shutdown
1072 =================
1073 */
1074 void UI_Shutdown( void ) {
1075 }
1076
1077 /*
1078 =================
1079 UI_Init
1080 =================
1081 */
1082 void UI_Init( void ) {
1083 UI_RegisterCvars();
1084
1085 UI_InitGameinfo();
1086
1087 // cache redundant calulations
1088 trap_GetGlconfig( &uis.glconfig );
1089
1090 // for 640x480 virtualized screen
1091 uis.xscale = uis.glconfig.vidWidth * (1.0/640.0);
1092 uis.yscale = uis.glconfig.vidHeight * (1.0/480.0);
1093 if ( uis.glconfig.vidWidth * 480 > uis.glconfig.vidHeight * 640 ) {
1094 // wide screen
1095 uis.bias = 0.5 * ( uis.glconfig.vidWidth - ( uis.glconfig.vidHeight * (640.0/480.0) ) );
1096 uis.xscale = uis.yscale;
1097 }
1098 else {
1099 // no wide screen
1100 uis.bias = 0;
1101 }
1102
1103 // initialize the menu system
1104 Menu_Cache();
1105
1106 uis.activemenu = NULL;
1107 uis.menusp = 0;
1108 }
1109
1110 /*
1111 ================
1112 UI_AdjustFrom640
1113
1114 Adjusted for resolution and screen aspect ratio
1115 ================
1116 */
1117 void UI_AdjustFrom640( float *x, float *y, float *w, float *h ) {
1118 // expect valid pointers
1119 *x = *x * uis.xscale + uis.bias;
1120 *y *= uis.yscale;
1121 *w *= uis.xscale;
1122 *h *= uis.yscale;
1123 }
1124
1125 void UI_DrawNamedPic( float x, float y, float width, float height, const char *picname ) {
1126 qhandle_t hShader;
1127
1128 hShader = trap_R_RegisterShaderNoMip( picname );
1129 UI_AdjustFrom640( &x, &y, &width, &height );
1130 trap_R_DrawStretchPic( x, y, width, height, 0, 0, 1, 1, hShader );
1131 }
1132
1133 void UI_DrawHandlePic( float x, float y, float w, float h, qhandle_t hShader ) {
1134 float s0;
1135 float s1;
1136 float t0;
1137 float t1;
1138
1139 if( w < 0 ) { // flip about vertical
1140 w = -w;
1141 s0 = 1;
1142 s1 = 0;
1143 }
1144 else {
1145 s0 = 0;
1146 s1 = 1;
1147 }
1148
1149 if( h < 0 ) { // flip about horizontal
1150 h = -h;
1151 t0 = 1;
1152 t1 = 0;
1153 }
1154 else {
1155 t0 = 0;
1156 t1 = 1;
1157 }
1158
1159 UI_AdjustFrom640( &x, &y, &w, &h );
1160 trap_R_DrawStretchPic( x, y, w, h, s0, t0, s1, t1, hShader );
1161 }
1162
1163 /*
1164 ================
1165 UI_FillRect
1166
1167 Coordinates are 640*480 virtual values
1168 =================
1169 */
1170 void UI_FillRect( float x, float y, float width, float height, const float *color ) {
1171 trap_R_SetColor( color );
1172
1173 UI_AdjustFrom640( &x, &y, &width, &height );
1174 trap_R_DrawStretchPic( x, y, width, height, 0, 0, 0, 0, uis.whiteShader );
1175
1176 trap_R_SetColor( NULL );
1177 }
1178
1179 /*
1180 ================
1181 UI_DrawRect
1182
1183 Coordinates are 640*480 virtual values
1184 =================
1185 */
1186 void UI_DrawRect( float x, float y, float width, float height, const float *color ) {
1187 trap_R_SetColor( color );
1188
1189 UI_AdjustFrom640( &x, &y, &width, &height );
1190
1191 trap_R_DrawStretchPic( x, y, width, 1, 0, 0, 0, 0, uis.whiteShader );
1192 trap_R_DrawStretchPic( x, y, 1, height, 0, 0, 0, 0, uis.whiteShader );
1193 trap_R_DrawStretchPic( x, y + height - 1, width, 1, 0, 0, 0, 0, uis.whiteShader );
1194 trap_R_DrawStretchPic( x + width - 1, y, 1, height, 0, 0, 0, 0, uis.whiteShader );
1195
1196 trap_R_SetColor( NULL );
1197 }
1198
1199 void UI_SetColor( const float *rgba ) {
1200 trap_R_SetColor( rgba );
1201 }
1202
1203 void UI_UpdateScreen( void ) {
1204 trap_UpdateScreen();
1205 }
1206
1207 /*
1208 =================
1209 UI_Refresh
1210 =================
1211 */
1212 void UI_Refresh( int realtime )
1213 {
1214 uis.frametime = realtime - uis.realtime;
1215 uis.realtime = realtime;
1216
1217 if ( !( trap_Key_GetCatcher() & KEYCATCH_UI ) ) {
1218 return;
1219 }
1220
1221 UI_UpdateCvars();
1222
1223 if ( uis.activemenu )
1224 {
1225 if (uis.activemenu->fullscreen)
1226 {
1227 // draw the background
1228 if( uis.activemenu->showlogo ) {
1229 UI_DrawHandlePic( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, uis.menuBackShader );
1230 }
1231 else {
1232 UI_DrawHandlePic( 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, uis.menuBackNoLogoShader );
1233 }
1234 }
1235
1236 if (uis.activemenu->draw)
1237 uis.activemenu->draw();
1238 else
1239 Menu_Draw( uis.activemenu );
1240
1241 if( uis.firstdraw ) {
1242 UI_MouseEvent( 0, 0 );
1243 uis.firstdraw = qfalse;
1244 }
1245 }
1246
1247 // draw cursor
1248 UI_SetColor( NULL );
1249 UI_DrawHandlePic( uis.cursorx-16, uis.cursory-16, 32, 32, uis.cursor);
1250
1251 #ifndef NDEBUG
1252 if (uis.debug)
1253 {
1254 // cursor coordinates
1255 UI_DrawString( 0, 0, va("(%d,%d)",uis.cursorx,uis.cursory), UI_LEFT|UI_SMALLFONT, colorRed );
1256 }
1257 #endif
1258
1259 // delay playing the enter sound until after the
1260 // menu has been drawn, to avoid delay while
1261 // caching images
1262 if (m_entersound)
1263 {
1264 trap_S_StartLocalSound( menu_in_sound, CHAN_LOCAL_SOUND );
1265 m_entersound = qfalse;
1266 }
1267 }
1268
1269 void UI_DrawTextBox (int x, int y, int width, int lines)
1270 {
1271 UI_FillRect( x + BIGCHAR_WIDTH/2, y + BIGCHAR_HEIGHT/2, ( width + 1 ) * BIGCHAR_WIDTH, ( lines + 1 ) * BIGCHAR_HEIGHT, colorBlack );
1272 UI_DrawRect( x + BIGCHAR_WIDTH/2, y + BIGCHAR_HEIGHT/2, ( width + 1 ) * BIGCHAR_WIDTH, ( lines + 1 ) * BIGCHAR_HEIGHT, colorWhite );
1273 }
1274
1275 qboolean UI_CursorInRect (int x, int y, int width, int height)
1276 {
1277 if (uis.cursorx < x ||
1278 uis.cursory < y ||
1279 uis.cursorx > x+width ||
1280 uis.cursory > y+height)
1281 return qfalse;
1282
1283 return qtrue;
1284 }