diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-12-03 08:12:24 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-12-03 08:17:30 +0100 |
commit | ea09c317db4d243e064da095370545e43ff4c0c7 (patch) | |
tree | 9085c2ca1c468cc7edf1f184060a0d5f8b471a21 | |
parent | 0ec9ab2a67ba246b50d266376e0cb2819f940960 (diff) |
editor: simplify find_clickable_scenery
-rw-r--r-- | editor/app.hpp | 2 | ||||
-rw-r--r-- | editor/draw.cpp | 45 | ||||
-rw-r--r-- | editor/update.cpp | 3 | ||||
-rw-r--r-- | src/scenery.cpp | 2 | ||||
-rw-r--r-- | src/scenery.hpp | 2 |
5 files changed, 28 insertions, 26 deletions
diff --git a/editor/app.hpp b/editor/app.hpp index c81f5f22..25391d62 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -87,7 +87,7 @@ private: void do_camera(float dt, const key_set& cmds, int mods); void reset_camera_offset(); - clickable_scenery* find_clickable_scenery(Vector2i pixel); + clickable_scenery* find_clickable_scenery(const Optional<Vector2i>& pixel); void do_quicksave(); void do_quickload(); diff --git a/editor/draw.cpp b/editor/draw.cpp index 61c8d07f..339e0f6c 100644 --- a/editor/draw.cpp +++ b/editor/draw.cpp @@ -64,33 +64,36 @@ void app::draw() render_menu(); } -clickable_scenery* app::find_clickable_scenery(Vector2i pixel_) +clickable_scenery* app::find_clickable_scenery(const Optional<Vector2i>& pixel_) { + if (!pixel_ || _editor.mode() != editor_mode::none) + return nullptr; + + const auto pixel = Vector2ui(*pixel_); clickable_scenery* item = nullptr; float depth = -1; - if (cursor.tile) - { - const auto array = M->clickable_scenery(); - const auto pixel = Vector2ui(pixel_); - for (clickable_scenery& c : array) - if (c.depth > depth && c.dest.contains(pixel)) + const auto array = M->clickable_scenery(); + for (clickable_scenery& c : array) + if (c.depth > depth && c.dest.contains(pixel)) + { + const auto pos_ = pixel - c.dest.min() + c.src.min(); + const auto pos = c.atlas.group(c.item.r).mirror_from.isEmpty() + ? pos_ + : Vector2ui(c.src.sizeX() - 1 - pos_[0], pos_[1]); + const auto stride = c.atlas.info().pixel_size[0]; + std::size_t idx = pos.y() * stride + pos.x(); + fm_debug_assert(idx < c.bitmask.size()); + if (c.bitmask[idx]) { - const auto pos_ = pixel - c.dest.min() + c.src.min(); - const auto pos = c.atlas.group(c.item.r).mirror_from.isEmpty() - ? pos_ - : Vector2ui(c.src.sizeX() - 1 - pos_[0], pos_[1]); - const auto stride = c.atlas.info().pixel_size[0]; - std::size_t idx = pos.y() * stride + pos.x(); - fm_debug_assert(idx < c.bitmask.size()); - if (c.bitmask[idx]) - { - depth = c.depth; - item = &c; - } + depth = c.depth; + item = &c; } - } - return item; + } + if (item && item->item.can_activate(item->atlas)) + return item; + else + return nullptr; } } // namespace floormat diff --git a/editor/update.cpp b/editor/update.cpp index ad4fb355..516d2536 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -141,8 +141,7 @@ void app::update(float dt) update_cursor_tile(cursor.pixel); clear_non_repeated_keys(); - if (clickable_scenery* s; _editor.mode() == editor_mode::none && cursor.tile && - (s = find_clickable_scenery(*cursor.pixel)) && s->item.can_activate()) + if (clickable_scenery* s = find_clickable_scenery(cursor.pixel)) M->set_cursor(std::uint32_t(Cursor::Hand)); else M->set_cursor(std::uint32_t(Cursor::Arrow)); diff --git a/src/scenery.cpp b/src/scenery.cpp index 28884ad8..89c04eec 100644 --- a/src/scenery.cpp +++ b/src/scenery.cpp @@ -50,7 +50,7 @@ scenery::scenery(door_tag_t, const anim_atlas& atlas, rotation r, bool is_open) fm_assert(atlas.group(r).frames.size() >= 2); } -bool scenery::can_activate() const noexcept +bool scenery::can_activate(const anim_atlas&) const noexcept { return interactive; } diff --git a/src/scenery.hpp b/src/scenery.hpp index b94e1d2a..5cfe3827 100644 --- a/src/scenery.hpp +++ b/src/scenery.hpp @@ -49,7 +49,7 @@ struct scenery final bool operator==(const scenery&) const noexcept; - bool can_activate() const noexcept; + bool can_activate(const anim_atlas& anim) const noexcept; bool activate(const anim_atlas& atlas); void update(float dt, const anim_atlas& anim); }; |