paint: keymap search fixes

This commit is contained in:
luboslenco
2026-08-23 16:33:28 +02:00
parent 152255d843
commit ba66c8aebf
3 changed files with 80 additions and 22 deletions
+1 -1
View File
@@ -86,7 +86,7 @@ any_map_t *keymap_get_default() {
any_map_set(keymap, "toggle_browser", "`"); any_map_set(keymap, "toggle_browser", "`");
any_map_set(keymap, "node_overview", "z"); any_map_set(keymap, "node_overview", "z");
any_map_set(keymap, "node_search", "space"); any_map_set(keymap, "node_search", "space");
any_map_set(keymap, "operator_search", "space"); any_map_set(keymap, "keymap_search", "space");
any_map_set(keymap, "decal_mask", "ctrl"); any_map_set(keymap, "decal_mask", "ctrl");
any_map_set(keymap, "brush_camera_align", "z"); any_map_set(keymap, "brush_camera_align", "z");
any_map_set(keymap, "grid_snap", "x"); any_map_set(keymap, "grid_snap", "x");
+75 -17
View File
@@ -1,47 +1,103 @@
#include "../global.h" #include "../global.h"
i32 ui_base_operator_search_offset = 0; static i32 _ui_search_offset = 0;
bool _ui_base_operator_search_first; static bool _ui_search_first;
static char *_ui_search_shortcut = NULL;
static string_array_t *_ui_search_keys = NULL;
static char *ui_search_key_name(char *key) {
if (string_equals(key, "ctrl")) {
key = "control";
}
for (i32 i = 0; i < keyboard_keys->length; ++i) {
if (string_equals(keyboard_keys->buffer[i], key)) {
return keyboard_keys->buffer[i];
}
}
return NULL;
}
static void ui_search_release_keys(void *_) {
for (i32 i = 0; i < _ui_search_keys->length; ++i) {
i32_map_set(keyboard_keys_down, _ui_search_keys->buffer[i], false);
}
array_free(_ui_search_keys);
free(_ui_search_keys);
_ui_search_keys = NULL;
}
static void ui_search_press_keys(void *_) {
if (!base_ui_enabled) {
sys_notify_on_next_frame(&ui_search_press_keys, NULL);
return;
}
// Simulate key press
any_array_t *parts = string_split(_ui_search_shortcut, "+");
_ui_search_keys = string_array_create(0);
for (i32 i = 0; i < parts->length; ++i) {
char *key = ui_search_key_name(parts->buffer[i]);
if (key == NULL) {
continue;
}
string_array_push(_ui_search_keys, key);
i32_map_set(keyboard_keys_down, key, true);
i32_map_set(keyboard_keys_started, key, true);
string_array_push(keyboard_keys_frame, key);
}
string_split_free(parts);
sys_notify_on_end_frame(&ui_search_release_keys, NULL);
}
static void ui_search_run(char *shortcut) {
char *key = string_index_of(shortcut, "+") > 0 ? shortcut + string_last_index_of(shortcut, "+") + 1 : shortcut;
_ui_search_shortcut = shortcut;
sys_notify_on_next_frame(&ui_search_press_keys, NULL);
}
void ui_base_operator_search_menu_draw() { void ui_base_operator_search_menu_draw() {
ui_menu_h = UI_ELEMENT_H() * 8; ui_menu_h = UI_ELEMENT_H() * 8;
ui_handle_t *search_handle = ui_handle(__ID__); ui_handle_t *search_handle = ui_handle(__ID__);
char *search = ui_text_input(search_handle, "", UI_ALIGN_LEFT, true, true); char *search = to_lower_case(ui_text_input(search_handle, "", UI_ALIGN_LEFT, true, true));
g_ui->changed = false; g_ui->changed = false;
if (_ui_base_operator_search_first) { if (_ui_search_first) {
_ui_base_operator_search_first = false; _ui_search_first = false;
search_handle->text = ""; search_handle->text = "";
ui_start_text_edit(search_handle, UI_ALIGN_LEFT); // Focus search bar ui_start_text_edit(search_handle, UI_ALIGN_LEFT); // Focus search bar
} }
if (search_handle->changed) { if (search_handle->changed) {
ui_base_operator_search_offset = 0; _ui_search_offset = 0;
} }
if (g_ui->is_key_pressed) { // Move selection if (g_ui->is_key_pressed) { // Move selection
if (g_ui->key_code == KEY_CODE_DOWN && ui_base_operator_search_offset < 6) { if (g_ui->key_code == KEY_CODE_DOWN && _ui_search_offset < 6) {
ui_base_operator_search_offset++; _ui_search_offset++;
} }
if (g_ui->key_code == KEY_CODE_UP && ui_base_operator_search_offset > 0) { if (g_ui->key_code == KEY_CODE_UP && _ui_search_offset > 0) {
ui_base_operator_search_offset--; _ui_search_offset--;
} }
} }
bool enter = keyboard_down("enter"); bool enter = keyboard_down("enter");
i32 count = 0; i32 count = 0;
i32 BUTTON_COL = g_theme->BUTTON_COL; i32 _BUTTON_COL = g_theme->BUTTON_COL;
bool _FILL_BUTTON_BG = g_theme->FILL_BUTTON_BG;
g_theme->FILL_BUTTON_BG = true;
bool _SHADOWS = g_theme->SHADOWS;
g_theme->SHADOWS = false;
string_array_t *keys = map_keys(g_keymap); string_array_t *keys = map_keys(g_keymap);
for (i32 i = 0; i < keys->length; ++i) { for (i32 i = 0; i < keys->length; ++i) {
char *n = keys->buffer[i]; char *n = keys->buffer[i];
if (string_index_of(n, search) >= 0) { if (string_index_of(to_lower_case(n), search) >= 0) {
g_theme->BUTTON_COL = count == ui_base_operator_search_offset ? g_theme->HIGHLIGHT_COL : g_theme->SEPARATOR_COL; g_theme->BUTTON_COL = count == _ui_search_offset ? g_theme->HIGHLIGHT_COL : g_theme->SEPARATOR_COL;
if (ui_button(n, UI_ALIGN_LEFT, any_map_get(g_keymap, n)) || (enter && count == ui_base_operator_search_offset)) { if (ui_button(n, UI_ALIGN_LEFT, any_map_get(g_keymap, n)) || (enter && count == _ui_search_offset)) {
if (enter) { if (enter) {
g_ui->changed = true; g_ui->changed = true;
count = 6; // Trigger break count = 6; // Trigger break
} }
// operator_run(n); ui_search_run(any_map_get(g_keymap, n));
} }
if (++count > 6) { if (++count > 6) {
break; break;
@@ -55,10 +111,12 @@ void ui_base_operator_search_menu_draw() {
g_ui->changed = true; g_ui->changed = true;
search_handle->text = ""; search_handle->text = "";
} }
g_theme->BUTTON_COL = BUTTON_COL; g_theme->BUTTON_COL = _BUTTON_COL;
g_theme->FILL_BUTTON_BG = _FILL_BUTTON_BG;
g_theme->SHADOWS = _SHADOWS;
} }
void ui_base_operator_search() { void ui_base_operator_search() {
_ui_base_operator_search_first = true; _ui_search_first = true;
ui_menu_draw(&ui_base_operator_search_menu_draw, -1, -1); ui_menu_draw(&ui_base_operator_search_menu_draw, -1, -1);
} }
+1 -1
View File
@@ -330,7 +330,7 @@ void util_shortcut_viewport() {
ui_menu_draw(&ui_base_menu_draw_viewport_mode, -1, -1); ui_menu_draw(&ui_base_menu_draw_viewport_mode, -1, -1);
} }
} }
if (keymap_shortcut(any_map_get(g_keymap, "operator_search"), SHORTCUT_TYPE_STARTED)) { if (keymap_shortcut(any_map_get(g_keymap, "keymap_search"), SHORTCUT_TYPE_STARTED)) {
ui_base_operator_search(); ui_base_operator_search();
} }
} }