-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOsuButton.cs
187 lines (159 loc) · 5.91 KB
/
OsuButton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// 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.
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;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics.Sprites;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Graphics.UserInterface
{
/// <summary>
/// A button with added default sound effects.
/// </summary>
public abstract partial class OsuButton : Button
{
public LocalisableString Text
{
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);
protected Box Hover;
protected Box Background;
protected SpriteText SpriteText;
private readonly Box flashLayer;
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)
Add(new HoverClickSounds(hoverSounds.Value) { Enabled = { BindTarget = Enabled } });
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
DefaultBackgroundColour = colours.BlueDark;
}
protected override void LoadComplete()
{
base.LoadComplete();
Colour = dimColour;
Enabled.BindValueChanged(_ => this.FadeColour(dimColour, 200, Easing.OutQuint));
}
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;
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);
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
Hover.FadeOut(800, Easing.OutQuint);
}
protected override bool OnMouseDown(MouseDownEvent e)
{
Content.ScaleTo(0.9f, 4000, Easing.OutQuint);
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)
};
}
}