From 115941a787580254cd18611b02cb1e6cf0eb3cc9 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 9 Dec 2023 12:56:23 -0600 Subject: [PATCH] [nostalgia/core/studio] Fix palette selection reversion on undo of palette change --- .../core/src/studio/tilesheeteditor-imgui.cpp | 27 +++++++++++-------- .../core/src/studio/tilesheeteditor-imgui.hpp | 2 ++ .../core/src/studio/tilesheeteditormodel.cpp | 3 ++- .../core/src/studio/tilesheeteditormodel.hpp | 1 + 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor-imgui.cpp index b8531567..b5d728b2 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor-imgui.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor-imgui.cpp @@ -47,20 +47,11 @@ TileSheetEditorImGui::TileSheetEditorImGui(turbine::Context &ctx, ox::CRStringVi m_tileSheetEditor(m_ctx, m_itemPath) { const auto lastSlash = ox::find(m_itemPath.rbegin(), m_itemPath.rend(), '/').offset(); m_itemName = m_itemPath.substr(lastSlash + 1); - // init palette idx - const auto &palPath = model()->palPath(); - auto sctx = applicationData(m_ctx); - const auto &palList = sctx->project->fileList(core::FileExt_npal); - for (std::size_t i = 0; const auto &pal : palList) { - if (palPath == pal) { - m_selectedPaletteIdx = i; - break; - } - ++i; - } + oxIgnoreError(setPaletteSelection()); // connect signal/slots undoStack()->changeTriggered.connect(this, &TileSheetEditorImGui::markUnsavedChanges); m_subsheetEditor.inputSubmitted.connect(this, &TileSheetEditorImGui::updateActiveSubsheet); + model()->paletteChanged.connect(this, &TileSheetEditorImGui::setPaletteSelection); } ox::String const&TileSheetEditorImGui::itemName() const noexcept { @@ -401,6 +392,20 @@ ox::Error TileSheetEditorImGui::updateActiveSubsheet(ox::StringView const&name, return model()->updateSubsheet(model()->activeSubSheetIdx(), name, cols, rows); } +ox::Error TileSheetEditorImGui::setPaletteSelection() noexcept { + const auto &palPath = model()->palPath(); + auto sctx = applicationData(m_ctx); + const auto &palList = sctx->project->fileList(core::FileExt_npal); + for (std::size_t i = 0; const auto &pal : palList) { + if (palPath == pal) { + m_selectedPaletteIdx = i; + break; + } + ++i; + } + return {}; +} + ox::Error TileSheetEditorImGui::markUnsavedChanges(const studio::UndoCommand*) noexcept { setUnsavedChanges(true); return {}; diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditor-imgui.hpp b/src/nostalgia/modules/core/src/studio/tilesheeteditor-imgui.hpp index 4d490f66..f70938ea 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditor-imgui.hpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditor-imgui.hpp @@ -106,6 +106,8 @@ class TileSheetEditorImGui: public studio::BaseEditor { ox::Error updateActiveSubsheet(ox::StringView const&name, int cols, int rows) noexcept; + ox::Error setPaletteSelection() noexcept; + // slots private: ox::Error markUnsavedChanges(const studio::UndoCommand*) noexcept; diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditormodel.cpp b/src/nostalgia/modules/core/src/studio/tilesheeteditormodel.cpp index 21762353..08d68245 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditormodel.cpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditormodel.cpp @@ -753,7 +753,8 @@ ox::Error TileSheetEditorModel::markUpdatedCmdId(const studio::UndoCommand *cmd) m_updated = true; const auto cmdId = cmd->commandId(); if (static_cast(cmdId) == CommandId::PaletteChange) { - oxReturnError(readObj(keelCtx(m_ctx), ox::StringView(m_img.defaultPalette.getPath().value)).moveTo(&m_pal)); + oxReturnError(readObj(keelCtx(m_ctx), m_img.defaultPalette).moveTo(&m_pal)); + paletteChanged.emit(); } auto tsCmd = dynamic_cast(cmd); auto idx = m_img.validateSubSheetIdx(tsCmd->subsheetIdx()); diff --git a/src/nostalgia/modules/core/src/studio/tilesheeteditormodel.hpp b/src/nostalgia/modules/core/src/studio/tilesheeteditormodel.hpp index f4ecbc4b..e6941b5c 100644 --- a/src/nostalgia/modules/core/src/studio/tilesheeteditormodel.hpp +++ b/src/nostalgia/modules/core/src/studio/tilesheeteditormodel.hpp @@ -20,6 +20,7 @@ class TileSheetEditorModel: public ox::SignalHandler { public: ox::Signal activeSubsheetChanged; + ox::Signal paletteChanged; private: static const Palette s_defaultPalette;