-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGameplayMenuOverlay.cs
277 lines (230 loc) · 9.15 KB
/
GameplayMenuOverlay.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// 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 System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Input.Bindings;
using osuTK;
using osuTK.Graphics;
using osu.Game.Localisation;
namespace osu.Game.Screens.Play
{
public abstract partial class GameplayMenuOverlay : OverlayContainer, IKeyBindingHandler<GlobalAction>
{
protected const int TRANSITION_DURATION = 200;
private const int button_height = 70;
private const float background_alpha = 0.75f;
protected override bool BlockScrollInput => false;
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
public Action? OnResume { get; init; }
public Action? OnRetry { get; init; }
public Action? OnQuit { get; init; }
/// <summary>
/// Action that is invoked when <see cref="GlobalAction.Back"/> is triggered.
/// </summary>
protected virtual Action BackAction => () =>
{
// We prefer triggering the button click as it will animate...
// but sometimes buttons aren't present (see FailOverlay's constructor as an example).
if (Buttons.Any())
Buttons.Last().TriggerClick();
else
OnQuit?.Invoke();
};
/// <summary>
/// Action that is invoked when <see cref="GlobalAction.Select"/> is triggered.
/// </summary>
protected virtual Action SelectAction => () => InternalButtons.Selected?.TriggerClick();
public abstract LocalisableString Header { get; }
protected SelectionCycleFillFlowContainer<DialogButton> InternalButtons = null!;
public IReadOnlyList<DialogButton> Buttons => InternalButtons;
private TextFlowContainer playInfoText = null!;
[Resolved]
private GlobalActionContainer globalAction { get; set; } = null!;
protected GameplayMenuOverlay()
{
RelativeSizeAxes = Axes.Both;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black,
Alpha = background_alpha,
},
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 50),
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Children = new Drawable[]
{
new OsuSpriteText
{
Text = Header,
Font = OsuFont.GetFont(size: 48),
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Colour = colours.Yellow,
},
InternalButtons = new SelectionCycleFillFlowContainer<DialogButton>
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Masking = true,
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(0.6f),
Radius = 50
},
},
playInfoText = new OsuTextFlowContainer(cp => cp.Font = OsuFont.GetFont(size: 18))
{
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
TextAnchor = Anchor.TopCentre,
AutoSizeAxes = Axes.Both,
}
}
},
};
if (OnResume != null)
AddButton(GameplayMenuOverlayStrings.Continue, colours.Green, () => OnResume.Invoke());
if (OnRetry != null)
AddButton(GameplayMenuOverlayStrings.Retry, colours.YellowDark, () => OnRetry.Invoke());
if (OnQuit != null)
AddButton(GameplayMenuOverlayStrings.Quit, new Color4(170, 27, 39, 255), () => OnQuit.Invoke());
State.ValueChanged += _ => InternalButtons.Deselect();
updateInfoText();
}
private int retries;
public int Retries
{
set
{
if (value == retries)
return;
retries = value;
if (IsLoaded)
updateInfoText();
}
}
protected override void PopIn()
{
this.FadeIn(TRANSITION_DURATION, Easing.In);
updateInfoText();
}
protected override void PopOut() => this.FadeOut(TRANSITION_DURATION, Easing.In);
protected void AddButton(LocalisableString text, Color4 colour, Action? action)
{
var button = new Button
{
Text = text,
ButtonColour = colour,
Origin = Anchor.TopCentre,
Anchor = Anchor.TopCentre,
Height = button_height,
Action = delegate
{
action?.Invoke();
Hide();
}
};
InternalButtons.Add(button);
}
public virtual bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
switch (e.Action)
{
case GlobalAction.SelectPrevious:
InternalButtons.SelectPrevious();
return true;
case GlobalAction.SelectNext:
InternalButtons.SelectNext();
return true;
case GlobalAction.Back:
BackAction.Invoke();
return true;
case GlobalAction.Select:
SelectAction.Invoke();
return true;
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
[Resolved]
private IGameplayClock? gameplayClock { get; set; }
[Resolved]
private GameplayState? gameplayState { get; set; }
private void updateInfoText()
{
playInfoText.Clear();
playInfoText.AddText(GameplayMenuOverlayStrings.RetryCount);
playInfoText.AddText(retries.ToString(), cp => cp.Font = cp.Font.With(weight: FontWeight.Bold));
if (getSongProgress() is int progress)
{
playInfoText.NewLine();
playInfoText.AddText(GameplayMenuOverlayStrings.SongProgress);
playInfoText.AddText($"{progress}%", cp => cp.Font = cp.Font.With(weight: FontWeight.Bold));
}
}
private int? getSongProgress()
{
if (gameplayClock == null || gameplayState == null)
return null;
(double firstHitTime, double lastHitTime) = gameplayState.Beatmap.CalculatePlayableBounds();
double playableLength = (lastHitTime - firstHitTime);
if (playableLength == 0)
return 0;
return (int)Math.Clamp(((gameplayClock.CurrentTime - firstHitTime) / playableLength) * 100, 0, 100);
}
private partial class Button : DialogButton
{
// required to ensure keyboard navigation always starts from an extremity (unless the cursor is moved)
protected override bool OnHover(HoverEvent e) => true;
protected override bool OnMouseMove(MouseMoveEvent e)
{
State = SelectionState.Selected;
return base.OnMouseMove(e);
}
}
protected override bool Handle(UIEvent e)
{
switch (e)
{
case ScrollEvent:
if (ReceivePositionalInputAt(e.ScreenSpaceMousePosition))
return globalAction.TriggerEvent(e);
break;
}
return base.Handle(e);
}
}
}