[studio/modlib] Add SelectionTracker

This commit is contained in:
Gary Talent 2024-05-25 21:31:47 -05:00
parent dc20c66797
commit 8ee016c130
2 changed files with 91 additions and 1 deletions

View File

@ -0,0 +1,90 @@
#pragma once
#include <imgui.h>
#include <ox/std/math.hpp>
#include <ox/std/point.hpp>
#include <ox/std/typetraits.hpp>
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<decltype(cb(x, y)), ox::Error>) {
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<int32_t>(cursor.x),
static_cast<int32_t>(cursor.y),
},
allowStart);
}
constexpr void updateCursorPoint(ImVec2 cursor, bool allowStart = true) noexcept {
updateCursorPoint(
ox::Point{
static_cast<int32_t>(cursor.x),
static_cast<int32_t>(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),
},
};
}
};
}

View File

@ -4,7 +4,6 @@
#pragma once
#include <studio/color.hpp>
#include <studio/context.hpp>
#include <studio/editor.hpp>
#include <studio/filedialog.hpp>
@ -13,6 +12,7 @@
#include <studio/itemmaker.hpp>
#include <studio/popup.hpp>
#include <studio/project.hpp>
#include <studio/selectiontracker.hpp>
#include <studio/task.hpp>
#include <studio/undocommand.hpp>
#include <studio/undostack.hpp>