diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-01 19:45:50 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-11-01 19:45:50 +0100 |
commit | 472767f60387caf955d55d8a43e39e156d5e9457 (patch) | |
tree | e4920444d18d2d43bc44fc6d15890bab882e2756 /editor | |
parent | 601c42ae7c7df2adbff931ff4ac30ba39499da69 (diff) |
editor: improve switching snap modes
Diffstat (limited to 'editor')
-rw-r--r-- | editor/editor.cpp | 64 | ||||
-rw-r--r-- | editor/editor.hpp | 4 |
2 files changed, 40 insertions, 28 deletions
diff --git a/editor/editor.cpp b/editor/editor.cpp index bb1948cc..f76a42d0 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -198,10 +198,6 @@ void tile_editor::set_rotation(editor_wall_rotation r) auto tile_editor::check_snap(int mods) const -> snap_mode { - enum : int { - KMOD_CTRL = 0x0040 | 0x0080, - KMOD_SHIFT = 0x0001 | 0x0002, - }; const bool ctrl = mods & kmod_ctrl, shift = mods & kmod_shift; if (!(ctrl | shift)) @@ -279,46 +275,60 @@ auto editor::get_snap_value(snap_mode snap, int mods) const -> snap_mode return snap_mode::none; } -void editor::on_mouse_move(world& world, global_coords& pos, int mods) +global_coords editor::apply_snap(global_coords pos, global_coords last, snap_mode snap) noexcept { - if (!current()) - return; + switch (snap) + { + default: + break; + case snap_mode::horizontal: + pos.y = last.y; + break; + case snap_mode::vertical: + pos.x = last.x; + break; + } + return pos; +} - if (_last_pos) +void editor::on_mouse_move(world& world, global_coords& pos, int mods) +{ + if (auto* mode = current(); mode && _last_pos && _last_pos->btn != button::none) { - auto& last_pos = *_last_pos; - const snap_mode snap = get_snap_value(last_pos.snap, mods); - switch (snap) + auto& last = *_last_pos; + Vector2i offset = pos - last.coord; + const snap_mode snap = get_snap_value(last.snap, mods); + global_coords draw_coord = apply_snap(last.draw_coord + offset, last.draw_coord, snap); + if (pos != _last_pos->coord) { - default: - break; - case snap_mode::horizontal: - pos.y = last_pos.coord.y; - break; - case snap_mode::vertical: - pos.x = last_pos.coord.x; - break; + _last_pos = { pos, draw_coord, snap, last.btn }; + on_click_(world, draw_coord, mods, last.btn); } - last_pos = { pos, snap, last_pos.btn }; - on_click(world, pos, mods, last_pos.btn); + pos = draw_coord; } } -void editor::on_click(world& world, global_coords pos, int mods, button b) +void editor::on_click_(world& world, global_coords pos, int mods, button b) { if (auto* mode = current(); mode != nullptr) - { if (auto opt = mode->get_selected(); opt) { - _last_pos = { pos, mode->check_snap(mods), _last_pos ? _last_pos->btn : b }; - switch (tile_image_proto empty; b) + switch (b) { case button::place: return mode->place_tile(world, pos, opt); - case button::remove: return mode->place_tile(world, pos, empty); + case button::remove: return mode->place_tile(world, pos, {}); default: break; } } - on_release(); + on_release(); +} + +void editor::on_click(world& world, global_coords pos, int mods, button b) +{ + if (auto* mode = current(); mode != nullptr) + { + _last_pos = { pos, pos, mode->check_snap(mods), b }; + on_click_(world, pos, mods, b); } } diff --git a/editor/editor.hpp b/editor/editor.hpp index 4d6b48df..66531814 100644 --- a/editor/editor.hpp +++ b/editor/editor.hpp @@ -88,6 +88,7 @@ struct editor final enum class button : std::uint8_t { none, place, remove, }; void on_click(world& world, global_coords pos, int mods, button b); + 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(); @@ -102,12 +103,13 @@ struct editor final private: snap_mode get_snap_value(snap_mode snap, int mods) const; + static global_coords apply_snap(global_coords pos, global_coords last, snap_mode snap) noexcept; tile_editor _floor{ editor_mode::floor, "floor" }; tile_editor _wall { editor_mode::walls, "wall" }; struct drag_pos final { - global_coords coord; + global_coords coord, draw_coord; snap_mode snap = snap_mode::none; button btn; }; |