2023-06-23 00:37:25 +08:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-11-07 21:26:35 +08:00
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
|
|
using osu.Framework.Bindables;
|
2024-05-04 22:00:22 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2024-05-04 22:32:18 +08:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2024-05-04 22:01:31 +08:00
|
|
|
|
using osu.Framework.Input.Events;
|
2019-11-07 21:26:35 +08:00
|
|
|
|
using osuTK;
|
2024-05-04 22:32:18 +08:00
|
|
|
|
using osuTK.Input;
|
2019-11-07 21:26:35 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
|
public partial class DrawableStatefulMenuItem : DrawableOsuMenuItem
|
2019-11-07 21:26:35 +08:00
|
|
|
|
{
|
2019-11-08 10:15:03 +08:00
|
|
|
|
protected new StatefulMenuItem Item => (StatefulMenuItem)base.Item;
|
2019-11-07 21:26:35 +08:00
|
|
|
|
|
2019-11-08 10:15:03 +08:00
|
|
|
|
public DrawableStatefulMenuItem(StatefulMenuItem item)
|
2019-11-07 21:26:35 +08:00
|
|
|
|
: base(item)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 10:15:03 +08:00
|
|
|
|
protected override TextContainer CreateTextContainer() => new ToggleTextContainer(Item);
|
2019-11-07 21:26:35 +08:00
|
|
|
|
|
2024-05-04 22:01:31 +08:00
|
|
|
|
protected override bool OnMouseDown(MouseDownEvent e)
|
2024-05-03 22:57:31 +08:00
|
|
|
|
{
|
2024-05-05 23:32:24 +08:00
|
|
|
|
// Right mouse button is a special case where we allow actioning without dismissing the menu.
|
|
|
|
|
// This is achieved by not calling `Clicked` (as done by the base implementation in OnClick).
|
|
|
|
|
if (IsActionable && e.Button == MouseButton.Right)
|
2024-06-01 13:29:23 +08:00
|
|
|
|
{
|
2024-05-05 23:32:24 +08:00
|
|
|
|
Item.Action.Value?.Invoke();
|
2024-06-01 13:29:23 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-05-03 22:57:31 +08:00
|
|
|
|
|
2024-06-01 13:29:23 +08:00
|
|
|
|
return false;
|
2024-05-03 22:57:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 13:32:20 +08:00
|
|
|
|
private partial class ToggleTextContainer : TextContainer
|
2019-11-07 21:26:35 +08:00
|
|
|
|
{
|
2019-11-08 10:15:03 +08:00
|
|
|
|
private readonly StatefulMenuItem menuItem;
|
|
|
|
|
private readonly Bindable<object> state;
|
|
|
|
|
private readonly SpriteIcon stateIcon;
|
2019-11-07 21:26:35 +08:00
|
|
|
|
|
2019-11-08 10:15:03 +08:00
|
|
|
|
public ToggleTextContainer(StatefulMenuItem menuItem)
|
2019-11-07 21:26:35 +08:00
|
|
|
|
{
|
2019-11-08 10:15:03 +08:00
|
|
|
|
this.menuItem = menuItem;
|
|
|
|
|
|
|
|
|
|
state = menuItem.State.GetBoundCopy();
|
|
|
|
|
|
|
|
|
|
Add(stateIcon = new SpriteIcon
|
2019-11-07 21:26:35 +08: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 10:15:03 +08:00
|
|
|
|
state.BindValueChanged(updateState, true);
|
2019-11-07 21:26:35 +08: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 10:15:03 +08: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 21:26:35 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|