2 * $Logfile: /Freespace2/code/MenuUI/SnazzyUI.cpp $
7 * Code to drive the Snazzy User Interface
10 * Revision 1.2 2002/05/07 03:16:46 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:09 root
17 * 7 7/16/99 1:49p Dave
18 * 8 bit aabitmaps. yay.
20 * 6 12/18/98 1:13a Dave
21 * Rough 1024x768 support for Direct3D. Proper detection and usage through
24 * 5 11/30/98 1:07p Dave
25 * 16 bit conversion, first run.
27 * 4 10/23/98 3:51p Dave
28 * Full support for tstrings.tbl and foreign languages. All that remains
29 * is to make it active in Fred.
31 * 3 10/13/98 9:28a Dave
32 * Started neatening up freespace.h. Many variables renamed and
33 * reorganized. Added AlphaColors.[h,cpp]
35 * 2 10/07/98 10:53a Dave
38 * 1 10/07/98 10:49a Dave
40 * 57 4/09/98 5:51p Lawrance
41 * Be able to disable sounds for certain menus
43 * 56 4/08/98 10:18a John
44 * Made version and snazzy info all render as white in all screen modes.
46 * 55 3/18/98 12:11p John
47 * redid some string externalization
49 * 54 3/05/98 11:15p Hoffoss
50 * Changed non-game key checking to use game_check_key() instead of
53 * 53 2/22/98 4:17p John
54 * More string externalization classification... 190 left to go!
56 * 52 2/03/98 11:52p Lawrance
57 * call snazzy_flush() from game_flush()
59 * 51 1/20/98 2:23p Dave
60 * Removed optimized build warnings. 99% done with ingame join fixes.
62 * 50 1/10/98 12:47a Lawrance
63 * rip out hud config text drawing
65 * 49 12/23/97 5:28p Hoffoss
66 * Made enter key act the same as clicking the mouse button in main hall
69 * 48 11/19/97 8:36p Dave
70 * Removed references to MainMenu.h
72 * 47 11/11/97 4:57p Dave
73 * Put in support for single vs. multiplayer pilots. Began work on
74 * multiplayer campaign saving. Put in initial player select screen
76 * 46 9/07/97 10:06p Lawrance
77 * let snazzy code keep track of mouse status
79 * 45 8/11/97 9:48p Lawrance
80 * don't poll keyboard if not requested
82 * 44 6/11/97 1:13p John
83 * Started fixing all the text colors in the game.
85 * 43 6/05/97 10:19a Lawrance
86 * before playing a sound, ensure it is valid
88 * 42 6/05/97 1:07a Lawrance
89 * changes to support sound interface
91 * 41 4/23/97 5:19p Lawrance
92 * split up misc sounds into: gamewide, ingame, and interface
94 * 40 4/18/97 2:54p Lawrance
95 * sounds now have a default volume, when playing, pass a scaling factor
96 * not the actual volume
98 * 39 3/31/97 5:45p Lawrance
99 * supporting changes to allow multiple streamed audio files
101 * 38 3/19/97 5:53p Lawrance
102 * integrating new Misc_sounds[] array (replaces old Game_sounds
105 * 37 3/01/97 2:13p Lawrance
106 * made to work with new cfile changes
108 * 36 2/25/97 11:11a Lawrance
109 * adding more functionality needed for ship selection screen
111 * 35 2/05/97 10:35a Lawrance
112 * supporting spooled music at menus, briefings, credits etc.
114 * 34 1/22/97 11:01a John
115 * Added code to stream wav files in during menus.
117 * 33 1/20/97 7:58p John
118 * Fixed some link errors with testcode.
120 * 32 1/13/97 5:36p Lawrance
121 * integrating new Game_sounds structure for general game sounds
123 * 31 1/07/97 6:56p Lawrance
126 * 30 12/10/96 4:18p Lawrance
127 * added snazzy_menu_close() call and integrated with existing menus
129 * 29 12/09/96 2:53p Lawrance
130 * fixed bug where both the snazzy code and ui code were reading the
131 * keyboard, and the keypress from the snazzy code was being lost
133 * 28 12/08/96 1:57a Lawrance
134 * integrating hud configuration
136 * 27 11/26/96 10:13a Allender
137 * fixed code to properly get text from the region
139 * 26 11/21/96 7:14p Lawrance
140 * converted menu code to use a file (menu.tbl) to get the data for the
143 * 25 11/15/96 2:14p Lawrance
144 * improved comments, removed some unnecssary #includes and code
146 * 24 11/15/96 12:09p John
147 * Added new UI code. Made mouse not return Enter when you click it.
148 * Changed the doSnazzyUI function and names to be snazzy_menu_xxx.
150 * 23 11/13/96 4:02p Lawrance
151 * complete over-haul of the menu system and the states associated with
154 * 22 11/13/96 10:30a John
155 * Added code to call game_poll instead of key_inkey.
157 * 21 11/13/96 8:32a Lawrance
158 * streamlined menu code
160 * 20 11/12/96 12:20p John
162 * 19 11/12/96 12:20p John
165 * 18 11/11/96 4:03p Lawrance
167 * 17 11/08/96 10:00a Lawrance
169 * 16 11/06/96 8:54a Lawrance
170 * added revision templates, made more efficient
181 #include "pcxutils.h"
183 #include "optionsmenu.h" // need the #defines for the menu choices
184 #include "techmenu.h" // need the #defines for the menu choices
185 #include "trainingmenu.h" // need the #defines for the menu choices
188 #include "snazzyui.h"
190 #include "hudconfig.h"
191 #include "gamesequence.h"
195 #include "freespace.h"
196 #include "alphacolors.h"
197 #include "localize.h"
200 extern int ascii_table[];
201 extern int shifted_ascii_table[];
203 static int Snazzy_mouse_left_was_down;
207 Snazzy_mouse_left_was_down = 0;
210 void snazzy_menu_init()
217 // This function will return an identifier that matches the region of the
218 // image the mouse pointer is currently over. The function works by working
221 // 1. An image that is displayed to the player
222 // 2. A second image, not seen, which has masks for certain areas of image 1
224 // The idea is to read the mouse, and determine if the mouse pointer is currently
225 // over one of these regions. Since these regions may be many different colors,
226 // the mask is checked instead, since the regions are always a uniform color
228 // The action parameter is used to return whether the region is clicked on or simply
229 // has the mouse over it. The #defines SNAZZY_OVER and SNAZZY_CLICKED are used.
231 // The purpose of the key_in parameter is to allow the caller to determine if any
232 // keys are pressed while within the snazzy_menu_do(). It is an optional parameter.
235 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)
237 int i, k, x, y, offset;
238 int choice = -1, mouse_on_choice = -1;
241 Assert(data != NULL);
242 Assert(num_regions > 0);
243 Assert(regions != NULL);
245 gr_reset_clip(); // don't remove
246 mouse_get_pos( &x, &y );
248 // boundary conditions
249 if((y > mask_h - 1) || (x > mask_w - 1)){
252 // check the pixel value under the mouse
253 offset = y * mask_w + x;
254 pixel_value = *(data + (offset));
261 k = game_check_key();
263 *key = k; // pass keypress back to caller
266 // if (mouse_down_count(MOUSE_LEFT_BUTTON) ) {
267 if ( !mouse_down(MOUSE_LEFT_BUTTON) && Snazzy_mouse_left_was_down ) {
268 if (pixel_value >= 0)
269 //nprintf(("Alan", "pixel val: %d\n", pixel_value));
270 for (i=0; i < num_regions; i++) {
271 if (pixel_value == regions[i].mask) {
272 choice = regions[i].mask;
273 if ( regions[i].click_sound != -1 ) {
274 snd_play( &Snds_iface[regions[i].click_sound], 0.0f );
282 choice = ESC_PRESSED;
287 for (i=0; i<num_regions; i++) {
288 if ( !regions[i].key )
290 if (ascii_table[k] == regions[i].key || shifted_ascii_table[k] == regions[i].key) {
291 choice = regions[i].mask;
292 if ( regions[i].click_sound != -1 ) {
293 snd_play( &Snds_iface[regions[i].click_sound], 0.0f );
303 if (pixel_value >= 0) {
304 for (i=0; i<num_regions; i++) {
305 if (pixel_value == regions[i].mask) {
306 mouse_on_choice = regions[i].mask;
312 gr_set_color_fast(&Color_white);
313 gr_set_font( FONT1 );
315 if ((mouse_on_choice >= 0) && (mouse_on_choice <= (num_regions)) && (i >=0)) {
316 gr_printf( 0x8000, 450, regions[i].text );
319 if ( mouse_down(MOUSE_LEFT_BUTTON) ){
320 Snazzy_mouse_left_was_down = 1;
322 Snazzy_mouse_left_was_down = 0;
325 if ( choice > -1 || choice == ESC_PRESSED ) {
326 *action = SNAZZY_CLICKED;
330 if ( mouse_on_choice > -1 ) {
331 *action = SNAZZY_OVER;
332 return mouse_on_choice;
338 // add_region() will set up a menu region
343 void snazzy_menu_add_region(MENU_REGION* region, char* text, int mask, int key, int click_sound)
347 strcpy(region->text, text);
348 region->click_sound = click_sound;
353 // read_menu_tbl() will parse through menu.tbl and store the different menu region data
358 void read_menu_tbl(char* menu_name, char* bkg_filename, char* mask_filename, MENU_REGION* regions, int* num_regions, int play_sound)
363 //char music_filename[128];
365 char seps[] = NOX(" ,\t");
374 fp = cfopen( NOX("menu.tbl"), "rt" );
376 Error(LOCATION, "menu.tbl could not be opened\n");
378 // close localization
385 while (cfgets(tmp_line, 132, fp)) {
386 p1 = strchr(tmp_line,'\n'); if (p1) *p1 = '\0';
387 p1 = strchr(tmp_line,';'); if (p1) *p1 = '\0';
388 p1 = p3 = strchr( tmp_line, '[' );
390 if (p3 && state == 1) {
391 // close localization
400 p2 = strchr( tmp_line, ']' );
402 if (!stricmp( ++p1, menu_name )) state = 1;
410 // parse a region line
411 p1 = strchr( tmp_line, '\"' );
413 p2 = strchr( tmp_line+1, '\"' );
415 nprintf(("Warning","Error parsing menu file\n"));
417 // close localization
423 strcpy(regions[*num_regions].text,++p1);
426 // get the tokens mask number
427 token = strtok( p2, seps );
428 regions[*num_regions].mask = atoi(token);
430 // get the hot key character
431 token = strtok( NULL, seps );
432 regions[*num_regions].key = token[0];
434 // stuff default click sound (not in menu.tbl)
436 regions[*num_regions].click_sound = SND_IFACE_MOUSE_CLICK;
438 regions[*num_regions].click_sound = -1;
441 *num_regions = *num_regions + 1;
445 // get the menu filenames
447 // Establish string and get the first token
448 token = strtok( tmp_line, seps );
451 // store the background filename
452 strcpy(bkg_filename, token);
454 // get the mask filename
455 token = strtok( NULL, seps );
456 strcpy(mask_filename, token);
463 // close localization
467 // snazzy_menu_close() is called when the menu using a snazzy interface is exited
471 void snazzy_menu_close()