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
{
/*---------------------------------------------------------------------------------*/
/* 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<typename T>
ListElementChangeCommand<T>::ListElementChangeCommand(System::Collections::Generic::List<T>^ list, int index, T newData, T oldData)
: list { list }
, index { index }
, index{ index }
, newData { newData }
, oldData { oldData }
{}
/*---------------------------------------------------------------------------------*/
/* ListElementChangeCommand - ICommand Functions */
/*---------------------------------------------------------------------------------*/
generic<typename T>
bool ListElementChangeCommand<T>::Execute()
{
@ -164,6 +182,93 @@ namespace SHADE
newData = otherCommand->newData;
return true;
}
}
/*---------------------------------------------------------------------------------*/
/* 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

@ -71,6 +71,38 @@ namespace SHADE
T newData;
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>
/// Class that is able to store a stack of actions that can be done and redone.