1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 02:17:46 +08:00
osu-lazer/osu.Game/Overlays/Toolbar/ToolbarButton.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

262 lines
9.2 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.Backgrounds;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Input.Bindings;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
2016-12-01 13:22:29 +08:00
namespace osu.Game.Overlays.Toolbar
{
public abstract partial class ToolbarButton : OsuClickableContainer, IKeyBindingHandler<GlobalAction>
{
2024-01-15 04:13:00 +08:00
public const float PADDING = 3;
protected GlobalAction? Hotkey { get; set; }
public void SetIcon(Drawable icon)
{
IconContainer.Icon = icon;
IconContainer.Show();
}
2018-04-13 17:19:50 +08:00
[Resolved]
2021-11-08 17:24:37 +08:00
private ReadableKeyCombinationProvider keyCombinationProvider { get; set; } = null!;
public void SetIcon(IconUsage icon) =>
SetIcon(new SpriteIcon
2020-09-03 15:28:53 +08:00
{
Icon = icon,
2020-09-03 15:28:53 +08:00
});
public LocalisableString Text
{
get => DrawableText.Text;
set => DrawableText.Text = value;
}
2018-04-13 17:19:50 +08:00
public LocalisableString TooltipMain
{
get => tooltip1.Text;
set => tooltip1.Text = value;
}
2018-04-13 17:19:50 +08:00
public LocalisableString TooltipSub
{
get => tooltip2.Text;
set => tooltip2.Text = value;
}
2018-04-13 17:19:50 +08:00
2017-02-10 15:26:43 +08:00
protected virtual Anchor TooltipAnchor => Anchor.TopLeft;
2018-04-13 17:19:50 +08:00
protected readonly Container ButtonContent;
protected ConstrainedIconContainer IconContainer;
protected SpriteText DrawableText;
protected Box HoverBackground;
2020-06-15 13:16:17 +08:00
private readonly Box flashBackground;
private readonly FillFlowContainer tooltipContainer;
private readonly SpriteText tooltip1;
private readonly SpriteText tooltip2;
private readonly SpriteText keyBindingTooltip;
2017-03-02 02:33:01 +08:00
protected FillFlowContainer Flow;
2018-04-13 17:19:50 +08:00
protected readonly Container BackgroundContent;
[Resolved]
private RealmAccess realm { get; set; } = null!;
2020-08-06 16:06:51 +08:00
protected ToolbarButton()
{
AutoSizeAxes = Axes.X;
RelativeSizeAxes = Axes.Y;
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
ButtonContent = new Container
{
Width = Toolbar.HEIGHT,
RelativeSizeAxes = Axes.Y,
2024-01-15 04:13:00 +08:00
Padding = new MarginPadding(PADDING),
Children = new Drawable[]
{
BackgroundContent = new Container
{
RelativeSizeAxes = Axes.Both,
Masking = true,
CornerRadius = 6,
CornerExponent = 3f,
Children = new Drawable[]
{
HoverBackground = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = OsuColour.Gray(80).Opacity(180),
Blending = BlendingParameters.Additive,
Alpha = 0,
},
flashBackground = new Box
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
Colour = Color4.White.Opacity(100),
Blending = BlendingParameters.Additive,
},
}
},
Flow = new FillFlowContainer
{
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5),
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Padding = new MarginPadding { Left = Toolbar.HEIGHT / 2, Right = Toolbar.HEIGHT / 2 },
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X,
Children = new Drawable[]
{
IconContainer = new ConstrainedIconContainer
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Size = new Vector2(20),
Alpha = 0,
},
DrawableText = new OsuSpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
},
},
},
},
2017-03-02 02:33:01 +08:00
tooltipContainer = new FillFlowContainer
{
2017-03-04 18:00:17 +08:00
Direction = FillDirection.Vertical,
2020-05-05 09:31:11 +08:00
RelativeSizeAxes = Axes.Both, // stops us being considered in parent's autosize
2024-07-02 23:19:04 +08:00
Anchor = TooltipAnchor.HasFlag(Anchor.x0) ? Anchor.BottomLeft : Anchor.BottomRight,
2017-02-10 15:26:43 +08:00
Origin = TooltipAnchor,
2024-07-02 23:19:04 +08:00
Position = new Vector2(TooltipAnchor.HasFlag(Anchor.x0) ? 5 : -5, 5),
Alpha = 0,
Children = new Drawable[]
{
tooltip1 = new OsuSpriteText
{
2017-02-10 15:26:43 +08:00
Anchor = TooltipAnchor,
Origin = TooltipAnchor,
2016-11-23 10:35:31 +08:00
Shadow = true,
Font = OsuFont.GetFont(size: 22, weight: FontWeight.Bold),
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
2017-02-10 15:26:43 +08:00
Anchor = TooltipAnchor,
Origin = TooltipAnchor,
Direction = FillDirection.Horizontal,
Children = new[]
{
tooltip2 = new OsuSpriteText { Shadow = true },
keyBindingTooltip = new OsuSpriteText { Shadow = true }
}
}
}
}
};
}
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load()
{
if (Hotkey != null)
{
realm.SubscribeToPropertyChanged(r => r.All<RealmKeyBinding>().FirstOrDefault(rkb => rkb.RulesetName == null && rkb.ActionInt == (int)Hotkey.Value), kb => kb.KeyCombinationString, updateKeyBindingTooltip);
}
}
protected override bool OnMouseDown(MouseDownEvent e) => false;
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnClick(ClickEvent e)
{
flashBackground.FadeIn(50).Then().FadeOutFromOne(800, Easing.OutQuint);
tooltipContainer.FadeOut(100);
2018-10-02 11:02:47 +08:00
return base.OnClick(e);
}
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
{
HoverBackground.FadeIn(200);
tooltipContainer.FadeIn(100);
2021-06-22 17:26:42 +08:00
return true;
}
2018-04-13 17:19:50 +08:00
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
{
HoverBackground.FadeOut(200);
tooltipContainer.FadeOut(100);
}
2021-09-16 17:26:12 +08:00
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
2022-04-03 21:24:00 +08:00
if (e.Action == Hotkey && !e.Repeat)
{
2021-08-04 16:27:44 +08:00
TriggerClick();
return true;
}
return false;
}
2021-09-16 17:26:12 +08:00
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
private void updateKeyBindingTooltip(string keyCombination)
{
string keyBindingString = keyCombinationProvider.GetReadableString(keyCombination);
2021-06-22 17:26:42 +08:00
keyBindingTooltip.Text = !string.IsNullOrEmpty(keyBindingString)
? $" ({keyBindingString})"
: string.Empty;
}
}
2018-04-13 17:19:50 +08:00
public partial class OpaqueBackground : Container
{
public OpaqueBackground()
{
RelativeSizeAxes = Axes.Both;
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = OsuColour.Gray(30)
},
new Triangles
{
RelativeSizeAxes = Axes.Both,
ColourLight = OsuColour.Gray(40),
ColourDark = OsuColour.Gray(20),
},
};
}
}
}