[nostalgia/studio] Get Undo/Redo working

This commit is contained in:
Gary Talent 2022-02-16 20:16:49 -06:00
parent 72ce8ea4d9
commit c92ecdf499
5 changed files with 30 additions and 12 deletions

View File

@ -42,10 +42,6 @@ bool Editor::unsavedChanges() const noexcept {
return m_unsavedChanges;
}
UndoStack *Editor::undoStack() {
return &m_cmdStack;
}
void Editor::setExportable(bool exportable) {
m_exportable = exportable;
exportableChanged.emit(exportable);

View File

@ -32,6 +32,7 @@ class NOSTALGIASTUDIO_EXPORT Editor: public Widget {
[[nodiscard]]
virtual ox::String itemName() const = 0;
[[nodiscard]]
virtual ox::String itemDisplayName() const;
virtual void cut();
@ -42,6 +43,14 @@ class NOSTALGIASTUDIO_EXPORT Editor: public Widget {
virtual void exportFile();
/**
* Returns the undo stack holding changes to the item being edited.
*/
[[nodiscard]]
virtual UndoStack *undoStack() noexcept {
return nullptr;
}
void close();
/**
@ -58,12 +67,6 @@ class NOSTALGIASTUDIO_EXPORT Editor: public Widget {
[[nodiscard]]
bool unsavedChanges() const noexcept;
/**
* Returns the undo stack holding changes to the item being edited.
*/
[[nodiscard]]
UndoStack *undoStack();
void setExportable(bool);
[[nodiscard]]

View File

@ -22,8 +22,8 @@ void UndoStack::redo() noexcept {
}
void UndoStack::undo() noexcept {
if (m_stackIdx < ox::MaxValue<decltype(m_stackIdx)>) {
m_stack[m_stackIdx--]->undo();
if (m_stackIdx) {
m_stack[--m_stackIdx]->undo();
}
}

View File

@ -87,6 +87,23 @@ void StudioUI::drawMenu() noexcept {
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Edit")) {
auto undoStack = m_acitveEditor ? m_acitveEditor->undoStack() : nullptr;
if (ImGui::MenuItem("Undo", "Ctrl+Z", false, undoStack)) {
m_acitveEditor->undoStack()->undo();
}
if (ImGui::MenuItem("Redo", "Ctrl+Y", false, undoStack)) {
m_acitveEditor->undoStack()->redo();
}
ImGui::Separator();
if (ImGui::MenuItem("Copy", "Ctrl+N")) {
}
if (ImGui::MenuItem("Cut", "Ctrl+X")) {
}
if (ImGui::MenuItem("Paste", "Ctrl+V")) {
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("View", false)) {
ImGui::EndMenu();
}
@ -147,6 +164,7 @@ void StudioUI::drawTabs() noexcept {
auto const &e = *it;
bool open = true;
if (ImGui::BeginTabItem(e->itemDisplayName().c_str(), &open)) {
m_acitveEditor = e.get();
e->draw(m_ctx);
ImGui::EndTabItem();
}

View File

@ -28,6 +28,7 @@ class StudioUI: public ox::SignalHandler {
ox::HashMap<ox::String, studio::EditorMaker::Func> m_editorMakers;
ProjectExplorer *m_projectExplorer = nullptr;
ox::Vector<ox::String> m_openFiles;
studio::Editor *m_acitveEditor = nullptr;
bool m_saveEnabled = false;
bool m_aboutEnabled = false;