diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2023-10-19 12:42:07 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2023-10-19 12:42:07 +0200 |
commit | 82eaa0e0de3a6057dab9ac0b2864f576c6ce6d5b (patch) | |
tree | 40a0993c92317f82af0e089edf56acd209dc2cfc | |
parent | 39dcec7217f2b68e8bda9384e8c635468e15ce19 (diff) |
a
-rw-r--r-- | editor/app.hpp | 1 | ||||
-rw-r--r-- | editor/camera.cpp | 20 |
2 files changed, 20 insertions, 1 deletions
diff --git a/editor/app.hpp b/editor/app.hpp index b0f2aa8e..0d99a01f 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -29,6 +29,7 @@ struct critter; struct cursor_state final { Optional<Vector2i> pixel; Optional<global_coords> tile; + Optional<Vector2b> subpixel; bool in_imgui = false; }; diff --git a/editor/camera.cpp b/editor/camera.cpp index 48a783cc..f9299a35 100644 --- a/editor/camera.cpp +++ b/editor/camera.cpp @@ -133,13 +133,31 @@ object_id app::object_at_cursor() void app::update_cursor_tile(const Optional<Vector2i>& pixel) { cursor.pixel = pixel; + // assert_invariant !!cursor.tile == !!cursor.subpixel; if (pixel) { auto coord = M->pixel_to_tile(Vector2d{*pixel}); - cursor.tile = global_coords{coord.chunk(), coord.local(), _z_level}; + auto tile = global_coords{coord.chunk(), coord.local(), _z_level}; + cursor.tile = tile; + + constexpr auto eps = 1e-6f; + constexpr auto m = TILE_SIZE2 * Vector2(1-eps, 1-eps); + const auto tile_ = Vector2(M->pixel_to_tile_(Vector2d(*pixel))); + const auto curchunk = Vector2(tile.chunk()); + const auto subpixel_ = Vector2(std::fmod(tile_[0], 1.f), std::fmod(tile_[1], 1.f)); + auto subpixel = m * Vector2(curchunk[0] < 0 ? 1 + subpixel_[0] : subpixel_[0], + curchunk[1] < 0 ? 1 + subpixel_[1] : subpixel_[1]); + constexpr auto half_tile = Vector2(iTILE_SIZE2/2); + subpixel -= half_tile; + subpixel.x() = Math::clamp(std::round(subpixel.x()), -half_tile.x(), half_tile.x()-1); + subpixel.y() = Math::clamp(std::round(subpixel.y()), -half_tile.y(), half_tile.y()-1); + cursor.subpixel = Vector2b(subpixel); } else + { cursor.tile = NullOpt; + cursor.subpixel = NullOpt; + } } } // namespace floormat |