From f840240aac35346ee88e69a42a0c40bfb19c2b35 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 25 Jan 2025 22:59:51 -0600 Subject: [PATCH] [nostalgia/gfx/studio/tilesheeteditor] Rework system for tracking current palette path --- .../tilesheeteditor/tilesheeteditor-imgui.cpp | 14 ++--- .../tilesheeteditor/tilesheeteditor-imgui.hpp | 7 +-- .../tilesheeteditor/tilesheeteditormodel.cpp | 54 ++++++++++++------- .../tilesheeteditor/tilesheeteditormodel.hpp | 7 ++- 4 files changed, 47 insertions(+), 35 deletions(-) diff --git a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp index e58bb1b0..fddf29eb 100644 --- a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp +++ b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditor-imgui.cpp @@ -70,8 +70,8 @@ static ox::Error toPngFile( ox::Vector &&pixels, Palette const&pal, size_t page, - unsigned width, - unsigned height) noexcept { + unsigned const width, + unsigned const height) noexcept { for (auto &c : pixels) { c = color32(color(pal, page, c)) | static_cast(0XFF << 24); } @@ -96,7 +96,6 @@ TileSheetEditorImGui::TileSheetEditorImGui(studio::StudioContext &sctx, ox::Stri // connect signal/slots m_subsheetEditor.inputSubmitted.connect(this, &TileSheetEditorImGui::updateActiveSubsheet); m_exportMenu.inputSubmitted.connect(this, &TileSheetEditorImGui::exportSubhseetToPng); - m_model.paletteChanged.connect(this, &TileSheetEditorImGui::setPaletteSelection); // load config auto const&config = studio::readConfig( keelCtx(m_sctx), itemPath()); @@ -125,7 +124,7 @@ bool TileSheetEditorImGui::acceptsClipboardPayload() const noexcept { return m_model.acceptsClipboardPayload(); } -void TileSheetEditorImGui::keyStateChanged(turbine::Key key, bool down) { +void TileSheetEditorImGui::keyStateChanged(turbine::Key const key, bool const down) { if (!down) { return; } @@ -526,14 +525,11 @@ void TileSheetEditorImGui::drawPaletteMenu() noexcept { } } -ox::Error TileSheetEditorImGui::updateActiveSubsheet(ox::StringView const&name, int cols, int rows) noexcept { +ox::Error TileSheetEditorImGui::updateActiveSubsheet( + ox::StringView const&name, int const cols, int const rows) noexcept { return m_model.updateSubsheet(m_model.activeSubSheetIdx(), name, cols, rows); } -ox::Error TileSheetEditorImGui::setPaletteSelection() noexcept { - return {}; -} - void TileSheetEditorImGui::setActiveSubsheet(TileSheet::SubSheetIdx path) noexcept { m_model.setActiveSubsheet(path); studio::editConfig(keelCtx(m_sctx), itemPath(), diff --git a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditor-imgui.hpp b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditor-imgui.hpp index 8fd58a54..ea42f57f 100644 --- a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditor-imgui.hpp +++ b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditor-imgui.hpp @@ -4,7 +4,6 @@ #pragma once -#include #include #include @@ -29,7 +28,7 @@ class TileSheetEditorImGui: public studio::Editor { public: ox::Signal inputSubmitted; void show(ox::StringViewCR name, int cols, int rows) noexcept; - void draw(turbine::Context &sctx) noexcept; + void draw(turbine::Context &tctx) noexcept; void close() noexcept; [[nodiscard]] constexpr bool isOpen() const noexcept { return m_show; } @@ -41,7 +40,7 @@ class TileSheetEditorImGui: public studio::Editor { public: ox::Signal inputSubmitted; void show() noexcept; - void draw(turbine::Context &sctx) noexcept; + void draw(turbine::Context &tctx) noexcept; void close() noexcept; [[nodiscard]] constexpr bool isOpen() const noexcept { return m_show; } @@ -99,8 +98,6 @@ class TileSheetEditorImGui: public studio::Editor { ox::Error updateActiveSubsheet(ox::StringView const&name, int cols, int rows) noexcept; - ox::Error setPaletteSelection() noexcept; - // slots private: void setActiveSubsheet(TileSheet::SubSheetIdx path) noexcept; diff --git a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditormodel.cpp b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditormodel.cpp index 44fcfdd6..cf7fd36d 100644 --- a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditormodel.cpp +++ b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditormodel.cpp @@ -42,17 +42,19 @@ Palette const TileSheetEditorModel::s_defaultPalette = { }; TileSheetEditorModel::TileSheetEditorModel( - studio::StudioContext &sctx, ox::StringViewCR path, studio::UndoStack &undoStack): - m_sctx(sctx), - m_tctx(m_sctx.tctx), - m_path(path), - m_img(*readObj(keelCtx(m_tctx), m_path).unwrapThrow()), - // ignore failure to load palette - m_pal(readObj(keelCtx(m_tctx), m_img.defaultPalette).value), - m_undoStack(undoStack) { + studio::StudioContext &sctx, ox::StringParam path, studio::UndoStack &undoStack): + m_sctx(sctx), + m_tctx(m_sctx.tctx), + m_path(std::move(path)), + m_img(*readObj(keelCtx(m_tctx), m_path).unwrapThrow()), + // ignore failure to load palette + m_pal(readObj(keelCtx(m_tctx), m_img.defaultPalette).value), + m_undoStack(undoStack) { normalizeSubsheets(m_img.subsheet); m_pal.updated.connect(this, &TileSheetEditorModel::markUpdated); m_undoStack.changeTriggered.connect(this, &TileSheetEditorModel::markUpdatedCmdId); + setPalPath(); + m_sctx.project->fileMoved.connect(this, &TileSheetEditorModel::handleFileRename); } void TileSheetEditorModel::cut() { @@ -116,18 +118,7 @@ bool TileSheetEditorModel::acceptsClipboardPayload() const noexcept { } ox::StringView TileSheetEditorModel::palPath() const noexcept { - auto &path = m_img.defaultPalette; - constexpr ox::StringView uuidPrefix = "uuid://"; - if (ox::beginsWith(path, uuidPrefix)) { - auto const uuid = substr(path, uuidPrefix.bytes()); - auto const out = keelCtx(m_tctx).uuidToPath.at(uuid); - if (out.error) { - return {}; - } - return *out.value; - } else { - return path; - } + return m_palPath; } ox::Error TileSheetEditorModel::setPalette(ox::StringViewCR path) noexcept { @@ -318,10 +309,33 @@ void TileSheetEditorModel::getFillPixels( } } +void TileSheetEditorModel::setPalPath() noexcept { + auto &path = m_img.defaultPalette; + constexpr ox::StringView uuidPrefix = "uuid://"; + if (ox::beginsWith(path, uuidPrefix)) { + auto const uuid = substr(path, uuidPrefix.bytes()); + auto const out = keelCtx(m_tctx).uuidToPath.at(uuid); + if (!out.error) { + m_palPath = *out.value; + } + } else { + m_palPath = path; + } +} + void TileSheetEditorModel::pushCommand(studio::UndoCommand *cmd) noexcept { std::ignore = m_undoStack.push(ox::UPtr{cmd}); m_ongoingDrawCommand = dynamic_cast(cmd); m_updated = true; } +ox::Error TileSheetEditorModel::handleFileRename(ox::StringViewCR newPath, ox::UUID const&id) noexcept { + if ((beginsWith(m_img.defaultPalette, "uuid://") && + substr(m_img.defaultPalette, 7) == id.toString()) || + m_img.defaultPalette == newPath) { + m_palPath = newPath; + } + return {}; +} + } diff --git a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditormodel.hpp b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditormodel.hpp index ef652051..66c75a2b 100644 --- a/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditormodel.hpp +++ b/src/nostalgia/modules/gfx/src/studio/tilesheeteditor/tilesheeteditormodel.hpp @@ -25,6 +25,7 @@ class TileSheetEditorModel: public ox::SignalHandler { studio::StudioContext &m_sctx; turbine::Context &m_tctx; ox::String m_path; + ox::String m_palPath; TileSheet m_img; TileSheet::SubSheetIdx m_activeSubsSheetIdx; keel::AssetRef m_pal; @@ -36,7 +37,7 @@ class TileSheetEditorModel: public ox::SignalHandler { bool m_updated = false; public: - TileSheetEditorModel(studio::StudioContext &sctx, ox::StringViewCR path, studio::UndoStack &undoStack); + TileSheetEditorModel(studio::StudioContext &sctx, ox::StringParam path, studio::UndoStack &undoStack); ~TileSheetEditorModel() override = default; @@ -132,8 +133,12 @@ class TileSheetEditorModel: public ox::SignalHandler { ox::Point const&pt, int oldColor) const noexcept; + void setPalPath() noexcept; + void pushCommand(studio::UndoCommand *cmd) noexcept; + ox::Error handleFileRename(ox::StringViewCR newPath, ox::UUID const&id) noexcept; + }; constexpr TileSheet const&TileSheetEditorModel::img() const noexcept {