2019-11-07 13:26:35 +00:00
|
|
|
// 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
|
|
|
|
{
|
2019-11-08 02:15:03 +00:00
|
|
|
public class DrawableStatefulMenuItem : DrawableOsuMenuItem
|
2019-11-07 13:26:35 +00:00
|
|
|
{
|
2019-11-08 02:15:03 +00:00
|
|
|
protected new StatefulMenuItem Item => (StatefulMenuItem)base.Item;
|
2019-11-07 13:26:35 +00:00
|
|
|
|
2019-11-08 02:15:03 +00:00
|
|
|
public DrawableStatefulMenuItem(StatefulMenuItem item)
|
2019-11-07 13:26:35 +00:00
|
|
|
: base(item)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-11-08 02:15:03 +00:00
|
|
|
protected override TextContainer CreateTextContainer() => new ToggleTextContainer(Item);
|
2019-11-07 13:26:35 +00:00
|
|
|
|
|
|
|
private class ToggleTextContainer : TextContainer
|
|
|
|
{
|
2019-11-08 02:15:03 +00:00
|
|
|
private readonly StatefulMenuItem menuItem;
|
|
|
|
private readonly Bindable<object> state;
|
|
|
|
private readonly SpriteIcon stateIcon;
|
2019-11-07 13:26:35 +00:00
|
|
|
|
2019-11-08 02:15:03 +00:00
|
|
|
public ToggleTextContainer(StatefulMenuItem menuItem)
|
2019-11-07 13:26:35 +00:00
|
|
|
{
|
2019-11-08 02:15:03 +00:00
|
|
|
this.menuItem = menuItem;
|
|
|
|
|
|
|
|
state = menuItem.State.GetBoundCopy();
|
|
|
|
|
|
|
|
Add(stateIcon = new SpriteIcon
|
2019-11-07 13:26:35 +00:00
|
|
|
{
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
Size = new Vector2(10),
|
|
|
|
Margin = new MarginPadding { Horizontal = MARGIN_HORIZONTAL },
|
|
|
|
AlwaysPresent = true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
2019-11-08 02:15:03 +00:00
|
|
|
state.BindValueChanged(updateState, true);
|
2019-11-07 13:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
// Todo: This is bad. This can maybe be done better with a refactor of DrawableOsuMenuItem.
|
2019-11-08 02:15:03 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-11-07 13:26:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|