1 #include "top_menu_editor.h"
2
3 #include "game/file_editor.h"
4 #include "game/game.h"
5 #include "game/system.h"
6 #include "graphics/image.h"
7 #include "graphics/menu.h"
8 #include "graphics/screen.h"
9 #include "graphics/window.h"
10 #include "scenario/editor_map.h"
11 #include "scenario/scenario.h"
12 #include "translation/translation.h"
13 #include "window/config.h"
14 #include "window/file_dialog.h"
15 #include "window/message_dialog.h"
16 #include "window/popup_dialog.h"
17 #include "window/select_list.h"
18 #include "window/editor/empire.h"
19 #include "window/editor/map.h"
20
21 void menu_file_new_map(int param);
22 static void menu_file_load_map(int param);
23 static void menu_file_save_map(int param);
24 static void menu_file_exit_to_menu(int param);
25 static void menu_file_exit_game(int param);
26
27 static void menu_options_general(int param);
28
29 static void menu_help_help(int param);
30 static void menu_help_about(int param);
31
32 static void menu_resets_herds(int param);
33 static void menu_resets_fish(int param);
34 static void menu_resets_invasions(int param);
35
36 static void menu_empire_choose(int param);
37
38 static menu_item menu_file[] = {
39 {7, 1, menu_file_new_map, 0},
40 {7, 2, menu_file_load_map, 0},
41 {7, 3, menu_file_save_map, 0},
42 {CUSTOM_TRANSLATION, TR_BUTTON_BACK_TO_MAIN_MENU, menu_file_exit_to_menu, 0},
43 {1, 5, menu_file_exit_game, 0}
44 };
45
46 static menu_item menu_options[] = {
47 {CUSTOM_TRANSLATION, TR_CONFIG_HEADER_GENERAL, menu_options_general, 0},
48 };
49
50 static menu_item menu_help[] = {
51 {3, 1, menu_help_help, 0},
52 {3, 7, menu_help_about, 0},
53 };
54
55 static menu_item menu_resets[] = {
56 {10, 1, menu_resets_herds, 0},
57 {10, 2, menu_resets_fish, 0},
58 {10, 3, menu_resets_invasions, 0},
59 };
60
61 static menu_item menu_empire[] = {
62 {149, 1, menu_empire_choose, 0},
63 };
64
65 static menu_bar_item menu[] = {
66 {7, menu_file, 5},
67 {2, menu_options, 1},
68 {3, menu_help, 2},
69 {10, menu_resets, 3},
70 {149, menu_empire, 1},
71 };
72
73 #define INDEX_OPTIONS 1
74
75 static struct {
76 int open_sub_menu;
77 int focus_menu_id;
78 int focus_sub_menu_id;
79 } data;
80
clear_state(void)81 static void clear_state(void)
82 {
83 data.open_sub_menu = 0;
84 data.focus_menu_id = 0;
85 data.focus_sub_menu_id = 0;
86 }
87
init(void)88 static void init(void)
89 {
90 menu[INDEX_OPTIONS].items[0].hidden = system_is_fullscreen_only();
91 }
92
draw_foreground(void)93 static void draw_foreground(void)
94 {
95 if (!data.open_sub_menu) {
96 return;
97 }
98 menu_draw(&menu[data.open_sub_menu - 1], data.focus_sub_menu_id);
99 }
100
handle_input(const mouse * m,const hotkeys * h)101 static void handle_input(const mouse *m, const hotkeys *h)
102 {
103 widget_top_menu_editor_handle_input(m, h);
104 }
105
top_menu_window_show(void)106 static void top_menu_window_show(void)
107 {
108 window_type window = {
109 WINDOW_EDITOR_TOP_MENU,
110 window_editor_map_draw_all,
111 draw_foreground,
112 handle_input
113 };
114 init();
115 window_show(&window);
116 }
117
widget_top_menu_editor_draw(void)118 void widget_top_menu_editor_draw(void)
119 {
120 int block_width = 24;
121 int image_base = image_group(GROUP_TOP_MENU);
122 int s_width = screen_width();
123 for (int i = 0; i * block_width < s_width; i++) {
124 image_draw(image_base + i % 8, i * block_width, 0);
125 }
126 menu_bar_draw(menu, 5, s_width);
127 }
128
handle_input_submenu(const mouse * m,const hotkeys * h)129 static int handle_input_submenu(const mouse *m, const hotkeys *h)
130 {
131 if (m->right.went_up || h->escape_pressed) {
132 clear_state();
133 window_go_back();
134 return 1;
135 }
136 int menu_id = menu_bar_handle_mouse(m, menu, 5, &data.focus_menu_id);
137 if (menu_id && menu_id != data.open_sub_menu) {
138 window_request_refresh();
139 data.open_sub_menu = menu_id;
140 }
141 if (!menu_handle_mouse(m, &menu[data.open_sub_menu - 1], &data.focus_sub_menu_id)) {
142 if (m->left.went_up) {
143 clear_state();
144 window_go_back();
145 return 1;
146 }
147 }
148 return 0;
149 }
150
handle_mouse_menu(const mouse * m)151 static int handle_mouse_menu(const mouse *m)
152 {
153 int menu_id = menu_bar_handle_mouse(m, menu, 5, &data.focus_menu_id);
154 if (menu_id && m->left.went_up) {
155 data.open_sub_menu = menu_id;
156 top_menu_window_show();
157 return 1;
158 }
159 return 0;
160 }
161
widget_top_menu_editor_handle_input(const mouse * m,const hotkeys * h)162 int widget_top_menu_editor_handle_input(const mouse *m, const hotkeys *h)
163 {
164 if (data.open_sub_menu) {
165 return handle_input_submenu(m, h);
166 } else {
167 return handle_mouse_menu(m);
168 }
169 }
170
map_size_selected(int size)171 static void map_size_selected(int size)
172 {
173 clear_state();
174 if (size >= 0 && size <= 5) {
175 game_file_editor_create_scenario(size);
176 window_editor_map_show();
177 } else {
178 window_go_back();
179 }
180 }
181
menu_file_new_map(int centered)182 void menu_file_new_map(int centered)
183 {
184 int x = 50;
185 int y = 50;
186 if (centered) {
187 x += 325;
188 y += 200;
189 }
190 window_select_list_show(x, y, 33, 7, map_size_selected);
191 }
192
menu_file_load_map(int param)193 static void menu_file_load_map(int param)
194 {
195 clear_state();
196 window_editor_map_show();
197 window_file_dialog_show(FILE_TYPE_SCENARIO, FILE_DIALOG_LOAD);
198 }
199
menu_file_save_map(int param)200 static void menu_file_save_map(int param)
201 {
202 clear_state();
203 window_editor_map_show();
204 window_file_dialog_show(FILE_TYPE_SCENARIO, FILE_DIALOG_SAVE);
205 }
206
menu_file_confirm_exit_to_menu(int accepted,int checked)207 static void menu_file_confirm_exit_to_menu(int accepted, int checked)
208 {
209 if (accepted) {
210 game_exit_editor();
211 } else {
212 window_editor_map_show();
213 }
214 }
215
menu_file_exit_to_menu(int param)216 static void menu_file_exit_to_menu(int param)
217 {
218 clear_state();
219 window_editor_map_show();
220 if (scenario_is_saved()) {
221 game_exit_editor();
222 } else {
223 window_popup_dialog_show(POPUP_DIALOG_EDITOR_QUIT_WITHOUT_SAVING, menu_file_confirm_exit_to_menu, 1);
224 }
225 }
226
menu_file_confirm_exit_game(int accepted,int checked)227 static void menu_file_confirm_exit_game(int accepted, int checked)
228 {
229 if (accepted) {
230 system_exit();
231 } else {
232 window_editor_map_show();
233 }
234 }
235
menu_file_exit_game(int param)236 static void menu_file_exit_game(int param)
237 {
238 clear_state();
239 window_popup_dialog_show(POPUP_DIALOG_QUIT, menu_file_confirm_exit_game, 1);
240 }
241
menu_options_general(int param)242 static void menu_options_general(int param)
243 {
244 clear_state();
245 window_editor_map_show();
246 window_config_show(CONFIG_PAGE_GENERAL, 0);
247 }
248
menu_help_help(int param)249 static void menu_help_help(int param)
250 {
251 clear_state();
252 window_go_back();
253 window_message_dialog_show(MESSAGE_DIALOG_EDITOR_HELP, window_editor_map_draw_all);
254 }
255
menu_help_about(int param)256 static void menu_help_about(int param)
257 {
258 clear_state();
259 window_go_back();
260 window_message_dialog_show(MESSAGE_DIALOG_EDITOR_ABOUT, window_editor_map_draw_all);
261 }
262
menu_resets_herds(int param)263 static void menu_resets_herds(int param)
264 {
265 scenario_editor_clear_herd_points();
266 clear_state();
267 window_go_back();
268 }
269
menu_resets_fish(int param)270 static void menu_resets_fish(int param)
271 {
272 scenario_editor_clear_fishing_points();
273 clear_state();
274 window_go_back();
275 }
276
menu_resets_invasions(int param)277 static void menu_resets_invasions(int param)
278 {
279 scenario_editor_clear_invasion_points();
280 clear_state();
281 window_go_back();
282 }
283
menu_empire_choose(int param)284 static void menu_empire_choose(int param)
285 {
286 clear_state();
287 window_go_back();
288 window_editor_empire_show();
289 }
290