[studio,nostalgia/studio] Make executing UndoCommands report errors
All checks were successful
Build / build (push) Successful in 2m31s

This commit is contained in:
Gary Talent 2024-05-23 21:50:27 -05:00
parent a1c89906bd
commit c0479604aa
7 changed files with 31 additions and 36 deletions

View File

@ -79,13 +79,13 @@ void PaletteEditorImGui::drawColorsEditor() noexcept {
if (ImGui::Button("Add", sz)) { if (ImGui::Button("Add", sz)) {
auto const colorSz = static_cast<int>(colors(m_pal, m_page)); auto const colorSz = static_cast<int>(colors(m_pal, m_page));
constexpr Color16 c = 0; constexpr Color16 c = 0;
undoStack()->push(ox::make_unique<AddColorCommand>(&m_pal, c, m_page, colorSz)); std::ignore = undoStack()->push(ox::make_unique<AddColorCommand>(&m_pal, c, m_page, colorSz));
} }
ImGui::SameLine(); ImGui::SameLine();
ImGui::BeginDisabled(m_selectedColorRow >= colors(m_pal, m_page)); ImGui::BeginDisabled(m_selectedColorRow >= colors(m_pal, m_page));
{ {
if (ImGui::Button("Remove", sz)) { if (ImGui::Button("Remove", sz)) {
undoStack()->push( std::ignore = undoStack()->push(
ox::make_unique<RemoveColorCommand>( ox::make_unique<RemoveColorCommand>(
&m_pal, &m_pal,
color(m_pal, m_page, static_cast<std::size_t>(m_selectedColorRow)), color(m_pal, m_page, static_cast<std::size_t>(m_selectedColorRow)),
@ -97,7 +97,7 @@ void PaletteEditorImGui::drawColorsEditor() noexcept {
ImGui::BeginDisabled(m_selectedColorRow <= 0); ImGui::BeginDisabled(m_selectedColorRow <= 0);
{ {
if (ImGui::Button("Move Up", sz)) { if (ImGui::Button("Move Up", sz)) {
undoStack()->push(ox::make_unique<MoveColorCommand>(&m_pal, m_page, m_selectedColorRow, -1)); std::ignore = undoStack()->push(ox::make_unique<MoveColorCommand>(&m_pal, m_page, m_selectedColorRow, -1));
--m_selectedColorRow; --m_selectedColorRow;
} }
} }
@ -106,7 +106,7 @@ void PaletteEditorImGui::drawColorsEditor() noexcept {
ImGui::BeginDisabled(m_selectedColorRow >= colors(m_pal, m_page) - 1); ImGui::BeginDisabled(m_selectedColorRow >= colors(m_pal, m_page) - 1);
{ {
if (ImGui::Button("Move Down", sz)) { if (ImGui::Button("Move Down", sz)) {
undoStack()->push(ox::make_unique<MoveColorCommand>(&m_pal, m_page, m_selectedColorRow, 1)); std::ignore = undoStack()->push(ox::make_unique<MoveColorCommand>(&m_pal, m_page, m_selectedColorRow, 1));
++m_selectedColorRow; ++m_selectedColorRow;
} }
} }
@ -155,17 +155,17 @@ void PaletteEditorImGui::drawPagesEditor() noexcept {
constexpr auto toolbarHeight = 40; constexpr auto toolbarHeight = 40;
auto const btnSz = ImVec2(paneSz.x / 3 - 5.5f, 24); auto const btnSz = ImVec2(paneSz.x / 3 - 5.5f, 24);
if (ImGui::Button("Add", btnSz)) { if (ImGui::Button("Add", btnSz)) {
undoStack()->push(ox::make_unique<AddPageCommand>(m_pal)); std::ignore = undoStack()->push(ox::make_unique<AddPageCommand>(m_pal));
m_page = m_pal.pages.size() - 1; m_page = m_pal.pages.size() - 1;
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Remove", btnSz)) { if (ImGui::Button("Remove", btnSz)) {
undoStack()->push(ox::make_unique<RemovePageCommand>(m_pal, m_page)); std::ignore = undoStack()->push(ox::make_unique<RemovePageCommand>(m_pal, m_page));
m_page = std::min(m_page, m_pal.pages.size() - 1); m_page = std::min(m_page, m_pal.pages.size() - 1);
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Duplicate", btnSz)) { if (ImGui::Button("Duplicate", btnSz)) {
undoStack()->push(ox::make_unique<DuplicatePageCommand>(m_pal, m_page, m_pal.pages.size())); std::ignore = undoStack()->push(ox::make_unique<DuplicatePageCommand>(m_pal, m_page, m_pal.pages.size()));
} }
ImGui::BeginTable("PageSelect", 2, tableFlags, ImVec2(paneSz.x, paneSz.y - (toolbarHeight + 5))); ImGui::BeginTable("PageSelect", 2, tableFlags, ImVec2(paneSz.x, paneSz.y - (toolbarHeight + 5)));
{ {
@ -198,7 +198,7 @@ void PaletteEditorImGui::drawColorEditor() noexcept {
ImGui::InputInt("Blue", &b, 1, 5); ImGui::InputInt("Blue", &b, 1, 5);
auto const newColor = color16(r, g, b, a); auto const newColor = color16(r, g, b, a);
if (c != newColor) { if (c != newColor) {
undoStack()->push(ox::make_unique<UpdateColorCommand>( std::ignore = undoStack()->push(ox::make_unique<UpdateColorCommand>(
&m_pal, m_page, static_cast<int>(m_selectedColorRow), c, newColor)); &m_pal, m_page, static_cast<int>(m_selectedColorRow), c, newColor));
} }
} }

View File

@ -305,7 +305,7 @@ void TileSheetEditorModel::getFillPixels(bool *pixels, ox::Point const&pt, int o
} }
void TileSheetEditorModel::pushCommand(studio::UndoCommand *cmd) noexcept { void TileSheetEditorModel::pushCommand(studio::UndoCommand *cmd) noexcept {
m_undoStack.push(ox::UPtr<studio::UndoCommand>(cmd)); std::ignore = m_undoStack.push(ox::UPtr<studio::UndoCommand>(cmd));
m_ongoingDrawCommand = dynamic_cast<DrawCommand*>(cmd); m_ongoingDrawCommand = dynamic_cast<DrawCommand*>(cmd);
m_updated = true; m_updated = true;
} }

View File

@ -197,10 +197,10 @@ void StudioUI::drawMenu() noexcept {
if (ImGui::BeginMenu("Edit")) { if (ImGui::BeginMenu("Edit")) {
auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr; auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr;
if (ImGui::MenuItem("Undo", "Ctrl+Z", false, undoStack && undoStack->canUndo())) { if (ImGui::MenuItem("Undo", "Ctrl+Z", false, undoStack && undoStack->canUndo())) {
undoStack->undo(); oxLogError(undoStack->undo());
} }
if (ImGui::MenuItem("Redo", "Ctrl+Y", false, undoStack && undoStack->canRedo())) { if (ImGui::MenuItem("Redo", "Ctrl+Y", false, undoStack && undoStack->canRedo())) {
undoStack->redo(); oxLogError(undoStack->redo());
} }
ImGui::Separator(); ImGui::Separator();
if (ImGui::MenuItem("Copy", "Ctrl+C", false, m_activeEditor && m_activeEditor->copyEnabled())) { if (ImGui::MenuItem("Copy", "Ctrl+C", false, m_activeEditor && m_activeEditor->copyEnabled())) {
@ -315,14 +315,14 @@ void StudioUI::toggleProjectExplorer() noexcept {
void StudioUI::redo() noexcept { void StudioUI::redo() noexcept {
auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr; auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr;
if (undoStack && undoStack->canRedo()) { if (undoStack && undoStack->canRedo()) {
m_activeEditor->undoStack()->redo(); oxLogError(m_activeEditor->undoStack()->redo());
} }
} }
void StudioUI::undo() noexcept { void StudioUI::undo() noexcept {
auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr; auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr;
if (undoStack && undoStack->canUndo()) { if (undoStack && undoStack->canUndo()) {
m_activeEditor->undoStack()->undo(); oxLogError(m_activeEditor->undoStack()->undo());
} }
} }

View File

@ -131,16 +131,14 @@ class Editor: public studio::BaseEditor {
[[nodiscard]] [[nodiscard]]
UndoStack *undoStack() noexcept final; UndoStack *undoStack() noexcept final;
void pushCommand(ox::UPtr<UndoCommand> &&cmd) noexcept; ox::Error pushCommand(ox::UPtr<UndoCommand> &&cmd) noexcept;
template<typename UC, typename ...Args> template<typename UC, typename ...Args>
bool pushCommand(Args&&... args) noexcept { ox::Error pushCommand(Args&&... args) noexcept {
try { try {
m_undoStack.push(ox::make_unique<UC>(ox::forward<Args>(args)...)); return m_undoStack.push(ox::make_unique<UC>(ox::forward<Args>(args)...));
return true;
} catch (ox::Exception const&ex) { } catch (ox::Exception const&ex) {
oxLogError(ex.toError()); return ex.toError();
return false;
} }
} }

View File

@ -19,11 +19,11 @@ class UndoStack {
std::size_t m_stackIdx = 0; std::size_t m_stackIdx = 0;
public: public:
void push(ox::UPtr<UndoCommand> &&cmd) noexcept; ox::Error push(ox::UPtr<UndoCommand> &&cmd) noexcept;
void redo() noexcept; ox::Error redo() noexcept;
void undo() noexcept; ox::Error undo() noexcept;
[[nodiscard]] [[nodiscard]]
constexpr bool canRedo() const noexcept { constexpr bool canRedo() const noexcept {

View File

@ -127,8 +127,8 @@ ox::CStringView Editor::itemDisplayName() const noexcept {
return m_itemName; return m_itemName;
} }
void Editor::pushCommand(ox::UPtr<UndoCommand> &&cmd) noexcept { ox::Error Editor::pushCommand(ox::UPtr<UndoCommand> &&cmd) noexcept {
m_undoStack.push(std::move(cmd)); return m_undoStack.push(std::move(cmd));
} }
UndoStack *Editor::undoStack() noexcept { UndoStack *Editor::undoStack() noexcept {

View File

@ -6,42 +6,39 @@
namespace studio { namespace studio {
void UndoStack::push(ox::UPtr<UndoCommand> &&cmd) noexcept { ox::Error UndoStack::push(ox::UPtr<UndoCommand> &&cmd) noexcept {
for (auto const i = m_stackIdx; i < m_stack.size();) { for (auto const i = m_stackIdx; i < m_stack.size();) {
std::ignore = m_stack.erase(i); std::ignore = m_stack.erase(i);
} }
if (cmd->redo()) { oxReturnError(cmd->redo());
return;
}
redoTriggered.emit(cmd.get()); redoTriggered.emit(cmd.get());
changeTriggered.emit(cmd.get()); changeTriggered.emit(cmd.get());
if (m_stack.empty() || !(*m_stack.back().value)->mergeWith(*cmd)) { if (m_stack.empty() || !(*m_stack.back().value)->mergeWith(*cmd)) {
m_stack.emplace_back(std::move(cmd)); m_stack.emplace_back(std::move(cmd));
++m_stackIdx; ++m_stackIdx;
} }
return {};
} }
void UndoStack::redo() noexcept { ox::Error UndoStack::redo() noexcept {
if (m_stackIdx < m_stack.size()) { if (m_stackIdx < m_stack.size()) {
auto &c = m_stack[m_stackIdx]; auto &c = m_stack[m_stackIdx];
if (c->redo()) { oxReturnError(c->redo());
return;
}
++m_stackIdx; ++m_stackIdx;
redoTriggered.emit(c.get()); redoTriggered.emit(c.get());
changeTriggered.emit(c.get()); changeTriggered.emit(c.get());
} }
return {};
} }
void UndoStack::undo() noexcept { ox::Error UndoStack::undo() noexcept {
if (m_stackIdx) { if (m_stackIdx) {
auto &c = m_stack[--m_stackIdx]; auto &c = m_stack[--m_stackIdx];
if (c->undo()) { oxReturnError(c->undo());
return;
}
undoTriggered.emit(c.get()); undoTriggered.emit(c.get());
changeTriggered.emit(c.get()); changeTriggered.emit(c.get());
} }
return {};
} }
} }