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 System;
5using osu.Framework.Graphics;
6using osu.Framework.Graphics.Containers;
7using osu.Framework.Graphics.Shapes;
8using osu.Framework.Graphics.Sprites;
9using osu.Framework.Input.Events;
10using osu.Framework.Localisation;
11using osuTK.Graphics;
12
13namespace osu.Framework.Testing.Drawables.Steps
14{
15 public abstract class StepButton : CompositeDrawable
16 {
17 public virtual int RequiredRepetitions => 1;
18
19 protected Box Light;
20 protected Box Background;
21 protected SpriteText SpriteText;
22
23 public Action Action { get; set; }
24
25 public LocalisableString Text
26 {
27 get => SpriteText.Text;
28 set => SpriteText.Text = value;
29 }
30
31 private Color4 lightColour = Color4.BlueViolet;
32
33 public Color4 LightColour
34 {
35 get => lightColour;
36 set
37 {
38 lightColour = value;
39 if (IsLoaded) Reset();
40 }
41 }
42
43 public readonly bool IsSetupStep;
44
45 protected virtual Color4 IdleColour => new Color4(0.15f, 0.15f, 0.15f, 1);
46
47 protected virtual Color4 RunningColour => new Color4(0.5f, 0.5f, 0.5f, 1);
48
49 protected StepButton(bool isSetupStep = false)
50 {
51 IsSetupStep = isSetupStep;
52
53 InternalChildren = new Drawable[]
54 {
55 Background = new Box
56 {
57 RelativeSizeAxes = Axes.Both,
58 Colour = IdleColour,
59 Anchor = Anchor.Centre,
60 Origin = Anchor.Centre,
61 },
62 Light = new Box
63 {
64 RelativeSizeAxes = Axes.Y,
65 Width = 5,
66 },
67 SpriteText = new SpriteText
68 {
69 Anchor = Anchor.CentreLeft,
70 Origin = Anchor.CentreLeft,
71 Font = FrameworkFont.Regular.With(size: 14),
72 X = 5,
73 Padding = new MarginPadding(5)
74 }
75 };
76
77 Height = 20;
78 RelativeSizeAxes = Axes.X;
79
80 BorderThickness = 1.5f;
81 BorderColour = new Color4(0.15f, 0.15f, 0.15f, 1);
82
83 Masking = true;
84 }
85
86 protected override void LoadComplete()
87 {
88 base.LoadComplete();
89 Reset();
90 }
91
92 protected override bool OnClick(ClickEvent e)
93 {
94 try
95 {
96 PerformStep(true);
97 }
98 catch (Exception exc)
99 {
100 Logging.Logger.Error(exc, $"Step {this} triggered an error");
101 }
102
103 return true;
104 }
105
106 /// <summary>
107 /// Reset this step to a default state.
108 /// </summary>
109 public virtual void Reset()
110 {
111 Background.DelayUntilTransformsFinished().FadeColour(IdleColour, 1000, Easing.OutQuint);
112 Light.FadeColour(lightColour);
113 }
114
115 public virtual void PerformStep(bool userTriggered = false)
116 {
117 Background.ClearTransforms();
118 Background.FadeColour(RunningColour, 400, Easing.OutQuint);
119
120 try
121 {
122 Action?.Invoke();
123 }
124 catch (Exception)
125 {
126 Failure();
127 throw;
128 }
129 }
130
131 protected virtual void Failure()
132 {
133 Background.DelayUntilTransformsFinished().FadeColour(new Color4(0.3f, 0.15f, 0.15f, 1), 1000, Easing.OutQuint);
134 Light.FadeColour(Color4.Red);
135 }
136
137 protected virtual void Success()
138 {
139 Background.FinishTransforms();
140 Background.FadeColour(IdleColour, 1000, Easing.OutQuint);
141
142 Light.FadeColour(Color4.YellowGreen);
143 }
144
145 public override string ToString() => Text.ToString();
146 }
147}