FS2_Open
Open source remastering of the Freespace 2 engine
snazzyui.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
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 the
6  * source.
7  *
8 */
9 
10 
11 
12 
13 #include "cfile/cfile.h"
14 #include "freespace2/freespace.h"
15 #include "gamesnd/gamesnd.h"
16 #include "globalincs/alphacolors.h"
17 #include "graphics/font.h"
18 #include "io/key.h"
19 #include "io/mouse.h"
20 #include "localization/localize.h"
21 #include "menuui/snazzyui.h"
22 
23 
24 extern int ascii_table[];
25 extern int shifted_ascii_table[];
26 
27 static int Snazzy_mouse_left_was_down;
28 
30 {
31  Snazzy_mouse_left_was_down = 0;
32 }
33 
35 {
36  game_flush();
37 }
38 
39 // snazzy_menu_do()
40 //
41 // This function will return an identifier that matches the region of the
42 // image the mouse pointer is currently over. The function works by working
43 // with two images
44 //
45 // 1. An image that is displayed to the player
46 // 2. A second image, not seen, which has masks for certain areas of image 1
47 //
48 // The idea is to read the mouse, and determine if the mouse pointer is currently
49 // over one of these regions. Since these regions may be many different colors,
50 // the mask is checked instead, since the regions are always a uniform color
51 //
52 // The action parameter is used to return whether the region is clicked on or simply
53 // has the mouse over it. The #defines SNAZZY_OVER and SNAZZY_CLICKED are used.
54 //
55 // The purpose of the key_in parameter is to allow the caller to determine if any
56 // keys are pressed while within the snazzy_menu_do(). It is an optional parameter.
57 //
58 
59 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)
60 {
61  int i, k, x, y, offset;
62  int choice = -1, mouse_on_choice = -1;
63  ubyte pixel_value = 0;
64 
65  Assert(data != NULL);
66  Assert(num_regions > 0);
67  Assert(regions != NULL);
68 
69  gr_reset_clip(); // don't remove
70  mouse_get_pos_unscaled( &x, &y );
71 
72  // boundary conditions
73  if((y < 0) || (y > mask_h - 1) || (x < 0) || (x > mask_w - 1)){
74  pixel_value = 255;
75  } else {
76  // check the pixel value under the mouse
77  offset = y * mask_w + x;
78  pixel_value = *(data + (offset));
79  }
80 
81  *action = -1;
82 
83  k = 0;
84  if ( poll_key ) {
85  k = game_check_key();
86  if (key)
87  *key = k; // pass keypress back to caller
88  }
89 
90 // if (mouse_down_count(MOUSE_LEFT_BUTTON) ) {
91  if ( !mouse_down(MOUSE_LEFT_BUTTON) && Snazzy_mouse_left_was_down ) {
92  //nprintf(("Alan", "pixel val: %d\n", pixel_value));
93  for (i=0; i < num_regions; i++) {
94  if (pixel_value == regions[i].mask) {
95  choice = regions[i].mask;
96  if ( regions[i].click_sound != -1 ) {
97  snd_play( &Snds_iface[regions[i].click_sound], 0.0f );
98  }
99  }
100  } // end for
101  }
102 
103  switch ( k ) {
104  case KEY_ESC:
105  choice = ESC_PRESSED;
106  break;
107 
108  default:
109  if ( k )
110  for (i=0; i<num_regions; i++) {
111  if ( !regions[i].key )
112  continue;
113  if (ascii_table[k] == regions[i].key || shifted_ascii_table[k] == regions[i].key) {
114  choice = regions[i].mask;
115  if ( regions[i].click_sound != -1 ) {
116  snd_play( &Snds_iface[regions[i].click_sound], 0.0f );
117  }
118  }
119  } // end for
120 
121  break;
122 
123  } // end switch
124 
125  for (i=0; i<num_regions; i++) {
126  if (pixel_value == regions[i].mask) {
127  mouse_on_choice = regions[i].mask;
128  break;
129  }
130  } // end for
131 
133  gr_set_font( FONT1 );
134 
135  if ((mouse_on_choice >= 0) && (mouse_on_choice <= (num_regions)) && (i >=0)) {
136  gr_printf_menu( 0x8000, 450, regions[i].text );
137  }
138 
140  Snazzy_mouse_left_was_down = 1;
141  } else {
142  Snazzy_mouse_left_was_down = 0;
143  }
144 
145  if ( choice > -1 || choice == ESC_PRESSED ) {
146  *action = SNAZZY_CLICKED;
147  return choice;
148  }
149 
150  if ( mouse_on_choice > -1 ) {
151  *action = SNAZZY_OVER;
152  return mouse_on_choice;
153  }
154 
155  return -1;
156 }
157 
158 // add_region() will set up a menu region
159 //
160 //
161 //
162 
163 void snazzy_menu_add_region(MENU_REGION* region, const char* text, int mask, int key, int click_sound)
164 {
165  region->mask = mask;
166  region->key = key;
167  strcpy_s(region->text, text);
168  region->click_sound = click_sound;
169 }
170 
171 
172 
173 // read_menu_tbl() will parse through menu.tbl and store the different menu region data
174 //
175 //
176 //
177 
178 void read_menu_tbl(char* menu_name, char* bkg_filename, char* mask_filename, MENU_REGION* regions, int* num_regions, int play_sound)
179 {
180  CFILE* fp;
181  int state=0;
182  char* p1, *p2, *p3;
183  //char music_filename[128];
184 
185  char seps[] = NOX(" ,\t");
186  char *token;
187  char tmp_line[132];
188 
189  *num_regions=0;
190 
191  fp = cfopen( NOX("menu.tbl"), "rt" );
192  if (fp == NULL) {
193  Error(LOCATION, "menu.tbl could not be opened\n");
194 
195  return;
196  }
197 
198 
199  while (cfgets(tmp_line, 132, fp)) {
200  p1 = strchr(tmp_line,'\n'); if (p1) *p1 = '\0';
201  p1 = strchr(tmp_line,';'); if (p1) *p1 = '\0';
202  p1 = p3 = strchr( tmp_line, '[' );
203 
204  if (p3 && state == 1) {
205  cfclose(fp);
206  return;
207  }
208 
209  if ( p1 || p3) {
210  if (!state) {
211  p2 = strchr( tmp_line, ']' );
212  if (p2) *p2 = 0;
213  if (!stricmp( ++p1, menu_name )) state = 1;
214  } else {
215  cfclose(fp);
216  break;
217  }
218  } else if (state) {
219 
220 
221  // parse a region line
222  p1 = strchr( tmp_line, '\"' );
223  if (p1) {
224  p2 = strchr( tmp_line+1, '\"' );
225  if (!p2) {
226  nprintf(("Warning","Error parsing menu file\n"));
227 
228  return;
229  }
230  *p2 = 0;
231  strcpy_s(regions[*num_regions].text,++p1);
232  p2++;
233 
234  // get the tokens mask number
235  token = strtok( p2, seps );
236  regions[*num_regions].mask = atoi(token);
237 
238  // get the hot key character
239  token = strtok( NULL, seps );
240  regions[*num_regions].key = token[0];
241 
242  // stuff default click sound (not in menu.tbl)
243  if ( play_sound ) {
244  regions[*num_regions].click_sound = SND_IFACE_MOUSE_CLICK;
245  } else {
246  regions[*num_regions].click_sound = -1;
247  }
248 
249  *num_regions = *num_regions + 1;
250 
251  }
252  else {
253  // get the menu filenames
254 
255  // Establish string and get the first token
256  token = strtok( tmp_line, seps );
257  if ( token != NULL )
258  {
259  // store the background filename
260  strcpy(bkg_filename, token);
261 
262  // get the mask filename
263  token = strtok( NULL, seps );
264  strcpy(mask_filename, token);
265  }
266  }
267  }
268  }
269  cfclose(fp);
270 }
271 
272 // snazzy_menu_close() is called when the menu using a snazzy interface is exited
273 //
274 //
275 
277 {
278  game_flush();
279 }
void game_flush()
Definition: fredstubs.cpp:83
int i
Definition: multi_pxo.cpp:466
#define MOUSE_LEFT_BUTTON
Definition: mouse.h:43
int ascii_table[]
Definition: key.cpp:72
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)
Definition: snazzyui.cpp:59
Assert(pm!=NULL)
GLclampf f
Definition: Glext.h:7097
#define ESC_PRESSED
Definition: snazzyui.h:16
Definition: cfile.h:28
void snazzy_flush()
Definition: snazzyui.cpp:29
void gr_set_color_fast(color *dst)
Definition: 2d.cpp:1197
int key
void snazzy_menu_add_region(MENU_REGION *region, const char *text, int mask, int key, int click_sound)
Definition: snazzyui.cpp:163
int mask
Definition: snazzyui.h:21
int mouse_get_pos_unscaled(int *xpos, int *ypos)
Definition: mouse.cpp:580
int mouse_down(int btn)
Definition: mouse.cpp:315
#define SNAZZY_OVER
Definition: snazzyui.h:28
#define gr_reset_clip
Definition: 2d.h:745
GLintptr offset
Definition: Glext.h:5497
#define cfopen(...)
Definition: cfile.h:134
#define nprintf(args)
Definition: pstypes.h:239
void snazzy_menu_close()
Definition: snazzyui.cpp:276
char * cfgets(char *buf, int n, CFILE *cfile)
Definition: cfile.cpp:1571
hull_check p1
Definition: lua.cpp:5052
int snd_play(game_snd *gs, float pan, float vol_scale, int priority, bool is_voice_msg)
Definition: sound.cpp:517
int click_sound
Definition: snazzyui.h:24
void snazzy_menu_init()
Definition: snazzyui.cpp:34
cfbp fp
Definition: cfile.cpp:1065
GLint GLint GLint GLint GLint x
Definition: Glext.h:5182
SCP_vector< game_snd > Snds_iface
Definition: gamesnd.cpp:20
unsigned char ubyte
Definition: pstypes.h:62
char * mask_filename
#define NOX(s)
Definition: pstypes.h:473
void _cdecl void void _cdecl Error(const char *filename, int line, SCP_FORMAT_STRING const char *format,...) SCP_FORMAT_STRING_ARGS(3
#define FONT1
Definition: font.h:65
color Color_white
Definition: alphacolors.cpp:32
#define KEY_ESC
Definition: key.h:124
int game_check_key()
Definition: fredstubs.cpp:65
#define LOCATION
Definition: pstypes.h:245
GLenum GLsizei GLenum GLenum const GLvoid * data
Definition: Gl.h:1509
#define SNAZZY_CLICKED
Definition: snazzyui.h:29
mouse click
Definition: gamesnd.h:287
int cfclose(CFILE *cfile)
Definition: cfile.cpp:895
void gr_set_font(int fontnum)
Definition: font.cpp:566
void _cdecl gr_printf_menu(int x, int y, const char *format,...)
Definition: font.cpp:314
GLenum GLint GLuint mask
Definition: Glext.h:5605
#define stricmp(s1, s2)
Definition: config.h:271
char text[MAX_CHAR]
Definition: snazzyui.h:23
GLint y
Definition: Gl.h:1505
void read_menu_tbl(char *menu_name, char *bkg_filename, char *mask_filename, MENU_REGION *regions, int *num_regions, int play_sound)
Definition: snazzyui.cpp:178
#define strcpy_s(...)
Definition: safe_strings.h:67
int shifted_ascii_table[]
Definition: key.cpp:83