mirror of
https://github.com/ppy/osu.git
synced 2025-01-15 14:12:54 +08:00
Implement stateful menu items (#6760)
Implement stateful menu items Co-authored-by: Dean Herbert <pe@ppy.sh>
This commit is contained in:
commit
2a7761bd56
145
osu.Game.Tests/Visual/UserInterface/TestSceneStatefulMenuItem.cs
Normal file
145
osu.Game.Tests/Visual/UserInterface/TestSceneStatefulMenuItem.cs
Normal file
@ -0,0 +1,145 @@
|
|||||||
|
// 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 NUnit.Framework;
|
||||||
|
using osu.Framework.Bindables;
|
||||||
|
using osu.Framework.Graphics;
|
||||||
|
using osu.Framework.Graphics.Sprites;
|
||||||
|
using osu.Game.Graphics.UserInterface;
|
||||||
|
using osuTK.Input;
|
||||||
|
|
||||||
|
namespace osu.Game.Tests.Visual.UserInterface
|
||||||
|
{
|
||||||
|
public class TestSceneStatefulMenuItem : ManualInputManagerTestScene
|
||||||
|
{
|
||||||
|
public override IReadOnlyList<Type> RequiredTypes => new[]
|
||||||
|
{
|
||||||
|
typeof(OsuMenu),
|
||||||
|
typeof(StatefulMenuItem),
|
||||||
|
typeof(TernaryStateMenuItem),
|
||||||
|
typeof(DrawableStatefulMenuItem),
|
||||||
|
};
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestTernaryMenuItem()
|
||||||
|
{
|
||||||
|
OsuMenu menu = null;
|
||||||
|
|
||||||
|
Bindable<TernaryState> state = new Bindable<TernaryState>(TernaryState.Indeterminate);
|
||||||
|
|
||||||
|
AddStep("create menu", () =>
|
||||||
|
{
|
||||||
|
state.Value = TernaryState.Indeterminate;
|
||||||
|
|
||||||
|
Child = menu = new OsuMenu(Direction.Vertical, true)
|
||||||
|
{
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
|
Items = new[]
|
||||||
|
{
|
||||||
|
new TernaryStateMenuItem("First"),
|
||||||
|
new TernaryStateMenuItem("Second") { State = { BindTarget = state } },
|
||||||
|
new TernaryStateMenuItem("Third") { State = { Value = TernaryState.True } },
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
checkState(TernaryState.Indeterminate);
|
||||||
|
|
||||||
|
click();
|
||||||
|
checkState(TernaryState.True);
|
||||||
|
|
||||||
|
click();
|
||||||
|
checkState(TernaryState.False);
|
||||||
|
|
||||||
|
click();
|
||||||
|
checkState(TernaryState.True);
|
||||||
|
|
||||||
|
click();
|
||||||
|
checkState(TernaryState.False);
|
||||||
|
|
||||||
|
AddStep("change state via bindable", () => state.Value = TernaryState.True);
|
||||||
|
|
||||||
|
void click() =>
|
||||||
|
AddStep("click", () =>
|
||||||
|
{
|
||||||
|
InputManager.MoveMouseTo(menu.ScreenSpaceDrawQuad.Centre);
|
||||||
|
InputManager.Click(MouseButton.Left);
|
||||||
|
});
|
||||||
|
|
||||||
|
void checkState(TernaryState expected)
|
||||||
|
=> AddAssert($"state is {expected}", () => state.Value == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestCustomState()
|
||||||
|
{
|
||||||
|
AddStep("create menu", () =>
|
||||||
|
{
|
||||||
|
Child = new OsuMenu(Direction.Vertical, true)
|
||||||
|
{
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
|
Items = new[]
|
||||||
|
{
|
||||||
|
new TestMenuItem("First", MenuItemType.Standard, getNextState),
|
||||||
|
new TestMenuItem("Second", MenuItemType.Standard, getNextState) { State = { Value = TestStates.State2 } },
|
||||||
|
new TestMenuItem("Third", MenuItemType.Standard, getNextState) { State = { Value = TestStates.State3 } },
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private TestStates getNextState(TestStates state)
|
||||||
|
{
|
||||||
|
switch (state)
|
||||||
|
{
|
||||||
|
case TestStates.State1:
|
||||||
|
return TestStates.State2;
|
||||||
|
|
||||||
|
case TestStates.State2:
|
||||||
|
return TestStates.State3;
|
||||||
|
|
||||||
|
case TestStates.State3:
|
||||||
|
return TestStates.State1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TestStates.State1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TestMenuItem : StatefulMenuItem<TestStates>
|
||||||
|
{
|
||||||
|
public TestMenuItem(string text, MenuItemType type, Func<TestStates, TestStates> changeStateFunc)
|
||||||
|
: base(text, changeStateFunc, type)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IconUsage? GetIconForState(TestStates state)
|
||||||
|
{
|
||||||
|
switch (state)
|
||||||
|
{
|
||||||
|
case TestStates.State1:
|
||||||
|
return FontAwesome.Solid.DiceOne;
|
||||||
|
|
||||||
|
case TestStates.State2:
|
||||||
|
return FontAwesome.Solid.DiceTwo;
|
||||||
|
|
||||||
|
case TestStates.State3:
|
||||||
|
return FontAwesome.Solid.DiceThree;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(state), state, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum TestStates
|
||||||
|
{
|
||||||
|
State1,
|
||||||
|
State2,
|
||||||
|
State3
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
// 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 TestSceneToggleMenuItem : OsuTestScene
|
||||||
|
{
|
||||||
|
public override IReadOnlyList<Type> RequiredTypes => new[]
|
||||||
|
{
|
||||||
|
typeof(OsuMenu),
|
||||||
|
typeof(ToggleMenuItem),
|
||||||
|
typeof(DrawableStatefulMenuItem)
|
||||||
|
};
|
||||||
|
|
||||||
|
public TestSceneToggleMenuItem()
|
||||||
|
{
|
||||||
|
Add(new OsuMenu(Direction.Vertical, true)
|
||||||
|
{
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
|
Items = new[]
|
||||||
|
{
|
||||||
|
new ToggleMenuItem("First"),
|
||||||
|
new ToggleMenuItem("Second") { State = { Value = true } }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
133
osu.Game/Graphics/UserInterface/DrawableOsuMenuItem.cs
Normal file
133
osu.Game/Graphics/UserInterface/DrawableOsuMenuItem.cs
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
// 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 osu.Framework.Allocation;
|
||||||
|
using osu.Framework.Audio;
|
||||||
|
using osu.Framework.Audio.Sample;
|
||||||
|
using osu.Framework.Graphics;
|
||||||
|
using osu.Framework.Graphics.Containers;
|
||||||
|
using osu.Framework.Graphics.Sprites;
|
||||||
|
using osu.Framework.Graphics.UserInterface;
|
||||||
|
using osu.Framework.Input.Events;
|
||||||
|
using osu.Game.Graphics.Sprites;
|
||||||
|
using osuTK.Graphics;
|
||||||
|
|
||||||
|
namespace osu.Game.Graphics.UserInterface
|
||||||
|
{
|
||||||
|
public class DrawableOsuMenuItem : Menu.DrawableMenuItem
|
||||||
|
{
|
||||||
|
public const int MARGIN_HORIZONTAL = 17;
|
||||||
|
public const int MARGIN_VERTICAL = 4;
|
||||||
|
private const int text_size = 17;
|
||||||
|
private const int transition_length = 80;
|
||||||
|
|
||||||
|
private SampleChannel sampleClick;
|
||||||
|
private SampleChannel sampleHover;
|
||||||
|
|
||||||
|
private TextContainer text;
|
||||||
|
|
||||||
|
public DrawableOsuMenuItem(MenuItem item)
|
||||||
|
: base(item)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[BackgroundDependencyLoader]
|
||||||
|
private void load(AudioManager audio)
|
||||||
|
{
|
||||||
|
sampleHover = audio.Samples.Get(@"UI/generic-hover");
|
||||||
|
sampleClick = audio.Samples.Get(@"UI/generic-select");
|
||||||
|
|
||||||
|
BackgroundColour = Color4.Transparent;
|
||||||
|
BackgroundColourHover = OsuColour.FromHex(@"172023");
|
||||||
|
|
||||||
|
updateTextColour();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateTextColour()
|
||||||
|
{
|
||||||
|
switch ((Item as OsuMenuItem)?.Type)
|
||||||
|
{
|
||||||
|
default:
|
||||||
|
case MenuItemType.Standard:
|
||||||
|
text.Colour = Color4.White;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MenuItemType.Destructive:
|
||||||
|
text.Colour = Color4.Red;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MenuItemType.Highlighted:
|
||||||
|
text.Colour = OsuColour.FromHex(@"ffcc22");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override bool OnHover(HoverEvent e)
|
||||||
|
{
|
||||||
|
sampleHover.Play();
|
||||||
|
text.BoldText.FadeIn(transition_length, Easing.OutQuint);
|
||||||
|
text.NormalText.FadeOut(transition_length, Easing.OutQuint);
|
||||||
|
return base.OnHover(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnHoverLost(HoverLostEvent e)
|
||||||
|
{
|
||||||
|
text.BoldText.FadeOut(transition_length, Easing.OutQuint);
|
||||||
|
text.NormalText.FadeIn(transition_length, Easing.OutQuint);
|
||||||
|
base.OnHoverLost(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override bool OnClick(ClickEvent e)
|
||||||
|
{
|
||||||
|
sampleClick.Play();
|
||||||
|
return base.OnClick(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected sealed override Drawable CreateContent() => text = CreateTextContainer();
|
||||||
|
protected virtual TextContainer CreateTextContainer() => new TextContainer();
|
||||||
|
|
||||||
|
protected class TextContainer : Container, IHasText
|
||||||
|
{
|
||||||
|
public string Text
|
||||||
|
{
|
||||||
|
get => NormalText.Text;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
NormalText.Text = value;
|
||||||
|
BoldText.Text = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly SpriteText NormalText;
|
||||||
|
public readonly SpriteText BoldText;
|
||||||
|
|
||||||
|
public TextContainer()
|
||||||
|
{
|
||||||
|
Anchor = Anchor.CentreLeft;
|
||||||
|
Origin = Anchor.CentreLeft;
|
||||||
|
|
||||||
|
AutoSizeAxes = Axes.Both;
|
||||||
|
|
||||||
|
Children = new Drawable[]
|
||||||
|
{
|
||||||
|
NormalText = new OsuSpriteText
|
||||||
|
{
|
||||||
|
Anchor = Anchor.CentreLeft,
|
||||||
|
Origin = Anchor.CentreLeft,
|
||||||
|
Font = OsuFont.GetFont(size: text_size),
|
||||||
|
Margin = new MarginPadding { Horizontal = MARGIN_HORIZONTAL, Vertical = MARGIN_VERTICAL },
|
||||||
|
},
|
||||||
|
BoldText = new OsuSpriteText
|
||||||
|
{
|
||||||
|
AlwaysPresent = true,
|
||||||
|
Alpha = 0,
|
||||||
|
Anchor = Anchor.CentreLeft,
|
||||||
|
Origin = Anchor.CentreLeft,
|
||||||
|
Font = OsuFont.GetFont(size: text_size, weight: FontWeight.Bold),
|
||||||
|
Margin = new MarginPadding { Horizontal = MARGIN_HORIZONTAL, Vertical = MARGIN_VERTICAL },
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
72
osu.Game/Graphics/UserInterface/DrawableStatefulMenuItem.cs
Normal file
72
osu.Game/Graphics/UserInterface/DrawableStatefulMenuItem.cs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
// 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 osu.Framework.Bindables;
|
||||||
|
using osu.Framework.Graphics;
|
||||||
|
using osu.Framework.Graphics.Sprites;
|
||||||
|
using osuTK;
|
||||||
|
|
||||||
|
namespace osu.Game.Graphics.UserInterface
|
||||||
|
{
|
||||||
|
public class DrawableStatefulMenuItem : DrawableOsuMenuItem
|
||||||
|
{
|
||||||
|
protected new StatefulMenuItem Item => (StatefulMenuItem)base.Item;
|
||||||
|
|
||||||
|
public DrawableStatefulMenuItem(StatefulMenuItem item)
|
||||||
|
: base(item)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override TextContainer CreateTextContainer() => new ToggleTextContainer(Item);
|
||||||
|
|
||||||
|
private class ToggleTextContainer : TextContainer
|
||||||
|
{
|
||||||
|
private readonly StatefulMenuItem menuItem;
|
||||||
|
private readonly Bindable<object> state;
|
||||||
|
private readonly SpriteIcon stateIcon;
|
||||||
|
|
||||||
|
public ToggleTextContainer(StatefulMenuItem menuItem)
|
||||||
|
{
|
||||||
|
this.menuItem = menuItem;
|
||||||
|
|
||||||
|
state = menuItem.State.GetBoundCopy();
|
||||||
|
|
||||||
|
Add(stateIcon = new SpriteIcon
|
||||||
|
{
|
||||||
|
Anchor = Anchor.CentreLeft,
|
||||||
|
Origin = Anchor.CentreLeft,
|
||||||
|
Size = new Vector2(10),
|
||||||
|
Margin = new MarginPadding { Horizontal = MARGIN_HORIZONTAL },
|
||||||
|
AlwaysPresent = true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void LoadComplete()
|
||||||
|
{
|
||||||
|
base.LoadComplete();
|
||||||
|
state.BindValueChanged(updateState, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Update()
|
||||||
|
{
|
||||||
|
base.Update();
|
||||||
|
|
||||||
|
// Todo: This is bad. This can maybe be done better with a refactor of DrawableOsuMenuItem.
|
||||||
|
stateIcon.X = BoldText.DrawWidth + 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateState(ValueChangedEvent<object> state)
|
||||||
|
{
|
||||||
|
var icon = menuItem.GetIconForState(state.NewValue);
|
||||||
|
|
||||||
|
if (icon == null)
|
||||||
|
stateIcon.Alpha = 0;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
stateIcon.Alpha = 1;
|
||||||
|
stateIcon.Icon = icon.Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,7 @@ using osu.Framework.Allocation;
|
|||||||
using osu.Framework.Extensions.Color4Extensions;
|
using osu.Framework.Extensions.Color4Extensions;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Effects;
|
using osu.Framework.Graphics.Effects;
|
||||||
|
using osu.Framework.Graphics.UserInterface;
|
||||||
|
|
||||||
namespace osu.Game.Graphics.UserInterface
|
namespace osu.Game.Graphics.UserInterface
|
||||||
{
|
{
|
||||||
@ -35,5 +36,7 @@ namespace osu.Game.Graphics.UserInterface
|
|||||||
|
|
||||||
protected override void AnimateOpen() => this.FadeIn(fade_duration, Easing.OutQuint);
|
protected override void AnimateOpen() => this.FadeIn(fade_duration, Easing.OutQuint);
|
||||||
protected override void AnimateClose() => this.FadeOut(fade_duration, Easing.OutQuint);
|
protected override void AnimateClose() => this.FadeOut(fade_duration, Easing.OutQuint);
|
||||||
|
|
||||||
|
protected override Menu CreateSubMenu() => new OsuContextMenu();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,12 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// 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.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
using osu.Framework.Allocation;
|
|
||||||
using osu.Framework.Audio;
|
|
||||||
using osu.Framework.Audio.Sample;
|
|
||||||
using osuTK.Graphics;
|
using osuTK.Graphics;
|
||||||
using osu.Framework.Extensions.Color4Extensions;
|
using osu.Framework.Extensions.Color4Extensions;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
using osu.Framework.Graphics.Sprites;
|
|
||||||
using osu.Framework.Graphics.UserInterface;
|
using osu.Framework.Graphics.UserInterface;
|
||||||
using osu.Framework.Input.Events;
|
|
||||||
using osu.Game.Graphics.Containers;
|
using osu.Game.Graphics.Containers;
|
||||||
using osu.Game.Graphics.Sprites;
|
|
||||||
using osuTK;
|
using osuTK;
|
||||||
|
|
||||||
namespace osu.Game.Graphics.UserInterface
|
namespace osu.Game.Graphics.UserInterface
|
||||||
@ -45,7 +39,16 @@ namespace osu.Game.Graphics.UserInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override DrawableMenuItem CreateDrawableMenuItem(MenuItem item) => new DrawableOsuMenuItem(item);
|
protected override DrawableMenuItem CreateDrawableMenuItem(MenuItem item)
|
||||||
|
{
|
||||||
|
switch (item)
|
||||||
|
{
|
||||||
|
case StatefulMenuItem stateful:
|
||||||
|
return new DrawableStatefulMenuItem(stateful);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new DrawableOsuMenuItem(item);
|
||||||
|
}
|
||||||
|
|
||||||
protected override ScrollContainer<Drawable> CreateScrollContainer(Direction direction) => new OsuScrollContainer(direction);
|
protected override ScrollContainer<Drawable> CreateScrollContainer(Direction direction) => new OsuScrollContainer(direction);
|
||||||
|
|
||||||
@ -53,122 +56,5 @@ namespace osu.Game.Graphics.UserInterface
|
|||||||
{
|
{
|
||||||
Anchor = Direction == Direction.Horizontal ? Anchor.BottomLeft : Anchor.TopRight
|
Anchor = Direction == Direction.Horizontal ? Anchor.BottomLeft : Anchor.TopRight
|
||||||
};
|
};
|
||||||
|
|
||||||
protected class DrawableOsuMenuItem : DrawableMenuItem
|
|
||||||
{
|
|
||||||
private const int margin_horizontal = 17;
|
|
||||||
private const int text_size = 17;
|
|
||||||
private const int transition_length = 80;
|
|
||||||
public const int MARGIN_VERTICAL = 4;
|
|
||||||
|
|
||||||
private SampleChannel sampleClick;
|
|
||||||
private SampleChannel sampleHover;
|
|
||||||
|
|
||||||
private TextContainer text;
|
|
||||||
|
|
||||||
public DrawableOsuMenuItem(MenuItem item)
|
|
||||||
: base(item)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
|
||||||
private void load(AudioManager audio)
|
|
||||||
{
|
|
||||||
sampleHover = audio.Samples.Get(@"UI/generic-hover");
|
|
||||||
sampleClick = audio.Samples.Get(@"UI/generic-select");
|
|
||||||
|
|
||||||
BackgroundColour = Color4.Transparent;
|
|
||||||
BackgroundColourHover = OsuColour.FromHex(@"172023");
|
|
||||||
|
|
||||||
updateTextColour();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateTextColour()
|
|
||||||
{
|
|
||||||
switch ((Item as OsuMenuItem)?.Type)
|
|
||||||
{
|
|
||||||
default:
|
|
||||||
case MenuItemType.Standard:
|
|
||||||
text.Colour = Color4.White;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MenuItemType.Destructive:
|
|
||||||
text.Colour = Color4.Red;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case MenuItemType.Highlighted:
|
|
||||||
text.Colour = OsuColour.FromHex(@"ffcc22");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override bool OnHover(HoverEvent e)
|
|
||||||
{
|
|
||||||
sampleHover.Play();
|
|
||||||
text.BoldText.FadeIn(transition_length, Easing.OutQuint);
|
|
||||||
text.NormalText.FadeOut(transition_length, Easing.OutQuint);
|
|
||||||
return base.OnHover(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void OnHoverLost(HoverLostEvent e)
|
|
||||||
{
|
|
||||||
text.BoldText.FadeOut(transition_length, Easing.OutQuint);
|
|
||||||
text.NormalText.FadeIn(transition_length, Easing.OutQuint);
|
|
||||||
base.OnHoverLost(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override bool OnClick(ClickEvent e)
|
|
||||||
{
|
|
||||||
sampleClick.Play();
|
|
||||||
return base.OnClick(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected sealed override Drawable CreateContent() => text = CreateTextContainer();
|
|
||||||
protected virtual TextContainer CreateTextContainer() => new TextContainer();
|
|
||||||
|
|
||||||
protected class TextContainer : Container, IHasText
|
|
||||||
{
|
|
||||||
public string Text
|
|
||||||
{
|
|
||||||
get => NormalText.Text;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
NormalText.Text = value;
|
|
||||||
BoldText.Text = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public readonly SpriteText NormalText;
|
|
||||||
public readonly SpriteText BoldText;
|
|
||||||
|
|
||||||
public TextContainer()
|
|
||||||
{
|
|
||||||
Anchor = Anchor.CentreLeft;
|
|
||||||
Origin = Anchor.CentreLeft;
|
|
||||||
|
|
||||||
AutoSizeAxes = Axes.Both;
|
|
||||||
|
|
||||||
Children = new Drawable[]
|
|
||||||
{
|
|
||||||
NormalText = new OsuSpriteText
|
|
||||||
{
|
|
||||||
Anchor = Anchor.CentreLeft,
|
|
||||||
Origin = Anchor.CentreLeft,
|
|
||||||
Font = OsuFont.GetFont(size: text_size),
|
|
||||||
Margin = new MarginPadding { Horizontal = margin_horizontal, Vertical = MARGIN_VERTICAL },
|
|
||||||
},
|
|
||||||
BoldText = new OsuSpriteText
|
|
||||||
{
|
|
||||||
AlwaysPresent = true,
|
|
||||||
Alpha = 0,
|
|
||||||
Anchor = Anchor.CentreLeft,
|
|
||||||
Origin = Anchor.CentreLeft,
|
|
||||||
Font = OsuFont.GetFont(size: text_size, weight: FontWeight.Bold),
|
|
||||||
Margin = new MarginPadding { Horizontal = margin_horizontal, Vertical = MARGIN_VERTICAL },
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,8 @@ namespace osu.Game.Graphics.UserInterface
|
|||||||
public readonly MenuItemType Type;
|
public readonly MenuItemType Type;
|
||||||
|
|
||||||
public OsuMenuItem(string text, MenuItemType type = MenuItemType.Standard)
|
public OsuMenuItem(string text, MenuItemType type = MenuItemType.Standard)
|
||||||
: base(text)
|
: this(text, type, null)
|
||||||
{
|
{
|
||||||
Type = type;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public OsuMenuItem(string text, MenuItemType type, Action action)
|
public OsuMenuItem(string text, MenuItemType type, Action action)
|
||||||
|
105
osu.Game/Graphics/UserInterface/StatefulMenuItem.cs
Normal file
105
osu.Game/Graphics/UserInterface/StatefulMenuItem.cs
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
// 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 osu.Framework.Bindables;
|
||||||
|
using osu.Framework.Graphics.Sprites;
|
||||||
|
|
||||||
|
namespace osu.Game.Graphics.UserInterface
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// An <see cref="OsuMenuItem"/> which contains and displays a state.
|
||||||
|
/// </summary>
|
||||||
|
public abstract class StatefulMenuItem : OsuMenuItem
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The current state that should be displayed.
|
||||||
|
/// </summary>
|
||||||
|
public readonly Bindable<object> State = new Bindable<object>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="StatefulMenuItem"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The text to display.</param>
|
||||||
|
/// <param name="changeStateFunc">A function that mutates a state to another state after this <see cref="StatefulMenuItem"/> is pressed.</param>
|
||||||
|
/// <param name="type">The type of action which this <see cref="StatefulMenuItem"/> performs.</param>
|
||||||
|
protected StatefulMenuItem(string text, Func<object, object> changeStateFunc, MenuItemType type = MenuItemType.Standard)
|
||||||
|
: this(text, changeStateFunc, type, null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="StatefulMenuItem"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The text to display.</param>
|
||||||
|
/// <param name="changeStateFunc">A function that mutates a state to another state after this <see cref="StatefulMenuItem"/> is pressed.</param>
|
||||||
|
/// <param name="type">The type of action which this <see cref="StatefulMenuItem"/> performs.</param>
|
||||||
|
/// <param name="action">A delegate to be invoked when this <see cref="StatefulMenuItem"/> is pressed.</param>
|
||||||
|
protected StatefulMenuItem(string text, Func<object, object> changeStateFunc, MenuItemType type, Action<object> action)
|
||||||
|
: base(text, type)
|
||||||
|
{
|
||||||
|
Action.Value = () =>
|
||||||
|
{
|
||||||
|
State.Value = changeStateFunc?.Invoke(State.Value) ?? State.Value;
|
||||||
|
action?.Invoke(State.Value);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves the icon to be displayed for a state.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="state">The state to retrieve the relevant icon for.</param>
|
||||||
|
/// <returns>The icon to be displayed for <paramref name="state"/>.</returns>
|
||||||
|
public abstract IconUsage? GetIconForState(object state);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract class StatefulMenuItem<T> : StatefulMenuItem
|
||||||
|
where T : struct
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The current state that should be displayed.
|
||||||
|
/// </summary>
|
||||||
|
public new readonly Bindable<T> State = new Bindable<T>();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="StatefulMenuItem"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The text to display.</param>
|
||||||
|
/// <param name="changeStateFunc">A function that mutates a state to another state after this <see cref="StatefulMenuItem"/> is pressed.</param>
|
||||||
|
/// <param name="type">The type of action which this <see cref="StatefulMenuItem"/> performs.</param>
|
||||||
|
protected StatefulMenuItem(string text, Func<T, T> changeStateFunc, MenuItemType type = MenuItemType.Standard)
|
||||||
|
: this(text, changeStateFunc, type, null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="StatefulMenuItem"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The text to display.</param>
|
||||||
|
/// <param name="changeStateFunc">A function that mutates a state to another state after this <see cref="StatefulMenuItem"/> is pressed.</param>
|
||||||
|
/// <param name="type">The type of action which this <see cref="StatefulMenuItem"/> performs.</param>
|
||||||
|
/// <param name="action">A delegate to be invoked when this <see cref="StatefulMenuItem"/> is pressed.</param>
|
||||||
|
protected StatefulMenuItem(string text, Func<T, T> changeStateFunc, MenuItemType type, Action<T> action)
|
||||||
|
: base(text, o => changeStateFunc?.Invoke((T)o) ?? o, type, o => action?.Invoke((T)o))
|
||||||
|
{
|
||||||
|
base.State.BindValueChanged(state =>
|
||||||
|
{
|
||||||
|
if (state.NewValue == null)
|
||||||
|
base.State.Value = default(T);
|
||||||
|
|
||||||
|
State.Value = (T)base.State.Value;
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
State.BindValueChanged(state => base.State.Value = state.NewValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed override IconUsage? GetIconForState(object state) => GetIconForState((T)state);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves the icon to be displayed for a state.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="state">The state to retrieve the relevant icon for.</param>
|
||||||
|
/// <returns>The icon to be displayed for <paramref name="state"/>.</returns>
|
||||||
|
public abstract IconUsage? GetIconForState(T state);
|
||||||
|
}
|
||||||
|
}
|
27
osu.Game/Graphics/UserInterface/TernaryState.cs
Normal file
27
osu.Game/Graphics/UserInterface/TernaryState.cs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// 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.Graphics.UserInterface
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// An on/off state with an extra indeterminate state.
|
||||||
|
/// </summary>
|
||||||
|
public enum TernaryState
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The current state is false.
|
||||||
|
/// </summary>
|
||||||
|
False,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The current state is a combination of <see cref="False"/> and <see cref="True"/>.
|
||||||
|
/// The state becomes <see cref="True"/> if the <see cref="TernaryStateMenuItem"/> is pressed.
|
||||||
|
/// </summary>
|
||||||
|
Indeterminate,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The current state is true.
|
||||||
|
/// </summary>
|
||||||
|
True
|
||||||
|
}
|
||||||
|
}
|
79
osu.Game/Graphics/UserInterface/TernaryStateMenuItem.cs
Normal file
79
osu.Game/Graphics/UserInterface/TernaryStateMenuItem.cs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
// 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 osu.Framework.Graphics.Sprites;
|
||||||
|
|
||||||
|
namespace osu.Game.Graphics.UserInterface
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// An <see cref="OsuMenuItem"/> with three possible states.
|
||||||
|
/// </summary>
|
||||||
|
public class TernaryStateMenuItem : StatefulMenuItem<TernaryState>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="TernaryStateMenuItem"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The text to display.</param>
|
||||||
|
/// <param name="type">The type of action which this <see cref="TernaryStateMenuItem"/> performs.</param>
|
||||||
|
public TernaryStateMenuItem(string text, MenuItemType type = MenuItemType.Standard)
|
||||||
|
: this(text, type, null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="TernaryStateMenuItem"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The text to display.</param>
|
||||||
|
/// <param name="type">The type of action which this <see cref="TernaryStateMenuItem"/> performs.</param>
|
||||||
|
/// <param name="action">A delegate to be invoked when this <see cref="TernaryStateMenuItem"/> is pressed.</param>
|
||||||
|
public TernaryStateMenuItem(string text, MenuItemType type, Action<TernaryState> action)
|
||||||
|
: this(text, getNextState, type, action)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="TernaryStateMenuItem"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The text to display.</param>
|
||||||
|
/// <param name="changeStateFunc">A function that mutates a state to another state after this <see cref="TernaryStateMenuItem"/> is pressed.</param>
|
||||||
|
/// <param name="type">The type of action which this <see cref="TernaryStateMenuItem"/> performs.</param>
|
||||||
|
/// <param name="action">A delegate to be invoked when this <see cref="TernaryStateMenuItem"/> is pressed.</param>
|
||||||
|
protected TernaryStateMenuItem(string text, Func<TernaryState, TernaryState> changeStateFunc, MenuItemType type, Action<TernaryState> action)
|
||||||
|
: base(text, changeStateFunc, type, action)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IconUsage? GetIconForState(TernaryState state)
|
||||||
|
{
|
||||||
|
switch (state)
|
||||||
|
{
|
||||||
|
case TernaryState.Indeterminate:
|
||||||
|
return FontAwesome.Solid.DotCircle;
|
||||||
|
|
||||||
|
case TernaryState.True:
|
||||||
|
return FontAwesome.Solid.Check;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static TernaryState getNextState(TernaryState state)
|
||||||
|
{
|
||||||
|
switch (state)
|
||||||
|
{
|
||||||
|
case TernaryState.False:
|
||||||
|
return TernaryState.True;
|
||||||
|
|
||||||
|
case TernaryState.Indeterminate:
|
||||||
|
return TernaryState.True;
|
||||||
|
|
||||||
|
case TernaryState.True:
|
||||||
|
return TernaryState.False;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(state), state, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
37
osu.Game/Graphics/UserInterface/ToggleMenuItem.cs
Normal file
37
osu.Game/Graphics/UserInterface/ToggleMenuItem.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// 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 osu.Framework.Graphics.Sprites;
|
||||||
|
|
||||||
|
namespace osu.Game.Graphics.UserInterface
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// An <see cref="OsuMenuItem"/> which displays an enabled or disabled state.
|
||||||
|
/// </summary>
|
||||||
|
public class ToggleMenuItem : StatefulMenuItem<bool>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="ToggleMenuItem"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The text to display.</param>
|
||||||
|
/// <param name="type">The type of action which this <see cref="ToggleMenuItem"/> performs.</param>
|
||||||
|
public ToggleMenuItem(string text, MenuItemType type = MenuItemType.Standard)
|
||||||
|
: this(text, type, null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="ToggleMenuItem"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="text">The text to display.</param>
|
||||||
|
/// <param name="type">The type of action which this <see cref="ToggleMenuItem"/> performs.</param>
|
||||||
|
/// <param name="action">A delegate to be invoked when this <see cref="ToggleMenuItem"/> is pressed.</param>
|
||||||
|
public ToggleMenuItem(string text, MenuItemType type, Action<bool> action)
|
||||||
|
: base(text, value => !value, type, action)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IconUsage? GetIconForState(bool state) => state ? (IconUsage?)FontAwesome.Solid.Check : null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user