From 196dd3d6372fa34b4af08467aa27fd5a8a90b5c0 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 25 May 2024 21:31:47 -0500 Subject: [PATCH] [studio/modlib] Add SelectionTracker --- .../include/studio/selectiontracker.hpp | 90 +++++++++++++++++++ .../studio/modlib/include/studio/studio.hpp | 2 +- 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 deps/nostalgia/src/olympic/studio/modlib/include/studio/selectiontracker.hpp diff --git a/deps/nostalgia/src/olympic/studio/modlib/include/studio/selectiontracker.hpp b/deps/nostalgia/src/olympic/studio/modlib/include/studio/selectiontracker.hpp new file mode 100644 index 0000000..8132e44 --- /dev/null +++ b/deps/nostalgia/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/deps/nostalgia/src/olympic/studio/modlib/include/studio/studio.hpp b/deps/nostalgia/src/olympic/studio/modlib/include/studio/studio.hpp index fd9c0ea..73f21f5 100644 --- a/deps/nostalgia/src/olympic/studio/modlib/include/studio/studio.hpp +++ b/deps/nostalgia/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