2019-11-07 15:03:35 +08: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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2024-07-18 17:20:22 +08:00
|
|
|
using System.Collections.Generic;
|
2024-04-24 15:19:10 +08:00
|
|
|
using System.Linq;
|
2019-11-07 15:03:35 +08:00
|
|
|
using osu.Framework.Allocation;
|
2024-10-03 21:32:25 +08:00
|
|
|
using osu.Framework.Bindables;
|
2020-03-11 09:18:41 +08:00
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2019-11-07 15:03:35 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
|
|
|
using osu.Framework.Input.Events;
|
2021-02-22 16:14:00 +08:00
|
|
|
using osu.Framework.Localisation;
|
2019-11-07 15:03:35 +08:00
|
|
|
using osu.Game.Graphics.Sprites;
|
2024-04-24 15:19:10 +08:00
|
|
|
using osuTK;
|
2019-11-07 15:03:35 +08:00
|
|
|
using osuTK.Graphics;
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class DrawableOsuMenuItem : Menu.DrawableMenuItem
|
2019-11-07 15:03:35 +08:00
|
|
|
{
|
2024-07-18 17:20:22 +08:00
|
|
|
public const int MARGIN_HORIZONTAL = 10;
|
2019-11-07 21:26:35 +08:00
|
|
|
public const int MARGIN_VERTICAL = 4;
|
2024-09-04 17:45:29 +08:00
|
|
|
public const int TEXT_SIZE = 17;
|
|
|
|
public const int TRANSITION_LENGTH = 80;
|
2019-11-07 15:03:35 +08:00
|
|
|
|
2024-10-03 21:32:25 +08:00
|
|
|
public BindableBool ShowCheckbox { get; } = new BindableBool();
|
|
|
|
|
2024-09-04 17:45:29 +08:00
|
|
|
private TextContainer text;
|
2024-07-18 17:20:22 +08:00
|
|
|
private HotkeyDisplay hotkey;
|
2022-11-03 16:44:54 +08:00
|
|
|
private HoverClickSounds hoverClickSounds;
|
2019-11-07 15:03:35 +08:00
|
|
|
|
|
|
|
public DrawableOsuMenuItem(MenuItem item)
|
|
|
|
: base(item)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2022-01-15 08:06:39 +08:00
|
|
|
private void load()
|
2019-11-07 15:03:35 +08:00
|
|
|
{
|
|
|
|
BackgroundColour = Color4.Transparent;
|
2020-03-11 09:18:41 +08:00
|
|
|
BackgroundColourHover = Color4Extensions.FromHex(@"172023");
|
2019-11-07 15:03:35 +08:00
|
|
|
|
2024-07-18 17:20:22 +08:00
|
|
|
AddInternal(hotkey = new HotkeyDisplay
|
|
|
|
{
|
|
|
|
Alpha = 0,
|
|
|
|
Anchor = Anchor.CentreRight,
|
|
|
|
Origin = Anchor.CentreRight,
|
|
|
|
Margin = new MarginPadding { Right = 10, Top = 1 },
|
|
|
|
});
|
2022-11-03 16:44:54 +08:00
|
|
|
AddInternal(hoverClickSounds = new HoverClickSounds());
|
2021-06-11 22:46:42 +08:00
|
|
|
|
2024-07-18 17:20:22 +08:00
|
|
|
updateText();
|
2024-04-24 15:19:10 +08:00
|
|
|
|
2024-07-18 17:20:22 +08:00
|
|
|
if (showChevron)
|
2024-04-24 15:19:10 +08:00
|
|
|
{
|
|
|
|
AddInternal(new SpriteIcon
|
|
|
|
{
|
2024-07-18 17:20:22 +08:00
|
|
|
Margin = new MarginPadding { Horizontal = 10, },
|
2024-04-24 15:19:10 +08:00
|
|
|
Size = new Vector2(8),
|
|
|
|
Icon = FontAwesome.Solid.ChevronRight,
|
|
|
|
Anchor = Anchor.CentreRight,
|
|
|
|
Origin = Anchor.CentreRight,
|
|
|
|
});
|
|
|
|
}
|
2023-08-21 18:05:20 +08:00
|
|
|
}
|
|
|
|
|
2024-07-18 17:20:22 +08:00
|
|
|
// Only add right chevron if direction of menu items is vertical (i.e. width is relative size, see `DrawableMenuItem.SetFlowDirection()`).
|
|
|
|
private bool showChevron => Item.Items.Any() && RelativeSizeAxes == Axes.X;
|
|
|
|
|
2023-08-21 18:05:20 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
2020-04-15 14:50:19 +08:00
|
|
|
|
2024-10-03 21:32:25 +08:00
|
|
|
ShowCheckbox.BindValueChanged(_ => updateState());
|
2020-04-15 14:50:19 +08:00
|
|
|
Item.Action.BindDisabledChanged(_ => updateState(), true);
|
2023-08-21 18:05:20 +08:00
|
|
|
FinishTransforms();
|
2019-11-07 15:03:35 +08:00
|
|
|
}
|
|
|
|
|
2024-07-18 17:20:22 +08:00
|
|
|
private void updateText()
|
2019-11-07 15:03:35 +08:00
|
|
|
{
|
2024-07-18 17:20:22 +08:00
|
|
|
var osuMenuItem = Item as OsuMenuItem;
|
|
|
|
|
|
|
|
switch (osuMenuItem?.Type)
|
2019-11-07 15:03:35 +08:00
|
|
|
{
|
|
|
|
default:
|
|
|
|
case MenuItemType.Standard:
|
2024-09-04 17:45:29 +08:00
|
|
|
text.Colour = Color4.White;
|
2019-11-07 15:03:35 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MenuItemType.Destructive:
|
2024-09-04 17:45:29 +08:00
|
|
|
text.Colour = Color4.Red;
|
2019-11-07 15:03:35 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MenuItemType.Highlighted:
|
2024-09-04 17:45:29 +08:00
|
|
|
text.Colour = Color4Extensions.FromHex(@"ffcc22");
|
2019-11-07 15:03:35 +08:00
|
|
|
break;
|
|
|
|
}
|
2024-07-18 17:20:22 +08:00
|
|
|
|
|
|
|
hotkey.Hotkey = osuMenuItem?.Hotkey ?? default;
|
|
|
|
hotkey.Alpha = EqualityComparer<Hotkey>.Default.Equals(hotkey.Hotkey, default) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void UpdateAfterChildren()
|
|
|
|
{
|
|
|
|
base.UpdateAfterChildren();
|
|
|
|
|
|
|
|
// this hack ensures that the menu can auto-size while leaving enough space for the hotkey display.
|
|
|
|
// the gist of it is that while the hotkey display is not in the text / "content" that determines sizing
|
|
|
|
// (because it cannot be, because we want the hotkey display to align to the *right* and not the left),
|
|
|
|
// enough padding to fit the hotkey with _its_ spacing is added as padding of the text to compensate.
|
2024-09-04 17:45:29 +08:00
|
|
|
text.Padding = new MarginPadding { Right = hotkey.Alpha > 0 || showChevron ? hotkey.DrawWidth + 15 : 0 };
|
2019-11-07 15:03:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
|
|
{
|
2020-04-15 14:50:19 +08:00
|
|
|
updateState();
|
2019-11-07 15:03:35 +08:00
|
|
|
return base.OnHover(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
|
|
|
{
|
2020-04-15 14:50:19 +08:00
|
|
|
updateState();
|
2019-11-07 15:03:35 +08:00
|
|
|
base.OnHoverLost(e);
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:50:19 +08:00
|
|
|
private void updateState()
|
|
|
|
{
|
2023-05-21 00:02:12 +08:00
|
|
|
hoverClickSounds.Enabled.Value = IsActionable;
|
|
|
|
Alpha = IsActionable ? 1 : 0.2f;
|
2020-04-15 14:50:19 +08:00
|
|
|
|
2023-05-21 00:02:12 +08:00
|
|
|
if (IsHovered && IsActionable)
|
2020-04-15 14:50:19 +08:00
|
|
|
{
|
2024-09-04 17:45:29 +08:00
|
|
|
text.BoldText.FadeIn(TRANSITION_LENGTH, Easing.OutQuint);
|
|
|
|
text.NormalText.FadeOut(TRANSITION_LENGTH, Easing.OutQuint);
|
2020-04-15 14:50:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-09-04 17:45:29 +08:00
|
|
|
text.BoldText.FadeOut(TRANSITION_LENGTH, Easing.OutQuint);
|
|
|
|
text.NormalText.FadeIn(TRANSITION_LENGTH, Easing.OutQuint);
|
2020-04-15 14:50:19 +08:00
|
|
|
}
|
2024-10-03 21:32:25 +08:00
|
|
|
|
|
|
|
text.CheckboxContainer.Alpha = ShowCheckbox.Value ? 1 : 0;
|
2020-04-15 14:50:19 +08:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:45:29 +08:00
|
|
|
protected sealed override Drawable CreateContent() => text = CreateTextContainer();
|
2019-11-07 15:03:35 +08:00
|
|
|
protected virtual TextContainer CreateTextContainer() => new TextContainer();
|
|
|
|
|
2024-07-18 17:20:22 +08:00
|
|
|
protected partial class TextContainer : Container, IHasText
|
2019-11-07 15:03:35 +08:00
|
|
|
{
|
2021-02-22 16:14:00 +08:00
|
|
|
public LocalisableString Text
|
2019-11-07 15:03:35 +08:00
|
|
|
{
|
|
|
|
get => NormalText.Text;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
NormalText.Text = value;
|
|
|
|
BoldText.Text = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public readonly SpriteText NormalText;
|
|
|
|
public readonly SpriteText BoldText;
|
2024-07-18 17:20:22 +08:00
|
|
|
public readonly Container CheckboxContainer;
|
2019-11-07 15:03:35 +08:00
|
|
|
|
|
|
|
public TextContainer()
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
2024-07-18 17:20:22 +08:00
|
|
|
Child = new FillFlowContainer
|
2019-11-07 15:03:35 +08:00
|
|
|
{
|
2024-07-18 15:46:06 +08:00
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
Origin = Anchor.CentreLeft,
|
2024-07-18 17:20:22 +08:00
|
|
|
|
2024-07-18 15:46:06 +08:00
|
|
|
AutoSizeAxes = Axes.Both,
|
2024-07-18 17:20:22 +08:00
|
|
|
Spacing = new Vector2(10),
|
|
|
|
Direction = FillDirection.Horizontal,
|
|
|
|
Padding = new MarginPadding { Horizontal = MARGIN_HORIZONTAL, Vertical = MARGIN_VERTICAL, },
|
|
|
|
|
2024-07-18 15:46:06 +08:00
|
|
|
Children = new Drawable[]
|
2019-11-07 15:03:35 +08:00
|
|
|
{
|
2024-07-18 17:20:22 +08:00
|
|
|
CheckboxContainer = new Container
|
2024-07-18 15:46:06 +08:00
|
|
|
{
|
2024-07-18 17:20:22 +08:00
|
|
|
RelativeSizeAxes = Axes.Y,
|
|
|
|
Width = MARGIN_HORIZONTAL,
|
2024-07-18 15:46:06 +08:00
|
|
|
},
|
2024-07-18 17:20:22 +08:00
|
|
|
new Container
|
2024-07-18 15:46:06 +08:00
|
|
|
{
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
Origin = Anchor.CentreLeft,
|
2024-07-18 17:20:22 +08:00
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
NormalText = new OsuSpriteText
|
|
|
|
{
|
|
|
|
AlwaysPresent = true, // ensures that the menu item does not change width when switching between normal and bold text.
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
Origin = Anchor.CentreLeft,
|
2024-09-04 17:45:29 +08:00
|
|
|
Font = OsuFont.GetFont(size: TEXT_SIZE),
|
2024-07-18 17:20:22 +08:00
|
|
|
},
|
|
|
|
BoldText = new OsuSpriteText
|
|
|
|
{
|
|
|
|
AlwaysPresent = true, // ensures that the menu item does not change width when switching between normal and bold text.
|
|
|
|
Alpha = 0,
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
Origin = Anchor.CentreLeft,
|
2024-09-04 17:45:29 +08:00
|
|
|
Font = OsuFont.GetFont(size: TEXT_SIZE, weight: FontWeight.Bold),
|
2024-07-18 17:20:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-11-07 15:03:35 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|