Improve keybind options UX

Changelog:
- Deal with set key to already binded key
    - Binded key will null the existing key
    - Non-binded will be reset back
- Colour code the options during and after key bind changes
main
En Yi 2025-08-23 22:52:20 +08:00
parent 0088fd64de
commit 95c38549b6
3 changed files with 66 additions and 6 deletions

View File

@ -151,5 +151,10 @@ const char* ExtGetKeyName(int key)
return "SPACE";
}
if (key == KEY_NULL)
{
return "NONE";
}
return "";
}

View File

@ -55,7 +55,7 @@ static void options_scene_render_func(Scene_t* scene)
Sprite_t* level_board = get_sprite(&scene->engine->assets, "lvl_board");
#define TITLE_FONT_SIZE 40
#define FONT_SIZE 24
#define FONT_SIZE 22
#define FONT_COLOUR BLACK
float start_x = scene->engine->intended_window_size.x / 2;
start_x -= level_board->frame_size.x / 2;
@ -84,16 +84,27 @@ static void options_scene_render_func(Scene_t* scene)
{
DrawTextEx(*menu_font, ">>", (Vector2){start_x,y_offset}, FONT_SIZE, 4, BLACK);
}
bool changed = false;
if (keybind_info.key == keybind_info.original_key)
{
sprintf(buffer, "%s : %s", keybind_info.action_name, keybind_info.key_name);
DrawTextEx(*menu_font, buffer, (Vector2){start_x+32, y_offset}, FONT_SIZE, 4, BLACK);
}
else
{
changed = true;
sprintf(buffer, "%s : %s => %s", keybind_info.action_name, keybind_info.original_key_name, keybind_info.key_name);
DrawTextEx(*menu_font, buffer, (Vector2){start_x+32, y_offset}, FONT_SIZE, 4, BLUE);
}
if (data->mode == OPTIONS_KEYBIND_MODE && line == data->curr_selection)
{
DrawTextEx(*menu_font, buffer, (Vector2){start_x+32, y_offset}, FONT_SIZE, 4, RED);
}
else
{
DrawTextEx(*menu_font, buffer, (Vector2){start_x+32, y_offset}, FONT_SIZE, 4, changed? BLUE : BLACK);
}
y_offset += FONT_SIZE;
line++;
}
@ -120,6 +131,11 @@ static void options_scene_render_func(Scene_t* scene)
y_offset += FONT_SIZE;
DrawTextEx(*menu_font, "Press a Key...", (Vector2){start_x+32, y_offset}, FONT_SIZE, 4, BLACK);
}
y_offset += FONT_SIZE;
if (data->keybind_status == 1)
{
DrawTextEx(*menu_font, "Reserved key! Try another key.", (Vector2){start_x+32, y_offset}, FONT_SIZE, 4, RED);
}
EndTextureMode();
}
@ -149,14 +165,51 @@ static void wait_for_keymap_input(Scene_t* scene)
return;
}
if (scene->engine->last_input_key == KEY_NULL)
int new_key = scene->engine->last_input_key;
if (new_key == KEY_NULL)
{
return;
}
data->keybinds_info.elems[data->curr_selection - 1].key = scene->engine->last_input_key;
data->keybinds_info.elems[data->curr_selection - 1].key_name = ExtGetKeyName(scene->engine->last_input_key);
// Invalid/Reserved key are empty strings
if (ExtGetKeyName(new_key)[0] == 0)
{
data->keybind_status = 1;
return;
}
{
// To check for keys already set by new_key
// Also to fill in any KEY_NULL if the original key is not new key
// Requires the index to modify the current element
KeyBindInfo_t info;
size_t i = 0;
sc_array_foreach(&data->keybinds_info, info)
{
if (info.key == new_key)
{
data->keybinds_info.elems[i].key = data->keybinds_info.elems[i].original_key;
data->keybinds_info.elems[i].key_name = ExtGetKeyName(data->keybinds_info.elems[i].key);
if (info.original_key == new_key)
{
data->keybinds_info.elems[i].key = KEY_NULL;
data->keybinds_info.elems[i].key_name = ExtGetKeyName(data->keybinds_info.elems[i].key);
}
}
else if (info.key == KEY_NULL)
{
data->keybinds_info.elems[i].key = data->keybinds_info.elems[i].original_key;
data->keybinds_info.elems[i].key_name = ExtGetKeyName(data->keybinds_info.elems[i].key);
}
i++;
}
}
data->keybinds_info.elems[data->curr_selection - 1].key = new_key;
data->keybinds_info.elems[data->curr_selection - 1].key_name = ExtGetKeyName(new_key);
data->keybind_status = 0;
data->mode = OPTIONS_NORMAL_MODE;
}
@ -281,6 +334,7 @@ static void reset_options_scene(Scene_t* scene)
}
data->curr_selection = 0;
data->mode = OPTIONS_NORMAL_MODE;
data->keybind_status = 0;
}
static void options_action_remap_func(Scene_t* scene)

View File

@ -155,6 +155,7 @@ sc_array_def(KeyBindInfo_t, keybinds);
typedef struct OptionSceneData {
struct sc_array_keybinds keybinds_info; // Selection index -> keybind info
int keybind_status;
uint16_t n_selections;
uint16_t curr_selection;
OptionSceneMode_t mode;