]> icculus.org git repositories - taylor/freespace2.git/blob - src/menuui/snazzyui.cpp
Initial revision
[taylor/freespace2.git] / src / menuui / snazzyui.cpp
1 /*
2  * $Logfile: /Freespace2/code/MenuUI/SnazzyUI.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  *  Code to drive the Snazzy User Interface
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:09  root
11  * Initial revision
12  *
13  * 
14  * 7     7/16/99 1:49p Dave
15  * 8 bit aabitmaps. yay.
16  * 
17  * 6     12/18/98 1:13a Dave
18  * Rough 1024x768 support for Direct3D. Proper detection and usage through
19  * the launcher.
20  * 
21  * 5     11/30/98 1:07p Dave
22  * 16 bit conversion, first run.
23  * 
24  * 4     10/23/98 3:51p Dave
25  * Full support for tstrings.tbl and foreign languages. All that remains
26  * is to make it active in Fred.
27  * 
28  * 3     10/13/98 9:28a Dave
29  * Started neatening up freespace.h. Many variables renamed and
30  * reorganized. Added AlphaColors.[h,cpp]
31  * 
32  * 2     10/07/98 10:53a Dave
33  * Initial checkin.
34  * 
35  * 1     10/07/98 10:49a Dave
36  * 
37  * 57    4/09/98 5:51p Lawrance
38  * Be able to disable sounds for certain menus
39  * 
40  * 56    4/08/98 10:18a John
41  * Made version and snazzy info all render as white in all screen modes.
42  * 
43  * 55    3/18/98 12:11p John
44  * redid some string externalization
45  * 
46  * 54    3/05/98 11:15p Hoffoss
47  * Changed non-game key checking to use game_check_key() instead of
48  * game_poll().
49  * 
50  * 53    2/22/98 4:17p John
51  * More string externalization classification... 190 left to go!
52  * 
53  * 52    2/03/98 11:52p Lawrance
54  * call snazzy_flush() from game_flush()
55  * 
56  * 51    1/20/98 2:23p Dave
57  * Removed optimized build warnings. 99% done with ingame join fixes.
58  * 
59  * 50    1/10/98 12:47a Lawrance
60  * rip out hud config text drawing
61  * 
62  * 49    12/23/97 5:28p Hoffoss
63  * Made enter key act the same as clicking the mouse button in main hall
64  * screen.
65  * 
66  * 48    11/19/97 8:36p Dave
67  * Removed references to MainMenu.h
68  * 
69  * 47    11/11/97 4:57p Dave
70  * Put in support for single vs. multiplayer pilots. Began work on
71  * multiplayer campaign saving. Put in initial player select screen
72  * 
73  * 46    9/07/97 10:06p Lawrance
74  * let snazzy code keep track of mouse status
75  * 
76  * 45    8/11/97 9:48p Lawrance
77  * don't poll keyboard if not requested 
78  * 
79  * 44    6/11/97 1:13p John
80  * Started fixing all the text colors in the game.
81  * 
82  * 43    6/05/97 10:19a Lawrance
83  * before playing a sound, ensure it is valid
84  * 
85  * 42    6/05/97 1:07a Lawrance
86  * changes to support sound interface
87  * 
88  * 41    4/23/97 5:19p Lawrance
89  * split up misc sounds into: gamewide, ingame, and interface
90  * 
91  * 40    4/18/97 2:54p Lawrance
92  * sounds now have a default volume, when playing, pass a scaling factor
93  * not the actual volume
94  * 
95  * 39    3/31/97 5:45p Lawrance
96  * supporting changes to allow multiple streamed audio files
97  * 
98  * 38    3/19/97 5:53p Lawrance
99  * integrating new Misc_sounds[] array (replaces old Game_sounds
100  * structure)
101  * 
102  * 37    3/01/97 2:13p Lawrance
103  * made to work with new cfile changes
104  * 
105  * 36    2/25/97 11:11a Lawrance
106  * adding more functionality needed for ship selection screen
107  * 
108  * 35    2/05/97 10:35a Lawrance
109  * supporting spooled music at menus, briefings, credits etc.
110  * 
111  * 34    1/22/97 11:01a John
112  * Added code to stream wav files in during menus.
113  * 
114  * 33    1/20/97 7:58p John
115  * Fixed some link errors with testcode.
116  * 
117  * 32    1/13/97 5:36p Lawrance
118  * integrating new Game_sounds structure for general game sounds
119  * 
120  * 31    1/07/97 6:56p Lawrance
121  * adding sound hooks
122  * 
123  * 30    12/10/96 4:18p Lawrance
124  * added snazzy_menu_close() call and integrated with existing menus
125  * 
126  * 29    12/09/96 2:53p Lawrance
127  * fixed bug where both the snazzy code and ui code were reading the
128  * keyboard, and the keypress from the snazzy code was being lost
129  * 
130  * 28    12/08/96 1:57a Lawrance
131  * integrating hud configuration
132  * 
133  * 27    11/26/96 10:13a Allender
134  * fixed code to properly get text from the region
135  * 
136  * 26    11/21/96 7:14p Lawrance
137  * converted menu code to use a file (menu.tbl) to get the data for the
138  * menu
139  * 
140  * 25    11/15/96 2:14p Lawrance
141  * improved comments, removed some unnecssary #includes and code
142  * 
143  * 24    11/15/96 12:09p John
144  * Added new UI code.  Made mouse not return Enter when you click it.
145  * Changed the doSnazzyUI function and names to be snazzy_menu_xxx.   
146  * 
147  * 23    11/13/96 4:02p Lawrance
148  * complete over-haul of the menu system and the states associated with
149  * them
150  * 
151  * 22    11/13/96 10:30a John
152  * Added code to call game_poll instead of key_inkey.
153  * 
154  * 21    11/13/96 8:32a Lawrance
155  * streamlined menu code
156  * 
157  * 20    11/12/96 12:20p John
158  * 
159  * 19    11/12/96 12:20p John
160  * added game pol
161  * 
162  * 18    11/11/96 4:03p Lawrance
163  * 
164  * 17    11/08/96 10:00a Lawrance
165  * 
166  * 16    11/06/96 8:54a Lawrance
167  * added revision templates, made more efficient
168  *
169  * $NoKeywords: $
170  *
171 */
172
173
174 #include <stdlib.h>
175
176 #include "pstypes.h"
177 #include "2d.h"
178 #include "pcxutils.h"
179 #include "key.h"
180 #include "optionsmenu.h"        // need the #defines for the menu choices
181 #include "techmenu.h"           // need the #defines for the menu choices
182 #include "trainingmenu.h"       // need the #defines for the menu choices
183 #include "font.h"
184 #include "mouse.h"
185 #include "snazzyui.h"
186 #include "cfile.h"
187 #include "hudconfig.h"
188 #include "gamesequence.h"
189 #include "hud.h"
190 #include "sound.h"
191 #include "gamesnd.h"
192 #include "freespace.h"
193 #include "alphacolors.h"
194 #include "localize.h"
195
196
197 extern int ascii_table[];
198 extern int shifted_ascii_table[];
199
200 static int Snazzy_mouse_left_was_down;
201
202 void snazzy_flush()
203 {
204         Snazzy_mouse_left_was_down = 0;
205 }
206
207 void snazzy_menu_init()
208 {
209         game_flush();
210 }
211
212 // snazzy_menu_do()
213 //
214 // This function will return an identifier that matches the region of the
215 // image the mouse pointer is currently over.  The function works by working
216 // with two images
217 //
218 // 1. An image that is displayed to the player
219 // 2. A second image, not seen, which has masks for certain areas of image 1
220 //
221 // The idea is to read the mouse, and determine if the mouse pointer is currently
222 // over one of these regions.  Since these regions may be many different colors,
223 // the mask is checked instead, since the regions are always a uniform color
224 //
225 //      The action parameter is used to return whether the region is clicked on or simply
226 // has the mouse over it.  The #defines SNAZZY_OVER and SNAZZY_CLICKED are used.
227 //
228 // The purpose of the key_in parameter is to allow the caller to determine if any
229 // keys are pressed while within the snazzy_menu_do().  It is an optional parameter.
230 // 
231
232 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)
233 {
234         int i, k, x, y, offset;
235         int choice = -1, mouse_on_choice = -1;
236         ubyte pixel_value;
237
238         Assert(data != NULL);
239         Assert(num_regions > 0);
240         Assert(regions != NULL);
241         
242         gr_reset_clip();  // don't remove
243         mouse_get_pos( &x, &y );
244
245         // boundary conditions
246         if((y > mask_h - 1) || (x > mask_w - 1)){
247                 pixel_value = 0;
248         } else {
249                 // check the pixel value under the mouse
250                 offset = y * mask_w + x;
251                 pixel_value = *(data + (offset));
252         }
253
254         *action = -1;
255
256         k = 0;
257         if ( poll_key ) {
258                 k = game_check_key();
259                 if (key)
260                         *key = k;  // pass keypress back to caller
261         }
262
263 //      if (mouse_down_count(MOUSE_LEFT_BUTTON) )       {
264         if ( !mouse_down(MOUSE_LEFT_BUTTON) && Snazzy_mouse_left_was_down ) {
265                 if (pixel_value >= 0)
266                         //nprintf(("Alan", "pixel val: %d\n", pixel_value));
267                         for (i=0; i < num_regions; i++) {
268                                 if (pixel_value == regions[i].mask) {
269                                         choice = regions[i].mask;
270                                         if ( regions[i].click_sound != -1 ) {
271                                                 snd_play( &Snds_iface[regions[i].click_sound], 0.0f );
272                                         }
273                                 }
274                         }       // end for
275         }
276
277         switch ( k ) {
278                 case KEY_ESC:
279                         choice = ESC_PRESSED;
280                         break;
281
282                 default:
283                         if ( k )
284                                 for (i=0; i<num_regions; i++) {
285                                         if ( !regions[i].key )
286                                                 continue;
287                                         if (ascii_table[k] == regions[i].key || shifted_ascii_table[k] == regions[i].key) {
288                                                 choice = regions[i].mask;
289                                                 if ( regions[i].click_sound != -1 ) {
290                                                         snd_play( &Snds_iface[regions[i].click_sound], 0.0f );
291                                                 }
292                                         }
293                         }       // end for
294
295                         break;
296
297         } // end switch
298
299         i = -1;
300         if (pixel_value >= 0) {
301                 for (i=0; i<num_regions; i++) {
302                         if (pixel_value == regions[i].mask) {
303                                 mouse_on_choice = regions[i].mask;      
304                                 break;
305                         }
306                 }       // end for
307         }
308
309         gr_set_color_fast(&Color_white);
310         gr_set_font( FONT1 );
311
312         if ((mouse_on_choice >= 0) && (mouse_on_choice <= (num_regions)) && (i >=0)) {
313                 gr_printf( 0x8000, 450, regions[i].text );
314         }
315
316         if ( mouse_down(MOUSE_LEFT_BUTTON) ){
317                 Snazzy_mouse_left_was_down = 1;
318         } else {
319                 Snazzy_mouse_left_was_down = 0;
320         }
321         
322         if ( choice > -1 || choice == ESC_PRESSED ) {
323                 *action = SNAZZY_CLICKED;
324                 return choice;
325         }
326
327         if ( mouse_on_choice > -1 ) {
328                 *action = SNAZZY_OVER;
329                 return mouse_on_choice;
330         }
331
332         return -1;
333 }
334
335 // add_region() will set up a menu region
336 //
337 //
338 //
339
340 void snazzy_menu_add_region(MENU_REGION* region, char* text, int mask, int key, int click_sound)
341 {
342         region->mask = mask;
343         region->key = key;
344         strcpy(region->text, text);
345         region->click_sound = click_sound;
346 }
347  
348
349
350 // read_menu_tbl() will parse through menu.tbl and store the different menu region data
351 //
352 //
353 //
354
355 void read_menu_tbl(char* menu_name, char* bkg_filename, char* mask_filename, MENU_REGION* regions, int* num_regions, int play_sound)
356 {
357         CFILE* fp;
358         int state=0;
359         char* p1, *p2, *p3;
360         //char music_filename[128];
361
362         char seps[]   = NOX(" ,\t");
363         char *token;
364         char tmp_line[132];
365
366         *num_regions=0;
367
368         // open localization
369         lcl_ext_open();
370
371         fp = cfopen( NOX("menu.tbl"), "rt" );
372         if (fp == NULL) {
373                 Error(LOCATION, "menu.tbl could not be opened\n");
374
375                 // close localization
376                 lcl_ext_close();
377
378                 return;
379         }
380
381
382         while (cfgets(tmp_line, 132, fp)) {
383                 p1 = strchr(tmp_line,'\n'); if (p1) *p1 = '\0';
384                 p1 = strchr(tmp_line,';'); if (p1) *p1 = '\0';
385                 p1 = p3 = strchr( tmp_line, '[' );
386
387                 if (p3 && state == 1) { 
388                         // close localization
389                         lcl_ext_close();
390
391                         cfclose(fp);
392                         return;
393                 }
394                 
395                 if ( p1 || p3)  {
396                         if (!state)     {
397                                 p2 = strchr( tmp_line, ']' );
398                                 if (p2) *p2 = 0;
399                                 if (!stricmp( ++p1, menu_name )) state = 1;
400                         } else {
401                                 cfclose(fp);
402                                 break;
403                         }
404                 } else if (state) {
405                         
406                 
407                         // parse a region line
408                         p1 = strchr( tmp_line, '\"' );
409                         if (p1) {
410                                 p2 = strchr( tmp_line+1, '\"' );
411                                 if (!p2) {
412                                         nprintf(("Warning","Error parsing menu file\n"));
413
414                                         // close localization
415                                         lcl_ext_close();
416
417                                         return;
418                                 }
419                                 *p2 = 0;
420                                 strcpy(regions[*num_regions].text,++p1);
421                                 p2++;
422
423                                 // get the tokens mask number
424                                 token = strtok( p2, seps );
425                                 regions[*num_regions].mask = atoi(token);
426                                 
427                                 // get the hot key character
428                                 token = strtok( NULL, seps );
429                                 regions[*num_regions].key = token[0];
430
431                                 // stuff default click sound (not in menu.tbl)
432                                 if ( play_sound ) {
433                                         regions[*num_regions].click_sound = SND_IFACE_MOUSE_CLICK;
434                                 } else {
435                                         regions[*num_regions].click_sound = -1;
436                                 }
437
438                                 *num_regions = *num_regions + 1;
439
440                         }
441                                 else {
442                                 // get the menu filenames
443
444                                 // Establish string and get the first token
445                                 token = strtok( tmp_line, seps );
446                                 if ( token != NULL )
447                                 {
448                                         // store the background filename
449                                         strcpy(bkg_filename, token);
450
451                                         // get the mask filename
452                                         token = strtok( NULL, seps );
453                                         strcpy(mask_filename, token);
454                                 }
455                         }
456                 }
457         }       
458         cfclose(fp);
459         
460         // close localization
461         lcl_ext_close();
462 }
463
464 // snazzy_menu_close() is called when the menu using a snazzy interface is exited
465 //
466 //
467
468 void snazzy_menu_close()
469 {
470         game_flush();
471 }