1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 06:42:54 +08:00

Merge pull request #8764 from smoogipoo/disable-undo-redo

Disable undo/redo menu items when they can't be performed
This commit is contained in:
Dean Herbert 2020-04-16 13:47:23 +09:00 committed by GitHub
commit 5ec8d49241
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 147 additions and 21 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

@ -0,0 +1,91 @@
// 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.
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Testing;
using osu.Game.Graphics.UserInterface;
using osuTK.Input;
namespace osu.Game.Tests.Visual.UserInterface
{
public class TestSceneOsuMenu : OsuManualInputManagerTestScene
{
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(OsuMenu),
typeof(DrawableOsuMenuItem)
};
private OsuMenu menu;
private bool actionPerformed;
[SetUp]
public void Setup() => Schedule(() =>
{
actionPerformed = false;
Child = menu = new OsuMenu(Direction.Vertical, true)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Items = new[]
{
new OsuMenuItem("standard", MenuItemType.Standard, performAction),
new OsuMenuItem("highlighted", MenuItemType.Highlighted, performAction),
new OsuMenuItem("destructive", MenuItemType.Destructive, performAction),
}
};
});
[Test]
public void TestClickEnabledMenuItem()
{
AddStep("move to first menu item", () => InputManager.MoveMouseTo(menu.ChildrenOfType<DrawableOsuMenuItem>().First()));
AddStep("click", () => InputManager.Click(MouseButton.Left));
AddAssert("action performed", () => actionPerformed);
}
[Test]
public void TestDisableMenuItemsAndClick()
{
AddStep("disable menu items", () =>
{
foreach (var item in menu.Items)
((OsuMenuItem)item).Action.Disabled = true;
});
AddStep("move to first menu item", () => InputManager.MoveMouseTo(menu.ChildrenOfType<DrawableOsuMenuItem>().First()));
AddStep("click", () => InputManager.Click(MouseButton.Left));
AddAssert("action not performed", () => !actionPerformed);
}
[Test]
public void TestEnableMenuItemsAndClick()
{
AddStep("disable menu items", () =>
{
foreach (var item in menu.Items)
((OsuMenuItem)item).Action.Disabled = true;
});
AddStep("enable menu items", () =>
{
foreach (var item in menu.Items)
((OsuMenuItem)item).Action.Disabled = false;
});
AddStep("move to first menu item", () => InputManager.MoveMouseTo(menu.ChildrenOfType<DrawableOsuMenuItem>().First()));
AddStep("click", () => InputManager.Click(MouseButton.Left));
AddAssert("action performed", () => actionPerformed);
}
private void performAction() => actionPerformed = true;
}
}

View File

@ -42,6 +42,8 @@ namespace osu.Game.Graphics.UserInterface
BackgroundColourHover = Color4Extensions.FromHex(@"172023"); BackgroundColourHover = Color4Extensions.FromHex(@"172023");
updateTextColour(); updateTextColour();
Item.Action.BindDisabledChanged(_ => updateState(), true);
} }
private void updateTextColour() private void updateTextColour()
@ -65,19 +67,33 @@ namespace osu.Game.Graphics.UserInterface
protected override bool OnHover(HoverEvent e) protected override bool OnHover(HoverEvent e)
{ {
sampleHover.Play(); updateState();
text.BoldText.FadeIn(transition_length, Easing.OutQuint);
text.NormalText.FadeOut(transition_length, Easing.OutQuint);
return base.OnHover(e); return base.OnHover(e);
} }
protected override void OnHoverLost(HoverLostEvent e) protected override void OnHoverLost(HoverLostEvent e)
{ {
text.BoldText.FadeOut(transition_length, Easing.OutQuint); updateState();
text.NormalText.FadeIn(transition_length, Easing.OutQuint);
base.OnHoverLost(e); base.OnHoverLost(e);
} }
private void updateState()
{
Alpha = Item.Action.Disabled ? 0.2f : 1;
if (IsHovered && !Item.Action.Disabled)
{
sampleHover.Play();
text.BoldText.FadeIn(transition_length, Easing.OutQuint);
text.NormalText.FadeOut(transition_length, Easing.OutQuint);
}
else
{
text.BoldText.FadeOut(transition_length, Easing.OutQuint);
text.NormalText.FadeIn(transition_length, Easing.OutQuint);
}
}
protected override bool OnClick(ClickEvent e) protected override bool OnClick(ClickEvent e)
{ {
sampleClick.Play(); sampleClick.Play();

View File

@ -107,6 +107,8 @@ namespace osu.Game.Screens.Edit
dependencies.CacheAs<IEditorChangeHandler>(changeHandler); dependencies.CacheAs<IEditorChangeHandler>(changeHandler);
EditorMenuBar menuBar; EditorMenuBar menuBar;
OsuMenuItem undoMenuItem;
OsuMenuItem redoMenuItem;
var fileMenuItems = new List<MenuItem> var fileMenuItems = new List<MenuItem>
{ {
@ -155,8 +157,8 @@ namespace osu.Game.Screens.Edit
{ {
Items = new[] Items = new[]
{ {
new EditorMenuItem("Undo", MenuItemType.Standard, undo), undoMenuItem = new EditorMenuItem("Undo", MenuItemType.Standard, undo),
new EditorMenuItem("Redo", MenuItemType.Standard, redo) redoMenuItem = new EditorMenuItem("Redo", MenuItemType.Standard, redo)
} }
} }
} }
@ -214,6 +216,9 @@ namespace osu.Game.Screens.Edit
} }
}); });
changeHandler.CanUndo.BindValueChanged(v => undoMenuItem.Action.Disabled = !v.NewValue, true);
changeHandler.CanRedo.BindValueChanged(v => redoMenuItem.Action.Disabled = !v.NewValue, true);
menuBar.Mode.ValueChanged += onModeChanged; menuBar.Mode.ValueChanged += onModeChanged;
bottomBackground.Colour = colours.Gray2; bottomBackground.Colour = colours.Gray2;

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;
} }
} }
} }