[nostalgia/studio] Add UndoCommand::mergeWith

This commit is contained in:
Gary Talent 2022-03-31 01:38:59 -05:00
parent 10d2f2c064
commit dd12857ba8
2 changed files with 11 additions and 2 deletions

View File

@ -6,16 +6,24 @@
namespace nostalgia::studio {
bool UndoCommand::mergeWith(const UndoCommand*) noexcept {
return false;
}
void UndoStack::push(UndoCommand *cmd) noexcept {
for (const auto i = m_stackIdx; i < m_stack.size();) {
oxIgnoreError(m_stack.erase(i));
}
m_stack.emplace_back(cmd);
++m_stackIdx;
auto cmdId = cmd->commandId();
cmd->redo();
redoTriggered.emit(cmdId);
changeTriggered.emit(cmdId);
if (!m_stack.size() || !m_stack.back().value->mergeWith(cmd)) {
m_stack.emplace_back(cmd);
++m_stackIdx;
} else {
delete cmd;
}
}
void UndoStack::redo() noexcept {

View File

@ -18,6 +18,7 @@ class UndoCommand {
virtual void undo() noexcept = 0;
[[nodiscard]]
virtual int commandId() const noexcept = 0;
virtual bool mergeWith(const UndoCommand *cmd) noexcept;
};
class UndoStack {