From f9081f669e06873638da48388a58049cc7eeca3d Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Sat, 27 Apr 2024 19:13:11 -0400 Subject: [PATCH] Added command buffer implementation in the editor layer --- Editor/src/Commands/CommandBuffer.h | 17 ++++++++++-- Editor/src/Commands/Commands/Commands.cpp | 19 ++++++++++++++ Editor/src/Commands/Commands/Commands.h | 32 ++++++++++++++++------- Editor/src/Commands/ICommand.h | 13 +++++++-- Editor/src/EditorApplication.h | 7 +++++ Editor/src/EditorLayer.cpp | 4 +-- Editor/src/EditorLayer.h | 5 ++++ Editor/src/Windows/EditorInterface.cpp | 15 ++++++++--- Editor/src/Windows/EditorInterface.h | 9 +++++-- 9 files changed, 99 insertions(+), 22 deletions(-) create mode 100644 Editor/src/Commands/Commands/Commands.cpp diff --git a/Editor/src/Commands/CommandBuffer.h b/Editor/src/Commands/CommandBuffer.h index 91c48a6e..0c4f2bf1 100644 --- a/Editor/src/Commands/CommandBuffer.h +++ b/Editor/src/Commands/CommandBuffer.h @@ -1,4 +1,5 @@ #pragma once +#include "ICommand.h" #include @@ -7,11 +8,23 @@ namespace NuakeEditor class CommandBuffer { private: + std::vector mCommands; public: - CommandBuffer(); - ~CommandBuffer(); + CommandBuffer() : mCommands(std::vector()) {} + ~CommandBuffer() = default; + void PushCommand(ICommand& command) + { + command.Execute(); + + mCommands.push_back(command); + } + + void PopCommand() + { + + } }; } \ No newline at end of file diff --git a/Editor/src/Commands/Commands/Commands.cpp b/Editor/src/Commands/Commands/Commands.cpp new file mode 100644 index 00000000..90dcc97b --- /dev/null +++ b/Editor/src/Commands/Commands/Commands.cpp @@ -0,0 +1,19 @@ +#include "Commands.h" + +#include + +namespace NuakeEditor +{ + bool SaveSceneCommand::Execute() + { + return true; + } + + bool SaveProjectCommand::Execute() + { + Engine::GetProject()->Save(); + Engine::GetCurrentScene()->Save(); + + return true; + } +} \ No newline at end of file diff --git a/Editor/src/Commands/Commands/Commands.h b/Editor/src/Commands/Commands/Commands.h index 086ebd08..a1d8737a 100644 --- a/Editor/src/Commands/Commands/Commands.h +++ b/Editor/src/Commands/Commands/Commands.h @@ -1,23 +1,35 @@ #pragma once + #include "../ICommand.h" +#include +#include +#include + + namespace NuakeEditor { - - class SaveSceneCommand : ICommand + using namespace Nuake; + + class SaveSceneCommand : public ICommand { + private: + Ref mScene; + public: - void Execute() override; + SaveSceneCommand(Ref scene) : mScene(scene) {} + + bool Execute() override; }; - class SaveProjectCommand : ICommand + class SaveProjectCommand : public ICommand { + private: + Ref mProject; + public: - void Execute() override; + SaveProjectCommand(Ref project) : mProject(project) {} + + bool Execute() override; }; - class SaveProjectCommand : ICommand - { - public: - void Execute() override; - }; } \ No newline at end of file diff --git a/Editor/src/Commands/ICommand.h b/Editor/src/Commands/ICommand.h index 1dc7c25c..36e9e2dc 100644 --- a/Editor/src/Commands/ICommand.h +++ b/Editor/src/Commands/ICommand.h @@ -5,7 +5,16 @@ namespace NuakeEditor class ICommand { public: - virtual void Execute() = 0; - virtual void Undo() = 0; + ICommand() = default; + + virtual bool Execute() + { + return false; + } + + virtual bool Undo() + { + return false; + } }; } \ No newline at end of file diff --git a/Editor/src/EditorApplication.h b/Editor/src/EditorApplication.h index c9e788da..7b05080e 100644 --- a/Editor/src/EditorApplication.h +++ b/Editor/src/EditorApplication.h @@ -5,9 +5,15 @@ #include "Windows/EditorInterface.h" #include "Misc/GizmoDrawer.h" +#include "Commands/CommandBuffer.h" + +using namespace NuakeEditor; class EditorApplication : public Nuake::Application { +private: + CommandBuffer mCommandBuffer; + public: EditorApplication(const Nuake::ApplicationSpecification& specification) : Application(specification), @@ -16,6 +22,7 @@ public: } virtual void OnInit() override; + // virtual void OnUpdate() override; virtual void OnShutdown() override; diff --git a/Editor/src/EditorLayer.cpp b/Editor/src/EditorLayer.cpp index 2b8d8425..c3566b9b 100644 --- a/Editor/src/EditorLayer.cpp +++ b/Editor/src/EditorLayer.cpp @@ -8,7 +8,7 @@ void EditorLayer::OnAttach() { - m_EditorInterface = new Nuake::EditorInterface(); + m_EditorInterface = new Nuake::EditorInterface(mCommandBuffer); m_GizmoDrawer = new GizmoDrawer(m_EditorInterface); } @@ -21,7 +21,6 @@ void EditorLayer::OnUpdate() auto sceneFramebuffer = GetApplication().GetWindow()->GetFrameBuffer(); - // Draw gizmos if (Ref currentScene = Nuake::Engine::GetCurrentScene(); currentScene) { @@ -64,6 +63,7 @@ void EditorLayer::OnUpdate() m_EditorInterface->Draw(); m_EditorInterface->Update(Nuake::Engine::GetTimestep()); + Nuake::Engine::EndDraw(); } diff --git a/Editor/src/EditorLayer.h b/Editor/src/EditorLayer.h index 1aedc4da..b02d3119 100644 --- a/Editor/src/EditorLayer.h +++ b/Editor/src/EditorLayer.h @@ -1,5 +1,6 @@ #pragma once #include +#include "Commands/CommandBuffer.h" namespace Nuake { @@ -9,15 +10,19 @@ namespace Nuake { class GizmoDrawer; +using namespace NuakeEditor; class EditorLayer : public Nuake::Layer { public: + EditorLayer() : mCommandBuffer() {} + virtual void OnAttach() override; virtual void OnUpdate() override; virtual void OnDetach() override; private: + CommandBuffer mCommandBuffer; Nuake::EditorInterface* m_EditorInterface; GizmoDrawer* m_GizmoDrawer; }; \ No newline at end of file diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index 772633b0..e9333064 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -54,6 +54,7 @@ #include #include #include +#include "../Commands/Commands/Commands.h" namespace Nuake { @@ -62,7 +63,8 @@ namespace Nuake { ImFont* boldFont; ImFont* EditorInterface::bigIconFont; - EditorInterface::EditorInterface() + EditorInterface::EditorInterface(CommandBuffer& commandBuffer) : + mCommandBuffer(&commandBuffer) { Logger::Log("Creating editor windows", "window", CRITICAL); filesystem = new FileSystemUI(this); @@ -2111,12 +2113,12 @@ namespace Nuake { } if (ImGui::MenuItem("Save", "CTRL+S")) { - Engine::GetProject()->Save(); - Engine::GetCurrentScene()->Save(); + PushCommand(SaveProjectCommand(Engine::GetProject())); + + Selection = EditorSelection(); SetStatusMessage("Project saved."); - Selection = EditorSelection(); } if (ImGui::MenuItem("Save as...", "CTRL+SHIFT+S")) { @@ -2455,6 +2457,11 @@ namespace Nuake { return entityTypeName; } + void EditorInterface::PushCommand(ICommand&& command) + { + mCommandBuffer->PushCommand(command); + } + bool EditorInterface::LoadProject(const std::string& projectPath) { FileSystem::SetRootDirectory(FileSystem::GetParentPath(projectPath)); diff --git a/Editor/src/Windows/EditorInterface.h b/Editor/src/Windows/EditorInterface.h index 838b51d5..f13bdec9 100644 --- a/Editor/src/Windows/EditorInterface.h +++ b/Editor/src/Windows/EditorInterface.h @@ -6,12 +6,15 @@ #include #include "src/Core/FileSystem.h" - #include "../Actions/EditorSelection.h" #include "EditorSelectionPanel.h" #include "WelcomeWindow.h" #include "AudioWindow.h" #include "../Windows/TrenchbroomConfiguratorWindow.h" +#include "../Commands/CommandBuffer.h" +#include "../Commands/ICommand.h" + +using namespace NuakeEditor; namespace Nuake { @@ -22,6 +25,7 @@ namespace Nuake { private: Ref SceneSnapshot; + NuakeEditor::CommandBuffer* mCommandBuffer; bool m_DrawGrid = false; bool m_DrawAxis = true; @@ -52,7 +56,7 @@ namespace Nuake EditorSelectionPanel SelectionPanel; TrenchbroomConfiguratorWindow m_TrenchhbroomConfigurator; - EditorInterface(); + EditorInterface(CommandBuffer& commandBuffer); static ImFont* bigIconFont; void BuildFonts(); @@ -76,5 +80,6 @@ namespace Nuake private: std::string GetEntityTypeName(const Entity& entity) const; + void PushCommand(ICommand&& command); }; }