diff --git a/src/olympic/studio/modlib/include/studio/selectiontracker.hpp b/src/olympic/studio/modlib/include/studio/selectiontracker.hpp new file mode 100644 index 00000000..8132e44f --- /dev/null +++ b/src/olympic/studio/modlib/include/studio/selectiontracker.hpp @@ -0,0 +1,90 @@ + +#pragma once + +#include + +#include +#include +#include + +namespace studio { + +struct Selection {ox::Point a, b;}; + +constexpr auto iterateSelection(studio::Selection const&sel, auto const&cb) { + for (auto x = sel.a.x; x <= sel.b.x; ++x) { + for (auto y = sel.a.y; y <= sel.b.y; ++y) { + if constexpr(ox::is_same_v) { + return cb(x, y); + } else { + cb(x, y); + } + } + } +}; + +class SelectionTracker { + private: + bool m_selectionOngoing{}; + ox::Point m_pointA; + ox::Point m_pointB; + public: + [[nodiscard]] + constexpr bool selectionOngoing() const noexcept { + return m_selectionOngoing; + } + + constexpr void startSelection(ox::Point cursor) noexcept { + m_pointA = cursor; + m_pointB = cursor; + m_selectionOngoing = true; + } + + constexpr void updateCursorPoint(ox::Point cursor, bool allowStart = true) noexcept { + if (!m_selectionOngoing && allowStart) { + m_pointA = cursor; + m_selectionOngoing = true; + } + if (m_selectionOngoing) { + m_pointB = cursor; + } + } + + constexpr void updateCursorPoint(ox::Vec2 cursor, bool allowStart = true) noexcept { + updateCursorPoint( + ox::Point{ + static_cast(cursor.x), + static_cast(cursor.y), + }, + allowStart); + } + + constexpr void updateCursorPoint(ImVec2 cursor, bool allowStart = true) noexcept { + updateCursorPoint( + ox::Point{ + static_cast(cursor.x), + static_cast(cursor.y), + }, + allowStart); + } + + constexpr void finishSelection() noexcept { + m_selectionOngoing = {}; + } + + [[nodiscard]] + constexpr Selection selection() const noexcept { + return { + { + ox::min(m_pointA.x, m_pointB.x), + ox::min(m_pointA.y, m_pointB.y), + }, + { + ox::max(m_pointA.x, m_pointB.x), + ox::max(m_pointA.y, m_pointB.y), + }, + }; + } +}; + +} \ No newline at end of file diff --git a/src/olympic/studio/modlib/include/studio/studio.hpp b/src/olympic/studio/modlib/include/studio/studio.hpp index fd9c0ea5..73f21f5f 100644 --- a/src/olympic/studio/modlib/include/studio/studio.hpp +++ b/src/olympic/studio/modlib/include/studio/studio.hpp @@ -4,7 +4,6 @@ #pragma once -#include #include #include #include @@ -13,6 +12,7 @@ #include #include #include +#include #include #include #include