summaryrefslogtreecommitdiffhomepage
path: root/editor
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-26 21:05:03 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-26 21:57:44 +0200
commit07f9e1834243783a8baa51da26869582214c6880 (patch)
treed347256d9f2df6279bab0e5bcde2ce0169c37313 /editor
parentdded6652999447b6a56f6c00d07e439a4c5d678c (diff)
add non-repeat keybindings
Diffstat (limited to 'editor')
-rw-r--r--editor/app.hpp19
-rw-r--r--editor/events.cpp10
-rw-r--r--editor/update.cpp41
3 files changed, 59 insertions, 11 deletions
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<tile_atlas>;
- 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<key> get_nonrepeat_keys();
void reset_camera_offset();
void update_cursor_tile(const std::optional<Vector2i>& pixel);
@@ -97,7 +100,7 @@ private:
wireframe_mesh<wireframe::quad> _wireframe_quad;
wireframe_mesh<wireframe::box> _wireframe_box;
editor _editor;
- enum_bitset<key> keys;
+ enum_bitset<key> 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<int>& 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<key>
+{
+ enum_bitset<key> ret;
+ using type = std::underlying_type_t<key>;
+ 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<key>;
+ 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