A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using osu.Framework.Bindables;
5using osu.Framework.Extensions.Color4Extensions;
6using osu.Framework.Graphics.Sprites;
7using osuTK.Graphics;
8using osu.Framework.Graphics.Shapes;
9using osu.Framework.Input.Events;
10using osu.Framework.Localisation;
11
12namespace osu.Framework.Graphics.UserInterface
13{
14 public class BasicButton : Button
15 {
16 public LocalisableString Text
17 {
18 get => SpriteText?.Text ?? default;
19 set
20 {
21 if (SpriteText != null)
22 SpriteText.Text = value;
23 }
24 }
25
26 public Color4 BackgroundColour
27 {
28 get => Background.Colour;
29 set => Background.FadeColour(value);
30 }
31
32 private Color4? flashColour;
33
34 /// <summary>
35 /// The colour the background will flash with when this button is clicked.
36 /// </summary>
37 public Color4 FlashColour
38 {
39 get => flashColour ?? BackgroundColour;
40 set => flashColour = value;
41 }
42
43 /// <summary>
44 /// The additive colour that is applied to the background when hovered.
45 /// </summary>
46 public Color4 HoverColour
47 {
48 get => Hover.Colour;
49 set => Hover.FadeColour(value);
50 }
51
52 private Color4 disabledColour = Color4.Gray;
53
54 /// <summary>
55 /// The additive colour that is applied to this button when disabled.
56 /// </summary>
57 public Color4 DisabledColour
58 {
59 get => disabledColour;
60 set
61 {
62 if (disabledColour == value)
63 return;
64
65 disabledColour = value;
66 Enabled.TriggerChange();
67 }
68 }
69
70 /// <summary>
71 /// The duration of the transition when hovering.
72 /// </summary>
73 public double HoverFadeDuration { get; set; } = 200;
74
75 /// <summary>
76 /// The duration of the flash when this button is clicked.
77 /// </summary>
78 public double FlashDuration { get; set; } = 200;
79
80 /// <summary>
81 /// The duration of the transition when toggling the Enabled state.
82 /// </summary>
83 public double DisabledFadeDuration { get; set; } = 200;
84
85 protected Box Hover;
86 protected Box Background;
87 protected SpriteText SpriteText;
88
89 public BasicButton()
90 {
91 AddRange(new Drawable[]
92 {
93 Background = new Box
94 {
95 Anchor = Anchor.Centre,
96 Origin = Anchor.Centre,
97 RelativeSizeAxes = Axes.Both,
98 Colour = FrameworkColour.BlueGreen
99 },
100 Hover = new Box
101 {
102 Alpha = 0,
103 Anchor = Anchor.Centre,
104 Origin = Anchor.Centre,
105 RelativeSizeAxes = Axes.Both,
106 Colour = Color4.White.Opacity(.1f),
107 Blending = BlendingParameters.Additive
108 },
109 SpriteText = CreateText()
110 });
111
112 Enabled.BindValueChanged(enabledChanged, true);
113 }
114
115 protected virtual SpriteText CreateText() => new SpriteText
116 {
117 Depth = -1,
118 Origin = Anchor.Centre,
119 Anchor = Anchor.Centre,
120 Font = FrameworkFont.Regular,
121 Colour = FrameworkColour.Yellow
122 };
123
124 protected override bool OnClick(ClickEvent e)
125 {
126 if (Enabled.Value)
127 Background.FlashColour(FlashColour, FlashDuration);
128
129 return base.OnClick(e);
130 }
131
132 protected override bool OnHover(HoverEvent e)
133 {
134 if (Enabled.Value)
135 Hover.FadeIn(HoverFadeDuration);
136
137 return base.OnHover(e);
138 }
139
140 protected override void OnHoverLost(HoverLostEvent e)
141 {
142 base.OnHoverLost(e);
143
144 Hover.FadeOut(HoverFadeDuration);
145 }
146
147 private void enabledChanged(ValueChangedEvent<bool> e)
148 {
149 this.FadeColour(e.NewValue ? Color4.White : DisabledColour, DisabledFadeDuration, Easing.OutQuint);
150 }
151 }
152}