1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 17:57:29 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/OsuButton.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

188 lines
5.9 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-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
2017-02-07 12:52:19 +08:00
using osu.Framework.Graphics.UserInterface;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics.Sprites;
2022-12-12 19:40:38 +08:00
using osuTK;
2018-11-20 15:51:59 +08:00
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
2017-02-07 12:52:19 +08:00
namespace osu.Game.Graphics.UserInterface
{
/// <summary>
/// A button with added default sound effects.
/// </summary>
2022-12-13 21:13:48 +08:00
public abstract partial class OsuButton : Button
2017-02-07 12:52:19 +08:00
{
public LocalisableString Text
{
2022-12-12 19:57:07 +08:00
get => SpriteText.Text;
set => SpriteText.Text = value;
}
private Color4? backgroundColour;
/// <summary>
/// Sets a custom background colour to this button, replacing the provided default.
/// </summary>
public virtual Color4 BackgroundColour
{
get => backgroundColour ?? defaultBackgroundColour;
set
{
backgroundColour = value;
Background.FadeColour(value);
}
}
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);
}
}
protected override Container<Drawable> Content { get; }
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) =>
// base call is checked for cases when `OsuClickableContainer` has masking applied to it directly (ie. externally in object initialisation).
base.ReceivePositionalInputAt(screenSpacePos)
// Implementations often apply masking / edge rounding at a content level, so it's imperative to check that as well.
&& Content.ReceivePositionalInputAt(screenSpacePos);
2022-12-12 19:40:38 +08:00
protected Box Hover;
protected Box Background;
protected SpriteText SpriteText;
private readonly Box flashLayer;
2022-12-13 21:13:48 +08:00
protected OsuButton(HoverSampleSet? hoverSounds = HoverSampleSet.Button)
{
Height = 40;
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,
Depth = float.MaxValue,
},
Hover = new Box
{
Alpha = 0,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
Colour = Color4.White,
Blending = BlendingParameters.Additive,
Depth = float.MinValue
},
SpriteText = CreateText(),
flashLayer = new Box
{
RelativeSizeAxes = Axes.Both,
Blending = BlendingParameters.Additive,
Depth = float.MinValue,
Colour = Color4.White.Opacity(0.5f),
Alpha = 0,
},
}
});
if (hoverSounds.HasValue)
AddInternal(new HoverClickSounds(hoverSounds.Value) { Enabled = { BindTarget = Enabled } });
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
DefaultBackgroundColour = colours.BlueDark;
2021-12-24 16:00:57 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2021-12-24 16:03:29 +08:00
Colour = dimColour;
Enabled.BindValueChanged(_ => this.FadeColour(dimColour, 200, Easing.OutQuint));
}
2021-12-24 16:03:29 +08:00
private Color4 dimColour => Enabled.Value ? Color4.White : Color4.Gray;
protected override bool OnClick(ClickEvent e)
{
if (Enabled.Value)
flashLayer.FadeOutFromOne(800, Easing.OutQuint);
return base.OnClick(e);
}
protected virtual float HoverLayerFinalAlpha => 0.1f;
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
{
if (Enabled.Value)
{
Hover.FadeTo(0.2f, 40, Easing.OutQuint)
.Then()
.FadeTo(HoverLayerFinalAlpha, 800, Easing.OutQuint);
}
return base.OnHover(e);
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
{
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
Hover.FadeOut(800, Easing.OutQuint);
}
2018-10-02 11:02:47 +08:00
protected override bool OnMouseDown(MouseDownEvent e)
{
Content.ScaleTo(0.9f, 4000, Easing.OutQuint);
2018-10-02 11:02:47 +08:00
return base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseUpEvent e)
{
Content.ScaleTo(1, 1000, Easing.OutElastic);
base.OnMouseUp(e);
}
protected virtual SpriteText CreateText() => new OsuSpriteText
{
Depth = -1,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Font = OsuFont.GetFont(weight: FontWeight.Bold)
};
2017-02-07 12:52:19 +08:00
}
}