[studio,nostalgia] Fix PaletteEditor color update command merging, add setObsolete

This commit is contained in:
Gary Talent 2024-09-30 23:07:14 -05:00
parent 1f6fefdb68
commit c6efabaa1d
6 changed files with 27 additions and 12 deletions

View File

@ -243,7 +243,7 @@ void PaletteEditorImGui::drawColorEditor() noexcept {
auto const&currentName = 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<UpdateColorInfoCommand>(
m_pal, m_selectedColorRow, ox::String{name.data()});
m_pal, m_selectedColorRow, ox::String{name});
}
}

View File

@ -188,19 +188,21 @@ UpdateColorInfoCommand::UpdateColorInfoCommand(
}
}
bool UpdateColorInfoCommand::mergeWith(UndoCommand const&cmd) noexcept {
if (cmd.commandId() != static_cast<int>(PaletteEditorCommandId::UpdateColor)) {
bool UpdateColorInfoCommand::mergeWith(UndoCommand &cmd) noexcept {
if (cmd.commandId() != static_cast<int>(PaletteEditorCommandId::UpdateColorInfo)) {
return false;
}
auto ucCmd = static_cast<UpdateColorInfoCommand const*>(&cmd);
auto ucCmd = dynamic_cast<UpdateColorInfoCommand const*>(&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<int>(PaletteEditorCommandId::UpdateColor);
return static_cast<int>(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<int>(PaletteEditorCommandId::UpdateColor)) {
return false;
}
auto ucCmd = static_cast<UpdateColorCommand const*>(&cmd);
auto ucCmd = dynamic_cast<UpdateColorCommand const*>(&cmd);
if (m_idx != ucCmd->m_idx) {
return false;
}

View File

@ -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;

View File

@ -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;
}
};
}

View File

@ -3,7 +3,7 @@
namespace studio {
bool UndoCommand::mergeWith(UndoCommand const&) noexcept {
bool UndoCommand::mergeWith(UndoCommand&) noexcept {
return false;
}

View File

@ -17,6 +17,10 @@ ox::Error UndoStack::push(ox::UPtr<UndoCommand> &&cmd) noexcept {
m_stack.emplace_back(std::move(cmd));
++m_stackIdx;
}
if ((*m_stack.back().unwrap())->isObsolete()) {
m_stack.pop_back();
--m_stackIdx;
}
return {};
}