Skip to content

Commit 0c027ef

Browse files
committed
ui: fixed scrollbar in filedialog
1 parent 82a8a4a commit 0c027ef

File tree

7 files changed

+79
-49
lines changed

7 files changed

+79
-49
lines changed

src/core/xstring.h

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ class xstring {
8888
[[nodiscard]]
8989
bool equal(const xstring& rhs) const { return (_p == rhs._p); }
9090

91+
[[nodiscard]]
92+
bool equal(pcstr rhs) const { return strcmp(c_str(), rhs) == 0; }
93+
9194
xstring& printf(const char* format, ...) {
9295
bstring<4096> buf;
9396
va_list p;
@@ -111,6 +114,7 @@ struct std::hash<xstring>
111114
};
112115

113116
inline bool operator==(xstring const& a, xstring const& b) { return a._get() == b._get(); }
117+
inline bool operator==(xstring const& a, pcstr b) { return a.equal(b); }
114118
inline bool operator!=(xstring const& a, xstring const& b) { return a._get() != b._get(); }
115119
inline bool operator<(xstring const& a, xstring const& b) { return a._get() < b._get(); }
116120
inline bool operator>(xstring const& a, xstring const& b) { return a._get() > b._get(); }

src/graphics/elements/scroll_list_panel.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,13 @@ void scroll_list_panel::draw() {
228228
if (using_custom_text_render) {
229229
custom_text_render(i, text, text_pos_x, text_pos_y, font);
230230
} else {
231-
if (ui_params.text_max_width != -1)
231+
if (ui_params.text_max_width != -1) {
232232
text_ellipsize(text, font, ui_params.text_max_width);
233+
}
233234
text_draw(text, text_pos_x, text_pos_y, font, 0);
234235
}
235236
}
237+
236238
scrollbar_draw(ui_params.pos, &scrollbar);
237239
WAS_DRAWN = true;
238240
}
@@ -244,15 +246,17 @@ scroll_list_panel::scroll_list_panel(int n_buttons,
244246
void (*fcc)(int param1, int param2),
245247
scrollable_list_ui_params params,
246248
bool use_file_finder,
247-
const char* dir,
248-
const char* ext) {
249+
pcstr dir,
250+
pcstr ext) {
249251
// gather the UI params
250252
ui_params = params;
251-
if (ui_params.buttons_size_x == -1)
253+
if (ui_params.buttons_size_x == -1) {
252254
ui_params.buttons_size_x = ui_params.blocks_x * DEFAULT_BLOCK_SIZE - ui_params.buttons_margin_x - 2;
255+
}
253256

254-
if (ui_params.text_max_width == -1)
257+
if (ui_params.text_max_width == -1) {
255258
ui_params.text_max_width = ui_params.buttons_size_x - ui_params.text_padding_x;
259+
}
256260

257261
// init dynamic button list
258262
num_buttons = n_buttons;

src/graphics/elements/ui.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,8 @@ void ui::eimage_button::draw() {
814814
flags |= (readonly ? UiFlags_Readonly : UiFlags_None);
815815

816816
image_button *btn = nullptr;
817+
pcstr pid = id.c_str();
818+
817819
vec2i tsize;
818820
if (img) {
819821
btn = &ui::img_button(img, pos, size, img_desc.offset);

src/scripts/ui.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ function link(config) { return __extend({type:"generic_button", hbody:false, bor
4040
function arrowup(config) { return __extend({type:"arrow_button", down:false}, config) }
4141
function arrowdown(config) { return __extend({type:"arrow_button", down:true}, config) }
4242
function background(config) { return __extend({type:"background", down:true}, config) }
43-
function help_button(config) { return image_button({margin:{left:14, bottom:-40}, size:[27, 27], pack:PACK_GENERAL, id:134 }) }
44-
function close_button(config) { return image_button({margin:{right:-40, bottom:-40}, size:[27, 27], pack:PACK_GENERAL, id:134, offset:4 }) }
4543
function resource_icon(config) { return __extend({ type : "resource_icon"}, config) }
4644

45+
function help_button(config) { var i = image_button({margin:{left:14, bottom:-40}, size:[27, 27], pack:PACK_GENERAL, id:134 }); return __extend(i, config) }
46+
function close_button(config) { var i = image_button({margin:{right:-40, bottom:-40}, size:[27, 27], pack:PACK_GENERAL, id:134, offset:4 }); return __extend(i, config) }
47+
function next_button(config) { var i = image_button({size:[27, 27], pack:PACK_GENERAL, id:90 }); return __extend(i, config) }
48+
4749
uioptions = {
4850
resource_icons : {pack:PACK_GENERAL, id:129},
4951
}
@@ -753,9 +755,9 @@ mission_briefing_window = {
753755
goal_immediate : { type : "label", pos : {x:32 + 16, y:134 + 32}, body : {w:31, h:1}, font : FONT_NORMAL_YELLOW, enabled: false },
754756
description_panel : { type : "inner_panel", pos : {x:32, y:200}, size: {w:33, h:14} },
755757
description_text : { type : "text", pos: [40, 216], size:[px(36), px(10)], wrap:px(34), font : FONT_NORMAL_WHITE_ON_DARK, font_link:FONT_NORMAL_YELLOW, rich:true, clip_area:true },
756-
difficulty_label : { type : "label", pos:[105, 433], size:[31, 14], font : FONT_NORMAL_BLACK_ON_LIGHT },
758+
difficulty_label : { type : "label", pos:[105, 433], size:[80, 14], font : FONT_NORMAL_BLACK_ON_LIGHT },
757759
back : { type:"image_button", pos:[26, 428], size:[31, 20], pack:PACK_GENERAL, id:90, offset:8 },
758-
start_mission : { type:"image_button", pos:[516, 430], size:[27, 27], pack:PACK_GENERAL, id:90, offset:0 },
760+
start_mission : next_button({pos:[516, 430]}),
759761
dec_difficulty : { type:"image_button", pos:[65, 428], size:[17, 17], pack:PACK_GENERAL, id:212, offset:0 },
760762
inc_difficulty : { type:"image_button", pos:[65 + 18, 428], size:[17, 17], pack:PACK_GENERAL, id:212, offset:3 },
761763
}

src/window/display_options.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void ui::display_options_window::ui_draw_foreground() {
7777
ui.begin_widget(pos);
7878

7979
ui.draw();
80+
8081
panel->ui_params.pos = ui["resolutions"].screen_pos();
8182
panel->draw();
8283

src/window/file_dialog.cpp

+54-37
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,25 @@
1010
#include "content/dir.h"
1111
#include "game/file_editor.h"
1212
#include "graphics/graphics.h"
13+
#include "graphics/elements/scroll_list_panel.h"
1314
#include "graphics/elements/generic_button.h"
1415
#include "graphics/elements/image_button.h"
1516
#include "graphics/elements/lang_text.h"
16-
#include "graphics/elements/panel.h"
1717
#include "graphics/elements/scrollbar.h"
18+
#include "graphics/elements/panel.h"
1819
#include "graphics/image_groups.h"
19-
#include "graphics/text.h"
2020
#include "graphics/window.h"
21+
#include "graphics/text.h"
2122
#include "input/input.h"
2223
#include "content/vfs.h"
2324
#include "io/gamefiles/lang.h"
2425
#include "io/gamestate/boilerplate.h"
2526
#include "widget/input_box.h"
2627
#include "window/window_city.h"
2728
#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"
3031
#include "io/manager.h"
31-
#include <game/settings.h>
32-
#include <string.h>
3332

3433
static const time_millis NOT_EXIST_MESSAGE_TIMEOUT = 500;
3534

@@ -48,7 +47,7 @@ static image_button image_buttons[] = {
4847

4948
static scrollable_list_ui_params ui_params = [] {
5049
scrollable_list_ui_params ret;
51-
ret.pos = { 144, 120 };
50+
//ret.pos = { 144, 120 };
5251
ret.blocks_x = 20;
5352
ret.blocks_y = NUM_FILES_IN_VIEW + 1;
5453
ret.draw_scrollbar_always = true;
@@ -57,7 +56,7 @@ static scrollable_list_ui_params ui_params = [] {
5756

5857
static input_box file_name_input = {144, 80, 20, 2, FONT_NORMAL_WHITE_ON_DARK};
5958

60-
struct file_dialog_data_t {
59+
struct file_dialog : public autoconfig_window_t<file_dialog> {
6160
time_millis message_not_exist_start_time;
6261
file_type type;
6362
file_dialog_type dialog_type;
@@ -67,36 +66,48 @@ struct file_dialog_data_t {
6766
uint8_t typed_name[MAX_FILE_NAME];
6867
char selected_file[MAX_FILE_NAME];
6968
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 {}
7077
};
7178

72-
file_dialog_data_t g_file_dialog_data;
79+
file_dialog g_file_dialog;
7380

7481
file_type_data saved_game_data = {"sav"};
7582
file_type_data saved_game_data_expanded = {"svx"};
7683
file_type_data map_file_data = {"map"};
7784

7885
static void set_chosen_filename(const char* name) {
79-
auto& data = g_file_dialog_data;
86+
auto& data = g_file_dialog;
8087
strcpy(data.selected_file, name);
8188
encoding_from_utf8(data.selected_file, data.typed_name, MAX_PLAYER_NAME);
8289
}
90+
8391
static void clear_chosen_filename() {
8492
set_chosen_filename("");
8593
}
94+
8695
static bool is_chosen_filename(int index) {
87-
auto& data = g_file_dialog_data;
96+
auto& data = g_file_dialog;
8897
return strcmp(data.selected_file, data.panel->get_selected_entry_text(FILE_NO_EXT)) == 0;
8998
}
99+
90100
static bool is_valid_chosen_filename() {
91-
auto& data = g_file_dialog_data;
101+
auto& data = g_file_dialog;
92102
if (strcmp(data.selected_file, "") == 0)
93103
return false;
94104
if (data.panel->get_entry_idx(data.selected_file) > -1)
95105
return true;
96106
return false;
97107
}
108+
98109
static const char* get_chosen_filename(void) {
99-
auto& data = g_file_dialog_data;
110+
auto& data = g_file_dialog;
100111
// Check if we should work with the selected file
101112
uint8_t selected_name[MAX_FILE_NAME];
102113
encoding_from_utf8(data.selected_file, selected_name, MAX_FILE_NAME);
@@ -113,15 +124,13 @@ static const char* get_chosen_filename(void) {
113124
}
114125

115126
static void init(file_type type, file_dialog_type dialog_type) {
116-
auto& data = g_file_dialog_data;
127+
auto& data = g_file_dialog;
117128
data.type = type;
118129
data.file_data = type == FILE_TYPE_SCENARIO ? &map_file_data : &saved_game_data;
119130

120131
// get last saved file name
121132
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));
125134
} else {
126135
encoding_from_utf8(data.file_data->last_loaded_file, data.typed_name, MAX_FILE_NAME);
127136
}
@@ -131,7 +140,7 @@ static void init(file_type type, file_dialog_type dialog_type) {
131140

132141
// populate file list
133142
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);
135144
strcat(folder_name, "/");
136145
if (type == FILE_TYPE_SCENARIO) {
137146
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) {
151160
}
152161

153162
static void draw_foreground(void) {
154-
auto& data = g_file_dialog_data;
163+
auto& data = g_file_dialog;
155164
graphics_set_to_dialog();
156165
uint8_t file[MAX_FILE_NAME] = {0};
157166

@@ -170,6 +179,7 @@ static void draw_foreground(void) {
170179
}
171180
lang_text_draw(43, 5, 224, 342, FONT_NORMAL_BLACK_ON_LIGHT);
172181

182+
data.panel->ui_params.pos = { 144, 120 };
173183
data.panel->draw();
174184

175185
image_buttons_draw({0, 0}, image_buttons, 2);
@@ -183,7 +193,7 @@ static void draw_foreground(void) {
183193
}
184194

185195
static void button_ok_cancel(int is_ok, int param2) {
186-
auto& data = g_file_dialog_data;
196+
auto& data = g_file_dialog;
187197
if (!is_ok) {
188198
input_box_stop(&file_name_input);
189199
window_go_back();
@@ -265,8 +275,9 @@ static void button_ok_cancel(int is_ok, int param2) {
265275

266276
strncpy(data.file_data->last_loaded_file, get_chosen_filename(), MAX_FILE_NAME - 1);
267277
}
278+
268279
static void button_select_file(int index, int param2) {
269-
auto& data = g_file_dialog_data;
280+
auto& data = g_file_dialog;
270281
if (index >= data.panel->get_total_entries()) {
271282
return clear_chosen_filename();
272283
}
@@ -288,13 +299,14 @@ static void button_select_file(int index, int param2) {
288299
// button_ok_cancel(1, 0);
289300
// }
290301
}
302+
291303
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)
293305
button_ok_cancel(1, 0);
294306
}
295307

296308
static void handle_input(const mouse* m, const hotkeys* h) {
297-
auto& data = g_file_dialog_data;
309+
auto& data = g_file_dialog;
298310
if (input_go_back_requested(m, h)) {
299311
input_box_stop(&file_name_input);
300312
window_go_back();
@@ -305,31 +317,36 @@ static void handle_input(const mouse* m, const hotkeys* h) {
305317
return;
306318
}
307319

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)) {
311326
return;
312327
}
313328
}
329+
314330
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");
325341
}
326342

327-
window_type window = {
343+
static window_type window = {
328344
WINDOW_FILE_DIALOG,
329345
window_draw_underlying_window,
330346
draw_foreground,
331347
handle_input
332348
};
349+
333350
init(type, dialog_type);
334351
window_show(&window);
335352
}

src/window/mission_briefing.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ int ui::mission_briefing_window::draw_background() {
4343

4444
auto &ui = g_mission_briefing;
4545

46-
ui["title"].text((pcstr)msg->title.text);
47-
ui["subtitle"].text((pcstr)msg->subtitle.text);
48-
ui["difficulty_label"].text(ui::str(153, g_settings.difficulty + 1));
46+
ui["title"] = (pcstr)msg->title.text;
47+
ui["subtitle"] = (pcstr)msg->subtitle.text;
48+
ui["difficulty_label"] = ui::str(153, g_settings.difficulty + 1);
4949

5050
const pcstr widgets[] = {"goal_0", "goal_1", "goal_2", "goal_3", "goal_4", "goal_5"};
5151
auto goal_label = widgets;

0 commit comments

Comments
 (0)