[jasper/world/studio/worldobjectseteditor] Add file drag/drop support

This commit is contained in:
Gary Talent 2025-01-19 14:32:41 -06:00
parent ac743834d3
commit 3df4e1662e
6 changed files with 72 additions and 44 deletions

View File

@ -7,19 +7,24 @@
#include "commands.hpp"
#include "addpalette.hpp"
#include <keel/media.hpp>
namespace jasper::world {
AddPalette::AddPalette(WorldObjectSet &doc, ox::FileAddress palAddr) noexcept:
m_doc(doc),
m_palAddr(std::move(palAddr)) {
AddPalette::AddPalette(WorldObjectSet &doc, ox::FileAddress palAddr, keel::Context &kctx) noexcept:
m_doc{doc},
m_palAddr{std::move(palAddr)},
m_kctx{kctx} {
}
ox::Error AddPalette::redo() noexcept {
m_doc.palettes.emplace_back(m_palAddr);
std::sort(m_doc.palettes.begin(), m_doc.palettes.end(),
[](ox::FileAddress const&a, ox::FileAddress const&b) {
return a.getPath().or_value("") < b.getPath().or_value("");
});
//std::sort(m_doc.palettes.begin(), m_doc.palettes.end(),
// [this](ox::FileAddress const&a, ox::FileAddress const&b) {
// auto const aPath = uuidUrlToPath(m_kctx, a.getPath().or_value("")).or_value("");
// auto const bPath = uuidUrlToPath(m_kctx, b.getPath().or_value("")).or_value("");
// return aPath < bPath;
// });
return {};
}

View File

@ -16,8 +16,9 @@ class AddPalette: public studio::UndoCommand {
private:
WorldObjectSet &m_doc;
ox::FileAddress m_palAddr;
keel::Context &m_kctx;
public:
AddPalette(WorldObjectSet &doc, ox::FileAddress palAddr) noexcept;
AddPalette(WorldObjectSet &doc, ox::FileAddress palAddr, keel::Context &kctx) noexcept;
ox::Error redo() noexcept override;
ox::Error undo() noexcept override;
[[nodiscard]]

View File

@ -5,36 +5,39 @@
#include "commands.hpp"
#include "changetilesheet.hpp"
#include <utility>
#include <keel/media.hpp>
namespace jasper::world {
ChangeTileSheet::ChangeTileSheet(
keel::Context &kctx,
WorldObjectSet &doc,
ox::FileAddress newVal,
size_t newIdx,
size_t &selectedTilesheetIdx) noexcept:
ox::String &path) noexcept:
m_kctx{kctx},
m_doc(doc),
m_newVal(std::move(newVal)),
m_oldIdx(selectedTilesheetIdx),
m_newIdx(newIdx),
m_selectedTilesheetIdx(selectedTilesheetIdx) {
m_valAlt(std::move(newVal)),
m_selectedTilesheet(path) {
}
ox::Error ChangeTileSheet::redo() noexcept {
m_oldVal = std::move(m_doc.tilesheet);
m_doc.tilesheet = std::move(m_newVal);
m_selectedTilesheetIdx = m_newIdx;
return {};
return swap();
}
ox::Error ChangeTileSheet::undo() noexcept {
m_newVal = std::move(m_doc.tilesheet);
m_doc.tilesheet = std::move(m_oldVal);
m_selectedTilesheetIdx = m_oldIdx;
return {};
return swap();
}
int ChangeTileSheet::commandId() const noexcept {
return static_cast<int>(WorldObjCommand::ChangeTileSheet);
}
ox::Error ChangeTileSheet::swap() noexcept {
std::swap(m_doc.tilesheet, m_valAlt);
OX_REQUIRE_M(path, m_doc.tilesheet.getPath());
m_selectedTilesheet = keel::uuidUrlToPath(m_kctx, path).or_value(path);
return {};
}
}

View File

@ -14,22 +14,23 @@ namespace jasper::world {
class ChangeTileSheet: public studio::UndoCommand {
private:
keel::Context &m_kctx;
WorldObjectSet &m_doc;
ox::FileAddress m_newVal;
ox::FileAddress m_oldVal;
size_t m_oldIdx{};
size_t m_newIdx{};
size_t &m_selectedTilesheetIdx;
ox::FileAddress m_valAlt;
ox::String &m_selectedTilesheet;
public:
ChangeTileSheet(
WorldObjectSet &doc,
ox::FileAddress newVal,
size_t newIdx,
size_t &selectedTilesheetIdx) noexcept;
keel::Context &kctx,
WorldObjectSet &doc,
ox::FileAddress newVal,
ox::String &path) noexcept;
ox::Error redo() noexcept override;
ox::Error undo() noexcept override;
[[nodiscard]]
int commandId() const noexcept override;
private:
ox::Error swap() noexcept;
};
}

View File

@ -29,11 +29,10 @@ WorldObjectSetEditorImGui::WorldObjectSetEditorImGui(
m_itemPath(itemPath()),
m_doc(*readObj<WorldObjectSet>(keelCtx(m_sctx), itemPath()).unwrapThrow()),
m_tileSheet(readObj<ncore::TileSheet>(keelCtx(m_sctx), m_doc.tilesheet).unwrapThrow()) {
auto const&tilesheetList = m_sctx.project->fileList(ncore::FileExt_ng);
auto &kctx = keelCtx(m_sctx);
auto const [tsPath, err] = getPath(kctx, m_doc.tilesheet);
if (!err) {
m_selectedTilesheetIdx = std::find(tilesheetList.begin(), tilesheetList.end(), tsPath).offset();
m_tilesheetPath = tsPath;
}
loadObj();
undoStack()->changeTriggered.connect(this, &WorldObjectSetEditorImGui::handleCmd);
@ -91,16 +90,12 @@ void WorldObjectSetEditorImGui::buildPaletteDisplayNameList() noexcept {
}
void WorldObjectSetEditorImGui::drawTileSheetSelector() noexcept {
auto const&tilesheetList = m_sctx.project->fileList(ncore::FileExt_ng);
auto sel = m_selectedTilesheetIdx;
if (ig::ComboBox("Tile Sheet", tilesheetList, sel)) {
auto const [uuid, err] = keel::pathToUuid(
keelCtx(m_sctx.tctx), tilesheetList[sel]);
auto constexpr tsFlags = ImGuiInputTextFlags_ReadOnly;
ig::InputText("Tile Sheet", m_tilesheetPath, tsFlags);
if (ig::DragDropTarget const d; d) {
auto const [fr, err] = ig::getDragDropPayload<studio::FileRef>("FileRef");
if (!err) {
auto const uuidUrl = ox::sfmt("uuid://{}", uuid.toString());
std::ignore = pushCommand<ChangeTileSheet>
(m_doc, ox::FileAddress(uuidUrl), sel, m_selectedTilesheetIdx);
loadObj();
std::ignore = setTileSheet(fr.path);
}
}
}
@ -287,6 +282,17 @@ void WorldObjectSetEditorImGui::drawPaletteList() noexcept {
drawPaletteListItems();
ImGui::EndTable();
}
if (ig::DragDropTarget const d; d) {
auto const [fr, err] = ig::getDragDropPayload<studio::FileRef>("FileRef");
if (!err) {
auto const [uuid, err] = keel::pathToUuid(
keelCtx(m_sctx), fr.path);
if (!err) {
auto const uuidUrl = ox::sfmt("uuid://{}", uuid.toString());
std::ignore = pushCommand<AddPalette>(m_doc, ox::FileAddress{uuidUrl}, keelCtx(m_sctx));
}
}
}
}
void WorldObjectSetEditorImGui::drawPaletteListItems() noexcept {
@ -320,7 +326,7 @@ void WorldObjectSetEditorImGui::drawAddPalettePopup() noexcept {
keelCtx(m_sctx.tctx), palettes[m_addPalPopup.selectedIdx]);
if (!err) {
auto const uuidUrl = ox::sfmt("uuid://{}", uuid.toString());
std::ignore = pushCommand<AddPalette>(m_doc, ox::FileAddress(uuidUrl));
std::ignore = pushCommand<AddPalette>(m_doc, ox::FileAddress{uuidUrl}, keelCtx(m_sctx));
}
}
ImGui::EndPopup();
@ -336,6 +342,16 @@ void WorldObjectSetEditorImGui::rmPalette() noexcept {
std::ignore = pushCommand<RmPalette>(m_doc, m_selectedPal);
}
ox::Error WorldObjectSetEditorImGui::setTileSheet(ox::StringViewCR path) noexcept {
auto const [uuid, err] = keel::pathToUuid(
keelCtx(m_sctx), path);
auto const uuidUrl = ox::sfmt("uuid://{}", uuid.toString());
std::ignore = pushCommand<ChangeTileSheet>
(keelCtx(m_sctx), m_doc, ox::FileAddress{uuidUrl}, m_tilesheetPath);
loadObj();
return {};
}
ox::Error WorldObjectSetEditorImGui::handleCmd(studio::UndoCommand const*cmd) noexcept {
if (dynamic_cast<ChangeTileSheet const*>(cmd)) {
auto &kctx = keelCtx(m_sctx.tctx);

View File

@ -23,7 +23,7 @@ class WorldObjectSetEditorImGui: public studio::Editor {
ox::Vector<ox::String> m_paletteDisplayNames;
size_t m_selectedObj{};
size_t m_selectedPal{};
size_t m_selectedTilesheetIdx{};
ox::String m_tilesheetPath{};
struct {
bool show{};
size_t selectedIdx{};
@ -70,6 +70,8 @@ class WorldObjectSetEditorImGui: public studio::Editor {
void rmPalette() noexcept;
ox::Error setTileSheet(ox::StringViewCR path) noexcept;
ox::Error handleCmd(studio::UndoCommand const*cmd) noexcept;
ox::Error saveItem() noexcept final;