1 /*
2 ===========================================================================
3 Copyright (C) 1999 - 2005, Id Software, Inc.
4 Copyright (C) 2000 - 2013, Raven Software, Inc.
5 Copyright (C) 2001 - 2013, Activision, Inc.
6 Copyright (C) 2013 - 2015, OpenJK contributors
7
8 This file is part of the OpenJK source code.
9
10 OpenJK is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License version 2 as
12 published by the Free Software Foundation.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 ===========================================================================
22 */
23
24 /**********************************************************************
25 UI_ATOMS.C
26
27 User interface building blocks and support functions.
28 **********************************************************************/
29 #include "ui_local.h"
30
31 #define NUM_UI_ARGSTRS (4)
32 #define UI_ARGSTR_MASK (NUM_UI_ARGSTRS-1)
33 static char tempArgStrs[NUM_UI_ARGSTRS][MAX_STRING_CHARS];
34
UI_Argv(int arg)35 static char *UI_Argv( int arg ) {
36 static int index=0;
37 char *s = tempArgStrs[index++ & UI_ARGSTR_MASK];
38 trap->Cmd_Argv( arg, s, MAX_STRING_CHARS );
39 return s;
40 }
41
42 #define NUM_UI_CVARSTRS (4)
43 #define UI_CVARSTR_MASK (NUM_UI_CVARSTRS-1)
44 static char tempCvarStrs[NUM_UI_CVARSTRS][MAX_CVAR_VALUE_STRING];
45
UI_Cvar_VariableString(const char * name)46 char *UI_Cvar_VariableString( const char *name ) {
47 static int index=0;
48 char *s = tempCvarStrs[index++ & UI_ARGSTR_MASK];
49 trap->Cvar_VariableStringBuffer( name, s, MAX_CVAR_VALUE_STRING );
50 return s;
51 }
52
UI_Cache_f(void)53 static void UI_Cache_f( void ) {
54 Display_CacheAll();
55 if ( trap->Cmd_Argc() == 2 ) {
56 int i;
57 for ( i=0; i<uiInfo.q3HeadCount; i++ ) {
58 trap->Print( "model %s\n", uiInfo.q3HeadNames[i] );
59 }
60 }
61 }
62
UI_OpenMenu_f(void)63 static void UI_OpenMenu_f( void ) {
64 Menus_CloseAll();
65 if ( Menus_ActivateByName( UI_Argv( 1 ) ) )
66 trap->Key_SetCatcher( KEYCATCH_UI );
67 }
68
UI_OpenSiegeMenu_f(void)69 static void UI_OpenSiegeMenu_f( void ) {
70 if ( trap->Cvar_VariableValue( "g_gametype" ) == GT_SIEGE ) {
71 Menus_CloseAll();
72 if ( Menus_ActivateByName( UI_Argv( 1 ) ) )
73 trap->Key_SetCatcher( KEYCATCH_UI );
74 }
75 }
76
77 typedef struct consoleCommand_s {
78 const char *cmd;
79 void (*func)(void);
80 } consoleCommand_t;
81
cmdcmp(const void * a,const void * b)82 int cmdcmp( const void *a, const void *b ) {
83 return Q_stricmp( (const char *)a, ((consoleCommand_t*)b)->cmd );
84 }
85
86 static consoleCommand_t commands[] = {
87 { "ui_cache", UI_Cache_f },
88 { "ui_load", UI_Load },
89 { "ui_openmenu", UI_OpenMenu_f },
90 { "ui_opensiegemenu", UI_OpenSiegeMenu_f },
91 { "ui_report", UI_Report },
92 };
93
94 static const size_t numCommands = ARRAY_LEN( commands );
95
96 /*
97 =================
98 UI_ConsoleCommand
99
100 The string has been tokenized and can be retrieved with
101 Cmd_Argc() / Cmd_Argv()
102 =================
103 */
UI_ConsoleCommand(int realTime)104 qboolean UI_ConsoleCommand( int realTime ) {
105 consoleCommand_t *command = NULL;
106
107 uiInfo.uiDC.frameTime = realTime - uiInfo.uiDC.realTime;
108 uiInfo.uiDC.realTime = realTime;
109
110 command = (consoleCommand_t *)Q_LinearSearch( UI_Argv( 0 ), commands, numCommands, sizeof( commands[0] ), cmdcmp );
111
112 if ( !command )
113 return qfalse;
114
115 command->func();
116 return qtrue;
117 }
118
UI_DrawHandlePic(float x,float y,float w,float h,qhandle_t hShader)119 void UI_DrawHandlePic( float x, float y, float w, float h, qhandle_t hShader ) {
120 float s0;
121 float s1;
122 float t0;
123 float t1;
124
125 if( w < 0 ) { // flip about vertical
126 w = -w;
127 s0 = 1;
128 s1 = 0;
129 }
130 else {
131 s0 = 0;
132 s1 = 1;
133 }
134
135 if( h < 0 ) { // flip about horizontal
136 h = -h;
137 t0 = 1;
138 t1 = 0;
139 }
140 else {
141 t0 = 0;
142 t1 = 1;
143 }
144
145 trap->R_DrawStretchPic( x, y, w, h, s0, t0, s1, t1, hShader );
146 }
147
148 /*
149 ================
150 UI_FillRect
151
152 Coordinates are 640*480 virtual values
153 =================
154 */
UI_FillRect(float x,float y,float width,float height,const float * color)155 void UI_FillRect( float x, float y, float width, float height, const float *color ) {
156 trap->R_SetColor( color );
157 trap->R_DrawStretchPic( x, y, width, height, 0, 0, 0, 0, uiInfo.uiDC.whiteShader );
158 trap->R_SetColor( NULL );
159 }
160
UI_DrawSides(float x,float y,float w,float h)161 void UI_DrawSides(float x, float y, float w, float h) {
162 trap->R_DrawStretchPic( x, y, 1, h, 0, 0, 0, 0, uiInfo.uiDC.whiteShader );
163 trap->R_DrawStretchPic( x + w - 1, y, 1, h, 0, 0, 0, 0, uiInfo.uiDC.whiteShader );
164 }
165
UI_DrawTopBottom(float x,float y,float w,float h)166 void UI_DrawTopBottom(float x, float y, float w, float h) {
167 trap->R_DrawStretchPic( x, y, w, 1, 0, 0, 0, 0, uiInfo.uiDC.whiteShader );
168 trap->R_DrawStretchPic( x, y + h - 1, w, 1, 0, 0, 0, 0, uiInfo.uiDC.whiteShader );
169 }
170 /*
171 ================
172 UI_DrawRect
173
174 Coordinates are 640*480 virtual values
175 =================
176 */
UI_DrawRect(float x,float y,float width,float height,const float * color)177 void UI_DrawRect( float x, float y, float width, float height, const float *color ) {
178 trap->R_SetColor( color );
179
180 UI_DrawTopBottom(x, y, width, height);
181 UI_DrawSides(x, y, width, height);
182
183 trap->R_SetColor( NULL );
184 }
185