Added ListElementChangeCommand

This commit is contained in:
Kah Wei 2022-11-11 00:49:20 +08:00
parent 5875107ce2
commit 80db641b6f
2 changed files with 67 additions and 1 deletions

View File

@ -117,4 +117,53 @@ namespace SHADE
return false;
}
generic<typename T>
ListElementChangeCommand<T>::ListElementChangeCommand(System::Collections::Generic::List<T>^ list, int index, T newData, T oldData)
: list { list }
, index { index }
, newData { newData }
, oldData { oldData }
{}
generic<typename T>
bool ListElementChangeCommand<T>::Execute()
{
if (list && index < System::Linq::Enumerable::Count(list))
{
list[index] = newData;
return true;
}
return false;
}
generic<typename T>
bool ListElementChangeCommand<T>::Unexceute()
{
if (list && index < System::Linq::Enumerable::Count(list))
{
list[index] = oldData;
return true;
}
return false;
}
generic<typename T>
bool ListElementChangeCommand<T>::Merge(ICommand^ command)
{
ListElementChangeCommand<T>^ otherCommand = safe_cast<ListElementChangeCommand<T>^>(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;
}
}
}

View File

@ -55,6 +55,23 @@ namespace SHADE
System::Object^ oldData;
};
generic<typename T>
private ref class ListElementChangeCommand sealed : public ICommand
{
public:
ListElementChangeCommand(System::Collections::Generic::List<T>^ list, int index, T newData, T oldData);
bool Execute() override;
bool Unexceute() override;
bool Merge(ICommand^ command) override;
private:
System::Collections::Generic::List<T>^ list;
int index;
T newData;
T oldData;
};
/// <summary>
/// Class that is able to store a stack of actions that can be done and redone.
/// </summary>