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