From c6efabaa1deba7f361123591c0c1c105e5676300 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 30 Sep 2024 23:07:14 -0500 Subject: [PATCH] [studio,nostalgia] Fix PaletteEditor color update command merging, add setObsolete --- .../studio/paletteeditor/paletteeditor-imgui.cpp | 4 ++-- .../src/studio/paletteeditor/paletteeditor.cpp | 14 ++++++++------ .../src/studio/paletteeditor/paletteeditor.hpp | 4 ++-- .../studio/modlib/include/studio/undocommand.hpp | 11 ++++++++++- src/olympic/studio/modlib/src/undocommand.cpp | 2 +- src/olympic/studio/modlib/src/undostack.cpp | 4 ++++ 6 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp index ca5461bd..a645014b 100644 --- a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp +++ b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor-imgui.cpp @@ -243,7 +243,7 @@ void PaletteEditorImGui::drawColorEditor() noexcept { auto const¤tName = m_pal.colorNames[m_selectedColorRow]; ox::IString<50> name; name = currentName; - auto const nameUpdated = ImGui::InputText("Name", name.data(), name.cap() + 1); + auto const nameUpdated = ig::InputText("Name", name); bool inputFocused = ImGui::IsItemFocused(); ImGui::Separator(); colorInput("Red", r, inputFocused); @@ -271,7 +271,7 @@ void PaletteEditorImGui::drawColorEditor() noexcept { } if (nameUpdated) { std::ignore = pushCommand( - m_pal, m_selectedColorRow, ox::String{name.data()}); + m_pal, m_selectedColorRow, ox::String{name}); } } diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor.cpp b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor.cpp index 854bc289..0762c732 100644 --- a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor.cpp +++ b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor.cpp @@ -188,19 +188,21 @@ UpdateColorInfoCommand::UpdateColorInfoCommand( } } -bool UpdateColorInfoCommand::mergeWith(UndoCommand const&cmd) noexcept { - if (cmd.commandId() != static_cast(PaletteEditorCommandId::UpdateColor)) { +bool UpdateColorInfoCommand::mergeWith(UndoCommand &cmd) noexcept { + if (cmd.commandId() != static_cast(PaletteEditorCommandId::UpdateColorInfo)) { return false; } - auto ucCmd = static_cast(&cmd); + auto ucCmd = dynamic_cast(&cmd); if (m_idx != ucCmd->m_idx) { return false; } + m_pal.colorNames[m_idx] = std::move(ucCmd->m_pal.colorNames[m_idx]); + setObsolete(m_altColorInfo == m_pal.colorNames[m_idx]); return true; } int UpdateColorInfoCommand::commandId() const noexcept { - return static_cast(PaletteEditorCommandId::UpdateColor); + return static_cast(PaletteEditorCommandId::UpdateColorInfo); } ox::Error UpdateColorInfoCommand::redo() noexcept { @@ -232,11 +234,11 @@ UpdateColorCommand::UpdateColorCommand( } } -bool UpdateColorCommand::mergeWith(UndoCommand const&cmd) noexcept { +bool UpdateColorCommand::mergeWith(UndoCommand &cmd) noexcept { if (cmd.commandId() != static_cast(PaletteEditorCommandId::UpdateColor)) { return false; } - auto ucCmd = static_cast(&cmd); + auto ucCmd = dynamic_cast(&cmd); if (m_idx != ucCmd->m_idx) { return false; } diff --git a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor.hpp b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor.hpp index 60efb601..7f61d163 100644 --- a/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor.hpp +++ b/src/nostalgia/modules/core/src/studio/paletteeditor/paletteeditor.hpp @@ -163,7 +163,7 @@ class UpdateColorInfoCommand: public studio::UndoCommand { ~UpdateColorInfoCommand() noexcept override = default; [[nodiscard]] - bool mergeWith(const UndoCommand &cmd) noexcept final; + bool mergeWith(UndoCommand &cmd) noexcept final; [[nodiscard]] int commandId() const noexcept final; @@ -194,7 +194,7 @@ class UpdateColorCommand: public studio::UndoCommand { ~UpdateColorCommand() noexcept override = default; [[nodiscard]] - bool mergeWith(const UndoCommand &cmd) noexcept final; + bool mergeWith(UndoCommand &cmd) noexcept final; [[nodiscard]] int commandId() const noexcept final; diff --git a/src/olympic/studio/modlib/include/studio/undocommand.hpp b/src/olympic/studio/modlib/include/studio/undocommand.hpp index 93aa2a27..ffd00091 100644 --- a/src/olympic/studio/modlib/include/studio/undocommand.hpp +++ b/src/olympic/studio/modlib/include/studio/undocommand.hpp @@ -17,13 +17,22 @@ class NoChangesException: public ox::Exception { }; class UndoCommand { + private: + bool m_obsolete{}; public: virtual ~UndoCommand() noexcept = default; virtual ox::Error redo() noexcept = 0; virtual ox::Error undo() noexcept = 0; [[nodiscard]] virtual int commandId() const noexcept = 0; - virtual bool mergeWith(UndoCommand const&cmd) noexcept; + virtual bool mergeWith(UndoCommand &cmd) noexcept; + constexpr void setObsolete(bool obsolete) noexcept { + m_obsolete = obsolete; + } + [[nodiscard]] + constexpr bool isObsolete() const noexcept { + return m_obsolete; + } }; } diff --git a/src/olympic/studio/modlib/src/undocommand.cpp b/src/olympic/studio/modlib/src/undocommand.cpp index 6a929cab..96dec70d 100644 --- a/src/olympic/studio/modlib/src/undocommand.cpp +++ b/src/olympic/studio/modlib/src/undocommand.cpp @@ -3,7 +3,7 @@ namespace studio { -bool UndoCommand::mergeWith(UndoCommand const&) noexcept { +bool UndoCommand::mergeWith(UndoCommand&) noexcept { return false; } diff --git a/src/olympic/studio/modlib/src/undostack.cpp b/src/olympic/studio/modlib/src/undostack.cpp index 77cb049b..1bcf209b 100644 --- a/src/olympic/studio/modlib/src/undostack.cpp +++ b/src/olympic/studio/modlib/src/undostack.cpp @@ -17,6 +17,10 @@ ox::Error UndoStack::push(ox::UPtr &&cmd) noexcept { m_stack.emplace_back(std::move(cmd)); ++m_stackIdx; } + if ((*m_stack.back().unwrap())->isObsolete()) { + m_stack.pop_back(); + --m_stackIdx; + } return {}; }