From 85cc97ca27e373a2910b1b660a9631393f5b1fa7 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 11 Nov 2022 12:07:05 +0800 Subject: [PATCH] Added implementation for ListElementAddCommand and ListElementRemoveCommand --- SHADE_Managed/src/Editor/UndoRedoStack.cxx | 115 ++++++++++++++++++++- SHADE_Managed/src/Editor/UndoRedoStack.hxx | 32 ++++++ 2 files changed, 142 insertions(+), 5 deletions(-) diff --git a/SHADE_Managed/src/Editor/UndoRedoStack.cxx b/SHADE_Managed/src/Editor/UndoRedoStack.cxx index 3feb43ed..10ef822c 100644 --- a/SHADE_Managed/src/Editor/UndoRedoStack.cxx +++ b/SHADE_Managed/src/Editor/UndoRedoStack.cxx @@ -24,6 +24,9 @@ of DigiPen Institute of Technology is prohibited. namespace SHADE { + /*---------------------------------------------------------------------------------*/ + /* UndoRedoStack - Properties */ + /*---------------------------------------------------------------------------------*/ bool UndoRedoStack::UndoActionPresent::get() { return commandStack->Count > 0 && latestActionIndex >= 0; @@ -33,7 +36,10 @@ namespace SHADE { return latestActionIndex >= 0 && latestActionIndex < commandStack->Count - 1; } - + + /*---------------------------------------------------------------------------------*/ + /* UndoRedoStack - Usage Functions */ + /*---------------------------------------------------------------------------------*/ void UndoRedoStack::Add(ICommand^ command) { // Erase any other actions ahead of the current action @@ -68,14 +74,20 @@ namespace SHADE cmd->Execute(); ++latestActionIndex; } - + + /*---------------------------------------------------------------------------------*/ + /* FieldChangeCommand - Constructor */ + /*---------------------------------------------------------------------------------*/ FieldChangeCommand::FieldChangeCommand(System::Object^ obj, System::Reflection::FieldInfo^ field, System::Object^ newData, System::Object^ oldData) : objectToChange { obj } , field { field } , newData { newData } , oldData { oldData } {} - + + /*---------------------------------------------------------------------------------*/ + /* FieldChangeCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ bool FieldChangeCommand::Execute() { if (field && objectToChange) @@ -116,15 +128,21 @@ namespace SHADE return false; } - + + /*---------------------------------------------------------------------------------*/ + /* ListElementChangeCommand - Constructor */ + /*---------------------------------------------------------------------------------*/ generic ListElementChangeCommand::ListElementChangeCommand(System::Collections::Generic::List^ list, int index, T newData, T oldData) : list { list } - , index { index } + , index{ index } , newData { newData } , oldData { oldData } {} + /*---------------------------------------------------------------------------------*/ + /* ListElementChangeCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ generic bool ListElementChangeCommand::Execute() { @@ -164,6 +182,93 @@ namespace SHADE newData = otherCommand->newData; return true; } + } + + /*---------------------------------------------------------------------------------*/ + /* ListElementAddCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ + generic + ListElementAddCommand::ListElementAddCommand(System::Collections::Generic::List^ list, int addIndex, T data) + : list { list } + , addIndex { addIndex } + , data { data } + {} + + /*---------------------------------------------------------------------------------*/ + /* ListElementAddCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ + generic + bool ListElementAddCommand::Execute() + { + if (list) + { + list->Insert(addIndex, data); + return true; + } + + return false; + } + + generic + bool ListElementAddCommand::Unexceute() + { + if (list && addIndex < System::Linq::Enumerable::Count(list)) + { + list->RemoveAt(addIndex); + return true; + } + + return false; + } + + generic + bool ListElementAddCommand::Merge(ICommand^) + { + // Not allowed + return false; } + /*---------------------------------------------------------------------------------*/ + /* ListElementRemoveCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ + generic + ListElementRemoveCommand::ListElementRemoveCommand(System::Collections::Generic::List^ list, int removeIndex, T data) + : list { list } + , removeIndex { removeIndex } + , data { data } + {} + + /*---------------------------------------------------------------------------------*/ + /* ListElementRemoveCommand - ICommand Functions */ + /*---------------------------------------------------------------------------------*/ + generic + bool ListElementRemoveCommand::Execute() + { + if (list && removeIndex < System::Linq::Enumerable::Count(list)) + { + list->RemoveAt(removeIndex); + return true; + } + + return false; + } + + generic + bool ListElementRemoveCommand::Unexceute() + { + if (list) + { + list->Insert(removeIndex, data); + return true; + } + + return false; + } + + generic + bool ListElementRemoveCommand::Merge(ICommand^) + { + // Not allowed + return false; + } } diff --git a/SHADE_Managed/src/Editor/UndoRedoStack.hxx b/SHADE_Managed/src/Editor/UndoRedoStack.hxx index dd78ecd9..ed9a625a 100644 --- a/SHADE_Managed/src/Editor/UndoRedoStack.hxx +++ b/SHADE_Managed/src/Editor/UndoRedoStack.hxx @@ -71,6 +71,38 @@ namespace SHADE T newData; T oldData; }; + + generic + private ref class ListElementAddCommand sealed : public ICommand + { + public: + ListElementAddCommand(System::Collections::Generic::List^ list, int addIndex, T data); + + bool Execute() override; + bool Unexceute() override; + bool Merge(ICommand^ command) override; + + private: + System::Collections::Generic::List^ list; + int addIndex; // New index of the added element + T data; + }; + + generic + private ref class ListElementRemoveCommand sealed : public ICommand + { + public: + ListElementRemoveCommand(System::Collections::Generic::List^ list, int removeIndex, T data); + + bool Execute() override; + bool Unexceute() override; + bool Merge(ICommand^ command) override; + + private: + System::Collections::Generic::List^ list; + int removeIndex; // Index of the element to remove at + T data; + }; /// /// Class that is able to store a stack of actions that can be done and redone.