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-07-13 07:28:18 +00:00
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2018-07-13 07:28:18 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2019-04-02 05:51:28 +00:00
|
|
|
|
using osu.Framework.Graphics.Effects;
|
2018-07-13 07:28:18 +00:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2018-10-02 03:02:47 +00:00
|
|
|
|
using osu.Framework.Input.Events;
|
2018-07-13 07:28:18 +00:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2018-11-20 07:51:59 +00:00
|
|
|
|
using osuTK.Graphics;
|
2018-07-13 07:28:18 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Highlight on hover, bounce on click.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class OsuAnimatedButton : OsuClickableContainer
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-06-10 16:18:49 +00:00
|
|
|
|
/// The colour that should be flashed when the <see cref="OsuAnimatedButton"/> is clicked.
|
2018-07-13 07:28:18 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
protected Color4 FlashColour = Color4.White.Opacity(0.3f);
|
|
|
|
|
|
|
|
|
|
private Color4 hoverColour = Color4.White.Opacity(0.1f);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-06-10 16:18:49 +00:00
|
|
|
|
/// The background colour of the <see cref="OsuAnimatedButton"/> while it is hovered.
|
2018-07-13 07:28:18 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
protected Color4 HoverColour
|
|
|
|
|
{
|
|
|
|
|
get => hoverColour;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
hoverColour = value;
|
|
|
|
|
hover.Colour = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 02:42:41 +00:00
|
|
|
|
[Resolved]
|
|
|
|
|
private OsuColour colours { get; set; }
|
|
|
|
|
|
2018-07-13 07:28:18 +00:00
|
|
|
|
protected override Container<Drawable> Content => content;
|
|
|
|
|
|
|
|
|
|
private readonly Container content;
|
|
|
|
|
private readonly Box hover;
|
|
|
|
|
|
|
|
|
|
public OsuAnimatedButton()
|
2021-06-11 11:42:10 +00:00
|
|
|
|
: base(HoverSampleSet.Button)
|
2018-07-13 07:28:18 +00:00
|
|
|
|
{
|
|
|
|
|
base.Content.Add(content = new Container
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
CornerRadius = 5,
|
|
|
|
|
Masking = true,
|
|
|
|
|
EdgeEffect = new EdgeEffectParameters
|
|
|
|
|
{
|
|
|
|
|
Colour = Color4.Black.Opacity(0.04f),
|
|
|
|
|
Type = EdgeEffectType.Shadow,
|
|
|
|
|
Radius = 5,
|
|
|
|
|
},
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
hover = new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Colour = HoverColour,
|
2019-08-21 04:29:50 +00:00
|
|
|
|
Blending = BlendingParameters.Additive,
|
2018-07-13 07:28:18 +00:00
|
|
|
|
Alpha = 0,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2021-12-24 02:42:41 +00:00
|
|
|
|
private void load()
|
2018-07-13 07:28:18 +00:00
|
|
|
|
{
|
2019-06-03 04:53:24 +00:00
|
|
|
|
if (AutoSizeAxes != Axes.None)
|
|
|
|
|
{
|
|
|
|
|
content.RelativeSizeAxes = (Axes.Both & ~AutoSizeAxes);
|
|
|
|
|
content.AutoSizeAxes = AutoSizeAxes;
|
|
|
|
|
}
|
2021-12-24 02:42:41 +00:00
|
|
|
|
}
|
2019-06-03 04:53:24 +00:00
|
|
|
|
|
2021-12-24 02:42:41 +00:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
2021-12-24 08:03:29 +00:00
|
|
|
|
Colour = dimColour;
|
|
|
|
|
Enabled.BindValueChanged(_ => this.FadeColour(dimColour, 200, Easing.OutQuint));
|
2018-07-13 07:28:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 08:03:29 +00:00
|
|
|
|
private Color4 dimColour => Enabled.Value ? Color4.White : colours.Gray9;
|
2021-12-24 02:42:41 +00:00
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
2018-07-13 07:28:18 +00:00
|
|
|
|
{
|
|
|
|
|
hover.FadeIn(500, Easing.OutQuint);
|
2018-10-02 03:02:47 +00:00
|
|
|
|
return base.OnHover(e);
|
2018-07-13 07:28:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
2018-07-13 07:28:18 +00:00
|
|
|
|
{
|
|
|
|
|
hover.FadeOut(500, Easing.OutQuint);
|
2018-10-02 03:02:47 +00:00
|
|
|
|
base.OnHoverLost(e);
|
2018-07-13 07:28:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override bool OnClick(ClickEvent e)
|
2018-07-13 07:28:18 +00:00
|
|
|
|
{
|
|
|
|
|
hover.FlashColour(FlashColour, 800, Easing.OutQuint);
|
2018-10-02 03:02:47 +00:00
|
|
|
|
return base.OnClick(e);
|
2018-07-13 07:28:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 03:02:47 +00:00
|
|
|
|
protected override bool OnMouseDown(MouseDownEvent e)
|
2018-07-13 07:28:18 +00:00
|
|
|
|
{
|
|
|
|
|
Content.ScaleTo(0.75f, 2000, Easing.OutQuint);
|
2018-10-02 03:02:47 +00:00
|
|
|
|
return base.OnMouseDown(e);
|
2018-07-13 07:28:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 09:17:21 +00:00
|
|
|
|
protected override void OnMouseUp(MouseUpEvent e)
|
2018-07-13 07:28:18 +00:00
|
|
|
|
{
|
|
|
|
|
Content.ScaleTo(1, 1000, Easing.OutElastic);
|
2020-01-20 09:17:21 +00:00
|
|
|
|
base.OnMouseUp(e);
|
2018-07-13 07:28:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|