From 07f9e1834243783a8baa51da26869582214c6880 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 26 Oct 2022 21:05:03 +0200 Subject: add non-repeat keybindings --- editor/app.hpp | 19 +++++++++++-------- editor/events.cpp | 10 ++++++++++ editor/update.cpp | 41 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 59 insertions(+), 11 deletions(-) (limited to 'editor') diff --git a/editor/app.hpp b/editor/app.hpp index 4cdaff60..1fe2fc16 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -36,6 +36,13 @@ struct app final : floormat_app fm_DECLARE_DELETED_COPY_ASSIGNMENT(app); fm_DECLARE_DEPRECATED_MOVE_ASSIGNMENT(app); + enum class key : int { + camera_up, camera_left, camera_right, camera_down, camera_reset, + rotate_tile, quicksave, quickload, + quit, + MAX = quit, COUNT, NO_REPEAT = rotate_tile, + }; + void update(float dt) override; void maybe_initialize_chunk(const chunk_coords& pos, chunk& c) override; void draw_msaa() override; @@ -61,19 +68,15 @@ struct app final : floormat_app private: using tile_atlas_ = std::shared_ptr; - enum class key : int { - camera_up, camera_left, camera_right, camera_down, camera_reset, - rotate_tile, quicksave, quickload, - quit, - MAX = quit, COUNT - }; - void maybe_initialize_chunk_(const chunk_coords& pos, chunk& c); void do_mouse_move(); void do_mouse_up_down(std::uint8_t button, bool is_down); + void do_camera(float dt); + void do_keys(); + enum_bitset get_nonrepeat_keys(); void reset_camera_offset(); void update_cursor_tile(const std::optional& pixel); @@ -97,7 +100,7 @@ private: wireframe_mesh _wireframe_quad; wireframe_mesh _wireframe_box; editor _editor; - enum_bitset keys; + enum_bitset keys, keys_repeat; cursor_state cursor; }; diff --git a/editor/events.cpp b/editor/events.cpp index 513f71c6..58330433 100644 --- a/editor/events.cpp +++ b/editor/events.cpp @@ -89,10 +89,16 @@ void app::on_key_up_down(const floormat::key_event& event, bool is_down) noexcep case SDLK_ESCAPE: return key::quit; }); if (x != key::COUNT) + { keys[x] = is_down; + keys_repeat[x] = is_down ? event.is_repeated : false; + } } else + { keys = {}; + keys_repeat = {}; + } } void app::on_text_input_event(const floormat::text_input_event& event) noexcept @@ -101,7 +107,10 @@ void app::on_text_input_event(const floormat::text_input_event& event) noexcept accessor(Containers::StringView, text) } e = {event.text}; if (_imgui.handleTextInputEvent(e)) + { keys = {}; + keys_repeat = {}; + } } void app::on_viewport_event(const Math::Vector2& size) noexcept @@ -113,6 +122,7 @@ void app::on_focus_out() noexcept { update_cursor_tile(std::nullopt); keys = {}; + keys_repeat = {}; } void app::on_mouse_leave() noexcept diff --git a/editor/update.cpp b/editor/update.cpp index 28c2ffd3..d4bfa2d5 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -49,19 +49,54 @@ void app::do_mouse_up_down(std::uint8_t button, bool is_down) _editor.on_release(); } +auto app::get_nonrepeat_keys() -> enum_bitset +{ + enum_bitset ret; + using type = std::underlying_type_t; + for (auto i = (type)key::NO_REPEAT; i < (type)key::COUNT; i++) + { + auto idx = key{i}; + if (keys_repeat[idx]) + keys[idx] = false; + else + ret[idx] = keys[idx]; + keys[idx] = false; + keys_repeat[idx] = false; + } + return ret; +} + void app::do_keys() { + auto k = get_nonrepeat_keys(); + using type = std::underlying_type_t; + for (auto i = (type)key::NO_REPEAT; i < (type)key::COUNT; i++) + { + auto idx = key{i}; + if (keys_repeat[idx]) + k[idx] = false; + keys[idx] = false; + keys_repeat[idx] = false; + } + + if (k[key::quicksave]) + do_quicksave(); + if (k[key::quickload]) + do_quickload(); + if (auto* ed = _editor.current(); ed && k[key::rotate_tile]) + ed->rotate_tile(); } void app::update(float dt) { - do_camera(dt); - draw_ui(); if (keys[key::quit]) M->quit(0); - if (!keys.any()) + else { + do_camera(dt); + draw_ui(); do_keys(); + } } } // namespace floormat -- cgit v1.2.3