naev 0.10.4
dialogue.c
Go to the documentation of this file.
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
20#include <stdarg.h>
21#include <stdlib.h>
22
23#include "naev.h"
26#include "dialogue.h"
27
28#include "input.h"
29#include "log.h"
30#include "conf.h"
31#include "menu.h"
32#include "ndata.h"
33#include "nstring.h"
34#include "opengl.h"
35#include "pause.h"
36#include "toolkit.h"
37
38static int dialogue_open;
39static int dlgid = 0;
41/*
42 * Custom widget scary stuff.
43 */
44typedef struct dialogue_update_s {
45 unsigned int wid;
46 int (*update)(double, void*);
47 void *data;
50 int (*event)(unsigned int, SDL_Event*, void*);
51 void *data;
52 int mx;
53 int my;
54 int w;
55 int h;
56 int last_w;
57 int last_h;
58};
59static int dialogue_custom_event( unsigned int wid, SDL_Event *event );
60
61/*
62 * Prototypes.
63 */
64/* extern */
65extern void main_loop( int update ); /* from naev.c */
66/* generic */
67static void dialogue_close( unsigned int wid, const char *str );
68static void dialogue_cancel( unsigned int wid, const char *str );
69/* dialogues */
70static glFont* dialogue_getSize( const char* title,
71 const char* msg, int* width, int* height );
72static void dialogue_YesNoClose( unsigned int wid, const char *str );
73static void dialogue_inputClose( unsigned int wid, const char *str );
74static void dialogue_choiceClose( unsigned int wid, const char *str );
75static void dialogue_listClose( unsigned int wid, const char *str );
76static void dialogue_listCancel( unsigned int wid, const char *str );
77/* secondary loop hack */
78static int toolkit_loop( int *loop_done, dialogue_update_t *du );
79
83typedef struct InputDialogue_ {
84 unsigned int input_wid;
85 int x;
86 int y;
87 int w;
88 int h;
89 void (*item_select_cb) (unsigned int wid, const char* wgtname,
90 int x, int y, int w, int h
91 );
93static void select_call_wrapper(unsigned int wid, const char* wgtname);
94
99{
100 return !!dialogue_open;
101}
102
106static void dialogue_close( unsigned int wid, const char *str )
107{
108 (void) str;
109 int *loop_done;
110 loop_done = window_getData( wid );
111 window_destroy( wid );
112 *loop_done = 1;
113}
114
118static void dialogue_cancel( unsigned int wid, const char *str )
119{
120 (void) str;
121 int *loop_done;
122 loop_done = window_getData( wid );
123 window_destroy( wid );
124 *loop_done = -1;
125}
126
132void dialogue_alert( const char *fmt, ... )
133{
134 char msg[STRMAX_SHORT];
135 va_list ap;
136 unsigned int wdw;
137 int h, done;
138
139 if (fmt == NULL) return;
140 else { /* get the message */
141 va_start(ap, fmt);
142 vsnprintf(msg, sizeof(msg), fmt, ap);
143 va_end(ap);
144 }
145
146 h = gl_printHeightRaw( &gl_defFont, 260, msg );
147
148 /* create the window */
149 wdw = window_create( "dlgAlert", _("Warning"), -1, -1, 300, 90 + h );
150 window_setData( wdw, &done );
151 window_addText( wdw, 20, -30, 260, h, 0, "txtAlert",
152 &gl_defFont, NULL, msg );
153 window_addButton( wdw, 135, 20, 50, 30, "btnOK", _("OK"),
155
156 toolkit_loop( &done, NULL );
157}
158
168static glFont* dialogue_getSize( const char* title,
169 const char* msg, int* width, int* height )
170{
171 glFont* font;
172 double w, h, d;
173 int i, titlelen, msglen;
174
175 /* Get title length. */
176 titlelen = gl_printWidthRaw( &gl_defFont, title );
177 msglen = gl_printWidthRaw( &gl_defFont, msg );
178
179 /* Try widths from 300 to 800 in 50 px increments.
180 * Each subsequent width gets an additional line, following this table:
181 *
182 * 300 px: 2 lines, 540 px total
183 * 350 px: 3 lines, 930 px total
184 * ...
185 * 800 px: 12 lines, 9600 px total
186 */
187 for (i=0; i<11; i++)
188 if (msglen < (260 + i * 50) * (2 + i))
189 break;
190
191 w = 300 + i * 50;
192 w = MAX(w, titlelen+40); /* Expand width if the title is long. */
193
194 /* Now we look at proportion. */
195 font = &gl_defFont;
196 h = gl_printHeightRaw( font, w-40, msg );
197
198 d = ((double)w/(double)h)*(3./4.); /* deformation factor. */
199 if (FABS(d) > 0.3) {
200 if (h > w)
201 w = h;
202 h = gl_printHeightRaw( font, w-40, msg );
203 }
204
205 /* Set values. */
206 (*width) = w;
207 (*height) = h;
208
209 return font;
210}
211
218void dialogue_msg( const char* caption, const char *fmt, ... )
219{
220 char msg[STRMAX];
221 va_list ap;
222
223 if (fmt == NULL) return;
224 else { /* get the message */
225 va_start(ap, fmt);
226 vsnprintf(msg, sizeof(msg), fmt, ap);
227 va_end(ap);
228 }
229
230 dialogue_msgRaw( caption, msg );
231}
232
240void dialogue_msgImg( const char* caption, const char *img, const char *fmt, ... )
241{
242 char msg[STRMAX];
243 va_list ap;
244
245 if (fmt == NULL) return;
246 else { /* get the message */
247 va_start(ap, fmt);
248 vsnprintf(msg, sizeof(msg), fmt, ap);
249 va_end(ap);
250 }
251
252 dialogue_msgImgRaw( caption, msg, img, -1, -1 );
253}
254
261void dialogue_msgRaw( const char* caption, const char *msg )
262{
263 int w,h;
264 glFont* font;
265 unsigned int msg_wid;
266 int done;
267
268 font = dialogue_getSize( caption, msg, &w, &h );
269
270 /* create the window */
271 msg_wid = window_create( "dlgMsg", caption, -1, -1, w, 110 + h );
272 window_setData( msg_wid, &done );
273 window_addText( msg_wid, 20, -40, w-40, h, 0, "txtMsg",
274 font, NULL, msg );
275 window_addButton( msg_wid, (w-50)/2, 20, 50, 30, "btnOK", _("OK"),
277
278 toolkit_loop( &done, NULL );
279}
280
290void dialogue_msgImgRaw( const char* caption, const char *msg, const char *img, int width, int height )
291{
292 int w, h, img_width, img_height;
293 glFont* font;
294 unsigned int msg_wid;
295 int done;
296 glTexture *gfx;
297 char buf[PATH_MAX];
298
299 /* Get the desired texture */
300 /* IMPORTANT : texture must not be freed here, it will be freed when the widget closes */
301 snprintf( buf, sizeof(buf), "%s%s", GFX_PATH, img );
302 gfx = gl_newImage( buf, 0 );
303 if (gfx == NULL)
304 return;
305
306 /* Find the popup's dimensions from text and image */
307 img_width = (width < 0) ? gfx->w : width;
308 img_height = (height < 0) ? gfx->h : height;
309 font = dialogue_getSize( caption, msg, &w, &h );
310 if (h < img_width) {
311 h = img_width;
312 }
313
314 /* Create the window */
315 msg_wid = window_create( "dlgMsgImg", caption, -1, -1, img_width + w, 110 + h );
316 window_setData( msg_wid, &done );
317
318 /* Add the text box */
319 window_addText( msg_wid, img_width+40, -40, w-40, h, 0, "txtMsg",
320 font, NULL, msg );
321
322 /* Add a placeholder rectangle for the image */
323 window_addRect( msg_wid, 20, -40, img_width, img_height,
324 "rctGFX", &cGrey10, 1 );
325
326 /* Actually add the texture in the rectangle */
327 window_addImage( msg_wid, 20, -40, img_width, img_height,
328 "ImgGFX", gfx, 0 );
329
330 /* Add the OK button */
331 window_addButton( msg_wid, (img_width+w -50)/2, 20, 50, 30, "btnOK", _("OK"),
333
334 toolkit_loop( &done, NULL );
335}
336
344int dialogue_YesNo( const char* caption, const char *fmt, ... )
345{
346 char msg[STRMAX];
347 va_list ap;
348
349 if (fmt == NULL) return -1;
350 else { /* get the message */
351 va_start(ap, fmt);
352 vsnprintf(msg, sizeof(msg), fmt, ap);
353 va_end(ap);
354 }
355
356 return dialogue_YesNoRaw( caption, msg );
357}
358
366int dialogue_YesNoRaw( const char* caption, const char *msg )
367{
368 unsigned int wid;
369 int w,h;
370 glFont* font;
371 int done[2];
372 char buf[STRMAX_SHORT];
373
374 font = dialogue_getSize( caption, msg, &w, &h );
375
376 /* create window */
377 snprintf( buf, sizeof(buf), "dlgYesNo%d", ++dlgid );
378 wid = window_create( buf, caption, -1, -1, w, h+110 );
379 window_setData( wid, &done );
380 /* text */
381 window_addText( wid, 20, -40, w-40, h, 0, "txtYesNo",
382 font, NULL, msg );
383 /* buttons */
384 window_addButtonKey( wid, w/2-100-10, 20, 100, 30, "btnYes", _("Yes"),
385 dialogue_YesNoClose, SDLK_y );
386 window_addButtonKey( wid, w/2+10, 20, 100, 30, "btnNo", _("No"),
387 dialogue_YesNoClose, SDLK_n );
388
389 /* tricky secondary loop */
390 done[1] = -1; /* Default to negative. */
391 toolkit_loop( done, NULL );
392
393 /* Close the dialogue. */
394 dialogue_close( wid, NULL );
395
396 /* return the result */
397 return done[1];
398}
404static void dialogue_YesNoClose( unsigned int wid, const char *str )
405{
406 int *loop_done, result;
407
408 /* store the result */
409 if (strcmp(str,"btnYes")==0)
410 result = 1;
411 else if (strcmp(str,"btnNo")==0)
412 result = 0;
413 else {
414 WARN(_("Unknown button clicked in YesNo dialogue!"));
415 result = 1;
416 }
417
418 /* set data. */
419 loop_done = window_getData( wid );
420 loop_done[0] = 1;
421 loop_done[1] = result;
422}
423
436char* dialogue_input( const char* title, int min, int max, const char *fmt, ... )
437{
438 char msg[STRMAX];
439 va_list ap;
440
442 return NULL;
443
444 if (fmt == NULL) return NULL;
445 else { /* get the message */
446 va_start(ap, fmt);
447 vsnprintf(msg, sizeof(msg), fmt, ap);
448 va_end(ap);
449 }
450
451 return dialogue_inputRaw( title, min, max, msg );
452}
453
465char* dialogue_inputRaw( const char* title, int min, int max, const char *msg )
466{
467 char *input;
468 int w, h, done;
469 glFont* font;
470
471 /* get text height */
472 font = dialogue_getSize( title, msg, &w, &h );
473 h = gl_printHeightRaw( font, w, msg );
474
475 /* create window */
476 input_dialogue.input_wid = window_create( "dlgInput", title, -1, -1, w+40, h+140 );
480 /* text */
481 window_addText( input_dialogue.input_wid, 20, -30, w, h, 0, "txtInput",
482 font, NULL, msg );
483 /* input */
484 window_addInput( input_dialogue.input_wid, 20, 20+30+10, w, 30,"inpInput", max, 1, NULL );
485 /* Illegal characters on Linux FS: : */
486 /* Illegal characters on Windows FS: < > : " / \ | ? * */
487 window_setInputFilter( input_dialogue.input_wid, "inpInput", "/:<>\"\\|?*" ); /* Remove illegal stuff. */
488 /* button */
489 window_addButton( input_dialogue.input_wid, -20, 20, 80, 30,
490 "btnClose", _("Done"), dialogue_inputClose );
491
492 /* tricky secondary loop */
493 done = 0;
494 input = NULL;
495 while ((done >= 0) && (!input ||
496 ((int)strlen(input) < min))) { /* must be longer than min */
497
498 if (input) {
499 dialogue_alert( n_(
500 "Input must be at least %d character long!",
501 "Input must be at least %d characters long!", min),
502 min );
503 free(input);
504 input = NULL;
505 }
506
507 if (toolkit_loop( &done, NULL ) != 0) { /* error in loop -> quit */
508 return NULL;
509 }
510
511 /* save the input */
512 if (done < 0)
513 input = NULL;
514 else
515 input = strdup(window_getInput(input_dialogue.input_wid, "inpInput"));
516 }
517
518 /* cleanup */
519 if (input != NULL) {
521 }
523
524 /* return the result */
525 return input;
526}
532static void dialogue_inputClose( unsigned int wid, const char *str )
533{
534 (void) str;
535 int *loop_done;
536
537 /* break the loop */
538 loop_done = window_getData( wid );
539 *loop_done = 1;
540}
541
542static int dialogue_listSelected = -1;
543static void dialogue_listCancel( unsigned int wid, const char *str )
544{
545 dialogue_listSelected = -1;
546 dialogue_cancel( wid, str );
547}
548static void dialogue_listClose( unsigned int wid, const char *str )
549{
550 dialogue_listSelected = toolkit_getListPos( wid, "lstDialogue" );
551 dialogue_close( wid, str );
552}
561static void select_call_wrapper(unsigned int wid, const char* wgtname)
562{
567}
576int dialogue_list( const char* title, char **items, int nitems, const char *fmt, ... )
577{
578 char msg[STRMAX_SHORT];
579 va_list ap;
580
581 if (input_dialogue.input_wid) return -1;
582
583 if (fmt == NULL) return -1;
584 else { /* get the message */
585 va_start(ap, fmt);
586 vsnprintf(msg, sizeof(msg), fmt, ap);
587 va_end(ap);
588 }
589
590 return dialogue_listPanelRaw( title, items, nitems, 0, 0, NULL, NULL, msg );
591}
600int dialogue_listRaw( const char* title, char **items, int nitems, const char *msg )
601{
602 if (input_dialogue.input_wid) return -1;
603 return dialogue_listPanelRaw( title, items, nitems, 0, 0, NULL, NULL, msg );
604}
621int dialogue_listPanel( const char* title, char **items, int nitems, int extrawidth,
622 int minheight, void (*add_widgets) (unsigned int wid, int x, int y, int w, int h),
623 void (*select_call) (unsigned int wid, const char* wgtname, int x, int y, int w, int h),
624 const char *fmt, ... )
625{
626 char msg[STRMAX_SHORT];
627 va_list ap;
628
630 return -1;
631
632 if (fmt == NULL)
633 return -1;
634
635 /* get the message */
636 va_start(ap, fmt);
637 vsnprintf(msg, sizeof(msg), fmt, ap);
638 va_end(ap);
639
640 return dialogue_listPanelRaw( title, items, nitems, extrawidth, minheight,
641 add_widgets, select_call, msg );
642}
659int dialogue_listPanelRaw( const char* title, char **items, int nitems, int extrawidth,
660 int minheight, void (*add_widgets) (unsigned int wid, int x, int y, int w, int h),
661 void (*select_call) (unsigned int wid, const char* wgtname, int x, int y, int w, int h),
662 const char *msg )
663{
664 int i;
665 int w, h, winw, winh;
666 glFont* font;
667 unsigned int wid;
668 int list_width, list_height;
669 int text_height, text_width;
670 int done;
671
672 if (input_dialogue.input_wid) return -1;
673
674 font = dialogue_getSize( title, msg, &text_width, &text_height );
675
676 /* Calculate size stuff. */
677 list_width = 0;
678 list_height = 0;
679 for (i=0; i<nitems; i++) {
680 list_width = MAX( list_width, gl_printWidthRaw( &gl_defFont, items[i] ) );
681 list_height += gl_defFont.h + 5;
682 }
683 list_height += 100;
684 if (list_height > 500)
685 h = (list_height*8)/10;
686 else
687 h = MAX( 300, list_height );
688
689 h = MIN( ((double)SCREEN_H*2.)/3., h );
690 w = MAX( list_width + 60, 500 );
691
692 winw = w + extrawidth;
693 winh = MAX( h, minheight );
694
695 h = winh;
696
697 /* Create the window. */
698 wid = window_create( "dlgListPanel", title, -1, -1, winw, winh );
699 window_setData( wid, &done );
700 window_addText( wid, 20, -40, w-40, text_height, 0, "txtMsg",
701 font, NULL, msg );
702 window_setAccept( wid, dialogue_listClose );
703 window_setCancel( wid, dialogue_listCancel );
704
705 if (add_widgets)
706 add_widgets(wid, w, 0, winw, winh);
707
708 if (select_call) {
709 input_dialogue.x = w;
710 input_dialogue.y = 0;
711 input_dialogue.w = winw;
712 input_dialogue.h = winh;
713 input_dialogue.item_select_cb = select_call;
714 }
715
716 /* Create the list. */
717 window_addList( wid, 20, -40-text_height-20,
718 w-40, h - (40+text_height+20) - (20+30+20),
719 "lstDialogue", items, nitems, 0, select_call_wrapper,
720 dialogue_listClose );
721
722 /* Create the buttons. */
723 window_addButton( wid, -20, 20, 120, 30,
724 "btnOK", _("OK"), dialogue_listClose );
725 window_addButton( wid, -20-120-20, 20, 120, 30,
726 "btnCancel", _("Cancel"), dialogue_listCancel );
727
728 toolkit_loop( &done, NULL );
729 /* cleanup */
730 input_dialogue.x = 0;
731 input_dialogue.y = 0;
732 input_dialogue.w = 0;
733 input_dialogue.h = 0;
735
736 return dialogue_listSelected;
737}
738static unsigned int choice_wid = 0;
739static char *choice_result;
740static int choice_nopts;
748void dialogue_makeChoice( const char *caption, const char *msg, int opts )
749{
750 int w,h;
751 glFont* font;
752
753 choice_result = NULL;
754 choice_nopts = opts;
755 font = dialogue_getSize( caption, msg, &w, &h );
756
757 /* create window */
758 choice_wid = window_create( "dlgChoice", caption, -1, -1, w, h+100+40*choice_nopts );
759 /* text */
760 window_addText( choice_wid, 20, -40, w-40, h, 0, "txtChoice",
761 font, NULL, msg );
762}
770void dialogue_addChoice( const char *caption, const char *msg, const char *opt)
771{
772 int w,h;
773
774 if (choice_nopts < 1)
775 return;
776
777 dialogue_getSize( caption, msg, &w, &h );
778
779 /* buttons. Add one for each option in the menu. */
780 window_addButton( choice_wid, w/2-125, choice_nopts*40, 250, 30, (char *) opt,
781 (char *) opt, dialogue_choiceClose );
782 choice_nopts --;
783
784}
793{
794 int done;
795 char *res;
796
797 /* tricky secondary loop */
798 window_setData( choice_wid, &done );
799 toolkit_loop( &done, NULL );
800
801 /* Save value. */
802 res = choice_result;
803 choice_result = NULL;
804
805 return res;
806}
812static void dialogue_choiceClose( unsigned int wid, const char *str )
813{
814 int *loop_done;
815
816 /* Save result. */
817 choice_result = strdup(str);
818
819 /* Finish loop. */
820 loop_done = window_getData( wid );
821 *loop_done = 1;
822
823 /* destroy the window */
824 choice_wid = 0;
825 window_destroy( wid );
826}
827
828static int dialogue_custom_event( unsigned int wid, SDL_Event *event )
829{
830 int mx, my;
831 struct dialogue_custom_data_s *cd;
832 void *data = window_getData( wid );
833 cd = (struct dialogue_custom_data_s*) data;
834
835 /* We translate mouse coords here. */
836 if ((event->type==SDL_MOUSEBUTTONDOWN) ||
837 (event->type==SDL_MOUSEBUTTONUP) ||
838 (event->type==SDL_MOUSEMOTION)) {
839 gl_windowToScreenPos( &mx, &my, event->button.x, event->button.y );
840 mx += cd->mx;
841 my += cd->my;
842 /* Ignore out of bounds. We have to implement checking here. */
843 if ((mx < 0) || (mx >= cd->w) || (my < 0) || (my >= cd->h))
844 return 0;
845 event->button.x = mx;
846 event->button.y = my;
847 }
848
849 return (*cd->event)( wid, event, cd->data );
850}
863void dialogue_custom( const char* caption, int width, int height,
864 int (*update) (double dt, void* data),
865 void (*render) (double x, double y, double w, double h, void* data),
866 int (*event) (unsigned int wid, SDL_Event* event, void* data),
867 void* data, int autofree )
868{
869 struct dialogue_custom_data_s cd;
871 unsigned int wid;
872 int done, fullscreen;
873 int wx, wy, wgtx, wgty;
874
875 fullscreen = ((width < 0) && (height < 0));
876
877 /* create the window */
878 if (fullscreen) {
879 wid = window_create( "dlgMsg", caption, -1, -1, -1, -1 );
880 window_setBorder( wid, 0 );
881 }
882 else
883 wid = window_create( "dlgMsg", caption, -1, -1, width+40, height+60 );
884 window_setData( wid, &done );
885 window_setFade( wid, NULL, 0. );
886
887 /* custom widget for all! */
888 if (fullscreen) {
889 width = gl_screen.nw;
890 height = gl_screen.nh;
891 wgtx = wgty = 0;
892 }
893 else {
894 wgtx = wgty = 20;
895 }
896 window_addCust( wid, wgtx, wgty, width, height, "cstCustom", 0, render, NULL, NULL, NULL, data );
897 if (autofree)
898 window_custAutoFreeData( wid, "cstCustom" );
899 window_custSetClipping( wid, "cstCustom", 1 );
900
901 /* set up event stuff. */
902 window_posWindow( wid, &wx, &wy );
903 window_posWidget( wid, "cstCustom", &wgtx, &wgty );
904 cd.event = event;
905 cd.data = data;
906 cd.mx = -wx-wgtx;
907 cd.my = -wy-wgty;
908 cd.w = width;
909 cd.h = height;
910 window_setData( wid, &cd );
911 if (event != NULL)
912 window_handleEvents( wid, &dialogue_custom_event );
913
914 /* dialogue stuff */
915 du.update = update;
916 du.data = data;
917 du.wid = wid;
918 toolkit_loop( &done, &du );
919}
920
928{
929 struct dialogue_custom_data_s *cd;
930 unsigned int wid = window_get( "dlgMsg" );
931 int w, h;
932 if (wid == 0)
933 return -1;
934
935 cd = (struct dialogue_custom_data_s*) window_getData( wid );
936 window_dimWindow( wid, &w, &h );
937
938 if (enable) {
939 cd->last_w = cd->w+40;
940 cd->last_h = cd->h+60;
941 window_resize( wid, -1, -1 );
942 window_moveWidget( wid, "cstCustom", 0, 0 );
943 window_resizeWidget( wid, "cstCustom", cd->last_w, cd->last_h );
944 window_move( wid, -1, -1 );
945 window_setBorder( wid, 0 );
946 }
947 else {
948 window_resize( wid, cd->last_w, cd->last_h );
949 window_moveWidget( wid, "cstCustom", 20, 20 );
950 window_move( wid, -1, -1 );
951 window_setBorder( wid, 1 );
952 }
953
954 return 0;
955}
956
964int dialogue_customResize( int width, int height )
965{
966 struct dialogue_custom_data_s *cd;
967 unsigned int wid = window_get( "dlgMsg" );
968 if (wid == 0)
969 return -1;
970 cd = (struct dialogue_custom_data_s*) window_getData( wid );
971 cd->last_w = width;
972 cd->last_h = height;
973 window_resize( wid, width+40, height+60 );
974 window_resizeWidget( wid, "cstCustom", width, height );
975 return 0;
976}
977
991static int toolkit_loop( int *loop_done, dialogue_update_t *du )
992{
993 unsigned int time_ms = SDL_GetTicks();
994 const double fps_max = (conf.fps_max > 0) ? 1./(double)conf.fps_max : fps_min;
995 int quit_game = 0;
996
997 /* Delay a toolkit iteration. */
999
1000 /* Increment dialogues. */
1001 dialogue_open++;
1002 *loop_done = 0;
1003
1004 /* Flush events so SDL_LOOPDONE doesn't propagate. */
1005 SDL_FlushEvent( SDL_LOOPDONE );
1006
1007 while (!(*loop_done) && toolkit_isOpen() && !naev_isQuit()) {
1008 SDL_Event event;
1009 unsigned int t;
1010 double dt;
1011
1012 /* Loop first so exit condition is checked before next iteration. */
1013 main_loop( 1 );
1014
1015 while (!naev_isQuit() && SDL_PollEvent(&event)) { /* event loop */
1016 if (event.type == SDL_QUIT) {
1017 if (menu_askQuit()) {
1018 naev_quit(); /* Quit is handled here */
1019 *loop_done = 1; /* Exit loop and exit game. */
1020 quit_game = 1;
1021 break;
1022 }
1023 }
1024 else if (event.type == SDL_LOOPDONE) {
1025 *loop_done = 1;
1026 SDL_PushEvent( &event ); /* Replicate event until out of all loops. */
1027 break;
1028 }
1029 else if (event.type == SDL_WINDOWEVENT &&
1030 event.window.event == SDL_WINDOWEVENT_RESIZED)
1031 naev_resize();
1032
1033 input_handle(&event); /* handles all the events and player keybinds */
1034 }
1035
1036 /* FPS Control. */
1037 /* Get elapsed. */
1038 t = SDL_GetTicks();
1039 dt = (double)(t - time_ms) / 1000.;
1040 time_ms = t;
1041 /* Sleep if necessary. */
1042 if (dt < fps_max) {
1043 double delay = fps_max - dt;
1044 SDL_Delay( (unsigned int)(delay * 1000.) );
1045 }
1046
1047 /* Update stuff. */
1048 if (du != NULL) {
1049 /* Run update. */
1050 if ((*du->update)(dt, du->data)) {
1051 /* Hack to override data. */
1052 window_setData( du->wid, loop_done );
1053 dialogue_close( du->wid, NULL );
1054 }
1055 }
1056 }
1057
1058 /* Close dialogue. */
1059 dialogue_open--;
1060 if (dialogue_open < 0)
1061 WARN(_("Dialogue counter not in sync!"));
1062
1063 return quit_game;
1064}
static char * choice_result
Definition: dialogue.c:739
static int toolkit_loop(int *loop_done, dialogue_update_t *du)
Creates a secondary loop until loop_done is set to 1 or the toolkit closes.
Definition: dialogue.c:991
char * dialogue_runChoice(void)
Run the dialog and return the clicked string.
Definition: dialogue.c:792
char * dialogue_inputRaw(const char *title, int min, int max, const char *msg)
Creates a dialogue that allows the player to write a message.
Definition: dialogue.c:465
static int dlgid
Definition: dialogue.c:39
void dialogue_alert(const char *fmt,...)
Displays an alert popup with only an ok button and a message.
Definition: dialogue.c:132
static void dialogue_choiceClose(unsigned int wid, const char *str)
Closes a choice dialogue.
Definition: dialogue.c:812
void main_loop(int update)
Split main loop from main() for secondary loop hack in toolkit.c.
Definition: naev.c:720
int dialogue_listPanelRaw(const char *title, char **items, int nitems, int extrawidth, int minheight, void(*add_widgets)(unsigned int wid, int x, int y, int w, int h), void(*select_call)(unsigned int wid, const char *wgtname, int x, int y, int w, int h), const char *msg)
Creates a list dialogue with OK and Cancel buttons, with a fixed message, as well as a small extra ar...
Definition: dialogue.c:659
char * dialogue_input(const char *title, int min, int max, const char *fmt,...)
Creates a dialogue that allows the player to write a message.
Definition: dialogue.c:436
int dialogue_listRaw(const char *title, char **items, int nitems, const char *msg)
Creates a list dialogue with OK and Cancel button.
Definition: dialogue.c:600
static void dialogue_close(unsigned int wid, const char *str)
Generic window close.
Definition: dialogue.c:106
static void select_call_wrapper(unsigned int wid, const char *wgtname)
used to pass appropriate information to the method that handles updating the extra information area i...
Definition: dialogue.c:561
int dialogue_YesNoRaw(const char *caption, const char *msg)
Runs a dialogue with both yes and no options.
Definition: dialogue.c:366
int dialogue_listPanel(const char *title, char **items, int nitems, int extrawidth, int minheight, void(*add_widgets)(unsigned int wid, int x, int y, int w, int h), void(*select_call)(unsigned int wid, const char *wgtname, int x, int y, int w, int h), const char *fmt,...)
Creates a list dialogue with OK and Cancel buttons, with a fixed message, as well as a small extra ar...
Definition: dialogue.c:621
void dialogue_custom(const char *caption, int width, int height, int(*update)(double dt, void *data), void(*render)(double x, double y, double w, double h, void *data), int(*event)(unsigned int wid, SDL_Event *event, void *data), void *data, int autofree)
Opens a custom dialogue window.
Definition: dialogue.c:863
void dialogue_msg(const char *caption, const char *fmt,...)
Opens a dialogue window with an ok button and a message.
Definition: dialogue.c:218
int dialogue_list(const char *title, char **items, int nitems, const char *fmt,...)
Creates a list dialogue with OK and Cancel button with a fixed message.
Definition: dialogue.c:576
void dialogue_addChoice(const char *caption, const char *msg, const char *opt)
Add a choice to the dialog.
Definition: dialogue.c:770
static glFont * dialogue_getSize(const char *title, const char *msg, int *width, int *height)
Gets the size needed for the dialogue.
Definition: dialogue.c:168
int dialogue_customResize(int width, int height)
Resizes a custom dialogue.
Definition: dialogue.c:964
void dialogue_msgImgRaw(const char *caption, const char *msg, const char *img, int width, int height)
Opens a dialogue window with an ok button, a fixed message and an image.
Definition: dialogue.c:290
void dialogue_makeChoice(const char *caption, const char *msg, int opts)
Create the choice dialog. Need to add choices with below method.
Definition: dialogue.c:748
static void dialogue_inputClose(unsigned int wid, const char *str)
Closes an input dialogue.
Definition: dialogue.c:532
static unsigned int choice_wid
Definition: dialogue.c:738
static int dialogue_open
Definition: dialogue.c:38
static int choice_nopts
Definition: dialogue.c:740
void dialogue_msgRaw(const char *caption, const char *msg)
Opens a dialogue window with an ok button and a fixed message.
Definition: dialogue.c:261
int dialogue_isOpen(void)
Checks to see if a dialogue is open.
Definition: dialogue.c:98
static InputDialogue input_dialogue
Definition: dialogue.c:424
static void dialogue_cancel(unsigned int wid, const char *str)
Generic window cancel.
Definition: dialogue.c:118
int dialogue_YesNo(const char *caption, const char *fmt,...)
Runs a dialogue with both yes and no options.
Definition: dialogue.c:344
int dialogue_customFullscreen(int enable)
Converts a custom dialogue to fullscreen.
Definition: dialogue.c:927
void dialogue_msgImg(const char *caption, const char *img, const char *fmt,...)
Opens a dialogue window with an ok button, a message and an image.
Definition: dialogue.c:240
static void dialogue_YesNoClose(unsigned int wid, const char *str)
Closes a yesno dialogue.
Definition: dialogue.c:404
int gl_printHeightRaw(const glFont *ft_font, const int width, const char *text)
Gets the height of a non-formatted string.
Definition: font.c:1026
int gl_printWidthRaw(const glFont *ft_font, const char *text)
Gets the width that it would take to print some text.
Definition: font.c:960
glFont gl_defFont
Definition: font.c:153
void input_handle(SDL_Event *event)
Handles global input.
Definition: input.c:1489
Handles the important game menus.
int menu_askQuit(void)
Menu to ask if player really wants to quit.
Definition: menu.c:685
static unsigned int time_ms
Definition: naev.c:102
void naev_quit(void)
Flags naev to quit.
Definition: naev.c:147
void naev_resize(void)
Wrapper for gl_resize that handles non-GL reinitialization.
Definition: naev.c:765
int naev_isQuit(void)
Get if Naev is trying to quit.
Definition: naev.c:155
const double fps_min
Definition: naev.c:118
Uint32 SDL_LOOPDONE
Definition: naev.c:101
Header file with generic functions and naev-specifics.
#define MIN(x, y)
Definition: naev.h:40
#define FABS(x)
Definition: naev.h:37
#define MAX(x, y)
Definition: naev.h:39
#define PATH_MAX
Definition: naev.h:50
void gl_windowToScreenPos(int *sx, int *sy, int wx, int wy)
Translates the window position to screen position.
Definition: opengl.c:616
glInfo gl_screen
Definition: opengl.c:51
glTexture * gl_newImage(const char *path, const unsigned int flags)
Loads an image as a texture.
Definition: opengl_tex.c:570
static const double d[]
Definition: rng.c:273
Used to store information for input dialogues.
Definition: dialogue.c:83
void(* item_select_cb)(unsigned int wid, const char *wgtname, int x, int y, int w, int h)
Definition: dialogue.c:89
unsigned int input_wid
Definition: dialogue.c:84
int fps_max
Definition: conf.h:114
Represents a font in memory.
Definition: font.h:16
int h
Definition: font.h:18
int nw
Definition: opengl.h:43
int nh
Definition: opengl.h:44
Abstraction for rendering sprite sheets.
Definition: opengl_tex.h:34
double w
Definition: opengl_tex.h:38
double h
Definition: opengl_tex.h:39
unsigned int window_create(const char *name, const char *displayname, const int x, const int y, const int w, const int h)
Creates a window.
Definition: toolkit.c:696
void window_setFade(unsigned int wid, const SimpleShader *shd, double length)
Sets the fade-in behaviour of a window.
Definition: toolkit.c:646
void window_setAccept(unsigned int wid, void(*accept)(unsigned int, const char *))
Sets the default accept function of the window.
Definition: toolkit.c:879
void window_dimWindow(unsigned int wid, int *w, int *h)
Gets the dimensions of a window.
Definition: toolkit.c:371
int toolkit_isOpen(void)
Checks to see if the toolkit is open.
Definition: toolkit.c:95
void window_move(unsigned int wid, int x, int y)
Moves a window to the specified coordinates.
Definition: toolkit.c:180
void window_setCancel(unsigned int wid, void(*cancel)(unsigned int, const char *))
Sets the default cancel function of the window.
Definition: toolkit.c:900
void window_moveWidget(unsigned int wid, const char *name, int x, int y)
Moves a widget.
Definition: toolkit.c:467
void window_resize(unsigned int wid, int w, int h)
Resizes the window.
Definition: toolkit.c:197
void toolkit_delay(void)
Delays the toolkit purge by an iteration, useful for dialogues.
Definition: toolkit.c:107
void window_posWidget(unsigned int wid, const char *name, int *x, int *y)
Gets a widget's position.
Definition: toolkit.c:442
unsigned int window_get(const char *wdwname)
Gets the ID of a window.
Definition: toolkit.c:671
void window_setBorder(unsigned int wid, int enable)
Sets or removes the border of a window.
Definition: toolkit.c:953
void window_posWindow(unsigned int wid, int *x, int *y)
Gets the dimensions of a window.
Definition: toolkit.c:393
void * window_getData(unsigned int wid)
Gets the custom data of a window.
Definition: toolkit.c:934
void window_setData(unsigned int wid, void *data)
Sets custom data for a window.
Definition: toolkit.c:917
void window_resizeWidget(unsigned int wid, const char *name, int w, int h)
Resizes a widget.
Definition: toolkit.c:495
void window_destroy(unsigned int wid)
Kills the window.
Definition: toolkit.c:1042
void window_handleEvents(unsigned int wid, int(*eventhandler)(unsigned int, SDL_Event *))
Sets the event handler for the window.
Definition: toolkit.c:989