-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDrawableSlider.cs
305 lines (251 loc) · 10.8 KB
/
DrawableSlider.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
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Caching;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Textures;
using osu.Framework.Platform;
using osu.Framework.Utils;
using osu.Game.Audio;
using osu.Game.Graphics;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Scoring;
using osu.Game.Rulesets.Tau.Configuration;
using osu.Game.Rulesets.Tau.Judgements;
using osu.Game.Rulesets.Tau.UI;
using osu.Game.Skinning;
using osuTK.Graphics;
namespace osu.Game.Rulesets.Tau.Objects.Drawables
{
public partial class DrawableSlider : DrawableAngledTauHitObject<Slider>
{
public Drawable SliderHead => headContainer.Child;
private readonly BindableFloat size = new(16f);
public BindableBool HighlightHardBeats = new(false);
public float PathDistance = TauPlayfield.BASE_SIZE.X / 2;
protected override TauAction[] Actions => HitObject.IsHard
? new[]
{
TauAction.HardButton1,
TauAction.HardButton2
}
: base.Actions;
private readonly SliderPath path;
private readonly Container headContainer;
private readonly Container<DrawableSliderTick> tickContainer;
private readonly Container<DrawableSliderRepeat> repeatContainer;
private readonly CircularContainer maskingContainer;
private readonly Cached drawCache = new();
private readonly PausableSkinnableSound slidingSample;
private bool inversed;
private Texture pathTexture;
private Texture pathTextureHighlighted;
public DrawableSlider()
: this(null)
{
}
public DrawableSlider(Slider obj)
: base(obj)
{
Size = TauPlayfield.BASE_SIZE;
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
AddRangeInternal(new Drawable[]
{
maskingContainer = new CircularContainer
{
Masking = false,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
AlwaysPresent = true
},
path = new SliderPath
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
PathRadius = 4,
PathDistance = PathDistance
},
}
},
headContainer = new Container { RelativeSizeAxes = Axes.Both },
tickContainer = new Container<DrawableSliderTick> { RelativeSizeAxes = Axes.Both },
repeatContainer = new Container<DrawableSliderRepeat> { RelativeSizeAxes = Axes.Both },
slidingSample = new PausableSkinnableSound { Looping = true }
});
path.Ticks = repeatContainer;
Tracking.BindValueChanged(updateSlidingSample);
}
protected override void AddNestedHitObject(DrawableHitObject hitObject)
{
base.AddNestedHitObject(hitObject);
switch (hitObject)
{
case DrawableSliderHead head:
headContainer.Child = head;
break;
case DrawableSliderHardBeat head:
headContainer.Child = head;
break;
case DrawableSliderRepeat repeat:
repeatContainer.Add(repeat);
break;
case DrawableSliderTick tick:
tickContainer.Add(tick);
break;
}
}
protected override DrawableHitObject CreateNestedHitObject(HitObject hitObject)
=> hitObject switch
{
SliderRepeat repeat => new DrawableSliderRepeat(repeat),
SliderHeadBeat head => new DrawableSliderHead(head),
SliderHardBeat head => new DrawableSliderHardBeat(head),
SliderTick tick => new DrawableSliderTick(tick),
_ => base.CreateNestedHitObject(hitObject)
};
protected override void ClearNestedHitObjects()
{
base.ClearNestedHitObjects();
headContainer.Clear(false);
repeatContainer.Clear(false);
tickContainer.Clear(false);
}
private float convertNoteSizeToSliderSize(float beatSize)
=> Interpolation.ValueAt(beatSize, 2f, 7f, 10f, 25f);
private Color4 convertHighlightSettingToColor(bool highlightHardBeats)
=> highlightHardBeats && HitObject.IsHard ? Color4.Orange : Color4.White;
[BackgroundDependencyLoader]
private void load(IRenderer renderer, GameHost host, TauRulesetConfigManager config)
{
config.BindWith(TauRulesetSettings.HighlightHardBeats, HighlightHardBeats);
NoteSize.BindValueChanged(value => path.PathRadius = convertNoteSizeToSliderSize(value.NewValue), true);
// HighlightHardBeats.BindValueChanged(value => updatePathTexture(value.NewValue, renderer));
host.DrawThread.Scheduler.AddDelayed(() => drawCache.Invalidate(), 0, true);
pathTexture = generateSmoothPathTexture(renderer, path.PathRadius, _ => Color4.White);
pathTextureHighlighted = generateSmoothPathTexture(renderer, path.PathRadius, _ => Color4.Orange);
}
[Resolved]
private TauCachedProperties properties { get; set; }
private double totalTimeHeld;
protected override void OnApply()
{
base.OnApply();
path.FadeColour = colour.ForHitResult(HitResult.Great);
HighlightHardBeats.BindValueChanged(value => path.Texture = value.NewValue && HitObject.IsHard ? pathTextureHighlighted : pathTexture, true);
totalTimeHeld = 0;
if (properties.InverseModEnabled.Value)
{
inversed = true;
maskingContainer.Masking = false;
path.Reverse = inversed;
// PathDistance = path.PathDistance = TauPlayfield.BaseSize.X;
}
}
protected override void OnFree()
{
base.OnFree();
trackingCheckpoints.Clear();
slidingSample?.ClearSamples();
HighlightHardBeats.UnbindEvents();
}
protected override void LoadSamples()
{
// Note: base.LoadSamples() isn't called since the slider plays the tail's hitsounds for the time being.
Samples.Samples = HitObject.TailSamples.Cast<ISampleInfo>().ToArray();
slidingSample.Samples = HitObject.CreateSlidingSamples().Cast<ISampleInfo>().ToArray();
}
public override void StopAllSamples()
{
base.StopAllSamples();
slidingSample?.Stop();
}
private void updateSlidingSample(ValueChangedEvent<bool> tracking)
{
if (tracking.NewValue)
slidingSample?.Play();
else
slidingSample?.Stop();
}
public BindableBool Tracking = new();
private const double tracking_checkpoint_interval = 5;
private readonly List<bool> trackingCheckpoints = new();
protected override void Update()
{
base.Update();
// This gives us about the same performance as if we were to just would update the path without this.
// The catch is that with this, we're giving the Update thread more breathing room to update everything
// else instead of worrying with updating the path vertices every update frame.
if (!drawCache.IsValid)
{
updatePath();
drawCache.Validate();
}
if (Time.Current < HitObject.StartTime || Time.Current >= HitObject.GetEndTime()) return;
// ReSharper disable once AssignmentInConditionalExpression
if (Tracking.Value = checkIfTracking())
{
totalTimeHeld += Time.Elapsed;
}
int trackingCheckpointIndex = (int)((Time.Current - HitObject.StartTime) / tracking_checkpoint_interval);
if (trackingCheckpointIndex >= 0)
{
while (trackingCheckpoints.Count <= trackingCheckpointIndex)
trackingCheckpoints.Add(trackingCheckpoints.Count == 0 ? Tracking.Value : trackingCheckpoints[^1]);
trackingCheckpoints[trackingCheckpointIndex] = Tracking.Value;
}
}
protected override void UpdateInitialTransforms()
{
base.UpdateInitialTransforms();
this.FadeInFromZero(HitObject.TimeFadeIn);
}
protected override void CheckForResult(bool userTriggered, double timeOffset)
{
Debug.Assert(HitObject.HitWindows != null);
if (!(Time.Current > HitObject.GetEndTime())) return;
double percentage = totalTimeHeld / HitObject.Duration;
var result = percentage switch
{
> .85 => HitResult.Great,
> .50 => HitResult.Ok,
_ => HitResult.Miss
};
// Some nested hitobjects may not be judged before the tail, so we need to make sure that we have them all judged beforehand.
// Thanks osu!.
// ~ Nora
foreach (var nested in NestedHitObjects.Where(n => !n.AllJudged))
{
if (nested is ICanApplyResult res)
res.ForcefullyApplyResult(result);
}
ApplyResult(result);
}
protected override JudgementResult CreateResult(Judgement judgement)
=> new TauJudgementResult(HitObject, judgement);
[Resolved]
private OsuColour colour { get; set; }
public double Velocity => PathDistance / HitObject.TimePreempt;
public double FadeTime => FADE_RANGE / Velocity;
protected override void UpdateHitStateTransforms(ArmedState state)
{
base.UpdateHitStateTransforms(state);
if (state is ArmedState.Hit or ArmedState.Miss)
LifetimeEnd = Time.Current + FadeTime;
else
LifetimeStart = HitObject.StartTime - HitObject.TimePreempt;
}
}
}