1 #include <string.h>
2 #include "video/video.h"
3 #include "utils/log.h"
4 #include "game/gui/menu_background.h"
5 #include "game/gui/textbutton.h"
6 #include "game/gui/dialog.h"
7
8 #define MAX_WIDTH 160
9
dialog_create(dialog * dlg,dialog_style style,const char * text,int x,int y)10 void dialog_create(dialog *dlg, dialog_style style, const char *text, int x, int y) {
11 int w, h;
12
13 text_settings tconf;
14 text_defaults(&tconf);
15 tconf.font = FONT_BIG;
16 tconf.cforeground = color_create(0, 121, 0, 255);
17
18 text_settings tconf_desc;
19 text_defaults(&tconf_desc);
20 tconf_desc.font = FONT_SMALL;
21 tconf_desc.cforeground = color_create(0, 121, 0, 255);
22
23 dlg->x = x;
24 dlg->y = y;
25 dlg->yes = NULL;
26 dlg->no = NULL;
27 dlg->ok = NULL;
28 dlg->visible = 0;
29 dlg->userdata = NULL;
30 dlg->clicked = NULL;
31 strncpy(dlg->text, text, sizeof(dlg->text)-1);
32 dlg->text[sizeof(dlg->text)-1] = 0;
33 font_get_wrapped_size_shadowed(&font_small, dlg->text, MAX_WIDTH, TEXT_SHADOW_RIGHT|TEXT_SHADOW_BOTTOM, &w, &h);
34 int tsize = text_char_width(&tconf);
35 menu_background_create(&dlg->background, MAX_WIDTH+30, h+24+tsize);
36
37 if(style == DIALOG_STYLE_YES_NO) {
38 dlg->yes = textbutton_create(&tconf, "YES", COM_ENABLED, NULL, NULL);
39 dlg->no = textbutton_create(&tconf, "NO", COM_ENABLED, NULL, NULL);
40 textbutton_set_border(dlg->yes, COLOR_BLUE);
41 textbutton_set_border(dlg->no, COLOR_BLUE);
42 component_layout(dlg->yes, x + 54, x + h + 6, 8, 8);
43 component_layout(dlg->no, x + 114, x + h + 6, 8, 8);
44 component_select(dlg->yes, 1);
45 dlg->result = DIALOG_RESULT_YES_OK;
46 } else if(style == DIALOG_STYLE_OK) {
47 dlg->ok = textbutton_create(&tconf, "OK", COM_ENABLED, NULL, NULL);
48 textbutton_set_border(dlg->ok, COLOR_BLUE);
49 component_layout(dlg->ok, x + 84, x + h + 6, 8, 8);
50 component_select(dlg->ok, 1);
51 dlg->result = DIALOG_RESULT_YES_OK;
52 }
53 }
54
dialog_free(dialog * dlg)55 void dialog_free(dialog *dlg) {
56 if(dlg->yes) {
57 component_free(dlg->yes);
58 }
59 if(dlg->no) {
60 component_free(dlg->no);
61 }
62 if(dlg->ok) {
63 component_free(dlg->ok);
64 }
65 surface_free(&dlg->background);
66 }
67
dialog_show(dialog * dlg,int visible)68 void dialog_show(dialog *dlg, int visible) {
69 dlg->visible = visible;
70 }
71
dialog_is_visible(dialog * dlg)72 int dialog_is_visible(dialog *dlg) {
73 return dlg->visible;
74 }
75
dialog_render(dialog * dlg)76 void dialog_render(dialog *dlg) {
77 if(!dlg->visible) { return; }
78 video_render_sprite(&dlg->background, dlg->x, dlg->y, BLEND_ALPHA, 0);
79 if(dlg->yes) {
80 component_render(dlg->yes);
81 }
82 if(dlg->no) {
83 component_render(dlg->no);
84 }
85 if(dlg->ok) {
86 component_render(dlg->ok);
87 }
88 font_render_wrapped_shadowed(&font_small, dlg->text, dlg->x+15, dlg->y+3, MAX_WIDTH, COLOR_GREEN, TEXT_SHADOW_RIGHT|TEXT_SHADOW_BOTTOM);
89 }
90
91
dialog_tick(dialog * dlg)92 void dialog_tick(dialog *dlg) {
93 if(!dlg->visible) { return; }
94 if(dlg->yes) {
95 component_tick(dlg->yes);
96 }
97 if(dlg->no) {
98 component_tick(dlg->no);
99 }
100 if(dlg->ok) {
101 component_tick(dlg->ok);
102 }
103 }
104
dialog_event(dialog * dlg,int action)105 void dialog_event(dialog *dlg, int action) {
106 if(!dlg->visible) { return; }
107 if(action == ACT_LEFT || action == ACT_RIGHT) {
108 if(dlg->yes && dlg->no) {
109 if(component_is_selected(dlg->yes)) {
110 component_select(dlg->yes, 0);
111 component_select(dlg->no, 1);
112 dlg->result = DIALOG_RESULT_NO;
113 } else if(component_is_selected(dlg->no)) {
114 component_select(dlg->yes, 1);
115 component_select(dlg->no, 0);
116 dlg->result = DIALOG_RESULT_YES_OK;
117 }
118 }
119 } else if(action == ACT_PUNCH || action == ACT_KICK) {
120 if(dlg->clicked) {
121 dlg->clicked(dlg, dlg->result);
122 }
123 dlg->visible = 0;
124 } else if(action == ACT_ESC) {
125 if(dlg->clicked) {
126 dlg->clicked(dlg, DIALOG_RESULT_CANCEL);
127 }
128 dlg->visible = 0;
129 }
130 }
131