[jasper/world/studio] Make tile drop work with map tile selection
Some checks failed
Build / build (push) Failing after 3m31s

This commit is contained in:
Gary Talent 2024-05-25 22:03:52 -05:00
parent ee86436d58
commit 46544872cd
2 changed files with 37 additions and 15 deletions

View File

@ -87,6 +87,12 @@ constexpr studio::Selection fbPtToTileAddr(
return sel;
}
[[nodiscard]]
constexpr bool inside(auto const val, int const min, int const max) noexcept {
auto const v = static_cast<int>(val);
return v < max && v > min;
};
WorldEditorImGui::WorldEditorImGui(studio::StudioContext &sctx, ox::StringView path):
Editor(path),
m_sctx(sctx),
@ -245,17 +251,13 @@ void WorldEditorImGui::handleSelection(ox::Size const&paneSz, float fbPaneScale)
auto const&io = ImGui::GetIO();
if (io.MouseDown[0]) {
auto const fbPos = world::fbPos(ox::Vec2{io.MousePos});
auto constexpr inside = [](auto val, int min, int max) {
auto const v = static_cast<int>(val);
return v < max && v > min;
};
auto const startSel = io.MouseClicked[0]
&& inside(fbPos.x, 0, paneSz.width)
&& inside(fbPos.y, 0, paneSz.height);
m_selection.updateCursorPoint(fbPos, startSel);
auto const scaledViewSz = static_cast<ox::Vec2>(m_view.drawSize()) * fbPaneScale;
m_selection.updateCursorPoint(fbPtToTileAddr(fbPos, scaledViewSz), startSel);
if (m_selection.selectionOngoing()) {
m_view.setSelection(fbPtToTileAddr(m_selection.selection(), scaledViewSz));
m_view.setSelection(m_selection.selection());
}
} else if (io.MouseReleased[0]) {
m_selection.finishSelection();
@ -273,18 +275,31 @@ ox::Error WorldEditorImGui::handleDrop(float fbPaneScale) noexcept {
static_cast<float>(viewSz.width) * fbPaneScale,
static_cast<float>(viewSz.height) * fbPaneScale});
if (tileAddr.x < m_doc.columns && tileAddr.y < m_doc.rows) {
ox::Vector<ModifyTilesCommand::Mod> mods;
if (tileSelected(tileAddr)) {
studio::iterateSelection(m_selection.selection(), [&](int32_t x, int32_t y) {
mods.emplace_back(ModifyTilesCommand::Mod{
.layer = m_activeLayer,
.tileAddr = {x, y},
.objId = objId.objId,
.setId = objId.setId,
});
});
} else {
mods = {
{
.layer = m_activeLayer,
.tileAddr = tileAddr,
.objId = objId.objId,
.setId = objId.setId,
},
};
}
std::ignore = pushCommand<ModifyTilesCommand>(
m_doc,
m_worldStatic,
m_objCache,
ox::Vector<ModifyTilesCommand::Mod>{
{
.layer = m_activeLayer,
.tileAddr = tileAddr,
.objId = objId.objId,
.setId = objId.setId,
},
});
std::move(mods));
}
oxReturnError(loadWorldStatic(m_objCache, m_doc).moveTo(m_worldStatic));
return ox::Error{};
@ -322,9 +337,13 @@ ox::Error WorldEditorImGui::loadObjectSets() noexcept {
}
ox::Error WorldEditorImGui::undoStackChanged(studio::UndoCommand const*cmd) {
auto const clearSelection = dynamic_cast<EditWorldSizeCommand const*>(cmd) != nullptr;
oxReturnError(m_view.setupWorld());
return {};
}
bool WorldEditorImGui::tileSelected(ox::Point const&pt) const noexcept {
auto const sel = m_selection.selection();
return inside(pt.x, sel.a.x, sel.b.x) && inside(pt.y, sel.a.y, sel.b.y);
}
}

View File

@ -85,6 +85,9 @@ class WorldEditorImGui: public studio::Editor {
ox::Error loadObjectSets() noexcept;
ox::Error undoStackChanged(studio::UndoCommand const*);
[[nodiscard]]
bool tileSelected(ox::Point const&pt) const noexcept;
};
}