Rename ThreeState -> TernaryState and add basic tests

This commit is contained in:
Dean Herbert 2019-11-11 18:56:18 +09:00
parent ce4843be22
commit bed62e0d2f
4 changed files with 86 additions and 56 deletions

View File

@ -0,0 +1,65 @@
// 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 osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Graphics.UserInterface;
using osuTK.Input;
namespace osu.Game.Tests.Visual.UserInterface
{
public class TestSceneTernaryMenuItem : ManualInputManagerTestScene
{
private readonly OsuMenu menu;
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(OsuMenu),
typeof(ThreeStateMenuItem),
typeof(DrawableStatefulMenuItem)
};
private readonly Bindable<TernaryState> state = new Bindable<TernaryState>(TernaryState.Indeterminate);
public TestSceneTernaryMenuItem()
{
Add(menu = new OsuMenu(Direction.Vertical, true)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Items = new[]
{
new ThreeStateMenuItem("First"),
new ThreeStateMenuItem("Second") { State = { BindTarget = state } },
new ThreeStateMenuItem("Third") { State = { Value = TernaryState.True } },
}
});
checkState(TernaryState.Indeterminate);
click();
checkState(TernaryState.True);
click();
checkState(TernaryState.False);
click();
checkState(TernaryState.True);
click();
checkState(TernaryState.False);
}
private void click() =>
AddStep("click", () =>
{
InputManager.MoveMouseTo(menu.ScreenSpaceDrawQuad.Centre);
InputManager.Click(MouseButton.Left);
});
private void checkState(TernaryState expected)
=> AddAssert($"state is {expected}", () => state.Value == expected);
}
}

View File

@ -1,35 +0,0 @@
// 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 osu.Framework.Graphics;
using osu.Game.Graphics.UserInterface;
namespace osu.Game.Tests.Visual.UserInterface
{
public class TestSceneThreeStateMenuItem : OsuTestScene
{
public override IReadOnlyList<Type> RequiredTypes => new[]
{
typeof(OsuMenu),
typeof(ThreeStateMenuItem),
typeof(DrawableStatefulMenuItem)
};
public TestSceneThreeStateMenuItem()
{
Add(new OsuMenu(Direction.Vertical, true)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Items = new[]
{
new ThreeStateMenuItem("First"),
new ThreeStateMenuItem("Second") { State = { Value = ThreeStates.Indeterminate } },
new ThreeStateMenuItem("Third") { State = { Value = ThreeStates.Enabled } },
}
});
}
}
}

View File

@ -6,22 +6,22 @@ namespace osu.Game.Graphics.UserInterface
/// <summary>
/// An on/off state with an extra indeterminate state.
/// </summary>
public enum ThreeStates
public enum TernaryState
{
/// <summary>
/// The current state is disabled.
/// The current state is false.
/// </summary>
Disabled,
False,
/// <summary>
/// The current state is a combination of <see cref="Disabled"/> and <see cref="Enabled"/>.
/// The state becomes <see cref="Enabled"/> if the <see cref="ThreeStateMenuItem"/> is pressed.
/// The current state is a combination of <see cref="False"/> and <see cref="True"/>.
/// The state becomes <see cref="True"/> if the <see cref="ThreeStateMenuItem"/> is pressed.
/// </summary>
Indeterminate,
/// <summary>
/// The current state is enabled.
/// The current state is true.
/// </summary>
Enabled
True
}
}

View File

@ -9,7 +9,7 @@ namespace osu.Game.Graphics.UserInterface
/// <summary>
/// An <see cref="OsuMenuItem"/> with three possible states.
/// </summary>
public class ThreeStateMenuItem : StatefulMenuItem<ThreeStates>
public class ThreeStateMenuItem : StatefulMenuItem<TernaryState>
{
/// <summary>
/// Creates a new <see cref="ThreeStateMenuItem"/>.
@ -27,7 +27,7 @@ namespace osu.Game.Graphics.UserInterface
/// <param name="text">The text to display.</param>
/// <param name="type">The type of action which this <see cref="ThreeStateMenuItem"/> performs.</param>
/// <param name="action">A delegate to be invoked when this <see cref="ThreeStateMenuItem"/> is pressed.</param>
public ThreeStateMenuItem(string text, MenuItemType type, Action<ThreeStates> action)
public ThreeStateMenuItem(string text, MenuItemType type, Action<TernaryState> action)
: this(text, getNextState, type, action)
{
}
@ -39,37 +39,37 @@ namespace osu.Game.Graphics.UserInterface
/// <param name="changeStateFunc">A function that mutates a state to another state after this <see cref="ThreeStateMenuItem"/> is pressed.</param>
/// <param name="type">The type of action which this <see cref="ThreeStateMenuItem"/> performs.</param>
/// <param name="action">A delegate to be invoked when this <see cref="ThreeStateMenuItem"/> is pressed.</param>
protected ThreeStateMenuItem(string text, Func<ThreeStates, ThreeStates> changeStateFunc, MenuItemType type, Action<ThreeStates> action)
protected ThreeStateMenuItem(string text, Func<TernaryState, TernaryState> changeStateFunc, MenuItemType type, Action<TernaryState> action)
: base(text, changeStateFunc, type, action)
{
}
public override IconUsage? GetIconForState(ThreeStates state)
public override IconUsage? GetIconForState(TernaryState state)
{
switch (state)
{
case ThreeStates.Indeterminate:
return FontAwesome.Regular.Circle;
case TernaryState.Indeterminate:
return FontAwesome.Solid.DotCircle;
case ThreeStates.Enabled:
case TernaryState.True:
return FontAwesome.Solid.Check;
}
return null;
}
private static ThreeStates getNextState(ThreeStates state)
private static TernaryState getNextState(TernaryState state)
{
switch (state)
{
case ThreeStates.Disabled:
return ThreeStates.Enabled;
case TernaryState.False:
return TernaryState.True;
case ThreeStates.Indeterminate:
return ThreeStates.Enabled;
case TernaryState.Indeterminate:
return TernaryState.True;
case ThreeStates.Enabled:
return ThreeStates.Disabled;
case TernaryState.True:
return TernaryState.False;
default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);