diff options
Diffstat (limited to 'editor')
-rw-r--r-- | editor/app.hpp | 1 | ||||
-rw-r--r-- | editor/editor.cpp | 32 | ||||
-rw-r--r-- | editor/editor.hpp | 5 | ||||
-rw-r--r-- | editor/events.cpp | 81 | ||||
-rw-r--r-- | editor/keys.hpp | 7 | ||||
-rw-r--r-- | editor/update.cpp | 18 |
6 files changed, 77 insertions, 67 deletions
diff --git a/editor/app.hpp b/editor/app.hpp index e7808dcc..aa4f6490 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -58,6 +58,7 @@ private: void on_mouse_up_down(const mouse_button_event& event, bool is_down) noexcept override; void on_mouse_scroll(const mouse_scroll_event& event) noexcept override; void on_key_up_down(const key_event& event, bool is_down) noexcept override; + std::tuple<key, int> resolve_keybinding(int k, int mods) const; void on_text_input_event(const text_input_event& event) noexcept override; //bool on_text_editing_event(const text_editing_event& event) noexcept override; void on_viewport_event(const Magnum::Math::Vector2<int>& size) noexcept override; diff --git a/editor/editor.cpp b/editor/editor.cpp index eb0d0f86..fc2d1a1f 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -295,29 +295,27 @@ void editor::on_mouse_move(world& world, global_coords& pos, int mods) pos.x = last_pos.coord.x; break; } - last_pos = { pos, snap }; - on_click(world, pos, mods); + last_pos = { pos, snap, last_pos.btn }; + on_click(world, pos, mods, last_pos.btn); } } -void editor::on_click(world& world, global_coords pos, int mods) +void editor::on_click(world& world, global_coords pos, int mods, button b) { - if (!current()) - return; - auto& mode = *current(); - - if (auto opt = mode.get_selected(); opt) + if (auto* mode = current(); mode != nullptr) { - if (!_last_pos) - _last_pos = { pos, snap_mode::none }; - auto snap = _last_pos->snap; - if (snap == snap_mode::none) - snap = mode.check_snap(mods); - _last_pos = { pos, snap }; - mode.place_tile(world, pos, opt); - } - else + if (auto opt = mode->get_selected(); opt) + { + _last_pos = { pos, mode->check_snap(mods), _last_pos ? _last_pos->btn : b }; + switch (tile_image empty; b) + { + case button::place: return mode->place_tile(world, pos, opt); + case button::remove: return mode->place_tile(world, pos, empty); + default: break; + } + } on_release(); + } } } // namespace floormat diff --git a/editor/editor.hpp b/editor/editor.hpp index a56e1fdb..460bb8c8 100644 --- a/editor/editor.hpp +++ b/editor/editor.hpp @@ -85,7 +85,9 @@ struct editor final tile_editor* current() noexcept; const tile_editor* current() const noexcept; - void on_click(world& world, global_coords pos, int mods); + enum class button : std::uint8_t { none, place, remove, }; + + void on_click(world& world, global_coords pos, int mods, button b); void on_mouse_move(world& world, global_coords& pos, int modifiers); void on_release(); @@ -107,6 +109,7 @@ private: struct drag_pos final { global_coords coord; snap_mode snap = snap_mode::none; + button btn; }; std::optional<drag_pos> _last_pos; editor_mode _mode = editor_mode::none; diff --git a/editor/events.cpp b/editor/events.cpp index 96b8ba8a..3c6268ee 100644 --- a/editor/events.cpp +++ b/editor/events.cpp @@ -91,59 +91,58 @@ void app::on_mouse_scroll(const mouse_scroll_event& event) noexcept _imgui.handleMouseScrollEvent(e); } -void app::on_key_up_down(const key_event& event, bool is_down) noexcept +auto app::resolve_keybinding(int k_, int mods_) const -> std::tuple<key, int> { - using KeyEvent = Platform::Sdl2Application::KeyEvent; - struct Ev final { - using Key = KeyEvent::Key; - using Modifier = KeyEvent::Modifier; - using Modifiers = KeyEvent::Modifiers; - accessor(Key, key) - accessor(Modifiers, modifiers) - } e = {Ev::Key(event.key), Ev::Modifier(event.mods)}; - [[maybe_unused]] constexpr int CTRL = kmod_ctrl; [[maybe_unused]] constexpr int SHIFT = kmod_shift; [[maybe_unused]] constexpr int ALT = kmod_alt; [[maybe_unused]] constexpr int SUPER = kmod_super; - const auto mods = fixup_mods(event.mods); - - const key x = fm_begin( - int k = event.key | mods; - constexpr kmod list[] = { kmod_none, kmod_super, kmod_alt, kmod_shift, kmod_ctrl, }; - int last = ~0; - for (kmod mod1 : list) + const int k = k_ | fixup_mods(mods_); + constexpr kmod list[] = { kmod_none, kmod_super, kmod_alt, kmod_shift, kmod_ctrl, }; + int last = ~0; + for (int k1 = k; kmod mod1 : list) + { + k1 &= ~mod1; + for (int k2 = k1; kmod mod2 : list) { - k &= ~mod1; - for (int k2 = k; kmod mod2 : list) + k2 &= ~mod2; + if (k2 == last) + continue; + last = k2; + switch (int mods = k2 & kmod_mask; k2) { - k2 &= ~mod2; - if (k2 == last) - continue; - last = k2; - switch (k2) - { - case SDLK_w: return key_camera_up; - case SDLK_a: return key_camera_left; - case SDLK_s: return key_camera_down; - case SDLK_d: return key_camera_right; - case SDLK_HOME: return key_camera_reset; - case SDLK_r: return key_rotate_tile; - case SDLK_1: return key_mode_none; - case SDLK_2: return key_mode_floor; - case SDLK_3: return key_mode_walls; - case SDLK_F5: return key_quicksave; - case SDLK_F9: return key_quickload; - case SDLK_q | CTRL: return key_quit; - default: break; - } + default: continue; + case SDLK_w: return { key_camera_up, mods }; + case SDLK_a: return { key_camera_left, mods }; + case SDLK_s: return { key_camera_down, mods }; + case SDLK_d: return { key_camera_right, mods }; + case SDLK_HOME: return { key_camera_reset, mods }; + case SDLK_r: return { key_rotate_tile, mods }; + case SDLK_1: return { key_mode_none, mods }; + case SDLK_2: return { key_mode_floor, mods }; + case SDLK_3: return { key_mode_walls, mods }; + case SDLK_F5: return { key_quicksave, mods }; + case SDLK_F9: return { key_quickload, mods }; + case SDLK_q | CTRL: return { key_quit, mods }; } } + } + return { key_COUNT, k & kmod_mask }; +} +void app::on_key_up_down(const key_event& event, bool is_down) noexcept +{ + using KeyEvent = Platform::Sdl2Application::KeyEvent; + struct Ev final { + using Key = KeyEvent::Key; + using Modifier = KeyEvent::Modifier; + using Modifiers = KeyEvent::Modifiers; + accessor(Key, key) + accessor(Modifiers, modifiers) + } e = {Ev::Key(event.key), Ev::Modifier(event.mods)}; - return key_COUNT; - ); + auto [x, mods] = resolve_keybinding(event.key, event.mods); if (x == key_COUNT) void(); diff --git a/editor/keys.hpp b/editor/keys.hpp index a94e771e..611f78e1 100644 --- a/editor/keys.hpp +++ b/editor/keys.hpp @@ -6,9 +6,10 @@ namespace floormat { enum kmod : int { kmod_none = 0x0000, kmod_shift = 0x0001 << 8, - kmod_ctrl = 0x0040 << 8, - kmod_alt = 0x0100 << 8, - kmod_super = 0x0400 << 8, + kmod_ctrl = 0x0040 << 9, + kmod_alt = 0x0100 << 10, + kmod_super = 0x0400 << 11, + kmod_mask = kmod_shift | kmod_ctrl | kmod_alt | kmod_super, }; enum key : std::uint32_t { diff --git a/editor/update.cpp b/editor/update.cpp index 567b10e6..b96d80fe 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -43,13 +43,21 @@ void app::do_mouse_move(int mods) void app::do_mouse_up_down(std::uint8_t button, bool is_down, int mods) { - if (cursor.tile && !cursor.in_imgui && button == mouse_button_left && is_down) - _editor.on_click(M->world(), *cursor.tile, mods); - else + if (cursor.tile && !cursor.in_imgui && is_down) { - _editor.on_release(); - update_cursor_tile(cursor.pixel); + auto& w = M->world(); + auto pos = *cursor.tile; + switch (button) + { + case mouse_button_left: + return _editor.on_click(w, pos, mods, editor::button::place); + case mouse_button_middle: + return _editor.on_click(w, pos, mods, editor::button::remove); + default: break; + } } + _editor.on_release(); + update_cursor_tile(cursor.pixel); } void app::do_key(key k, int mods) |