Quake-III-Arena

Quake III Arena GPL Source Release
Log | Files | Refs

ui_confirm.c (6765B)


      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 Foobar; 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 =======================================================================
     25 
     26 CONFIRMATION MENU
     27 
     28 =======================================================================
     29 */
     30 
     31 
     32 #include "ui_local.h"
     33 
     34 
     35 #define ART_CONFIRM_FRAME	"menu/art/cut_frame"
     36 
     37 #define ID_CONFIRM_NO		10
     38 #define ID_CONFIRM_YES		11
     39 
     40 
     41 typedef struct {
     42 	menuframework_s menu;
     43 
     44 	menutext_s		no;
     45 	menutext_s		yes;
     46 
     47 	int				slashX;
     48 	const char *	question;
     49 	void			(*draw)( void );
     50 	void			(*action)( qboolean result );
     51 	
     52 	int style;
     53 	const char **lines;
     54 } confirmMenu_t;
     55 
     56 
     57 static confirmMenu_t	s_confirm;
     58 
     59 
     60 /*
     61 =================
     62 ConfirmMenu_Event
     63 =================
     64 */
     65 static void ConfirmMenu_Event( void* ptr, int event ) {
     66 	qboolean	result;
     67 
     68 	if( event != QM_ACTIVATED ) {
     69 		return;
     70 	}
     71 
     72 	UI_PopMenu();
     73 
     74 	if( ((menucommon_s*)ptr)->id == ID_CONFIRM_NO ) {
     75 		result = qfalse;
     76 	}
     77 	else {
     78 		result = qtrue;
     79 	}
     80 
     81 	if( s_confirm.action ) {
     82 		s_confirm.action( result );
     83 	}
     84 }
     85 
     86 
     87 /*
     88 =================
     89 ConfirmMenu_Key
     90 =================
     91 */
     92 static sfxHandle_t ConfirmMenu_Key( int key ) {
     93 	switch ( key ) {
     94 	case K_KP_LEFTARROW:
     95 	case K_LEFTARROW:
     96 	case K_KP_RIGHTARROW:
     97 	case K_RIGHTARROW:
     98 		key = K_TAB;
     99 		break;
    100 
    101 	case 'n':
    102 	case 'N':
    103 		ConfirmMenu_Event( &s_confirm.no, QM_ACTIVATED );
    104 		break;
    105 
    106 	case 'y':
    107 	case 'Y':
    108 		ConfirmMenu_Event( &s_confirm.yes, QM_ACTIVATED );
    109 		break;
    110 	}
    111 
    112 	return Menu_DefaultKey( &s_confirm.menu, key );
    113 }
    114 
    115 
    116 /*
    117 =================
    118 MessaheMenu_Draw
    119 =================
    120 */
    121 static void MessageMenu_Draw( void ) {
    122 	int i,y;
    123 	
    124 	UI_DrawNamedPic( 142, 118, 359, 256, ART_CONFIRM_FRAME );
    125 	
    126 	y = 188;
    127 	for(i=0; s_confirm.lines[i]; i++)
    128 	{
    129 		UI_DrawProportionalString( 320, y, s_confirm.lines[i], s_confirm.style, color_red );
    130 		y += 18;
    131 	}
    132 
    133 	Menu_Draw( &s_confirm.menu );
    134 
    135 	if( s_confirm.draw ) {
    136 		s_confirm.draw();
    137 	}
    138 }
    139 
    140 /*
    141 =================
    142 ConfirmMenu_Draw
    143 =================
    144 */
    145 static void ConfirmMenu_Draw( void ) {
    146 	UI_DrawNamedPic( 142, 118, 359, 256, ART_CONFIRM_FRAME );
    147 	UI_DrawProportionalString( 320, 204, s_confirm.question, s_confirm.style, color_red );
    148 	UI_DrawProportionalString( s_confirm.slashX, 265, "/", UI_LEFT|UI_INVERSE, color_red );
    149 
    150 	Menu_Draw( &s_confirm.menu );
    151 
    152 	if( s_confirm.draw ) {
    153 		s_confirm.draw();
    154 	}
    155 }
    156 
    157 
    158 /*
    159 =================
    160 ConfirmMenu_Cache
    161 =================
    162 */
    163 void ConfirmMenu_Cache( void ) {
    164 	trap_R_RegisterShaderNoMip( ART_CONFIRM_FRAME );
    165 }
    166 
    167 
    168 /*
    169 =================
    170 UI_ConfirmMenu_Stlye
    171 =================
    172 */
    173 void UI_ConfirmMenu_Style( const char *question, int style, void (*draw)( void ), void (*action)( qboolean result ) ) {
    174 	uiClientState_t	cstate;
    175 	int	n1, n2, n3;
    176 	int	l1, l2, l3;
    177 
    178 	// zero set all our globals
    179 	memset( &s_confirm, 0, sizeof(s_confirm) );
    180 
    181 	ConfirmMenu_Cache();
    182 
    183 	n1 = UI_ProportionalStringWidth( "YES/NO" );
    184 	n2 = UI_ProportionalStringWidth( "YES" ) + PROP_GAP_WIDTH;
    185 	n3 = UI_ProportionalStringWidth( "/" )  + PROP_GAP_WIDTH;
    186 	l1 = 320 - ( n1 / 2 );
    187 	l2 = l1 + n2;
    188 	l3 = l2 + n3;
    189 	s_confirm.slashX = l2;
    190 
    191 	s_confirm.question = question;
    192 	s_confirm.draw = draw;
    193 	s_confirm.action = action;
    194 	s_confirm.style = style;
    195 
    196 	s_confirm.menu.draw       = ConfirmMenu_Draw;
    197 	s_confirm.menu.key        = ConfirmMenu_Key;
    198 	s_confirm.menu.wrapAround = qtrue;
    199 
    200 	trap_GetClientState( &cstate );
    201 	if ( cstate.connState >= CA_CONNECTED ) {
    202 		s_confirm.menu.fullscreen = qfalse;
    203 	}
    204 	else {
    205 		s_confirm.menu.fullscreen = qtrue;
    206 	}
    207 
    208 	s_confirm.yes.generic.type		= MTYPE_PTEXT;      
    209 	s_confirm.yes.generic.flags		= QMF_LEFT_JUSTIFY|QMF_PULSEIFFOCUS; 
    210 	s_confirm.yes.generic.callback	= ConfirmMenu_Event;
    211 	s_confirm.yes.generic.id		= ID_CONFIRM_YES;
    212 	s_confirm.yes.generic.x			= l1;
    213 	s_confirm.yes.generic.y			= 264;
    214 	s_confirm.yes.string			= "YES";
    215 	s_confirm.yes.color				= color_red;
    216 	s_confirm.yes.style				= UI_LEFT;
    217 
    218 	s_confirm.no.generic.type		= MTYPE_PTEXT;      
    219 	s_confirm.no.generic.flags		= QMF_LEFT_JUSTIFY|QMF_PULSEIFFOCUS; 
    220 	s_confirm.no.generic.callback	= ConfirmMenu_Event;
    221 	s_confirm.no.generic.id			= ID_CONFIRM_NO;
    222 	s_confirm.no.generic.x		    = l3;
    223 	s_confirm.no.generic.y		    = 264;
    224 	s_confirm.no.string				= "NO";
    225 	s_confirm.no.color			    = color_red;
    226 	s_confirm.no.style			    = UI_LEFT;
    227 
    228 	Menu_AddItem( &s_confirm.menu,	&s_confirm.yes );             
    229 	Menu_AddItem( &s_confirm.menu,	&s_confirm.no );
    230 
    231 	UI_PushMenu( &s_confirm.menu );
    232 
    233 	Menu_SetCursorToItem( &s_confirm.menu, &s_confirm.no );
    234 }
    235 
    236 /*
    237 =================
    238 UI_ConfirmMenu
    239 =================
    240 */
    241 void UI_ConfirmMenu( const char *question, void (*draw)( void ), void (*action)( qboolean result ) ) {
    242 	UI_ConfirmMenu_Style(question, UI_CENTER|UI_INVERSE, draw, action);
    243 }
    244 
    245 /*
    246 =================
    247 UI_Message
    248 hacked over from Confirm stuff
    249 =================
    250 */
    251 void UI_Message( const char **lines ) {
    252 	uiClientState_t	cstate;
    253 	int n1, l1;
    254 	
    255 	// zero set all our globals
    256 	memset( &s_confirm, 0, sizeof(s_confirm) );
    257 
    258 	ConfirmMenu_Cache();
    259 
    260 	n1 = UI_ProportionalStringWidth( "OK" );
    261 	l1 = 320 - ( n1 / 2 );
    262 	
    263 	s_confirm.lines = lines;
    264 	s_confirm.style = UI_CENTER|UI_INVERSE|UI_SMALLFONT;
    265 
    266 	s_confirm.menu.draw       = MessageMenu_Draw;
    267 	s_confirm.menu.key        = ConfirmMenu_Key;
    268 	s_confirm.menu.wrapAround = qtrue;
    269 	
    270 	trap_GetClientState( &cstate );
    271 	if ( cstate.connState >= CA_CONNECTED ) {
    272 		s_confirm.menu.fullscreen = qfalse;
    273 	}
    274 	else {
    275 		s_confirm.menu.fullscreen = qtrue;
    276 	}
    277 
    278 	s_confirm.yes.generic.type		= MTYPE_PTEXT;      
    279 	s_confirm.yes.generic.flags		= QMF_LEFT_JUSTIFY|QMF_PULSEIFFOCUS; 
    280 	s_confirm.yes.generic.callback	= ConfirmMenu_Event;
    281 	s_confirm.yes.generic.id		= ID_CONFIRM_YES;
    282 	s_confirm.yes.generic.x			= l1;
    283 	s_confirm.yes.generic.y			= 280;
    284 	s_confirm.yes.string			= "OK";
    285 	s_confirm.yes.color				= color_red;
    286 	s_confirm.yes.style				= UI_LEFT;
    287 
    288 	Menu_AddItem( &s_confirm.menu,	&s_confirm.yes );
    289 	
    290 	UI_PushMenu( &s_confirm.menu );
    291 
    292 	Menu_SetCursorToItem( &s_confirm.menu, &s_confirm.yes );
    293 }