diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-21 15:56:31 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-21 15:56:31 +0200 |
commit | 361d35b800f7f3f217324104bc181ef4a49e10e1 (patch) | |
tree | ae5b63b10ba0cc918e416bd22fe448edde781527 /main | |
parent | 33530af5f134ea91a24a0dc3333765dbd891f01a (diff) |
editor work
Diffstat (limited to 'main')
-rw-r--r-- | main/app.cpp | 21 | ||||
-rw-r--r-- | main/app.hpp | 20 | ||||
-rw-r--r-- | main/camera.cpp | 2 | ||||
-rw-r--r-- | main/editor.cpp | 69 | ||||
-rw-r--r-- | main/editor.hpp | 9 | ||||
-rw-r--r-- | main/update.cpp | 16 |
6 files changed, 89 insertions, 48 deletions
diff --git a/main/app.cpp b/main/app.cpp index 07271e27..157c523f 100644 --- a/main/app.cpp +++ b/main/app.cpp @@ -105,17 +105,7 @@ void app::mousePressEvent(Platform::Sdl2Application::MouseEvent& event) else if (_cursor_tile) { const auto& tile = *_cursor_tile; - int button; - switch (event.button()) - { - case MouseEvent::Button::Left: button = 0; break; - case MouseEvent::Button::Right: button = 1; break; - case MouseEvent::Button::Middle: button = 2; break; - case MouseEvent::Button::X1: button = 5; break; - case MouseEvent::Button::X2: button = 6; break; - default: button = -1; break; - } - do_mouse_click(tile, button); + do_mouse_click(tile, (int)event.button()); } } @@ -123,12 +113,7 @@ void app::mouseReleaseEvent(Platform::Sdl2Application::MouseEvent& event) { if (_imgui.handleMouseReleaseEvent(event)) return event.setAccepted(); -#if 0 - using Button = Platform::Sdl2Application::MouseEvent::Button; - if (event.button() == Button::Left) - { - } -#endif + do_mouse_release((int)event.button()); } void app::mouseMoveEvent(Platform::Sdl2Application::MouseMoveEvent& event) @@ -139,6 +124,8 @@ void app::mouseMoveEvent(Platform::Sdl2Application::MouseMoveEvent& event) else _cursor_pixel = event.position(); recalc_cursor_tile(); + if (_cursor_tile) + do_mouse_move(*_cursor_tile); } void app::mouseScrollEvent(Platform::Sdl2Application::MouseScrollEvent& event) diff --git a/main/app.hpp b/main/app.hpp index 883316be..5ae0ff9a 100644 --- a/main/app.hpp +++ b/main/app.hpp @@ -39,21 +39,23 @@ private: void do_key(KeyEvent::Key k, KeyEvent::Modifiers m, bool pressed, bool repeated); void do_mouse_click(global_coords pos, int button); + void do_mouse_release(int button); + void do_mouse_move(global_coords pos); void do_camera(double dt); void reset_camera_offset(); void recalc_cursor_tile(); void recalc_viewport(Vector2i size); - void viewportEvent(ViewportEvent& event) override; - void mousePressEvent(MouseEvent& event) override; - void mouseReleaseEvent(MouseEvent& event) override; - void mouseMoveEvent(MouseMoveEvent& event) override; - void mouseScrollEvent(MouseScrollEvent& event) override; - void textInputEvent(TextInputEvent& event) override; - void keyPressEvent(KeyEvent& event) override; - void keyReleaseEvent(KeyEvent& event) override; - void anyEvent(SDL_Event& event) override; + [[maybe_unused]] void viewportEvent(ViewportEvent& event) override; + [[maybe_unused]] void mousePressEvent(MouseEvent& event) override; + [[maybe_unused]] void mouseReleaseEvent(MouseEvent& event) override; + [[maybe_unused]] void mouseMoveEvent(MouseMoveEvent& event) override; + [[maybe_unused]] void mouseScrollEvent(MouseScrollEvent& event) override; + [[maybe_unused]] void textInputEvent(TextInputEvent& event) override; + [[maybe_unused]] void keyPressEvent(KeyEvent& event) override; + [[maybe_unused]] void keyReleaseEvent(KeyEvent& event) override; + [[maybe_unused]] void anyEvent(SDL_Event& event) override; void event_focus_out(); void event_focus_in(); diff --git a/main/camera.cpp b/main/camera.cpp index a063b619..31e50626 100644 --- a/main/camera.cpp +++ b/main/camera.cpp @@ -37,6 +37,8 @@ void app::do_camera(double dt) return; } recalc_cursor_tile(); + if (_cursor_tile) + do_mouse_move(*_cursor_tile); } void app::reset_camera_offset() diff --git a/main/editor.cpp b/main/editor.cpp index 8183b4a4..7a19d841 100644 --- a/main/editor.cpp +++ b/main/editor.cpp @@ -122,23 +122,29 @@ std::optional<std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t>> tile_type:: { switch (_selection_mode) { - case sel_none: return std::nullopt; - case sel_tile: return _selected_tile; - case sel_perm: return get_selected_perm(); - default : unreachable(); + case sel_none: + return std::nullopt; + case sel_tile: + return _selected_tile; + case sel_perm: + return get_selected_perm(); + default: + fm_warn_once("invalid editor mode '%u'", (unsigned)_selection_mode); + break; } } -void tile_type::place_tile(world& world, global_coords pos, std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t> img) +void tile_type::place_tile(world& world, global_coords pos, + const std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t>& img) { const auto& [c, t] = world[pos]; + const auto& [atlas, variant] = img; switch (_mode) { case editor_mode::select: - fm_warn("wrong tile mode 'select'"); break; + break; case editor_mode::floor: { const auto& [c, t] = world[pos]; - auto& [atlas, variant] = img; t.ground_image = { atlas, variant }; break; } @@ -153,21 +159,48 @@ editor::editor() set_mode(editor_mode::floor); // TODO } -void editor::maybe_place_tile(world& world, const global_coords pos, int mouse_button) +void editor::set_mode(editor_mode mode) +{ + _mode = mode; + on_release(); +} + +void editor::on_release() { - if (mouse_button == 0) + _last_pos = std::nullopt; +} + +void editor::on_mouse_move(world& world, const global_coords pos) +{ + if (_last_pos && *_last_pos != pos) { - switch (_mode) + _last_pos = pos; + on_click(world, pos); + } +} + +void editor::on_click(world& world, global_coords pos) +{ + switch (_mode) + { + case editor_mode::select: + break; + case editor_mode::floor: { + auto opt = _floor.get_selected(); + if (opt) { - case editor_mode::select: break; - case editor_mode::floor: { - auto opt = _floor.get_selected(); - if (opt) - _floor.place_tile(world, pos, *opt); - break; - } - case editor_mode::walls: break; // TODO + _last_pos = pos; + _floor.place_tile(world, pos, *opt); } + else + on_release(); + break; + } + case editor_mode::walls: + break; // TODO + default: + fm_warn_once("invalid editor mode '%u'", (unsigned)_mode); + break; } } diff --git a/main/editor.hpp b/main/editor.hpp index efdeb5d9..c96c0b2f 100644 --- a/main/editor.hpp +++ b/main/editor.hpp @@ -38,7 +38,7 @@ struct tile_type final bool is_tile_selected(const std::shared_ptr<tile_atlas>& atlas, std::uint8_t variant); bool is_permutation_selected(const std::shared_ptr<tile_atlas>& atlas); std::optional<std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t>> get_selected(); - void place_tile(world& world, global_coords pos, std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t> img); + void place_tile(world& world, global_coords pos, const std::tuple<std::shared_ptr<tile_atlas>, std::uint8_t>& img); private: enum selection_mode : std::uint8_t { @@ -65,12 +65,14 @@ struct editor final [[nodiscard]] bool dirty() const { return _dirty; } void set_dirty(bool value) { _dirty = value; } [[nodiscard]] editor_mode mode() const { return _mode; } - void set_mode(editor_mode mode) { _mode = mode; } + void set_mode(editor_mode mode); tile_type& floor() { return _floor; } const tile_type& floor() const { return _floor; } - void maybe_place_tile(world& world, global_coords pos, int mouse_button); + void on_click(world& world, global_coords pos); + void on_mouse_move(world& world, const global_coords pos); + void on_release(); editor(); editor(editor&&) noexcept = default; @@ -79,6 +81,7 @@ struct editor final private: tile_type _floor{editor_mode::floor, "floor"}; + std::optional<global_coords> _last_pos; editor_mode _mode = editor_mode::select; bool _dirty = false; }; diff --git a/main/update.cpp b/main/update.cpp index 8b0e221f..fac359e8 100644 --- a/main/update.cpp +++ b/main/update.cpp @@ -18,7 +18,21 @@ void app::make_test_chunk(chunk& c) void app::do_mouse_click(const global_coords pos, int button) { - _editor.maybe_place_tile(_world, pos, button); + if (button == SDL_BUTTON_LEFT) + _editor.on_click(_world, pos); + else + _editor.on_release(); +} + +void app::do_mouse_release(int button) +{ + (void)button; + _editor.on_release(); +} + +void app::do_mouse_move(global_coords pos) +{ + _editor.on_mouse_move(_world, pos); } void app::update(double dt) |