1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-05 10:33:22 +08:00

Add PropertyChangeCommand

This commit is contained in:
Marvin Schürz 2024-10-10 15:27:03 +02:00
parent 1924463465
commit 4a2995d7e5

View File

@ -0,0 +1,36 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
namespace osu.Game.Screens.Edit.Commands
{
public abstract class PropertyChangeCommand<TTarget, TProperty> : IEditorCommand, IMergeableCommand where TTarget : class
{
protected abstract TProperty ReadValue(TTarget target);
protected abstract void WriteValue(TTarget target, TProperty value);
protected abstract PropertyChangeCommand<TTarget, TProperty> CreateInstance(TTarget target, TProperty value);
public readonly TTarget Target;
public readonly TProperty Value;
protected PropertyChangeCommand(TTarget target, TProperty value)
{
Target = target;
Value = value;
}
public void Apply() => WriteValue(Target, Value);
public IEditorCommand CreateUndo() => CreateInstance(Target, Value);
public IEditorCommand? MergeWith(IEditorCommand previous)
{
if (previous is PropertyChangeCommand<TTarget, TProperty> command && command.Target == Target)
return this;
return null;
}
}
}