From 80db641b6f0353ae14d3c01567551b86cc0c95f5 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 11 Nov 2022 00:49:20 +0800 Subject: [PATCH] Added ListElementChangeCommand --- SHADE_Managed/src/Editor/UndoRedoStack.cxx | 49 ++++++++++++++++++++++ SHADE_Managed/src/Editor/UndoRedoStack.hxx | 19 ++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/SHADE_Managed/src/Editor/UndoRedoStack.cxx b/SHADE_Managed/src/Editor/UndoRedoStack.cxx index ae0a1dee..3feb43ed 100644 --- a/SHADE_Managed/src/Editor/UndoRedoStack.cxx +++ b/SHADE_Managed/src/Editor/UndoRedoStack.cxx @@ -117,4 +117,53 @@ namespace SHADE return false; } + generic + ListElementChangeCommand::ListElementChangeCommand(System::Collections::Generic::List^ list, int index, T newData, T oldData) + : list { list } + , index { index } + , newData { newData } + , oldData { oldData } + {} + + generic + bool ListElementChangeCommand::Execute() + { + if (list && index < System::Linq::Enumerable::Count(list)) + { + list[index] = newData; + return true; + } + + return false; + } + + generic + bool ListElementChangeCommand::Unexceute() + { + if (list && index < System::Linq::Enumerable::Count(list)) + { + list[index] = oldData; + return true; + } + + return false; + } + + generic + bool ListElementChangeCommand::Merge(ICommand^ command) + { + ListElementChangeCommand^ otherCommand = safe_cast^>(command); + if (otherCommand == nullptr) + { + Debug::LogWarning("[Field Change Command] Attempted to merge two incompatible commands!"); + return false; + } + + if (command && list == otherCommand->list && index == otherCommand->index) + { + newData = otherCommand->newData; + return true; + } + } + } diff --git a/SHADE_Managed/src/Editor/UndoRedoStack.hxx b/SHADE_Managed/src/Editor/UndoRedoStack.hxx index 69f462e3..dd78ecd9 100644 --- a/SHADE_Managed/src/Editor/UndoRedoStack.hxx +++ b/SHADE_Managed/src/Editor/UndoRedoStack.hxx @@ -38,7 +38,7 @@ namespace SHADE /// Whether the merge was successful or not. bool Merge(ICommand^ command); }; - + private ref class FieldChangeCommand sealed : public ICommand { public: @@ -55,6 +55,23 @@ namespace SHADE System::Object^ oldData; }; + generic + private ref class ListElementChangeCommand sealed : public ICommand + { + public: + ListElementChangeCommand(System::Collections::Generic::List^ list, int index, T newData, T oldData); + + bool Execute() override; + bool Unexceute() override; + bool Merge(ICommand^ command) override; + + private: + System::Collections::Generic::List^ list; + int index; + T newData; + T oldData; + }; + /// /// Class that is able to store a stack of actions that can be done and redone. ///