osu/osu.Game/Graphics/UserInterface/OsuAnimatedButton.cs

129 lines
3.8 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-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 class OsuAnimatedButton : OsuClickableContainer
{
/// <summary>
/// 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>
/// 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;
}
}
[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()
: 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]
private void load()
2018-07-13 07:28:18 +00:00
{
if (AutoSizeAxes != Axes.None)
{
content.RelativeSizeAxes = (Axes.Both & ~AutoSizeAxes);
content.AutoSizeAxes = AutoSizeAxes;
}
}
protected override void LoadComplete()
{
base.LoadComplete();
Colour = enablementColour;
Enabled.BindValueChanged(_ => this.FadeColour(enablementColour, 200, Easing.OutQuint));
2018-07-13 07:28:18 +00:00
}
private Color4 enablementColour => Enabled.Value ? Color4.White : colours.Gray9;
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
}
protected override void OnMouseUp(MouseUpEvent e)
2018-07-13 07:28:18 +00:00
{
Content.ScaleTo(1, 1000, Easing.OutElastic);
base.OnMouseUp(e);
2018-07-13 07:28:18 +00:00
}
}
}