1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 20:07:25 +08:00

Add disabled state to menu items

This commit is contained in:
smoogipoo 2020-04-15 15:50:19 +09:00
parent 2af2a49f22
commit 102c1d9095
3 changed files with 118 additions and 5 deletions

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 actionPeformed;
[SetUp]
public void Setup() => Schedule(() =>
{
actionPeformed = 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", () => actionPeformed);
}
[Test]
public void TestDisableMenuItemsAndClick()
{
AddStep("disable menu items", () =>
{
foreach (var item in menu.Items)
((OsuMenuItem)item).Enabled.Value = false;
});
AddStep("move to first menu item", () => InputManager.MoveMouseTo(menu.ChildrenOfType<DrawableOsuMenuItem>().First()));
AddStep("click", () => InputManager.Click(MouseButton.Left));
AddAssert("action not performed", () => !actionPeformed);
}
[Test]
public void TestEnableMenuItemsAndClick()
{
AddStep("disable menu items", () =>
{
foreach (var item in menu.Items)
((OsuMenuItem)item).Enabled.Value = false;
});
AddStep("enable menu items", () =>
{
foreach (var item in menu.Items)
((OsuMenuItem)item).Enabled.Value = true;
});
AddStep("move to first menu item", () => InputManager.MoveMouseTo(menu.ChildrenOfType<DrawableOsuMenuItem>().First()));
AddStep("click", () => InputManager.Click(MouseButton.Left));
AddAssert("action performed", () => actionPeformed);
}
private void performAction() => actionPeformed = true;
}
}

View File

@ -42,6 +42,8 @@ namespace osu.Game.Graphics.UserInterface
BackgroundColourHover = Color4Extensions.FromHex(@"172023");
updateTextColour();
Item.Action.BindDisabledChanged(_ => updateState(), true);
}
private void updateTextColour()
@ -65,19 +67,33 @@ namespace osu.Game.Graphics.UserInterface
protected override bool OnHover(HoverEvent e)
{
sampleHover.Play();
text.BoldText.FadeIn(transition_length, Easing.OutQuint);
text.NormalText.FadeOut(transition_length, Easing.OutQuint);
updateState();
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
text.BoldText.FadeOut(transition_length, Easing.OutQuint);
text.NormalText.FadeIn(transition_length, Easing.OutQuint);
updateState();
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)
{
sampleClick.Play();

View File

@ -2,12 +2,15 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Bindables;
using osu.Framework.Graphics.UserInterface;
namespace osu.Game.Graphics.UserInterface
{
public class OsuMenuItem : MenuItem
{
public readonly Bindable<bool> Enabled = new Bindable<bool>(true);
public readonly MenuItemType Type;
public OsuMenuItem(string text, MenuItemType type = MenuItemType.Standard)
@ -19,6 +22,9 @@ namespace osu.Game.Graphics.UserInterface
: base(text, action)
{
Type = type;
Enabled.BindValueChanged(enabled => Action.Disabled = !enabled.NewValue);
Action.BindDisabledChanged(disabled => Enabled.Value = !disabled);
}
}
}