2 * $Logfile: /Freespace2/code/Popup/Popup.cpp $
7 * Code for displaying pop-up dialog boxes
10 * Revision 1.2 2002/05/07 03:16:51 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:10 root
17 * 12 10/14/99 2:00p Jefff
18 * added include for os_poll to fix build error
20 * 11 10/14/99 10:18a Daveb
21 * Fixed incorrect CD checking problem on standalone server.
23 * 10 8/16/99 9:50a Jefff
24 * added mouseover webcursor options to user-defined popup buttons.
26 * 9 8/11/99 5:47p Jefff
27 * fixed button bitmap loading
29 * 8 8/04/99 10:53a Dave
30 * Added title to the user tips popup.
32 * 7 8/02/99 9:13p Dave
35 * 6 6/18/99 5:16p Dave
36 * Added real beam weapon lighting. Fixed beam weapon sounds. Added MOTD
37 * dialog to PXO screen.
39 * 5 6/01/99 3:52p Dave
40 * View footage screen. Fixed xstrings to not display the & symbol. Popup,
41 * dead popup, pxo find player popup, pxo private room popup.
43 * 4 2/11/99 3:08p Dave
44 * PXO refresh button. Very preliminary squad war support.
46 * 3 10/13/98 9:29a Dave
47 * Started neatening up freespace.h. Many variables renamed and
48 * reorganized. Added AlphaColors.[h,cpp]
50 * 2 10/07/98 10:53a Dave
53 * 1 10/07/98 10:51a Dave
55 * 36 5/11/98 11:39p Dave
58 * 35 3/22/98 10:59p Allender
59 * don't stop time in multiplayer when "in mission"
61 * 34 3/17/98 12:41a Lawrance
62 * Support \n's between title and body
64 * 33 3/12/98 4:11p Dave
65 * AL: Check that max popup lines isn't exceeded
67 * 32 2/22/98 12:19p John
68 * Externalized some strings
70 * 31 2/10/98 3:26p Hoffoss
71 * Don't check for 'dead key set' unless in the mission.
73 * 30 2/05/98 11:21p Lawrance
74 * move death popup to below letterbox view
76 * 29 2/05/98 11:09a Dave
77 * Fixed an ingame join bug. Fixed a read-only file problem with
78 * multiplauer file xfer.
80 * 28 2/03/98 11:52p Lawrance
81 * Don't highlight default choice if there is only one.
83 * 27 2/03/98 8:18p Dave
84 * More MT stats transfer stuff
86 * 26 1/30/98 3:05p Lawrance
87 * reposition text on death popup
89 * 25 1/29/98 6:55p Lawrance
90 * Integrate new art for the death popup
92 * 24 1/28/98 6:24p Dave
93 * Made standalone use ~8 megs less memory. Fixed multiplayer submenu
96 * 23 1/27/98 5:52p Lawrance
97 * support new "tiny" popups
99 * 22 1/27/98 5:01p Dave
100 * Put in support for leaving multiplayer games in the pause state. Fixed
101 * a popup bug which freed saved screens incorrectly. Reworked scoring
102 * kill and assist evaluation.
104 * 21 1/27/98 3:25p Hoffoss
105 * Made popups use the correct button icons for default positive and
108 * 20 1/26/98 6:28p Lawrance
109 * Use '&' meta char for underlining, change how keyboard usage works so
110 * fits better with mouse usage.
112 * 19 1/23/98 12:01a Lawrance
113 * Fix bug when in single-choice popups.
115 * 18 1/22/98 10:45p Lawrance
116 * Implment default selections and arrow key navigation.
118 * 17 1/20/98 5:52p Lawrance
119 * center popup text in X and Y directions. Support single affirmative
120 * icon special placement.
122 * 16 1/19/98 11:37p Lawrance
123 * Fixing Optimization build warnings
125 * 15 1/17/98 10:04p Lawrance
126 * fix errors in popup comments
128 * 14 1/15/98 2:52p Lawrance
129 * Fix problem with reading bogus buttons.
131 * 13 1/14/98 6:55p Dave
132 * Fixed a slew of multiplayer bugs. Made certain important popups ignore
133 * the escape character.
135 * 12 1/14/98 6:42p Hoffoss
136 * Massive changes to UI code. A lot cleaner and better now. Did all
137 * this to get the new UI_DOT_SLIDER to work properly, which the old code
138 * wasn't flexible enough to handle.
140 * 11 1/14/98 12:23p Lawrance
141 * Support for three choice popups.
143 * 10 1/13/98 5:37p Dave
144 * Reworked a lot of standalone interface code. Put in single and
145 * multiplayer popups for death sequence. Solidified multiplayer kick
148 * 9 1/13/98 4:06p Lawrance
149 * Add underline char for shortcuts.
151 * 8 1/11/98 11:14p Lawrance
152 * Don't grey out background when popup is active.
154 * 7 1/08/98 10:32a Lawrance
155 * Grey out screen when a popup appears.
157 * 6 1/02/98 9:08p Lawrance
158 * Integrated art for popups, expanded options.
160 * 5 12/30/97 4:30p Sandeep
161 * Added conditional popups
163 * 4 12/26/97 10:01p Lawrance
164 * Allow keyboard shortcuts for popup buttons
166 * 3 12/24/97 9:49p Lawrance
167 * ensure mouse gets drawn when popup menu is up
169 * 2 12/24/97 8:54p Lawrance
170 * Integrating new popup code
172 * 1 12/24/97 3:51p Lawrance
179 #include "freespace.h"
180 #include "gamesequence.h"
187 #include "animplay.h"
188 #include "contexthelp.h"
189 #include "keycontrol.h"
192 #include "alphacolors.h"
195 #define POPUP_MAX_CHOICES 3 // max number of buttons allowed on popup
197 #define POPUP_MAX_LINE_CHARS 256 // max chars of msg text allowed per line
198 #define POPUP_MAX_LINES 30 // max lines of text allowed
199 #define POPUP_MAX_CHARS 2048 // total max chars
200 #define POPUP_INPUT_MAX_CHARS 255 // max length of input string
202 #define POPUP_NOCHANGE 100
203 #define POPUP_ABORT 101
205 int Popup_max_display[GR_NUM_RESOLUTIONS] = {
210 char *Popup_slider_name[GR_NUM_RESOLUTIONS] = {
215 int Popup_slider_coords[GR_NUM_RESOLUTIONS][4] = {
224 ////////////////////////////////////////////////////////////////
225 // Internal popup flags
226 ////////////////////////////////////////////////////////////////
227 #define PF_INPUT (1<<0) // contents of the box is an inputbox and a caption
229 ////////////////////////////////////////////////////////////////
231 ////////////////////////////////////////////////////////////////
232 typedef struct popup_info
234 int nchoices; // number of choices user can pick
235 char *button_text[POPUP_MAX_CHOICES]; // button text
236 int keypress[POPUP_MAX_CHOICES]; // button keypress shortcut
237 int shortcut_index[POPUP_MAX_CHOICES]; // what char should be underlines for shortcut
238 char raw_text[POPUP_MAX_CHARS]; // the unbroken text for the popup
239 char title[POPUP_MAX_LINE_CHARS]; // title text for popup (optional)
241 char msg_lines[POPUP_MAX_LINES][POPUP_MAX_LINE_CHARS]; // lines of text in popup
242 char input_text[POPUP_INPUT_MAX_CHARS]; // input box text (if this is an inputbox popup)
243 int max_input_text_len;
244 int web_cursor_flag[POPUP_MAX_CHOICES]; // flag for using web cursor over button
247 ////////////////////////////////////////////////////////////////
248 // UI Data and constants
249 ////////////////////////////////////////////////////////////////
250 UI_WINDOW Popup_window;
251 UI_BUTTON Popup_buttons[POPUP_MAX_CHOICES]; // actual lit buttons
252 UI_BUTTON Popup_button_regions[POPUP_MAX_CHOICES]; // fake buttons used for mouse detection over text
253 UI_INPUTBOX Popup_input; // input box for the popup
254 UI_SLIDER2 Popup_slider; // if we have more text in the popup than can be displayed at once
256 // extents for message portion of popup
257 int Popup_text_coords[GR_NUM_RESOLUTIONS][4] = {
267 // offset from the first y text line value to place the centered input box
268 int Popup_input_y_offset[GR_NUM_RESOLUTIONS] = {
273 // offset from the first y text line value to start drawing text
274 int Popup_input_text_y_offset[GR_NUM_RESOLUTIONS] = {
279 typedef struct popup_background
281 char *filename; // filename for background
282 int coords[2]; // coords to draw background at
285 ////////////////////////////////////////////////////////////////
287 ////////////////////////////////////////////////////////////////
288 static int Popup_is_active=0;
289 static int Popup_should_die=0; // popup should quit during the next iteration of its loop
291 static popup_info Popup_info;
292 static int Popup_flags;
294 static int Title_coords[GR_NUM_RESOLUTIONS][5] =
312 static int Button_regions[GR_NUM_RESOLUTIONS][3][4] = {
314 {464, 232, 510, 250}, // upper right pixel of text, lower right pixel of button
315 {464, 262, 510, 279},
319 {752, 373, 806, 406}, // upper right pixel of text, lower right pixel of button
320 {752, 421, 806, 461},
325 static int Button_coords[GR_NUM_RESOLUTIONS][3][2] =
328 {474, 224}, // upper left pixel
333 {758, 358}, // upper left pixel
339 static popup_background Popup_background[GR_NUM_RESOLUTIONS][4] =
347 {"2_Pop2", 206, 158},
348 {"2_Pop2", 206, 158},
349 {"2_Pop3", 206, 158},
353 #define BUTTON_NEGATIVE 0
354 #define BUTTON_POSITIVE 1
355 #define BUTTON_GENERIC_FIRST 2
356 #define BUTTON_GENERIC_SECOND 3
357 #define BUTTON_GENERIC_THIRD 4
358 static char *Popup_button_filenames[GR_NUM_RESOLUTIONS][2][5] =
361 {"Pop_00", // negative
362 "Pop_01", // positive
363 "Pop_02", // first generic
364 "Pop_03", // second generic
365 "Pop_04"}, // third generic
367 {"Pop_00", // negative
368 "Pop_01", // positive
369 "PopD_00", // first generic
370 "PopD_01", // second generic
371 "PopD_02"}, // third generic
374 {"2_Pop_00", // negative
375 "2_Pop_01", // positive
376 "2_Pop_02", // first generic
377 "2_Pop_03", // second generic
378 "2_Pop_04"}, // third generic
380 {"2_Pop_00", // negative
381 "2_Pop_01", // positive
382 "2_PopD_00", // first generic
383 "2_PopD_01", // second generic
384 "2_PopD_02"}, // third generic
388 int Popup_running_state;
389 int Popup_default_choice; // which choice is highlighted (ie what gets choosen when enter is pressed)
391 // see if any popup buttons have been pressed
392 // exit: POPUP_NOCHANGE => no buttons pressed
393 // >=0 => button index that was pressed
394 int popup_check_buttons(popup_info *pi)
399 for ( i = 0; i < pi->nchoices; i++ ) {
400 b = &Popup_button_regions[i];
401 if ( b->pressed() ) {
405 b = &Popup_buttons[i];
406 if ( b->pressed() ) {
411 return POPUP_NOCHANGE;
414 // maybe play a sound when key up/down is pressed to switch default choice
415 void popup_play_default_change_sound(popup_info *pi)
417 if ( pi->nchoices > 1 ) {
421 // only play if mouse not currently highlighting a choice
423 for ( i = 0; i < pi->nchoices; i++ ) {
424 br = &Popup_button_regions[i];
425 b = &Popup_buttons[i];
426 if ( br->button_down() ) {
431 if ( br->button_hilighted() && !b->button_down() ) {
436 if ( b->button_hilighted() ) {
442 gamesnd_play_iface(SND_USER_SELECT);
447 // do any key processing here
448 // input: pi => data about the popup
449 // k => key that was pressed
451 // exit: 0 .. nchoices-1 => choice selected through keypress
452 // POPUP_ABORT => abort the popup
453 // POPUP_NOCHANGE => nothing happenned
454 int popup_process_keys(popup_info *pi, int k, int flags)
459 return POPUP_NOCHANGE;
462 for ( i = 0; i < pi->nchoices; i++ ) {
463 if ( pi->keypress[i] == key_to_ascii(k) ) {
464 Popup_default_choice=i;
465 Popup_buttons[i].press_button();
473 // select the current default choice
474 return Popup_default_choice;
478 // only process the escape key if this flag is not set
479 if(!(flags & PF_IGNORE_ESC)){
487 popup_play_default_change_sound(pi);
488 Popup_default_choice++;
489 if ( Popup_default_choice >= pi->nchoices ) {
490 Popup_default_choice=0;
496 case KEY_SHIFTED+KEY_TAB:
497 popup_play_default_change_sound(pi);
498 Popup_default_choice--;
499 if ( Popup_default_choice < 0 ) {
500 Popup_default_choice=pi->nchoices-1;
509 masked_k = k & ~KEY_CTRLED; // take out CTRL modifier only
510 if ( (PF_ALLOW_DEAD_KEYS) && (Game_mode & GM_IN_MISSION) ) {
511 process_set_of_keys(masked_k, Dead_key_set_size, Dead_key_set);
512 button_info_do(&Player->bi); // call functions based on status of button_info bit vectors
515 return POPUP_NOCHANGE;
518 // Split off the title and break up the body lines
519 void popup_split_lines(popup_info *pi, int flags)
521 int nlines, i, body_offset = 0;
522 int n_chars[POPUP_MAX_LINES];
523 char *p_str[POPUP_MAX_LINES];
528 nlines = split_str(pi->raw_text, 1000, n_chars, p_str, POPUP_MAX_LINES);
529 Assert(nlines >= 0 && nlines <= POPUP_MAX_LINES );
531 if ( flags & (PF_TITLE | PF_TITLE_BIG) ) {
532 // get first line out
533 strncpy(pi->title, p_str[0], n_chars[0]);
534 pi->title[n_chars[0]] = 0;
538 if ( flags & PF_BODY_BIG ) {
542 nlines = split_str(pi->raw_text, Popup_text_coords[gr_screen.res][2], n_chars, p_str, POPUP_MAX_LINES);
543 Assert(nlines >= 0 && nlines <= POPUP_MAX_LINES );
545 pi->nlines = nlines - body_offset;
547 for ( i = 0; i < pi->nlines; i++ ) {
548 Assert(n_chars[i+body_offset] < POPUP_MAX_LINE_CHARS);
549 strncpy(pi->msg_lines[i], p_str[i+body_offset], n_chars[i+body_offset]);
550 pi->msg_lines[i][n_chars[i+body_offset]] = 0;
556 // figure out what filename to use for the button icon
557 char *popup_get_button_filename(popup_info *pi, int i, int flags)
562 // check for special button texts and if found, use specialized buttons for them.
563 if ((!stricmp(pi->button_text[i], POPUP_OK + 1) || !stricmp(pi->button_text[i], POPUP_YES + 1)) && !(flags & PF_NO_SPECIAL_BUTTONS)){
564 return Popup_button_filenames[gr_screen.res][is_tiny][BUTTON_POSITIVE];
567 if ((!stricmp(pi->button_text[i], POPUP_CANCEL + 1) || !stricmp(pi->button_text[i], POPUP_NO + 1)) && !(flags & PF_NO_SPECIAL_BUTTONS)){
568 return Popup_button_filenames[gr_screen.res][is_tiny][BUTTON_NEGATIVE];
571 switch (pi->nchoices) {
576 if ( (flags & PF_USE_AFFIRMATIVE_ICON) && !(flags & PF_NO_SPECIAL_BUTTONS) ) {
577 fname = Popup_button_filenames[gr_screen.res][is_tiny][BUTTON_POSITIVE];
578 } else if ( flags & PF_USE_NEGATIVE_ICON && !(flags & PF_NO_SPECIAL_BUTTONS) ) {
579 fname = Popup_button_filenames[gr_screen.res][is_tiny][BUTTON_NEGATIVE];
581 fname = Popup_button_filenames[gr_screen.res][is_tiny][BUTTON_GENERIC_FIRST];
586 if ( flags & PF_USE_NEGATIVE_ICON && i==0 ) {
587 fname = Popup_button_filenames[gr_screen.res][is_tiny][BUTTON_NEGATIVE];
591 if ( flags & PF_USE_AFFIRMATIVE_ICON && i==1 ) {
592 fname = Popup_button_filenames[gr_screen.res][is_tiny][BUTTON_POSITIVE];
597 fname = Popup_button_filenames[gr_screen.res][is_tiny][BUTTON_GENERIC_FIRST];
599 fname = Popup_button_filenames[gr_screen.res][is_tiny][BUTTON_GENERIC_SECOND];
607 fname = Popup_button_filenames[gr_screen.res][is_tiny][BUTTON_GENERIC_FIRST];
610 fname = Popup_button_filenames[gr_screen.res][is_tiny][BUTTON_GENERIC_SECOND];
613 fname = Popup_button_filenames[gr_screen.res][is_tiny][BUTTON_GENERIC_THIRD];
622 // bogus handling function for slider (we don't need it to do anything)
623 void popup_slider_bogus()
627 // init the Popup window
628 int popup_init(popup_info *pi, int flags)
632 popup_background *pbg;
635 if(pi->nchoices == 0){
636 pbg = &Popup_background[gr_screen.res][0];
638 pbg = &Popup_background[gr_screen.res][pi->nchoices-1];
641 // anytime in single player, and multiplayer, not in mission, go ahead and stop time
642 if ( (Game_mode & GM_NORMAL) || ((Game_mode && GM_MULTIPLAYER) && !(Game_mode & GM_IN_MISSION)) ){
646 // create base window
647 Popup_window.create(pbg->coords[0], pbg->coords[1], Popup_text_coords[gr_screen.res][2]+100, Popup_text_coords[gr_screen.res][3]+50, 0);
648 Popup_window.set_foreground_bmap(pbg->filename);
651 for (i=0; i<pi->nchoices; i++) {
652 b = &Popup_buttons[i];
653 // accommodate single-choice positive icon being positioned differently
654 if ( (pi->nchoices == 1) && (flags&PF_USE_AFFIRMATIVE_ICON) ) {
655 b->create(&Popup_window, "", Button_coords[gr_screen.res][i+1][0], Button_coords[gr_screen.res][i+1][1], 30, 25, 0, 1);
657 b->create(&Popup_window, "", Button_coords[gr_screen.res][i][0], Button_coords[gr_screen.res][i][1], 30, 25, 0, 1);
660 fname = popup_get_button_filename(pi, i, flags);
661 b->set_bmaps(fname, 3, 0);
662 b->set_highlight_action(common_play_highlight_sound);
663 if ( pi->keypress[i] >= 0 ) {
664 b->set_hotkey(pi->keypress[i]);
667 // create invisible buttons to detect mouse presses... can't use mask since button region is dynamically sized
670 gr_get_string_size(&w, &h, pi->button_text[i]);
671 lx = Button_regions[gr_screen.res][i][0] - w;
672 b = &Popup_button_regions[i];
674 // accommodate single-choice positive icon being positioned differently
675 if ( (pi->nchoices == 1) && (flags&PF_USE_AFFIRMATIVE_ICON) ) {
676 b->create(&Popup_window, "", lx, Button_regions[gr_screen.res][i+1][1], Button_regions[gr_screen.res][i+1][2]-lx, Button_regions[gr_screen.res][i+1][3]-Button_regions[gr_screen.res][i+1][1], 0, 1);
678 b->create(&Popup_window, "", lx, Button_regions[gr_screen.res][i][1], Button_regions[gr_screen.res][i][2]-lx, Button_regions[gr_screen.res][i][3]-Button_regions[gr_screen.res][i][1], 0, 1);
685 if (Web_cursor_bitmap >= 0) {
686 if (flags & PF_WEB_CURSOR_1) {
687 Popup_buttons[1].set_custom_cursor_bmap(Web_cursor_bitmap);
689 if (flags & PF_WEB_CURSOR_2) {
690 Popup_buttons[2].set_custom_cursor_bmap(Web_cursor_bitmap);
694 // if this is an input popup, create and center the popup
695 if(flags & PF_INPUT){
696 Popup_input.create(&Popup_window, Popup_text_coords[gr_screen.res][0], pbg->coords[1] + Popup_input_y_offset[gr_screen.res], Popup_text_coords[gr_screen.res][2], pi->max_input_text_len, "", UI_INPUTBOX_FLAG_INVIS | UI_INPUTBOX_FLAG_ESC_CLR | UI_INPUTBOX_FLAG_ESC_FOC | UI_INPUTBOX_FLAG_KEYTHRU | UI_INPUTBOX_FLAG_TEXT_CEN);
697 Popup_input.set_focus();
700 Popup_default_choice=0;
701 Popup_should_die = 0;
703 if (flags & PF_RUN_STATE) {
704 Popup_running_state = 1;
706 Popup_running_state = 0;
709 popup_split_lines(pi, flags);
711 // create the popup slider (which we may not need to use
712 Popup_slider.create(&Popup_window, Popup_slider_coords[gr_screen.res][0], Popup_slider_coords[gr_screen.res][1], Popup_slider_coords[gr_screen.res][2], Popup_slider_coords[gr_screen.res][3], pi->nlines > Popup_max_display[gr_screen.res] ? pi->nlines - Popup_max_display[gr_screen.res] : 0,
713 Popup_slider_name[gr_screen.res], popup_slider_bogus, popup_slider_bogus, NULL);
718 // called when a popup goes away
719 void popup_close(popup_info *pi,int screen)
723 gamesnd_play_iface(SND_POPUP_DISAPPEAR); // play sound when popup disappears
725 for (i=0; i<pi->nchoices; i++ ) {
726 if ( pi->button_text[i] != NULL ) {
727 free(pi->button_text[i]);
728 pi->button_text[i] = NULL;
733 gr_free_screen(screen);
735 Popup_window.destroy();
736 anim_ignore_next_frametime(); // to avoid skips in animation since next frametime is saturated
740 Popup_running_state = 0;
742 // anytime in single player, and multiplayer, not in mission, go ahead and stop time
743 if ( (Game_mode & GM_NORMAL) || ((Game_mode && GM_MULTIPLAYER) && !(Game_mode & GM_IN_MISSION)) )
747 // set the popup text color
748 void popup_set_text_color(int flags)
750 if ( flags & PF_BODY_RED ) {
751 gr_set_color_fast(&Color_red);
755 if ( flags & PF_BODY_GREEN ) {
756 gr_set_color_fast(&Color_green);
760 if ( flags & PF_BODY_BLUE ) {
761 gr_set_color_fast(&Color_blue);
765 gr_set_color_fast(&Color_bright_blue);
768 // set the popup text color
769 void popup_set_title_color(int flags)
771 if ( flags & PF_TITLE_RED ) {
772 gr_set_color_fast(&Color_red);
776 if ( flags & PF_TITLE_GREEN ) {
777 gr_set_color_fast(&Color_green);
781 if ( flags & PF_TITLE_BLUE ) {
782 gr_set_color_fast(&Color_blue);
786 if ( flags & PF_TITLE_WHITE ) {
787 gr_set_color_fast(&Color_bright_white);
791 gr_set_color_fast(&Color_bright_blue);
794 // Draw the title centered within the popup
795 void popup_draw_title(int sy, char *line, int flags)
799 if ( flags & PF_TITLE_BIG ) {
805 gr_get_string_size(&w, &h, line);
806 sx = fl2i(Title_coords[gr_screen.res][4] - w/2.0f + 0.5f);
808 popup_set_title_color(flags);
809 gr_string(sx,sy,line);
812 // calculate the starting display index
813 int popup_calc_starting_index(popup_info *pi)
815 // we're basically ignoring any titles here.
816 if(pi->nlines <= Popup_max_display[gr_screen.res]){
820 // otherwise, we want to see what item index the slider is on
821 return Popup_slider.get_currentItem();
824 // Figure out the y-coord to start drawing the popup text. The text
825 // is centered vertically within the popup.
826 int popup_calc_starting_y(popup_info *pi, int flags)
829 int num_lines = pi->nlines > Popup_max_display[gr_screen.res] ? Popup_max_display[gr_screen.res] : pi->nlines;
831 if ( flags & (PF_TITLE | PF_TITLE_BIG) ) {
832 if ( flags & PF_TITLE_BIG ) {
837 total_h += gr_get_font_height();
840 if ( flags & PF_BODY_BIG ) {
846 total_h += num_lines * gr_get_font_height();
847 sy = fl2i((Popup_text_coords[gr_screen.res][1] + Popup_text_coords[gr_screen.res][3]/2.0f) - total_h/2.0f + 0.5f);
849 // if this is an input style box, add in some y
850 if(flags & PF_INPUT){
851 sy += Popup_input_text_y_offset[gr_screen.res];
857 // Draw the message text nicely formatted in the popup
858 void popup_draw_msg_text(popup_info *pi, int flags)
864 // figure out the starting display
865 line_index = popup_calc_starting_index(pi);
867 // figure out the starting y:
868 sy = popup_calc_starting_y(pi, flags);
870 // draw title if required
871 if ( flags & (PF_TITLE | PF_TITLE_BIG) ) {
872 popup_draw_title(sy, pi->title, flags);
873 sy += gr_get_font_height();
877 if ( flags & PF_BODY_BIG ) {
883 popup_set_text_color(flags);
885 for ( i = line_index; i < pi->nlines; i++, line_count++ ) {
886 // if we've already displayed the max # of lines
887 if(line_count >= Popup_max_display[gr_screen.res]){
891 gr_get_string_size(&w, &h, pi->msg_lines[i]);
892 sx = fl2i(Title_coords[gr_screen.res][4] - w/2.0f + 0.5f);
893 gr_string(sx, sy + line_count * h, pi->msg_lines[i]);
899 gr_set_color_fast(&Color_bright_red);
900 gr_string(Title_coords[gr_screen.res][4], sy + (Popup_max_display[gr_screen.res]) * h, XSTR("More", 459));
903 gr_set_font(FONT1); // reset back to regular font size
906 // Draw the button text nicely formatted in the popup
907 void popup_draw_button_text(popup_info *pi, int flags)
911 gr_set_color_fast(&Color_bright_blue);
913 for ( i=0; i < pi->nchoices; i++ ) {
914 gr_get_string_size(&w, &h, pi->button_text[i]);
916 if ( (pi->nchoices == 1) && (flags&PF_USE_AFFIRMATIVE_ICON) ) {
917 sx = Button_regions[gr_screen.res][i+1][0]-w;
918 sy = Button_regions[gr_screen.res][i+1][1]+4;
920 sx = Button_regions[gr_screen.res][i][0]-w;
921 sy = Button_regions[gr_screen.res][i][1]+4;
924 gr_string(sx, sy, pi->button_text[i]);
926 // figure out where to draw underline char
927 if ( pi->shortcut_index[i] > 0 ) {
928 int cut=pi->shortcut_index[i];
929 char save_char=pi->button_text[i][cut];
930 pi->button_text[i][cut] = 0;
931 gr_get_string_size(&w, &h, pi->button_text[i]);
932 pi->button_text[i][cut] = save_char;
936 if ( pi->shortcut_index[i] >= 0 ) {
937 gr_printf(sx, sy, NOX("%c"), 95);
942 // See if any of the button should change appearance based on mouse position
943 void popup_force_draw_buttons(popup_info *pi)
945 int i,mouse_is_highlighting=0;
948 for ( i = 0; i < pi->nchoices; i++ ) {
949 br = &Popup_button_regions[i];
950 b = &Popup_buttons[i];
951 if ( br->button_down() ) {
953 mouse_is_highlighting=1;
957 if ( (b->button_hilighted()) || (br->button_hilighted() && !b->button_down()) ) {
958 Popup_default_choice=i;
959 mouse_is_highlighting=1;
964 // Only if mouse is not highlighting an option, let the default choice be drawn highlighted
965 if ( (!mouse_is_highlighting) && (pi->nchoices>1) ) {
966 for ( i = 0; i < pi->nchoices; i++ ) {
967 b = &Popup_buttons[i];
968 // highlight the default choice
969 if ( i == Popup_default_choice ) {
977 // 0..nchoices-1 => choice
978 int popup_do(popup_info *pi, int flags)
980 int screen_id, choice = -1, done = 0;
982 screen_id = gr_save_screen();
984 if ( popup_init(pi, flags) == -1 ){
993 // if we were killed by a call to popup_kill_any_active(), kill the popup
994 if(Popup_should_die){
999 // if we're flagged as should be running the state underneath, then do so
1000 if(flags & PF_RUN_STATE){
1001 game_do_state(gameseq_get_state());
1003 // otherwise just run the common functions (for networking,etc)
1005 game_set_frametime(-1);
1006 game_do_state_common(gameseq_get_state(),flags & PF_NO_NETWORKING); // do stuff common to all states
1009 k = Popup_window.process(); // poll for input, handle mouse
1010 choice = popup_process_keys(pi, k, flags);
1011 if ( choice != POPUP_NOCHANGE ) {
1016 choice = popup_check_buttons(pi);
1017 if ( choice != POPUP_NOCHANGE ) {
1022 // don't draw anything
1023 if(!(flags & PF_RUN_STATE)){
1024 gr_restore_screen(screen_id);
1027 // if this is an input popup, store the input text
1028 if(flags & PF_INPUT){
1029 Popup_input.get_text(pi->input_text);
1032 Popup_window.draw();
1033 popup_force_draw_buttons(pi);
1034 popup_draw_msg_text(pi, flags);
1035 popup_draw_button_text(pi, flags);
1039 popup_close(pi,screen_id);
1043 int popup_do_with_condition(popup_info *pi, int flags, int(*condition)())
1045 int screen_id, choice = -1, done = 0;
1047 screen_id = gr_save_screen();
1048 if ( popup_init(pi, flags) == -1 )
1056 game_set_frametime(-1);
1057 game_do_state_common(gameseq_get_state()); // do stuff common to all states
1058 gr_restore_screen(screen_id);
1060 // draw one frame first
1061 Popup_window.draw();
1062 popup_force_draw_buttons(pi);
1063 popup_draw_msg_text(pi, flags);
1064 popup_draw_button_text(pi, flags);
1067 // test the condition function or process for the window
1068 if ((test = condition()) > 0) {
1072 k = Popup_window.process(); // poll for input, handle mouse
1073 choice = popup_process_keys(pi, k, flags);
1074 if ( choice != POPUP_NOCHANGE ) {
1079 choice = popup_check_buttons(pi);
1080 if ( choice != POPUP_NOCHANGE ) {
1087 popup_close(pi,screen_id);
1092 // maybe assign a keyboard shortcut to this button
1093 // input: pi => popup information so far
1094 // i => number of choice
1095 // str => string for button press
1096 void popup_maybe_assign_keypress(popup_info *pi, int n, char *str)
1098 int i,j,len=0,next_char_is_shortcut=0;
1101 pi->shortcut_index[n]=-1;
1105 pi->button_text[n] = (char*)malloc(len);
1106 memset(pi->button_text[n], 0, len);
1109 // copy chars over, watching for underline meta-char '&'
1110 for (i=0; i<len-1; i++) {
1111 if ( str[i] == '&' ) {
1112 pi->shortcut_index[n]=i;
1113 next_char_is_shortcut=1;
1115 if ( next_char_is_shortcut ) {
1116 next_char_is_shortcut=0;
1117 char first_char_string[2];
1118 first_char_string[0]=str[i];
1119 first_char_string[1]=0;
1120 strlwr(first_char_string);
1121 pi->keypress[n] = first_char_string[0];
1123 pi->button_text[n][j++]=str[i];
1128 // input: flags => flags => formatting specificatons (PF_...)
1129 // nchoices => number of choices popup has
1130 // text_1 => text for first button
1132 // text_n => text for last button
1133 // msg text => text msg for popup (can be of form "%s",pl->text)
1135 // exit: choice selected (0..nchoices-1)
1136 // will return -1 if there was an error or popup was aborted
1140 // rval = popup(0, 2, POPUP_OK, POPUP_CANCEL, "Sorry %s, try again", pl->callsign);
1141 int popup(int flags, int nchoices, ... )
1147 if ( Popup_is_active ) {
1148 Int3(); // should never happen
1152 Popup_flags = flags;
1154 Assert( nchoices > 0 && nchoices <= POPUP_MAX_CHOICES );
1155 Popup_info.nchoices = nchoices;
1157 va_start(args, nchoices );
1160 for (i=0; i<nchoices; i++ ) {
1161 s = va_arg( args, char * );
1162 Popup_info.button_text[i] = NULL;
1163 popup_maybe_assign_keypress(&Popup_info, i, s);
1167 format = va_arg( args, char * );
1168 Popup_info.raw_text[0] = 0;
1169 vsprintf(Popup_info.raw_text, format, args);
1171 Assert(strlen(Popup_info.raw_text) < POPUP_MAX_CHARS );
1173 gamesnd_play_iface(SND_POPUP_APPEAR); // play sound when popup appears
1176 Popup_is_active = 1;
1178 choice = popup_do( &Popup_info, flags );
1187 // determine if a popup is being drawn
1190 return Popup_is_active;
1193 // This function displays a popup message box and every frame it checks the condition() function
1194 // which is passed in as an argument.
1195 // If the condition() returns TRUE, the message box ends itself. This function returns whatever
1196 // the condition() did if the condition() occurred, and FALSE if the cancel button was pressed.
1197 int popup_till_condition(int (*condition)(), ...)
1204 if ( Popup_is_active ) {
1205 Int3(); // should never happen
1209 Popup_info.nchoices = 1;
1213 va_start(args, condition );
1216 s = va_arg( args, char * );
1217 Popup_info.button_text[0] = NULL;
1218 popup_maybe_assign_keypress(&Popup_info, 0, s);
1221 format = va_arg( args, char * );
1222 Popup_info.raw_text[0] = 0;
1223 vsprintf(Popup_info.raw_text, format, args);
1225 Popup_info.raw_text[POPUP_MAX_CHARS-1] = '\0';
1227 gamesnd_play_iface(SND_POPUP_APPEAR); // play sound when popup appears
1230 Popup_is_active = 1;
1232 choice = popup_do_with_condition( &Popup_info, flags, condition );
1241 // popup to return the value from an input box
1242 char *popup_input(int flags, char *caption, int max_output_len)
1244 if ( Popup_is_active ) {
1245 Int3(); // should never happen
1249 // make it an inputbox type popup
1250 Popup_flags = flags;
1251 Popup_flags |= PF_INPUT;
1253 // add a cancel button
1254 Popup_info.nchoices = 0;
1255 // popup_maybe_assign_keypress(&Popup_info, 0, "&Cancel");
1258 Assert(caption != NULL);
1259 strcpy(Popup_info.raw_text, caption);
1260 Assert(strlen(Popup_info.raw_text) < POPUP_MAX_CHARS );
1262 // set input text length
1263 if((max_output_len > POPUP_INPUT_MAX_CHARS) || (max_output_len == -1)){
1264 Popup_info.max_input_text_len = POPUP_INPUT_MAX_CHARS - 1;
1266 Popup_info.max_input_text_len = max_output_len;
1269 // zero the popup input text
1270 memset(Popup_info.input_text, 0, POPUP_INPUT_MAX_CHARS);
1272 gamesnd_play_iface(SND_POPUP_APPEAR); // play sound when popup appears
1275 Popup_is_active = 1;
1277 // if the user cancelled
1278 if(popup_do(&Popup_info, Popup_flags) == POPUP_ABORT){
1282 // otherwise return the
1283 return Popup_info.input_text;
1286 int popup_running_state()
1288 return Popup_running_state;
1291 // kill any active popup, forcing it to return -1 (similar to ESC)
1292 void popup_kill_any_active()
1294 if(Popup_is_active){
1295 Popup_should_die = 1;
1299 // change the text inside of the popup
1300 void popup_change_text(char *new_text)
1302 // copy the raw text
1303 strncpy(Popup_info.raw_text,new_text,POPUP_MAX_CHARS);
1305 // recalculate all display information
1306 popup_split_lines(&Popup_info,Popup_flags);