Added implementation for ListElementAddCommand and ListElementRemoveCommand

This commit is contained in:
Kah Wei 2022-11-11 12:07:05 +08:00
parent 80db641b6f
commit 85cc97ca27
2 changed files with 142 additions and 5 deletions

View File

@ -24,6 +24,9 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE namespace SHADE
{ {
/*---------------------------------------------------------------------------------*/
/* UndoRedoStack - Properties */
/*---------------------------------------------------------------------------------*/
bool UndoRedoStack::UndoActionPresent::get() bool UndoRedoStack::UndoActionPresent::get()
{ {
return commandStack->Count > 0 && latestActionIndex >= 0; return commandStack->Count > 0 && latestActionIndex >= 0;
@ -34,6 +37,9 @@ namespace SHADE
return latestActionIndex >= 0 && latestActionIndex < commandStack->Count - 1; return latestActionIndex >= 0 && latestActionIndex < commandStack->Count - 1;
} }
/*---------------------------------------------------------------------------------*/
/* UndoRedoStack - Usage Functions */
/*---------------------------------------------------------------------------------*/
void UndoRedoStack::Add(ICommand^ command) void UndoRedoStack::Add(ICommand^ command)
{ {
// Erase any other actions ahead of the current action // Erase any other actions ahead of the current action
@ -69,6 +75,9 @@ namespace SHADE
++latestActionIndex; ++latestActionIndex;
} }
/*---------------------------------------------------------------------------------*/
/* FieldChangeCommand - Constructor */
/*---------------------------------------------------------------------------------*/
FieldChangeCommand::FieldChangeCommand(System::Object^ obj, System::Reflection::FieldInfo^ field, System::Object^ newData, System::Object^ oldData) FieldChangeCommand::FieldChangeCommand(System::Object^ obj, System::Reflection::FieldInfo^ field, System::Object^ newData, System::Object^ oldData)
: objectToChange { obj } : objectToChange { obj }
, field { field } , field { field }
@ -76,6 +85,9 @@ namespace SHADE
, oldData { oldData } , oldData { oldData }
{} {}
/*---------------------------------------------------------------------------------*/
/* FieldChangeCommand - ICommand Functions */
/*---------------------------------------------------------------------------------*/
bool FieldChangeCommand::Execute() bool FieldChangeCommand::Execute()
{ {
if (field && objectToChange) if (field && objectToChange)
@ -117,14 +129,20 @@ namespace SHADE
return false; return false;
} }
/*---------------------------------------------------------------------------------*/
/* ListElementChangeCommand - Constructor */
/*---------------------------------------------------------------------------------*/
generic<typename T> generic<typename T>
ListElementChangeCommand<T>::ListElementChangeCommand(System::Collections::Generic::List<T>^ list, int index, T newData, T oldData) ListElementChangeCommand<T>::ListElementChangeCommand(System::Collections::Generic::List<T>^ list, int index, T newData, T oldData)
: list { list } : list { list }
, index { index } , index{ index }
, newData { newData } , newData { newData }
, oldData { oldData } , oldData { oldData }
{} {}
/*---------------------------------------------------------------------------------*/
/* ListElementChangeCommand - ICommand Functions */
/*---------------------------------------------------------------------------------*/
generic<typename T> generic<typename T>
bool ListElementChangeCommand<T>::Execute() bool ListElementChangeCommand<T>::Execute()
{ {
@ -166,4 +184,91 @@ namespace SHADE
} }
} }
/*---------------------------------------------------------------------------------*/
/* ListElementAddCommand - ICommand Functions */
/*---------------------------------------------------------------------------------*/
generic<typename T>
ListElementAddCommand<T>::ListElementAddCommand(System::Collections::Generic::List<T>^ list, int addIndex, T data)
: list { list }
, addIndex { addIndex }
, data { data }
{}
/*---------------------------------------------------------------------------------*/
/* ListElementAddCommand - ICommand Functions */
/*---------------------------------------------------------------------------------*/
generic<typename T>
bool ListElementAddCommand<T>::Execute()
{
if (list)
{
list->Insert(addIndex, data);
return true;
}
return false;
}
generic<typename T>
bool ListElementAddCommand<T>::Unexceute()
{
if (list && addIndex < System::Linq::Enumerable::Count(list))
{
list->RemoveAt(addIndex);
return true;
}
return false;
}
generic<typename T>
bool ListElementAddCommand<T>::Merge(ICommand^)
{
// Not allowed
return false;
}
/*---------------------------------------------------------------------------------*/
/* ListElementRemoveCommand - ICommand Functions */
/*---------------------------------------------------------------------------------*/
generic<typename T>
ListElementRemoveCommand<T>::ListElementRemoveCommand(System::Collections::Generic::List<T>^ list, int removeIndex, T data)
: list { list }
, removeIndex { removeIndex }
, data { data }
{}
/*---------------------------------------------------------------------------------*/
/* ListElementRemoveCommand - ICommand Functions */
/*---------------------------------------------------------------------------------*/
generic<typename T>
bool ListElementRemoveCommand<T>::Execute()
{
if (list && removeIndex < System::Linq::Enumerable::Count(list))
{
list->RemoveAt(removeIndex);
return true;
}
return false;
}
generic<typename T>
bool ListElementRemoveCommand<T>::Unexceute()
{
if (list)
{
list->Insert(removeIndex, data);
return true;
}
return false;
}
generic<typename T>
bool ListElementRemoveCommand<T>::Merge(ICommand^)
{
// Not allowed
return false;
}
} }

View File

@ -72,6 +72,38 @@ namespace SHADE
T oldData; T oldData;
}; };
generic<typename T>
private ref class ListElementAddCommand sealed : public ICommand
{
public:
ListElementAddCommand(System::Collections::Generic::List<T>^ list, int addIndex, T data);
bool Execute() override;
bool Unexceute() override;
bool Merge(ICommand^ command) override;
private:
System::Collections::Generic::List<T>^ list;
int addIndex; // New index of the added element
T data;
};
generic<typename T>
private ref class ListElementRemoveCommand sealed : public ICommand
{
public:
ListElementRemoveCommand(System::Collections::Generic::List<T>^ list, int removeIndex, T data);
bool Execute() override;
bool Unexceute() override;
bool Merge(ICommand^ command) override;
private:
System::Collections::Generic::List<T>^ list;
int removeIndex; // Index of the element to remove at
T data;
};
/// <summary> /// <summary>
/// Class that is able to store a stack of actions that can be done and redone. /// Class that is able to store a stack of actions that can be done and redone.
/// </summary> /// </summary>