2024-10-10 21:27:03 +08:00
|
|
|
// 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
|
|
|
|
{
|
2024-10-10 21:30:24 +08:00
|
|
|
public abstract class PropertyChangeCommand<TTarget, TProperty> : IEditorCommand, IMergeableCommand where TTarget : class
|
|
|
|
{
|
|
|
|
protected abstract TProperty ReadValue(TTarget target);
|
2024-10-10 21:27:03 +08:00
|
|
|
|
2024-10-10 21:30:24 +08:00
|
|
|
protected abstract void WriteValue(TTarget target, TProperty value);
|
2024-10-10 21:27:03 +08:00
|
|
|
|
2024-10-10 21:30:24 +08:00
|
|
|
protected abstract PropertyChangeCommand<TTarget, TProperty> CreateInstance(TTarget target, TProperty value);
|
2024-10-10 21:27:03 +08:00
|
|
|
|
2024-10-10 21:30:24 +08:00
|
|
|
public readonly TTarget Target;
|
2024-10-10 21:27:03 +08:00
|
|
|
|
2024-10-10 21:30:24 +08:00
|
|
|
public readonly TProperty Value;
|
2024-10-10 21:27:03 +08:00
|
|
|
|
2024-10-10 21:30:24 +08:00
|
|
|
protected PropertyChangeCommand(TTarget target, TProperty value)
|
|
|
|
{
|
|
|
|
Target = target;
|
|
|
|
Value = value;
|
|
|
|
}
|
2024-10-10 21:27:03 +08:00
|
|
|
|
2024-10-10 21:30:24 +08:00
|
|
|
public void Apply() => WriteValue(Target, Value);
|
2024-10-10 21:27:03 +08:00
|
|
|
|
2024-10-10 21:30:24 +08:00
|
|
|
public IEditorCommand CreateUndo() => CreateInstance(Target, Value);
|
2024-10-10 21:27:03 +08:00
|
|
|
|
2024-10-10 21:30:24 +08:00
|
|
|
public IEditorCommand? MergeWith(IEditorCommand previous)
|
|
|
|
{
|
|
|
|
if (previous is PropertyChangeCommand<TTarget, TProperty> command && command.Target == Target)
|
|
|
|
return this;
|
2024-10-10 21:27:03 +08:00
|
|
|
|
2024-10-10 21:30:24 +08:00
|
|
|
return null;
|
|
|
|
}
|
2024-10-10 21:27:03 +08:00
|
|
|
}
|
|
|
|
}
|