2019-01-24 08:43:03 +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.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-08-06 08:18:45 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
2020-08-06 09:22:17 +00:00
|
|
|
|
using osu.Framework.Caching;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2021-02-25 06:38:56 +00:00
|
|
|
|
using osu.Framework.Extensions.EnumExtensions;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2019-04-02 05:51:28 +00:00
|
|
|
|
using osu.Framework.Graphics.Effects;
|
2020-08-06 08:17:24 +00:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2020-09-03 07:29:15 +00:00
|
|
|
|
using osu.Framework.Graphics.Textures;
|
2020-08-06 08:17:24 +00:00
|
|
|
|
using osu.Framework.Input.Bindings;
|
|
|
|
|
using osu.Framework.Input.Events;
|
2021-02-22 08:14:00 +00:00
|
|
|
|
using osu.Framework.Localisation;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
|
using osu.Game.Graphics.Backgrounds;
|
2020-08-06 08:17:24 +00:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2020-08-06 08:17:24 +00:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
2020-08-06 08:18:45 +00:00
|
|
|
|
using osu.Game.Input;
|
2020-08-06 08:17:24 +00:00
|
|
|
|
using osu.Game.Input.Bindings;
|
2018-11-20 07:51:59 +00:00
|
|
|
|
using osuTK;
|
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Toolbar
|
|
|
|
|
{
|
2020-08-06 08:17:24 +00:00
|
|
|
|
public abstract class ToolbarButton : OsuClickableContainer, IKeyBindingHandler<GlobalAction>
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2020-08-06 08:17:24 +00:00
|
|
|
|
protected GlobalAction? Hotkey { get; set; }
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public void SetIcon(Drawable icon)
|
|
|
|
|
{
|
|
|
|
|
IconContainer.Icon = icon;
|
|
|
|
|
IconContainer.Show();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 07:28:53 +00:00
|
|
|
|
[Resolved]
|
|
|
|
|
private TextureStore textures { get; set; }
|
|
|
|
|
|
|
|
|
|
public void SetIcon(string texture) =>
|
|
|
|
|
SetIcon(new Sprite
|
|
|
|
|
{
|
|
|
|
|
Texture = textures.Get(texture),
|
|
|
|
|
});
|
|
|
|
|
|
2021-02-22 08:14:00 +00:00
|
|
|
|
public LocalisableString Text
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => DrawableText.Text;
|
|
|
|
|
set => DrawableText.Text = value;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-22 08:14:00 +00:00
|
|
|
|
public LocalisableString TooltipMain
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => tooltip1.Text;
|
|
|
|
|
set => tooltip1.Text = value;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-22 08:14:00 +00:00
|
|
|
|
public LocalisableString TooltipSub
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2019-02-28 04:58:19 +00:00
|
|
|
|
get => tooltip2.Text;
|
|
|
|
|
set => tooltip2.Text = value;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual Anchor TooltipAnchor => Anchor.TopLeft;
|
|
|
|
|
|
|
|
|
|
protected ConstrainedIconContainer IconContainer;
|
|
|
|
|
protected SpriteText DrawableText;
|
|
|
|
|
protected Box HoverBackground;
|
2020-06-15 05:16:17 +00:00
|
|
|
|
private readonly Box flashBackground;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
private readonly FillFlowContainer tooltipContainer;
|
|
|
|
|
private readonly SpriteText tooltip1;
|
|
|
|
|
private readonly SpriteText tooltip2;
|
2020-08-06 08:18:45 +00:00
|
|
|
|
private readonly SpriteText keyBindingTooltip;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
protected FillFlowContainer Flow;
|
|
|
|
|
|
2020-08-06 08:18:45 +00:00
|
|
|
|
[Resolved]
|
2020-08-06 09:22:17 +00:00
|
|
|
|
private KeyBindingStore keyBindings { get; set; }
|
2020-08-06 08:18:45 +00:00
|
|
|
|
|
2020-08-06 08:06:51 +00:00
|
|
|
|
protected ToolbarButton()
|
2021-01-15 06:02:17 +00:00
|
|
|
|
: base(HoverSampleSet.Toolbar)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2020-09-03 07:29:15 +00:00
|
|
|
|
Width = Toolbar.HEIGHT;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
RelativeSizeAxes = Axes.Y;
|
|
|
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
HoverBackground = new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2020-06-15 05:02:21 +00:00
|
|
|
|
Colour = OsuColour.Gray(80).Opacity(180),
|
|
|
|
|
Blending = BlendingParameters.Additive,
|
|
|
|
|
Alpha = 0,
|
|
|
|
|
},
|
2020-06-15 05:16:17 +00:00
|
|
|
|
flashBackground = new Box
|
2020-06-15 05:02:21 +00:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2020-06-15 07:14:35 +00:00
|
|
|
|
Alpha = 0,
|
|
|
|
|
Colour = Color4.White.Opacity(100),
|
2019-08-21 04:29:50 +00:00
|
|
|
|
Blending = BlendingParameters.Additive,
|
2018-04-13 09:19:50 +00:00
|
|
|
|
},
|
|
|
|
|
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,
|
2020-09-03 07:29:15 +00:00
|
|
|
|
Size = new Vector2(26),
|
2018-04-13 09:19:50 +00:00
|
|
|
|
Alpha = 0,
|
|
|
|
|
},
|
|
|
|
|
DrawableText = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
tooltipContainer = new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
Direction = FillDirection.Vertical,
|
2020-05-05 01:31:11 +00:00
|
|
|
|
RelativeSizeAxes = Axes.Both, // stops us being considered in parent's autosize
|
2021-02-25 06:38:56 +00:00
|
|
|
|
Anchor = TooltipAnchor.HasFlagFast(Anchor.x0) ? Anchor.BottomLeft : Anchor.BottomRight,
|
2018-04-13 09:19:50 +00:00
|
|
|
|
Origin = TooltipAnchor,
|
2021-02-25 06:38:56 +00:00
|
|
|
|
Position = new Vector2(TooltipAnchor.HasFlagFast(Anchor.x0) ? 5 : -5, 5),
|
2018-04-13 09:19:50 +00:00
|
|
|
|
Alpha = 0,
|
2020-08-06 08:18:45 +00:00
|
|
|
|
Children = new Drawable[]
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
tooltip1 = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Anchor = TooltipAnchor,
|
|
|
|
|
Origin = TooltipAnchor,
|
|
|
|
|
Shadow = true,
|
2019-02-12 04:04:46 +00:00
|
|
|
|
Font = OsuFont.GetFont(size: 22, weight: FontWeight.Bold),
|
2018-04-13 09:19:50 +00:00
|
|
|
|
},
|
2020-08-06 08:18:45 +00:00
|
|
|
|
new FillFlowContainer
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2020-08-06 08:18:45 +00:00
|
|
|
|
AutoSizeAxes = Axes.Both,
|
2018-04-13 09:19:50 +00:00
|
|
|
|
Anchor = TooltipAnchor,
|
|
|
|
|
Origin = TooltipAnchor,
|
2020-08-06 08:18:45 +00:00
|
|
|
|
Direction = FillDirection.Horizontal,
|
|
|
|
|
Children = new[]
|
|
|
|
|
{
|
|
|
|
|
tooltip2 = new OsuSpriteText { Shadow = true },
|
|
|
|
|
keyBindingTooltip = new OsuSpriteText { Shadow = true }
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-06 09:22:17 +00:00
|
|
|
|
private readonly Cached tooltipKeyBinding = new Cached();
|
|
|
|
|
|
2020-08-06 08:18:45 +00:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
|
|
|
|
{
|
2020-08-06 09:22:17 +00:00
|
|
|
|
keyBindings.KeyBindingChanged += () => tooltipKeyBinding.Invalidate();
|
|
|
|
|
updateKeyBindingTooltip();
|
2020-08-06 08:18:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-06 09:22:17 +00:00
|
|
|
|
private void updateKeyBindingTooltip()
|
2020-08-06 08:18:45 +00:00
|
|
|
|
{
|
2020-08-06 09:22:17 +00:00
|
|
|
|
if (tooltipKeyBinding.IsValid)
|
|
|
|
|
return;
|
2020-08-06 08:18:45 +00:00
|
|
|
|
|
2020-08-06 09:22:17 +00:00
|
|
|
|
var binding = keyBindings.Query().Find(b => (GlobalAction)b.Action == Hotkey);
|
2020-08-06 08:18:45 +00:00
|
|
|
|
var keyBindingString = binding?.KeyCombination.ReadableString();
|
|
|
|
|
keyBindingTooltip.Text = !string.IsNullOrEmpty(keyBindingString) ? $" ({keyBindingString})" : string.Empty;
|
2020-08-06 09:22:17 +00:00
|
|
|
|
|
|
|
|
|
tooltipKeyBinding.Validate();
|
2020-08-06 08:18:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override bool OnMouseDown(MouseDownEvent e) => true;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override bool OnClick(ClickEvent e)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2020-06-15 07:14:35 +00:00
|
|
|
|
flashBackground.FadeOutFromOne(800, Easing.OutQuint);
|
2019-02-28 23:43:04 +00:00
|
|
|
|
tooltipContainer.FadeOut(100);
|
2018-10-02 03:02:47 +00:00
|
|
|
|
return base.OnClick(e);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2020-08-06 09:22:17 +00:00
|
|
|
|
updateKeyBindingTooltip();
|
|
|
|
|
|
2020-06-15 05:02:21 +00:00
|
|
|
|
HoverBackground.FadeIn(200);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
tooltipContainer.FadeIn(100);
|
2018-10-02 03:02:47 +00:00
|
|
|
|
return base.OnHover(e);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2020-06-15 05:02:21 +00:00
|
|
|
|
HoverBackground.FadeOut(200);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
tooltipContainer.FadeOut(100);
|
|
|
|
|
}
|
2020-08-06 08:17:24 +00:00
|
|
|
|
|
|
|
|
|
public bool OnPressed(GlobalAction action)
|
|
|
|
|
{
|
|
|
|
|
if (action == Hotkey)
|
|
|
|
|
{
|
|
|
|
|
Click();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnReleased(GlobalAction action)
|
|
|
|
|
{
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class OpaqueBackground : Container
|
|
|
|
|
{
|
|
|
|
|
public OpaqueBackground()
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
Masking = true;
|
|
|
|
|
MaskingSmoothness = 0;
|
|
|
|
|
EdgeEffect = new EdgeEffectParameters
|
|
|
|
|
{
|
|
|
|
|
Type = EdgeEffectType.Shadow,
|
|
|
|
|
Colour = Color4.Black.Opacity(40),
|
|
|
|
|
Radius = 5,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|