[jasper/world/studio] Make selection tracking safer, allow clearing selection
All checks were successful
Build / build (push) Successful in 3m10s

This commit is contained in:
Gary Talent 2024-05-25 22:22:24 -05:00
parent a49af0bc32
commit 704434bc3c
4 changed files with 23 additions and 8 deletions

View File

@ -255,12 +255,17 @@ void WorldEditorImGui::handleSelection(ox::Size const&paneSz, float fbPaneScale)
&& inside(fbPos.x, 0, paneSz.width) && inside(fbPos.x, 0, paneSz.width)
&& inside(fbPos.y, 0, paneSz.height); && inside(fbPos.y, 0, paneSz.height);
auto const scaledViewSz = static_cast<ox::Vec2>(m_view.drawSize()) * fbPaneScale; auto const scaledViewSz = static_cast<ox::Vec2>(m_view.drawSize()) * fbPaneScale;
m_selection.updateCursorPoint(fbPtToTileAddr(fbPos, scaledViewSz), startSel); m_selTracker.updateCursorPoint(fbPtToTileAddr(fbPos, scaledViewSz), startSel);
if (m_selection.selectionOngoing()) { if (m_selTracker.selectionOngoing()) {
m_view.setSelection(m_selection.selection()); m_selection.emplace(m_selTracker.selection());
m_view.setSelection(*m_selection);
} }
} else if (io.MouseReleased[0]) { } else if (io.MouseReleased[0]) {
m_selection.finishSelection(); m_selTracker.finishSelection();
}
if (io.KeyCtrl && io.KeysDown[ImGuiKey_G]) {
m_selection.reset();
m_view.clearSelection();
} }
} }
@ -277,7 +282,7 @@ ox::Error WorldEditorImGui::handleDrop(float fbPaneScale) noexcept {
if (tileAddr.x < m_doc.columns && tileAddr.y < m_doc.rows) { if (tileAddr.x < m_doc.columns && tileAddr.y < m_doc.rows) {
ox::Vector<ModifyTilesCommand::Mod> mods; ox::Vector<ModifyTilesCommand::Mod> mods;
if (tileSelected(tileAddr)) { if (tileSelected(tileAddr)) {
studio::iterateSelection(m_selection.selection(), [&](int32_t x, int32_t y) { studio::iterateSelection(*m_selection, [&](int32_t x, int32_t y) {
mods.emplace_back(ModifyTilesCommand::Mod{ mods.emplace_back(ModifyTilesCommand::Mod{
.layer = m_activeLayer, .layer = m_activeLayer,
.tileAddr = {x, y}, .tileAddr = {x, y},
@ -342,7 +347,10 @@ ox::Error WorldEditorImGui::undoStackChanged(studio::UndoCommand const*) {
} }
bool WorldEditorImGui::tileSelected(ox::Point const&pt) const noexcept { bool WorldEditorImGui::tileSelected(ox::Point const&pt) const noexcept {
auto const sel = m_selection.selection(); if (!m_selection) {
return false;
}
auto const&sel = *m_selection;
return inside(pt.x, sel.a.x, sel.b.x) && inside(pt.y, sel.a.y, sel.b.y); return inside(pt.x, sel.a.x, sel.b.x) && inside(pt.y, sel.a.y, sel.b.y);
} }

View File

@ -19,7 +19,8 @@ namespace jasper::world {
class WorldEditorImGui: public studio::Editor { class WorldEditorImGui: public studio::Editor {
private: private:
studio::SelectionTracker m_selection; studio::SelectionTracker m_selTracker;
ox::Optional<studio::Selection> m_selection;
uint8_t m_activeLayer{}; uint8_t m_activeLayer{};
studio::StudioContext &m_sctx; studio::StudioContext &m_sctx;
studio::ig::FilePicker m_objSetPicker{ studio::ig::FilePicker m_objSetPicker{

View File

@ -37,12 +37,16 @@ void WorldEditorView::draw(ox::Size const&targetSz) noexcept {
m_highlighter.draw(); m_highlighter.draw();
} }
void WorldEditorView::setSelection(studio::Selection const&sel) noexcept { void WorldEditorView::clearSelection() noexcept {
if (m_selection) { if (m_selection) {
studio::iterateSelection(*m_selection, [this](int32_t x, int32_t y) { studio::iterateSelection(*m_selection, [this](int32_t x, int32_t y) {
std::ignore = m_highlighter.setTileHighlight({x, y}, false); std::ignore = m_highlighter.setTileHighlight({x, y}, false);
}); });
} }
}
void WorldEditorView::setSelection(studio::Selection const&sel) noexcept {
clearSelection();
m_selection.emplace(sel); m_selection.emplace(sel);
studio::iterateSelection(*m_selection, [this](int32_t x, int32_t y) { studio::iterateSelection(*m_selection, [this](int32_t x, int32_t y) {
std::ignore = m_highlighter.setTileHighlight({x, y}, true); std::ignore = m_highlighter.setTileHighlight({x, y}, true);

View File

@ -39,6 +39,8 @@ class WorldEditorView {
void draw(ox::Size const&targetSz) noexcept; void draw(ox::Size const&targetSz) noexcept;
void clearSelection() noexcept;
void setSelection(studio::Selection const&sel) noexcept; void setSelection(studio::Selection const&sel) noexcept;
[[nodiscard]] [[nodiscard]]