2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/MenuUI/SnazzyUI.cpp $
15 * Code to drive the Snazzy User Interface
18 * Revision 1.3 2002/06/09 04:41:22 relnev
19 * added copyright header
21 * Revision 1.2 2002/05/07 03:16:46 theoddone33
22 * The Great Newline Fix
24 * Revision 1.1.1.1 2002/05/03 03:28:09 root
28 * 7 7/16/99 1:49p Dave
29 * 8 bit aabitmaps. yay.
31 * 6 12/18/98 1:13a Dave
32 * Rough 1024x768 support for Direct3D. Proper detection and usage through
35 * 5 11/30/98 1:07p Dave
36 * 16 bit conversion, first run.
38 * 4 10/23/98 3:51p Dave
39 * Full support for tstrings.tbl and foreign languages. All that remains
40 * is to make it active in Fred.
42 * 3 10/13/98 9:28a Dave
43 * Started neatening up freespace.h. Many variables renamed and
44 * reorganized. Added AlphaColors.[h,cpp]
46 * 2 10/07/98 10:53a Dave
49 * 1 10/07/98 10:49a Dave
51 * 57 4/09/98 5:51p Lawrance
52 * Be able to disable sounds for certain menus
54 * 56 4/08/98 10:18a John
55 * Made version and snazzy info all render as white in all screen modes.
57 * 55 3/18/98 12:11p John
58 * redid some string externalization
60 * 54 3/05/98 11:15p Hoffoss
61 * Changed non-game key checking to use game_check_key() instead of
64 * 53 2/22/98 4:17p John
65 * More string externalization classification... 190 left to go!
67 * 52 2/03/98 11:52p Lawrance
68 * call snazzy_flush() from game_flush()
70 * 51 1/20/98 2:23p Dave
71 * Removed optimized build warnings. 99% done with ingame join fixes.
73 * 50 1/10/98 12:47a Lawrance
74 * rip out hud config text drawing
76 * 49 12/23/97 5:28p Hoffoss
77 * Made enter key act the same as clicking the mouse button in main hall
80 * 48 11/19/97 8:36p Dave
81 * Removed references to MainMenu.h
83 * 47 11/11/97 4:57p Dave
84 * Put in support for single vs. multiplayer pilots. Began work on
85 * multiplayer campaign saving. Put in initial player select screen
87 * 46 9/07/97 10:06p Lawrance
88 * let snazzy code keep track of mouse status
90 * 45 8/11/97 9:48p Lawrance
91 * don't poll keyboard if not requested
93 * 44 6/11/97 1:13p John
94 * Started fixing all the text colors in the game.
96 * 43 6/05/97 10:19a Lawrance
97 * before playing a sound, ensure it is valid
99 * 42 6/05/97 1:07a Lawrance
100 * changes to support sound interface
102 * 41 4/23/97 5:19p Lawrance
103 * split up misc sounds into: gamewide, ingame, and interface
105 * 40 4/18/97 2:54p Lawrance
106 * sounds now have a default volume, when playing, pass a scaling factor
107 * not the actual volume
109 * 39 3/31/97 5:45p Lawrance
110 * supporting changes to allow multiple streamed audio files
112 * 38 3/19/97 5:53p Lawrance
113 * integrating new Misc_sounds[] array (replaces old Game_sounds
116 * 37 3/01/97 2:13p Lawrance
117 * made to work with new cfile changes
119 * 36 2/25/97 11:11a Lawrance
120 * adding more functionality needed for ship selection screen
122 * 35 2/05/97 10:35a Lawrance
123 * supporting spooled music at menus, briefings, credits etc.
125 * 34 1/22/97 11:01a John
126 * Added code to stream wav files in during menus.
128 * 33 1/20/97 7:58p John
129 * Fixed some link errors with testcode.
131 * 32 1/13/97 5:36p Lawrance
132 * integrating new Game_sounds structure for general game sounds
134 * 31 1/07/97 6:56p Lawrance
137 * 30 12/10/96 4:18p Lawrance
138 * added snazzy_menu_close() call and integrated with existing menus
140 * 29 12/09/96 2:53p Lawrance
141 * fixed bug where both the snazzy code and ui code were reading the
142 * keyboard, and the keypress from the snazzy code was being lost
144 * 28 12/08/96 1:57a Lawrance
145 * integrating hud configuration
147 * 27 11/26/96 10:13a Allender
148 * fixed code to properly get text from the region
150 * 26 11/21/96 7:14p Lawrance
151 * converted menu code to use a file (menu.tbl) to get the data for the
154 * 25 11/15/96 2:14p Lawrance
155 * improved comments, removed some unnecssary #includes and code
157 * 24 11/15/96 12:09p John
158 * Added new UI code. Made mouse not return Enter when you click it.
159 * Changed the doSnazzyUI function and names to be snazzy_menu_xxx.
161 * 23 11/13/96 4:02p Lawrance
162 * complete over-haul of the menu system and the states associated with
165 * 22 11/13/96 10:30a John
166 * Added code to call game_poll instead of key_inkey.
168 * 21 11/13/96 8:32a Lawrance
169 * streamlined menu code
171 * 20 11/12/96 12:20p John
173 * 19 11/12/96 12:20p John
176 * 18 11/11/96 4:03p Lawrance
178 * 17 11/08/96 10:00a Lawrance
180 * 16 11/06/96 8:54a Lawrance
181 * added revision templates, made more efficient
192 #include "pcxutils.h"
194 #include "optionsmenu.h" // need the #defines for the menu choices
195 #include "techmenu.h" // need the #defines for the menu choices
196 #include "trainingmenu.h" // need the #defines for the menu choices
199 #include "snazzyui.h"
201 #include "hudconfig.h"
202 #include "gamesequence.h"
206 #include "freespace.h"
207 #include "alphacolors.h"
208 #include "localize.h"
211 extern int ascii_table[];
212 extern int shifted_ascii_table[];
214 static int Snazzy_mouse_left_was_down;
218 Snazzy_mouse_left_was_down = 0;
221 void snazzy_menu_init()
228 // This function will return an identifier that matches the region of the
229 // image the mouse pointer is currently over. The function works by working
232 // 1. An image that is displayed to the player
233 // 2. A second image, not seen, which has masks for certain areas of image 1
235 // The idea is to read the mouse, and determine if the mouse pointer is currently
236 // over one of these regions. Since these regions may be many different colors,
237 // the mask is checked instead, since the regions are always a uniform color
239 // The action parameter is used to return whether the region is clicked on or simply
240 // has the mouse over it. The #defines SNAZZY_OVER and SNAZZY_CLICKED are used.
242 // The purpose of the key_in parameter is to allow the caller to determine if any
243 // keys are pressed while within the snazzy_menu_do(). It is an optional parameter.
246 int snazzy_menu_do(ubyte *data, int mask_w, int mask_h, int num_regions, MENU_REGION *regions, int *action, int poll_key, int *key)
248 int i, k, x, y, offset;
249 int choice = -1, mouse_on_choice = -1;
252 Assert(data != NULL);
253 Assert(num_regions > 0);
254 Assert(regions != NULL);
256 gr_reset_clip(); // don't remove
257 mouse_get_pos( &x, &y );
259 // boundary conditions
260 if((y > mask_h - 1) || (x > mask_w - 1)){
263 // check the pixel value under the mouse
264 offset = y * mask_w + x;
265 pixel_value = *(data + (offset));
272 k = game_check_key();
274 *key = k; // pass keypress back to caller
277 // if (mouse_down_count(MOUSE_LEFT_BUTTON) ) {
278 if ( !mouse_down(MOUSE_LEFT_BUTTON) && Snazzy_mouse_left_was_down ) {
279 if (pixel_value >= 0)
280 //nprintf(("Alan", "pixel val: %d\n", pixel_value));
281 for (i=0; i < num_regions; i++) {
282 if (pixel_value == regions[i].mask) {
283 choice = regions[i].mask;
284 if ( regions[i].click_sound != -1 ) {
285 snd_play( &Snds_iface[regions[i].click_sound], 0.0f );
293 choice = ESC_PRESSED;
298 for (i=0; i<num_regions; i++) {
299 if ( !regions[i].key )
301 if (ascii_table[k] == regions[i].key || shifted_ascii_table[k] == regions[i].key) {
302 choice = regions[i].mask;
303 if ( regions[i].click_sound != -1 ) {
304 snd_play( &Snds_iface[regions[i].click_sound], 0.0f );
314 if (pixel_value >= 0) {
315 for (i=0; i<num_regions; i++) {
316 if (pixel_value == regions[i].mask) {
317 mouse_on_choice = regions[i].mask;
323 gr_set_color_fast(&Color_white);
324 gr_set_font( FONT1 );
326 if ((mouse_on_choice >= 0) && (mouse_on_choice <= (num_regions)) && (i >=0)) {
327 gr_printf( 0x8000, 450, regions[i].text );
330 if ( mouse_down(MOUSE_LEFT_BUTTON) ){
331 Snazzy_mouse_left_was_down = 1;
333 Snazzy_mouse_left_was_down = 0;
336 if ( choice > -1 || choice == ESC_PRESSED ) {
337 *action = SNAZZY_CLICKED;
341 if ( mouse_on_choice > -1 ) {
342 *action = SNAZZY_OVER;
343 return mouse_on_choice;
349 // add_region() will set up a menu region
354 void snazzy_menu_add_region(MENU_REGION* region, char* text, int mask, int key, int click_sound)
358 strcpy(region->text, text);
359 region->click_sound = click_sound;
364 // read_menu_tbl() will parse through menu.tbl and store the different menu region data
369 void read_menu_tbl(char* menu_name, char* bkg_filename, char* mask_filename, MENU_REGION* regions, int* num_regions, int play_sound)
374 //char music_filename[128];
376 char seps[] = NOX(" ,\t");
385 fp = cfopen( NOX("menu.tbl"), "rt" );
387 Error(LOCATION, "menu.tbl could not be opened\n");
389 // close localization
396 while (cfgets(tmp_line, 132, fp)) {
397 p1 = strchr(tmp_line,'\n'); if (p1) *p1 = '\0';
398 p1 = strchr(tmp_line,';'); if (p1) *p1 = '\0';
399 p1 = p3 = strchr( tmp_line, '[' );
401 if (p3 && state == 1) {
402 // close localization
411 p2 = strchr( tmp_line, ']' );
413 if (!stricmp( ++p1, menu_name )) state = 1;
421 // parse a region line
422 p1 = strchr( tmp_line, '\"' );
424 p2 = strchr( tmp_line+1, '\"' );
426 nprintf(("Warning","Error parsing menu file\n"));
428 // close localization
434 strcpy(regions[*num_regions].text,++p1);
437 // get the tokens mask number
438 token = strtok( p2, seps );
439 regions[*num_regions].mask = atoi(token);
441 // get the hot key character
442 token = strtok( NULL, seps );
443 regions[*num_regions].key = token[0];
445 // stuff default click sound (not in menu.tbl)
447 regions[*num_regions].click_sound = SND_IFACE_MOUSE_CLICK;
449 regions[*num_regions].click_sound = -1;
452 *num_regions = *num_regions + 1;
456 // get the menu filenames
458 // Establish string and get the first token
459 token = strtok( tmp_line, seps );
462 // store the background filename
463 strcpy(bkg_filename, token);
465 // get the mask filename
466 token = strtok( NULL, seps );
467 strcpy(mask_filename, token);
474 // close localization
478 // snazzy_menu_close() is called when the menu using a snazzy interface is exited
482 void snazzy_menu_close()