Skip to content

Commit

Permalink
File dialog fixes
Browse files Browse the repository at this point in the history
- Fix crashes when using the arrow keys on empty file lists
- Fix typing text moving the selected file if key set to scrolling hotkey
- Save dialog: fix typed text selecting a save being case sensitive
- Prevent saving with an empty name
- Fix "clear text" button positioning on save file dialog
  • Loading branch information
crudelios committed Aug 7, 2023
1 parent cab4711 commit a91b117
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .ci_scripts/install_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ then
install_sdl_macos "SDL2_mixer" $SDL_MIXER_VERSION
elif [ "$BUILD_TARGET" == "android" ]
then
if [ ! -f augustus.keystore ]
if [ ! -f "android/augustus.keystore" ]
then
BUILDTYPE=debug
else
Expand Down
5 changes: 3 additions & 2 deletions src/input/hotkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ static void set_definition_for_action(hotkey_action action, hotkey_definition *d
def->value = BUILDING_HIGHWAY;
break;

default:
default:
def->action = 0;
}
}
Expand Down Expand Up @@ -556,7 +556,8 @@ void hotkey_key_pressed(key_type key, key_modifier_type modifiers, int repeat)
int found_action = 0;
for (int i = 0; i < data.num_definitions; i++) {
hotkey_definition *def = &data.definitions[i];
if ((window_is(WINDOW_ASSET_PREVIEWER) || window_is(WINDOW_EDITOR_EMPIRE)) && key == KEY_TYPE_F5 && def->action != &data.hotkey_state.f5_pressed) {
if ((window_is(WINDOW_ASSET_PREVIEWER) || window_is(WINDOW_EDITOR_EMPIRE)) &&
key == KEY_TYPE_F5 && def->action != &data.hotkey_state.f5_pressed) {
continue;
}
if (def->key == key && def->modifiers == modifiers && (!repeat || def->repeatable)) {
Expand Down
22 changes: 22 additions & 0 deletions src/platform/keyboard_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "input/keys.h"
#include "input/keyboard.h"
#include "input/mouse.h"
#include "input/scroll.h"

static int is_alt_down(SDL_KeyboardEvent *event)
{
Expand Down Expand Up @@ -284,6 +285,27 @@ void platform_handle_key_down(SDL_KeyboardEvent *event)

// handle hotkeys
key_type key = get_key_from_scancode(event->keysym.scancode);

if (keyboard_is_capturing()) {
// Special event keys to handle when text input is active
switch (key) {
case KEY_TYPE_UP:
scroll_arrow_up(1);
break;
case KEY_TYPE_DOWN:
scroll_arrow_down(1);
// Fixed hotkeys - always handle them
case KEY_TYPE_ENTER:
case KEY_TYPE_ESCAPE:
case KEY_TYPE_F5:
case KEY_TYPE_DELETE:
case KEY_TYPE_BACKSPACE:
break;
default:
return;
}
}

key_modifier_type mod = get_modifier(event->keysym.mod);
hotkey_key_pressed(key, mod, event->repeat);

Expand Down
28 changes: 21 additions & 7 deletions src/window/file_dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ static input_box main_input = {
.font = FONT_NORMAL_WHITE,
.text = data.filter_text,
.text_length = FILTER_TEXT_SIZE,
.put_clear_button_outside_box = 1,
.on_change = input_box_changed
};

Expand Down Expand Up @@ -284,11 +283,13 @@ static void init(file_type type, file_dialog_type dialog_type)
main_input.text = data.typed_name;
main_input.text_length = FILE_NAME_MAX;
main_input.width_blocks = 38;
main_input.put_clear_button_outside_box = 0;
} else {
main_input.placeholder = lang_get_string(CUSTOM_TRANSLATION, TR_SAVE_DIALOG_FILTER);
main_input.text = data.filter_text;
main_input.text_length = FILTER_TEXT_SIZE;
main_input.width_blocks = 18;
main_input.put_clear_button_outside_box = 1;
}

input_box_start(&main_input);
Expand Down Expand Up @@ -407,7 +408,13 @@ static void draw_foreground(void)
}
} else {
if (data.dialog_type == FILE_DIALOG_SAVE) {
text_draw_centered(translation_for(TR_SAVE_DIALOG_NEW_FILE), 362, 246, 246, FONT_NORMAL_BLACK, 0);
if (*data.typed_name) {
text_draw_centered(translation_for(TR_SAVE_DIALOG_NEW_FILE),
362, 246, 246, FONT_NORMAL_BLACK, 0);
} else {
text_draw_centered(translation_for(TR_SAVE_DIALOG_SELECT_FILE),
362, 246, 246, FONT_NORMAL_BLACK, 0);
}
} else {
translation_key key = data.savegame_info_status == SAVEGAME_STATUS_INVALID ?
TR_SAVE_DIALOG_INVALID_FILE : TR_SAVE_DIALOG_INCOMPATIBLE_VERSION;
Expand Down Expand Up @@ -587,7 +594,9 @@ static void input_box_changed(int is_addition_at_end)
scroll_index = find_first_file_with_prefix(data.selected_file);
}
file_append_extension(data.selected_file, data.file_data->extension);
if (scroll_index >= 0 && strcmp(data.selected_file, data.filtered_file_list.files[scroll_index].name) == 0) {
if (scroll_index >= 0 &&
platform_file_manager_compare_filename(data.selected_file,
data.filtered_file_list.files[scroll_index].name) == 0) {
data.selected_index = scroll_index + 1;
} else {
data.selected_index = 0;
Expand Down Expand Up @@ -659,6 +668,10 @@ static void button_ok_cancel(int is_ok, int param2)
return;
}

if (!*data.typed_name) {
return;
}

const char *filename = prepare_filename();

if (data.dialog_type != FILE_DIALOG_SAVE && !file_exists(filename, NOT_LOCALIZED)) {
Expand Down Expand Up @@ -766,16 +779,17 @@ static void button_toggle_sort_type(int param1, int param2)

static void button_select_file(int index, int param2)
{
if (index + scrollbar.scroll_position >= data.filtered_file_list.num_files) {
int selection = index + scrollbar.scroll_position;
if (selection < 0 || selection >= data.filtered_file_list.num_files) {
if (data.double_click) {
data.double_click = 0;
}
return;
}
if (strcmp(data.selected_file, data.filtered_file_list.files[scrollbar.scroll_position + index].name) != 0) {
data.selected_index = scrollbar.scroll_position + index + 1;
if (strcmp(data.selected_file, data.filtered_file_list.files[selection].name) != 0) {
data.selected_index = selection + 1;
data.message_not_exist_start_time = 0;
strncpy(data.selected_file, data.filtered_file_list.files[scrollbar.scroll_position + index].name, FILE_NAME_MAX - 1);
strncpy(data.selected_file, data.filtered_file_list.files[selection].name, FILE_NAME_MAX - 1);
if (data.dialog_type == FILE_DIALOG_SAVE) {
file_remove_extension(data.selected_file);
}
Expand Down

0 comments on commit a91b117

Please sign in to comment.