10
10
#include " content/dir.h"
11
11
#include " game/file_editor.h"
12
12
#include " graphics/graphics.h"
13
+ #include " graphics/elements/scroll_list_panel.h"
13
14
#include " graphics/elements/generic_button.h"
14
15
#include " graphics/elements/image_button.h"
15
16
#include " graphics/elements/lang_text.h"
16
- #include " graphics/elements/panel.h"
17
17
#include " graphics/elements/scrollbar.h"
18
+ #include " graphics/elements/panel.h"
18
19
#include " graphics/image_groups.h"
19
- #include " graphics/text.h"
20
20
#include " graphics/window.h"
21
+ #include " graphics/text.h"
21
22
#include " input/input.h"
22
23
#include " content/vfs.h"
23
24
#include " io/gamefiles/lang.h"
24
25
#include " io/gamestate/boilerplate.h"
25
26
#include " widget/input_box.h"
26
27
#include " window/window_city.h"
27
28
#include " window/editor/map.h"
28
-
29
- #include " graphics/elements/scroll_list_panel .h"
29
+ # include " window/autoconfig_window.h "
30
+ #include " game/settings .h"
30
31
#include " io/manager.h"
31
- #include < game/settings.h>
32
- #include < string.h>
33
32
34
33
static const time_millis NOT_EXIST_MESSAGE_TIMEOUT = 500 ;
35
34
@@ -48,7 +47,7 @@ static image_button image_buttons[] = {
48
47
49
48
static scrollable_list_ui_params ui_params = [] {
50
49
scrollable_list_ui_params ret;
51
- ret.pos = { 144 , 120 };
50
+ // ret.pos = { 144, 120 };
52
51
ret.blocks_x = 20 ;
53
52
ret.blocks_y = NUM_FILES_IN_VIEW + 1 ;
54
53
ret.draw_scrollbar_always = true ;
@@ -57,7 +56,7 @@ static scrollable_list_ui_params ui_params = [] {
57
56
58
57
static input_box file_name_input = {144 , 80 , 20 , 2 , FONT_NORMAL_WHITE_ON_DARK};
59
58
60
- struct file_dialog_data_t {
59
+ struct file_dialog : public autoconfig_window_t <file_dialog> {
61
60
time_millis message_not_exist_start_time;
62
61
file_type type;
63
62
file_dialog_type dialog_type;
@@ -67,36 +66,48 @@ struct file_dialog_data_t {
67
66
uint8_t typed_name[MAX_FILE_NAME];
68
67
char selected_file[MAX_FILE_NAME];
69
68
scroll_list_panel* panel = nullptr ;
69
+
70
+ virtual int handle_mouse (const mouse *m) override { return 0 ; }
71
+ virtual int draw_background () override { return 0 ; }
72
+ virtual void draw_foreground () override {}
73
+ virtual void ui_draw_foreground () override {}
74
+ virtual int get_tooltip_text () override { return 0 ; }
75
+ virtual int ui_handle_mouse (const mouse *m) override { return 0 ; }
76
+ virtual void init () override {}
70
77
};
71
78
72
- file_dialog_data_t g_file_dialog_data ;
79
+ file_dialog g_file_dialog ;
73
80
74
81
file_type_data saved_game_data = {" sav" };
75
82
file_type_data saved_game_data_expanded = {" svx" };
76
83
file_type_data map_file_data = {" map" };
77
84
78
85
static void set_chosen_filename (const char * name) {
79
- auto & data = g_file_dialog_data ;
86
+ auto & data = g_file_dialog ;
80
87
strcpy (data.selected_file , name);
81
88
encoding_from_utf8 (data.selected_file , data.typed_name , MAX_PLAYER_NAME);
82
89
}
90
+
83
91
static void clear_chosen_filename () {
84
92
set_chosen_filename (" " );
85
93
}
94
+
86
95
static bool is_chosen_filename (int index) {
87
- auto & data = g_file_dialog_data ;
96
+ auto & data = g_file_dialog ;
88
97
return strcmp (data.selected_file , data.panel ->get_selected_entry_text (FILE_NO_EXT)) == 0 ;
89
98
}
99
+
90
100
static bool is_valid_chosen_filename () {
91
- auto & data = g_file_dialog_data ;
101
+ auto & data = g_file_dialog ;
92
102
if (strcmp (data.selected_file , " " ) == 0 )
93
103
return false ;
94
104
if (data.panel ->get_entry_idx (data.selected_file ) > -1 )
95
105
return true ;
96
106
return false ;
97
107
}
108
+
98
109
static const char * get_chosen_filename (void ) {
99
- auto & data = g_file_dialog_data ;
110
+ auto & data = g_file_dialog ;
100
111
// Check if we should work with the selected file
101
112
uint8_t selected_name[MAX_FILE_NAME];
102
113
encoding_from_utf8 (data.selected_file , selected_name, MAX_FILE_NAME);
@@ -113,15 +124,13 @@ static const char* get_chosen_filename(void) {
113
124
}
114
125
115
126
static void init (file_type type, file_dialog_type dialog_type) {
116
- auto & data = g_file_dialog_data ;
127
+ auto & data = g_file_dialog ;
117
128
data.type = type;
118
129
data.file_data = type == FILE_TYPE_SCENARIO ? &map_file_data : &saved_game_data;
119
130
120
131
// get last saved file name
121
132
if (strlen (data.file_data ->last_loaded_file ) == 0 ) {
122
- set_chosen_filename ((const char *)lang_get_string (9 , type == FILE_TYPE_SCENARIO ? 7 : 6 ));
123
- // string_copy(lang_get_string(9, type == FILE_TYPE_SCENARIO ? 7 : 6), data.typed_name, FILE_NAME_MAX);
124
- // encoding_to_utf8(data.typed_name, data.file_data->last_loaded_file, FILE_NAME_MAX, 0);
133
+ set_chosen_filename ((pcstr)lang_get_string (9 , type == FILE_TYPE_SCENARIO ? 7 : 6 ));
125
134
} else {
126
135
encoding_from_utf8 (data.file_data ->last_loaded_file , data.typed_name , MAX_FILE_NAME);
127
136
}
@@ -131,7 +140,7 @@ static void init(file_type type, file_dialog_type dialog_type) {
131
140
132
141
// populate file list
133
142
char folder_name[MAX_FILE_NAME] = " Save/" ;
134
- strcat (folder_name, (const char * )g_settings.player_name );
143
+ strcat (folder_name, (pcstr )g_settings.player_name );
135
144
strcat (folder_name, " /" );
136
145
if (type == FILE_TYPE_SCENARIO) {
137
146
data.panel ->change_file_path (" Maps/" , map_file_data.extension );
@@ -151,7 +160,7 @@ static void init(file_type type, file_dialog_type dialog_type) {
151
160
}
152
161
153
162
static void draw_foreground (void ) {
154
- auto & data = g_file_dialog_data ;
163
+ auto & data = g_file_dialog ;
155
164
graphics_set_to_dialog ();
156
165
uint8_t file[MAX_FILE_NAME] = {0 };
157
166
@@ -170,6 +179,7 @@ static void draw_foreground(void) {
170
179
}
171
180
lang_text_draw (43 , 5 , 224 , 342 , FONT_NORMAL_BLACK_ON_LIGHT);
172
181
182
+ data.panel ->ui_params .pos = { 144 , 120 };
173
183
data.panel ->draw ();
174
184
175
185
image_buttons_draw ({0 , 0 }, image_buttons, 2 );
@@ -183,7 +193,7 @@ static void draw_foreground(void) {
183
193
}
184
194
185
195
static void button_ok_cancel (int is_ok, int param2) {
186
- auto & data = g_file_dialog_data ;
196
+ auto & data = g_file_dialog ;
187
197
if (!is_ok) {
188
198
input_box_stop (&file_name_input);
189
199
window_go_back ();
@@ -265,8 +275,9 @@ static void button_ok_cancel(int is_ok, int param2) {
265
275
266
276
strncpy (data.file_data ->last_loaded_file , get_chosen_filename (), MAX_FILE_NAME - 1 );
267
277
}
278
+
268
279
static void button_select_file (int index, int param2) {
269
- auto & data = g_file_dialog_data ;
280
+ auto & data = g_file_dialog ;
270
281
if (index >= data.panel ->get_total_entries ()) {
271
282
return clear_chosen_filename ();
272
283
}
@@ -288,13 +299,14 @@ static void button_select_file(int index, int param2) {
288
299
// button_ok_cancel(1, 0);
289
300
// }
290
301
}
302
+
291
303
static void button_double_click (int param1, int param2) {
292
- if (g_file_dialog_data .dialog_type != FILE_DIALOG_DELETE)
304
+ if (g_file_dialog .dialog_type != FILE_DIALOG_DELETE)
293
305
button_ok_cancel (1 , 0 );
294
306
}
295
307
296
308
static void handle_input (const mouse* m, const hotkeys* h) {
297
- auto & data = g_file_dialog_data ;
309
+ auto & data = g_file_dialog ;
298
310
if (input_go_back_requested (m, h)) {
299
311
input_box_stop (&file_name_input);
300
312
window_go_back ();
@@ -305,31 +317,36 @@ static void handle_input(const mouse* m, const hotkeys* h) {
305
317
return ;
306
318
}
307
319
308
- const mouse* m_dialog = mouse_in_dialog (m);
309
- if (input_box_handle_mouse (m_dialog, &file_name_input) || data.panel ->input_handle (m_dialog)
310
- || image_buttons_handle_mouse (m_dialog, {0 , 0 }, image_buttons, 2 , 0 )) {
320
+ mouse m_dialog = *mouse_in_dialog (m);
321
+ m_dialog.x -= 140 ;
322
+ m_dialog.y -= 120 ;
323
+ if (input_box_handle_mouse (&m_dialog, &file_name_input)
324
+ || data.panel ->input_handle (&m_dialog)
325
+ || image_buttons_handle_mouse (&m_dialog, {0 , 0 }, image_buttons, 2 , 0 )) {
311
326
return ;
312
327
}
313
328
}
329
+
314
330
void window_file_dialog_show (file_type type, file_dialog_type dialog_type) {
315
- if (!g_file_dialog_data .panel ) {
316
- g_file_dialog_data .panel = new scroll_list_panel (NUM_FILES_IN_VIEW,
317
- button_select_file,
318
- button_none,
319
- button_double_click,
320
- button_none,
321
- ui_params,
322
- true ,
323
- " Save/" ,
324
- " folders" );
331
+ if (!g_file_dialog .panel ) {
332
+ g_file_dialog .panel = new scroll_list_panel (NUM_FILES_IN_VIEW,
333
+ button_select_file,
334
+ button_none,
335
+ button_double_click,
336
+ button_none,
337
+ ui_params,
338
+ true ,
339
+ " Save/" ,
340
+ " folders" );
325
341
}
326
342
327
- window_type window = {
343
+ static window_type window = {
328
344
WINDOW_FILE_DIALOG,
329
345
window_draw_underlying_window,
330
346
draw_foreground,
331
347
handle_input
332
348
};
349
+
333
350
init (type, dialog_type);
334
351
window_show (&window);
335
352
}
0 commit comments