From: Brendan Hansen Date: Wed, 9 Mar 2022 03:17:07 +0000 (-0600) Subject: bugfix with repeated keys X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9d4254435a56371c704dc080223226fb4e519e08;p=bar-game.git bugfix with repeated keys --- diff --git a/src/gfx/ui.onyx b/src/gfx/ui.onyx index 16baaca..6afc690 100644 --- a/src/gfx/ui.onyx +++ b/src/gfx/ui.onyx @@ -222,9 +222,8 @@ Textbox_Theme :: struct { cursor_animation := 0.0f; cursor_animation_speed := 0.02f; - @HACK // Otherwise the backspace button deletes a character - // every frame, which obviously is way too fast. - backspace_timeout := 0.0f; + @HACK // Otherwise the action keys are evaluated every frame. + action_key_timeout := 0.0f; } textbox_editing_state := Textbox_Editing_State.{}; @@ -286,36 +285,32 @@ draw_textbox :: (use r: Rect, text_buffer: ^[..] u8, placeholder := null_str, th textbox_editing_state.cursor_position = screen_to_cursor(^font, text_x, text_y, text, ~~mx, ~~my); } - move_towards(^textbox_editing_state.backspace_timeout, 0, 0.05f); + move_towards(^textbox_editing_state.action_key_timeout, 0, 0.05f); + if textbox_editing_state.action_key_timeout == 0 { + keys := input_get_keys_this_frame(); + for key: iter.as_iterator(^keys) { + textbox_editing_state.action_key_timeout = 0.17f; - keys := input_get_keys_this_frame(); - for key: iter.as_iterator(^keys) { - switch key.key { - case GLFW_KEY_ESCAPE { - set_active_item(0); - input_release_keys(); - } + switch key.key { + case GLFW_KEY_ESCAPE { + set_active_item(0); + input_release_keys(); + } - case GLFW_KEY_LEFT do textbox_editing_state.cursor_position -= 1; - case GLFW_KEY_RIGHT do textbox_editing_state.cursor_position += 1; - case GLFW_KEY_END do textbox_editing_state.cursor_position = text_buffer.count; - case GLFW_KEY_HOME do textbox_editing_state.cursor_position = 0; + case GLFW_KEY_LEFT do textbox_editing_state.cursor_position -= 1; + case GLFW_KEY_RIGHT do textbox_editing_state.cursor_position += 1; + case GLFW_KEY_END do textbox_editing_state.cursor_position = text_buffer.count; + case GLFW_KEY_HOME do textbox_editing_state.cursor_position = 0; - case GLFW_KEY_BACKSPACE { - if textbox_editing_state.backspace_timeout == 0 { + case GLFW_KEY_BACKSPACE { if textbox_editing_state.cursor_position > 0 { array.delete(text_buffer, textbox_editing_state.cursor_position - 1); } textbox_editing_state.cursor_position = math.max(~~0, textbox_editing_state.cursor_position - 1); - - textbox_editing_state.backspace_timeout = 0.20f; } - } - case GLFW_KEY_DELETE { - if textbox_editing_state.backspace_timeout == 0 { + case GLFW_KEY_DELETE { array.delete(text_buffer, textbox_editing_state.cursor_position); - textbox_editing_state.backspace_timeout = 0.20f; } } }