Add CanUndo/CanRedo bindables

This commit is contained in:
smoogipoo
2020-04-15 15:50:43 +09:00
parent 102c1d9095
commit e8c955ed9b
2 changed files with 28 additions and 14 deletions

View File

@ -15,15 +15,18 @@ namespace osu.Game.Tests.Editor
{ {
var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap())); var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap()));
Assert.That(handler.HasUndoState, Is.False); Assert.That(handler.CanUndo.Value, Is.False);
Assert.That(handler.CanRedo.Value, Is.False);
handler.SaveState(); handler.SaveState();
Assert.That(handler.HasUndoState, Is.True); Assert.That(handler.CanUndo.Value, Is.True);
Assert.That(handler.CanRedo.Value, Is.False);
handler.RestoreState(-1); handler.RestoreState(-1);
Assert.That(handler.HasUndoState, Is.False); Assert.That(handler.CanUndo.Value, Is.False);
Assert.That(handler.CanRedo.Value, Is.True);
} }
[Test] [Test]
@ -31,20 +34,20 @@ namespace osu.Game.Tests.Editor
{ {
var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap())); var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap()));
Assert.That(handler.HasUndoState, Is.False); Assert.That(handler.CanUndo.Value, Is.False);
for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES; i++) for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES; i++)
handler.SaveState(); handler.SaveState();
Assert.That(handler.HasUndoState, Is.True); Assert.That(handler.CanUndo.Value, Is.True);
for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES; i++) for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES; i++)
{ {
Assert.That(handler.HasUndoState, Is.True); Assert.That(handler.CanUndo.Value, Is.True);
handler.RestoreState(-1); handler.RestoreState(-1);
} }
Assert.That(handler.HasUndoState, Is.False); Assert.That(handler.CanUndo.Value, Is.False);
} }
[Test] [Test]
@ -52,20 +55,20 @@ namespace osu.Game.Tests.Editor
{ {
var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap())); var handler = new EditorChangeHandler(new EditorBeatmap(new Beatmap()));
Assert.That(handler.HasUndoState, Is.False); Assert.That(handler.CanUndo.Value, Is.False);
for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES * 2; i++) for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES * 2; i++)
handler.SaveState(); handler.SaveState();
Assert.That(handler.HasUndoState, Is.True); Assert.That(handler.CanUndo.Value, Is.True);
for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES; i++) for (int i = 0; i < EditorChangeHandler.MAX_SAVED_STATES; i++)
{ {
Assert.That(handler.HasUndoState, Is.True); Assert.That(handler.CanUndo.Value, Is.True);
handler.RestoreState(-1); handler.RestoreState(-1);
} }
Assert.That(handler.HasUndoState, Is.False); Assert.That(handler.CanUndo.Value, Is.False);
} }
} }
} }

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using osu.Framework.Bindables;
using osu.Game.Beatmaps.Formats; using osu.Game.Beatmaps.Formats;
using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Objects;
@ -15,8 +16,10 @@ namespace osu.Game.Screens.Edit
/// </summary> /// </summary>
public class EditorChangeHandler : IEditorChangeHandler public class EditorChangeHandler : IEditorChangeHandler
{ {
private readonly LegacyEditorBeatmapPatcher patcher; public readonly Bindable<bool> CanUndo = new Bindable<bool>();
public readonly Bindable<bool> CanRedo = new Bindable<bool>();
private readonly LegacyEditorBeatmapPatcher patcher;
private readonly List<byte[]> savedStates = new List<byte[]>(); private readonly List<byte[]> savedStates = new List<byte[]>();
private int currentState = -1; private int currentState = -1;
@ -45,8 +48,6 @@ namespace osu.Game.Screens.Edit
SaveState(); SaveState();
} }
public bool HasUndoState => currentState > 0;
private void hitObjectAdded(HitObject obj) => SaveState(); private void hitObjectAdded(HitObject obj) => SaveState();
private void hitObjectRemoved(HitObject obj) => SaveState(); private void hitObjectRemoved(HitObject obj) => SaveState();
@ -90,6 +91,8 @@ namespace osu.Game.Screens.Edit
} }
currentState = savedStates.Count - 1; currentState = savedStates.Count - 1;
updateBindables();
} }
/// <summary> /// <summary>
@ -114,6 +117,14 @@ namespace osu.Game.Screens.Edit
currentState = newState; currentState = newState;
isRestoring = false; isRestoring = false;
updateBindables();
}
private void updateBindables()
{
CanUndo.Value = savedStates.Count > 0 && currentState > 0;
CanRedo.Value = currentState < savedStates.Count - 1;
} }
} }
} }