forked from Keriew/augustus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_dialog.c
364 lines (322 loc) · 12.7 KB
/
file_dialog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "file_dialog.h"
#include "core/calc.h"
#include "core/dir.h"
#include "core/encoding.h"
#include "core/file.h"
#include "core/image_group.h"
#include "core/lang.h"
#include "core/string.h"
#include "core/time.h"
#include "game/file.h"
#include "game/file_editor.h"
#include "graphics/generic_button.h"
#include "graphics/graphics.h"
#include "graphics/image.h"
#include "graphics/image_button.h"
#include "graphics/lang_text.h"
#include "graphics/panel.h"
#include "graphics/scrollbar.h"
#include "graphics/text.h"
#include "graphics/window.h"
#include "input/input.h"
#include "platform/file_manager.h"
#include "widget/input_box.h"
#include "window/city.h"
#include "window/editor/map.h"
#include "window/plain_message_dialog.h"
#include <string.h>
#define NUM_FILES_IN_VIEW 12
#define MAX_FILE_WINDOW_TEXT_WIDTH (18 * BLOCK_SIZE)
static const time_millis NOT_EXIST_MESSAGE_TIMEOUT = 500;
static void button_ok_cancel(int is_ok, int param2);
static void button_select_file(int index, int param2);
static void on_scroll(void);
static image_button image_buttons[] = {
{344, 335, 39, 26, IB_NORMAL, GROUP_OK_CANCEL_SCROLL_BUTTONS, 0, button_ok_cancel, button_none, 1, 0, 1},
{392, 335, 39, 26, IB_NORMAL, GROUP_OK_CANCEL_SCROLL_BUTTONS, 4, button_ok_cancel, button_none, 0, 0, 1},
};
static generic_button file_buttons[] = {
{160, 128, 288, 16, button_select_file, button_none, 0, 0},
{160, 144, 288, 16, button_select_file, button_none, 1, 0},
{160, 160, 288, 16, button_select_file, button_none, 2, 0},
{160, 176, 288, 16, button_select_file, button_none, 3, 0},
{160, 192, 288, 16, button_select_file, button_none, 4, 0},
{160, 208, 288, 16, button_select_file, button_none, 5, 0},
{160, 224, 288, 16, button_select_file, button_none, 6, 0},
{160, 240, 288, 16, button_select_file, button_none, 7, 0},
{160, 256, 288, 16, button_select_file, button_none, 8, 0},
{160, 272, 288, 16, button_select_file, button_none, 9, 0},
{160, 288, 288, 16, button_select_file, button_none, 10, 0},
{160, 304, 288, 16, button_select_file, button_none, 11, 0},
};
static scrollbar_type scrollbar = { 464, 120, 206, on_scroll };
typedef struct {
char extension[4];
char last_loaded_file[FILE_NAME_MAX];
} file_type_data;
static struct {
time_millis message_not_exist_start_time;
file_type type;
file_dialog_type dialog_type;
int focus_button_id;
int double_click;
const dir_listing *file_list;
file_type_data *file_data;
uint8_t typed_name[FILE_NAME_MAX];
uint8_t previously_seen_typed_name[FILE_NAME_MAX];
char selected_file[FILE_NAME_MAX];
} data;
static input_box file_name_input = { 144, 80, 20, 2, FONT_NORMAL_WHITE, 0, data.typed_name, FILE_NAME_MAX };
static file_type_data saved_game_data = { "sav" };
static file_type_data saved_game_data_expanded = { "svx" };
static file_type_data scenario_data = { "map" };
static int find_first_file_with_prefix(const char *prefix)
{
int len = (int) strlen(prefix);
if (len == 0) {
return -1;
}
int left = 0;
int right = data.file_list->num_files;
while (left < right) {
int middle = (left + right) / 2;
if (platform_file_manager_compare_filename_prefix(data.file_list->files[middle], prefix, len) >= 0) {
right = middle;
} else {
left = middle + 1;
}
}
if (platform_file_manager_compare_filename_prefix(data.file_list->files[left], prefix, len) == 0) {
return left;
} else {
return -1;
}
}
static void scroll_to_typed_text(void)
{
if (data.file_list->num_files <= NUM_FILES_IN_VIEW) {
// No need to scroll
return;
}
char name_utf8[FILE_NAME_MAX];
encoding_to_utf8(data.typed_name, name_utf8, FILE_NAME_MAX, encoding_system_uses_decomposed());
int index = find_first_file_with_prefix(name_utf8);
if (index >= 0) {
scrollbar_reset(&scrollbar, calc_bound(index, 0, data.file_list->num_files - NUM_FILES_IN_VIEW));
}
}
static void init(file_type type, file_dialog_type dialog_type)
{
data.type = type;
data.file_data = type == FILE_TYPE_SCENARIO ? &scenario_data : &saved_game_data;
data.dialog_type = dialog_type;
data.message_not_exist_start_time = 0;
data.double_click = 0;
data.focus_button_id = 0;
if (strlen(data.file_data->last_loaded_file) > 0) {
encoding_from_utf8(data.file_data->last_loaded_file, data.typed_name, FILE_NAME_MAX);
file_remove_extension((char *) data.typed_name);
} else if (dialog_type == FILE_DIALOG_SAVE) {
// Suggest default filename
string_copy(lang_get_string(9, type == FILE_TYPE_SCENARIO ? 7 : 6), data.typed_name, FILE_NAME_MAX);
if (type == FILE_TYPE_SAVED_GAME) {
file_append_extension((char *) data.typed_name, saved_game_data_expanded.extension);
}
encoding_to_utf8(data.typed_name, data.file_data->last_loaded_file, FILE_NAME_MAX, 0);
} else {
// Use empty string
data.typed_name[0] = 0;
}
string_copy(data.typed_name, data.previously_seen_typed_name, FILE_NAME_MAX);
if (data.dialog_type != FILE_DIALOG_SAVE) {
if (type == FILE_TYPE_SCENARIO) {
data.file_list = dir_find_files_with_extension(".", scenario_data.extension);
} else {
data.file_list = dir_find_files_with_extension(".", data.file_data->extension);
data.file_list = dir_append_files_with_extension(saved_game_data_expanded.extension);
}
} else {
if (type == FILE_TYPE_SCENARIO) {
data.file_list = dir_find_files_with_extension(".", scenario_data.extension);
} else {
data.file_list = dir_find_files_with_extension(".", saved_game_data_expanded.extension);
}
}
scrollbar_init(&scrollbar, 0, data.file_list->num_files - NUM_FILES_IN_VIEW);
scroll_to_typed_text();
strncpy(data.selected_file, data.file_data->last_loaded_file, FILE_NAME_MAX);
input_box_start(&file_name_input);
}
static void draw_foreground(void)
{
graphics_in_dialog();
uint8_t file[FILE_NAME_MAX];
outer_panel_draw(128, 40, 24, 21);
input_box_draw(&file_name_input);
inner_panel_draw(144, 120, 20, 13);
// title
if (data.message_not_exist_start_time
&& time_get_millis() - data.message_not_exist_start_time < NOT_EXIST_MESSAGE_TIMEOUT) {
lang_text_draw_centered(43, 2, 160, 50, 304, FONT_LARGE_BLACK);
} else if (data.dialog_type == FILE_DIALOG_DELETE) {
lang_text_draw_centered(43, 6, 160, 50, 304, FONT_LARGE_BLACK);
} else {
int text_id = data.dialog_type + (data.type == FILE_TYPE_SCENARIO ? 3 : 0);
lang_text_draw_centered(43, text_id, 160, 50, 304, FONT_LARGE_BLACK);
}
lang_text_draw(43, 5, 224, 342, FONT_NORMAL_BLACK);
for (int i = 0; i < NUM_FILES_IN_VIEW; i++) {
font_t font = FONT_NORMAL_GREEN;
if (data.focus_button_id == i + 1) {
font = FONT_NORMAL_WHITE;
}
encoding_from_utf8(data.file_list->files[scrollbar.scroll_position + i], file, FILE_NAME_MAX);
//file_remove_extension(file);
text_ellipsize(file, font, MAX_FILE_WINDOW_TEXT_WIDTH);
text_draw(file, 160, 130 + 16 * i, font, 0);
}
image_buttons_draw(0, 0, image_buttons, 2);
scrollbar_draw(&scrollbar);
graphics_reset_dialog();
}
static int should_scroll_to_typed_text(void)
{
if (string_equals(data.previously_seen_typed_name, data.typed_name)) {
return 0;
}
int scroll = 0;
// Only scroll when adding characters to the typed name
if (string_length(data.typed_name) > string_length(data.previously_seen_typed_name)) {
scroll = 1;
}
string_copy(data.typed_name, data.previously_seen_typed_name, FILE_NAME_MAX);
return scroll;
}
static void handle_input(const mouse *m, const hotkeys *h)
{
data.double_click = m->left.double_click;
if (input_box_is_accepted(&file_name_input)) {
button_ok_cancel(1, 0);
return;
}
const mouse *m_dialog = mouse_in_dialog(m);
if (input_box_handle_mouse(m_dialog, &file_name_input) ||
generic_buttons_handle_mouse(m_dialog, 0, 0, file_buttons, NUM_FILES_IN_VIEW, &data.focus_button_id) ||
image_buttons_handle_mouse(m_dialog, 0, 0, image_buttons, 2, 0) ||
scrollbar_handle_mouse(&scrollbar, m_dialog)) {
return;
}
if (input_go_back_requested(m, h)) {
input_box_stop(&file_name_input);
window_go_back();
}
if (should_scroll_to_typed_text()) {
scroll_to_typed_text();
}
}
static char *get_chosen_filename(void)
{
// Check if we should work with the selected file
uint8_t selected_name[FILE_NAME_MAX];
encoding_from_utf8(data.selected_file, selected_name, FILE_NAME_MAX);
if (string_equals(selected_name, data.typed_name)) {
// User has not modified the string after selecting it: use filename
return data.selected_file;
}
// We should use the typed name, which needs to be converted to UTF-8...
static char typed_file[FILE_NAME_MAX];
encoding_to_utf8(data.typed_name, typed_file, FILE_NAME_MAX, encoding_system_uses_decomposed());
return typed_file;
}
static void button_ok_cancel(int is_ok, int param2)
{
if (!is_ok) {
input_box_stop(&file_name_input);
window_go_back();
return;
}
char *filename = get_chosen_filename();
if (data.dialog_type != FILE_DIALOG_SAVE && !file_exists(filename, NOT_LOCALIZED)) {
data.message_not_exist_start_time = time_get_millis();
return;
}
if (data.dialog_type == FILE_DIALOG_LOAD) {
if (data.type == FILE_TYPE_SAVED_GAME) {
int result = game_file_load_saved_game(filename);
if (result == 1) {
input_box_stop(&file_name_input);
window_city_show();
} else if (result == 0) {
data.message_not_exist_start_time = time_get_millis();
return;
} else if (result == -1) {
window_plain_message_dialog_show(TR_SAVEGAME_LARGER_VERSION_TITLE, TR_SAVEGAME_LARGER_VERSION_MESSAGE, 1);
return;
}
} else if (data.type == FILE_TYPE_SCENARIO) {
if (game_file_editor_load_scenario(filename)) {
input_box_stop(&file_name_input);
window_editor_map_show();
} else {
data.message_not_exist_start_time = time_get_millis();
return;
}
}
} else if (data.dialog_type == FILE_DIALOG_SAVE) {
input_box_stop(&file_name_input);
if (data.type == FILE_TYPE_SAVED_GAME) {
if (!file_has_extension(filename, saved_game_data_expanded.extension)) {
file_append_extension(filename, saved_game_data_expanded.extension);
}
game_file_write_saved_game(filename);
window_city_show();
} else if (data.type == FILE_TYPE_SCENARIO) {
if (!file_has_extension(filename, scenario_data.extension)) {
file_append_extension(filename, scenario_data.extension);
}
game_file_editor_write_scenario(filename);
window_editor_map_show();
}
} else if (data.dialog_type == FILE_DIALOG_DELETE) {
if (game_file_delete_saved_game(filename)) {
dir_find_files_with_extension(".", data.file_data->extension);
dir_append_files_with_extension(saved_game_data_expanded.extension);
if (scrollbar.scroll_position + NUM_FILES_IN_VIEW >= data.file_list->num_files) {
--scrollbar.scroll_position;
}
if (scrollbar.scroll_position < 0) {
scrollbar.scroll_position = 0;
}
}
}
strncpy(data.file_data->last_loaded_file, filename, FILE_NAME_MAX - 1);
}
static void on_scroll(void)
{
data.message_not_exist_start_time = 0;
}
static void button_select_file(int index, int param2)
{
if (index < data.file_list->num_files) {
strncpy(data.selected_file, data.file_list->files[scrollbar.scroll_position + index], FILE_NAME_MAX - 1);
encoding_from_utf8(data.selected_file, data.typed_name, FILE_NAME_MAX);
string_copy(data.typed_name, data.previously_seen_typed_name, FILE_NAME_MAX);
input_box_refresh_text(&file_name_input);
data.message_not_exist_start_time = 0;
}
if (data.dialog_type != FILE_DIALOG_DELETE && data.double_click) {
data.double_click = 0;
button_ok_cancel(1, 0);
}
}
void window_file_dialog_show(file_type type, file_dialog_type dialog_type)
{
window_type window = {
WINDOW_FILE_DIALOG,
window_draw_underlying_window,
draw_foreground,
handle_input
};
init(type, dialog_type);
window_show(&window);
}