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-04-13 17:19:50 +08:00
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2018-08-01 15:45:48 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
|
|
|
|
using osu.Framework.Graphics;
|
2019-11-06 13:08:52 +08:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-08-01 15:45:48 +08:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics.UserInterface;
|
2018-10-02 11:02:47 +08:00
|
|
|
|
using osu.Framework.Input.Events;
|
2021-02-22 16:14:00 +08:00
|
|
|
|
using osu.Framework.Localisation;
|
2018-08-01 15:45:48 +08:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterface
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A button with added default sound effects.
|
|
|
|
|
/// </summary>
|
2019-11-06 13:08:52 +08:00
|
|
|
|
public class OsuButton : Button
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2021-02-22 16:14:00 +08:00
|
|
|
|
public LocalisableString Text
|
2019-11-06 13:08:52 +08:00
|
|
|
|
{
|
2021-02-25 13:06:21 +08:00
|
|
|
|
get => SpriteText?.Text ?? default;
|
2019-11-06 13:08:52 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (SpriteText != null)
|
|
|
|
|
SpriteText.Text = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Color4? backgroundColour;
|
|
|
|
|
|
2022-05-09 23:52:09 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets a custom background colour to this button, replacing the provided default.
|
|
|
|
|
/// </summary>
|
2019-11-06 13:08:52 +08:00
|
|
|
|
public Color4 BackgroundColour
|
|
|
|
|
{
|
2022-05-09 23:52:09 +08:00
|
|
|
|
get => backgroundColour ?? defaultBackgroundColour;
|
2019-11-06 13:08:52 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
backgroundColour = value;
|
|
|
|
|
Background.FadeColour(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-09 23:52:09 +08:00
|
|
|
|
private Color4 defaultBackgroundColour;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets a default background colour to this button.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected Color4 DefaultBackgroundColour
|
|
|
|
|
{
|
|
|
|
|
get => defaultBackgroundColour;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
defaultBackgroundColour = value;
|
|
|
|
|
|
|
|
|
|
if (backgroundColour == null)
|
|
|
|
|
Background.FadeColour(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 13:08:52 +08:00
|
|
|
|
protected override Container<Drawable> Content { get; }
|
|
|
|
|
|
|
|
|
|
protected Box Hover;
|
|
|
|
|
protected Box Background;
|
|
|
|
|
protected SpriteText SpriteText;
|
2018-08-01 15:45:48 +08:00
|
|
|
|
|
2021-06-11 19:42:10 +08:00
|
|
|
|
public OsuButton(HoverSampleSet? hoverSounds = HoverSampleSet.Button)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2018-08-01 15:45:48 +08:00
|
|
|
|
Height = 40;
|
|
|
|
|
|
2019-11-06 13:08:52 +08:00
|
|
|
|
AddInternal(Content = new Container
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Masking = true,
|
|
|
|
|
CornerRadius = 5,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
Background = new Box
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
},
|
|
|
|
|
Hover = new Box
|
|
|
|
|
{
|
|
|
|
|
Alpha = 0,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Colour = Color4.White.Opacity(.1f),
|
|
|
|
|
Blending = BlendingParameters.Additive,
|
|
|
|
|
Depth = float.MinValue
|
|
|
|
|
},
|
|
|
|
|
SpriteText = CreateText(),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-11-08 16:00:47 +08:00
|
|
|
|
if (hoverSounds.HasValue)
|
2022-11-03 16:44:54 +08:00
|
|
|
|
AddInternal(new HoverClickSounds(hoverSounds.Value) { Enabled = { BindTarget = Enabled } });
|
2018-08-01 15:45:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-10 01:55:26 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
2018-08-01 15:45:48 +08:00
|
|
|
|
{
|
2022-05-09 23:52:09 +08:00
|
|
|
|
DefaultBackgroundColour = colours.BlueDark;
|
2021-12-24 16:00:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
2018-08-01 15:45:48 +08:00
|
|
|
|
|
2021-12-24 16:03:29 +08:00
|
|
|
|
Colour = dimColour;
|
|
|
|
|
Enabled.BindValueChanged(_ => this.FadeColour(dimColour, 200, Easing.OutQuint));
|
2018-08-01 15:45:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 16:03:29 +08:00
|
|
|
|
private Color4 dimColour => Enabled.Value ? Color4.White : Color4.Gray;
|
2021-12-24 10:50:48 +08:00
|
|
|
|
|
2019-11-06 13:08:52 +08:00
|
|
|
|
protected override bool OnClick(ClickEvent e)
|
2018-08-01 15:45:48 +08:00
|
|
|
|
{
|
2019-11-06 13:08:52 +08:00
|
|
|
|
if (Enabled.Value)
|
2022-05-09 23:52:09 +08:00
|
|
|
|
Background.FlashColour(BackgroundColour.Lighten(0.4f), 200);
|
2019-11-06 13:08:52 +08:00
|
|
|
|
|
|
|
|
|
return base.OnClick(e);
|
2018-08-01 15:45:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
2018-08-01 15:45:48 +08:00
|
|
|
|
{
|
2019-11-06 13:08:52 +08:00
|
|
|
|
if (Enabled.Value)
|
|
|
|
|
Hover.FadeIn(200, Easing.OutQuint);
|
|
|
|
|
|
|
|
|
|
return base.OnHover(e);
|
2018-08-01 15:45:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
2018-08-01 15:45:48 +08:00
|
|
|
|
{
|
2018-10-02 11:02:47 +08:00
|
|
|
|
base.OnHoverLost(e);
|
2019-11-06 13:08:52 +08:00
|
|
|
|
|
|
|
|
|
Hover.FadeOut(300);
|
2018-08-01 15:45:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override bool OnMouseDown(MouseDownEvent e)
|
2018-08-01 15:45:48 +08:00
|
|
|
|
{
|
|
|
|
|
Content.ScaleTo(0.9f, 4000, Easing.OutQuint);
|
2018-10-02 11:02:47 +08:00
|
|
|
|
return base.OnMouseDown(e);
|
2018-08-01 15:45:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 17:17:21 +08:00
|
|
|
|
protected override void OnMouseUp(MouseUpEvent e)
|
2018-08-01 15:45:48 +08:00
|
|
|
|
{
|
|
|
|
|
Content.ScaleTo(1, 1000, Easing.OutElastic);
|
2020-01-20 17:17:21 +08:00
|
|
|
|
base.OnMouseUp(e);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
2018-08-01 15:45:48 +08:00
|
|
|
|
|
2019-11-06 13:08:52 +08:00
|
|
|
|
protected virtual SpriteText CreateText() => new OsuSpriteText
|
2018-08-01 15:45:48 +08:00
|
|
|
|
{
|
|
|
|
|
Depth = -1,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Anchor = Anchor.Centre,
|
2019-02-12 12:04:46 +08:00
|
|
|
|
Font = OsuFont.GetFont(weight: FontWeight.Bold)
|
2018-08-01 15:45:48 +08:00
|
|
|
|
};
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|