-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathBeatmapOffsetControl.cs
340 lines (282 loc) · 12.3 KB
/
BeatmapOffsetControl.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// 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.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Framework.Utils;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Input.Bindings;
using osu.Game.Localisation;
using osu.Game.Overlays;
using osu.Game.Overlays.Settings;
using osu.Game.Overlays.Settings.Sections.Audio;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Scoring;
using osu.Game.Scoring;
using osu.Game.Screens.Ranking.Statistics;
using osuTK;
namespace osu.Game.Screens.Play.PlayerSettings
{
public partial class BeatmapOffsetControl : CompositeDrawable, IKeyBindingHandler<GlobalAction>
{
public Bindable<ScoreInfo?> ReferenceScore { get; } = new Bindable<ScoreInfo?>();
private Bindable<ScoreInfo?> lastAppliedScore { get; } = new Bindable<ScoreInfo?>();
public BindableDouble Current { get; } = new BindableDouble
{
MinValue = -50,
MaxValue = 50,
Precision = 0.1,
};
private readonly FillFlowContainer referenceScoreContainer;
[Resolved]
private RealmAccess realm { get; set; } = null!;
[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; } = null!;
[Resolved]
private OsuColour colours { get; set; } = null!;
[Resolved]
private Player? player { get; set; }
[Resolved]
private IGameplayClock? gameplayClock { get; set; }
private double lastPlayAverage;
private double lastPlayBeatmapOffset;
private HitEventTimingDistributionGraph? lastPlayGraph;
private SettingsButton? useAverageButton;
private IDisposable? beatmapOffsetSubscription;
private Task? realmWriteTask;
public BeatmapOffsetControl()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChild = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(10),
Children = new Drawable[]
{
new OffsetSliderBar
{
KeyboardStep = 5,
LabelText = BeatmapOffsetControlStrings.AudioOffsetThisBeatmap,
Current = Current,
},
referenceScoreContainer = new FillFlowContainer
{
Spacing = new Vector2(10),
Direction = FillDirection.Vertical,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
},
}
};
}
[BackgroundDependencyLoader]
private void load(SessionStatics statics)
{
statics.BindWith(Static.LastAppliedOffsetScore, lastAppliedScore);
}
protected override void LoadComplete()
{
base.LoadComplete();
beatmapOffsetSubscription = realm.SubscribeToPropertyChanged(
r => r.Find<BeatmapInfo>(beatmap.Value.BeatmapInfo.ID)?.UserSettings,
settings => settings.Offset,
val =>
{
// At the point we reach here, it's not guaranteed that all realm writes have taken place (there may be some in-flight).
// We are only aware of writes that originated from our own flow, so if we do see one that's active we can avoid handling the feedback value arriving.
if (realmWriteTask == null)
Current.Value = val;
if (realmWriteTask?.IsCompleted == true)
{
// we can also mark any in-flight write that is managed locally as "seen" and start handling any incoming changes again.
realmWriteTask = null;
}
});
Current.BindValueChanged(currentChanged);
ReferenceScore.BindValueChanged(scoreChanged, true);
}
private void currentChanged(ValueChangedEvent<double> offset)
{
Scheduler.AddOnce(updateOffset);
void updateOffset()
{
// the last play graph is relative to the offset at the point of the last play, so we need to factor that out.
double adjustmentSinceLastPlay = lastPlayBeatmapOffset - Current.Value;
// Negative is applied here because the play graph is considering a hit offset, not track (as we currently use for clocks).
lastPlayGraph?.UpdateOffset(-adjustmentSinceLastPlay);
// ensure the previous write has completed. ignoring performance concerns, if we don't do this, the async writes could be out of sequence.
if (realmWriteTask?.IsCompleted == false)
{
Scheduler.AddOnce(updateOffset);
return;
}
if (useAverageButton != null)
{
useAverageButton.Enabled.Value = !Precision.AlmostEquals(lastPlayAverage, adjustmentSinceLastPlay, Current.Precision / 2);
}
realmWriteTask = realm.WriteAsync(r =>
{
var setInfo = r.Find<BeatmapSetInfo>(beatmap.Value.BeatmapSetInfo.ID);
if (setInfo == null) // only the case for tests.
return;
// Apply to all difficulties in a beatmap set if they have the same audio
// (they generally always share timing).
foreach (var b in setInfo.Beatmaps)
{
BeatmapUserSettings userSettings = b.UserSettings;
double val = Current.Value;
if (userSettings.Offset != val && b.AudioEquals(beatmap.Value.BeatmapInfo))
userSettings.Offset = val;
}
});
}
}
private void scoreChanged(ValueChangedEvent<ScoreInfo?> score)
{
referenceScoreContainer.Clear();
if (score.NewValue == null)
return;
if (score.NewValue.Equals(lastAppliedScore.Value))
return;
if (!score.NewValue.BeatmapInfo.AsNonNull().Equals(beatmap.Value.BeatmapInfo))
return;
if (score.NewValue.Mods.Any(m => !m.UserPlayable || m is IHasNoTimedInputs))
return;
var hitEvents = score.NewValue.HitEvents;
if (!(hitEvents.CalculateAverageHitError() is double average))
return;
referenceScoreContainer.Children = new Drawable[]
{
new OsuSpriteText
{
Text = BeatmapOffsetControlStrings.PreviousPlay
},
};
// affecting unstable rate here is used as a substitute of determining if a hit event represents a *timed* hit event,
// i.e. an user input that the user had to *time to the track*,
// i.e. one that it *makes sense to use* when doing anything with timing and offsets.
if (hitEvents.Count(HitEventExtensions.AffectsUnstableRate) < 10)
{
referenceScoreContainer.AddRange(new Drawable[]
{
new OsuTextFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Colour = colours.Red1,
Text = BeatmapOffsetControlStrings.PreviousPlayTooShortToUseForCalibration
},
});
return;
}
lastPlayAverage = average;
lastPlayBeatmapOffset = Current.Value;
LinkFlowContainer globalOffsetText;
referenceScoreContainer.AddRange(new Drawable[]
{
lastPlayGraph = new HitEventTimingDistributionGraph(hitEvents)
{
RelativeSizeAxes = Axes.X,
Height = 50,
},
new AverageHitError(hitEvents),
useAverageButton = new SettingsButton
{
Text = BeatmapOffsetControlStrings.CalibrateUsingLastPlay,
Action = () =>
{
Current.Value = lastPlayBeatmapOffset - lastPlayAverage;
lastAppliedScore.Value = ReferenceScore.Value;
},
Enabled = { Value = !Precision.AlmostEquals(lastPlayAverage, 0, Current.Precision / 2) }
},
globalOffsetText = new LinkFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
}
});
if (settings != null)
{
globalOffsetText.AddText("You can also ");
globalOffsetText.AddLink("adjust the global offset", () => settings.ShowAtControl<AudioOffsetAdjustControl>());
globalOffsetText.AddText(" based off this play.");
}
}
[Resolved]
private SettingsOverlay? settings { get; set; }
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
beatmapOffsetSubscription?.Dispose();
}
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
// General limitations to ensure players don't do anything too weird.
// These match stable for now.
if (player is SubmittingPlayer)
{
// TODO: the blocking conditions should probably display a message.
if (player?.IsBreakTime.Value == false && gameplayClock?.CurrentTime - gameplayClock?.StartTime > 10000)
return false;
if (gameplayClock?.IsPaused.Value == true)
return false;
}
// To match stable, this should adjust by 5 ms, or 1 ms when holding alt.
// But that is hard to make work with global actions due to the operating mode.
// Let's use the more precise as a default for now.
const double amount = 1;
switch (e.Action)
{
case GlobalAction.IncreaseOffset:
Current.Value += amount;
return true;
case GlobalAction.DecreaseOffset:
Current.Value -= amount;
return true;
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
public static LocalisableString GetOffsetExplanatoryText(double offset)
{
return offset == 0
? LocalisableString.Interpolate($@"{offset:0.0} ms")
: LocalisableString.Interpolate($@"{offset:0.0} ms {getEarlyLateText(offset)}");
LocalisableString getEarlyLateText(double value)
{
Debug.Assert(value != 0);
return value > 0
? BeatmapOffsetControlStrings.HitObjectsAppearEarlier
: BeatmapOffsetControlStrings.HitObjectsAppearLater;
}
}
private partial class OffsetSliderBar : PlayerSliderBar<double>
{
protected override Drawable CreateControl() => new CustomSliderBar();
protected partial class CustomSliderBar : SliderBar
{
public override LocalisableString TooltipText => GetOffsetExplanatoryText(Current.Value);
}
}
}
}