1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-15 15:12:57 +08:00

Simplify ScrollingHitObjectContainer logic

This commit is contained in:
ekrctb 2020-11-24 13:34:09 +09:00
parent 5c743adbae
commit 8f39b54e58
2 changed files with 22 additions and 56 deletions

View File

@ -2,13 +2,10 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic; using System.Collections.Generic;
using JetBrains.Annotations;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Caching;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Layout; using osu.Framework.Layout;
using osu.Framework.Threading;
using osu.Game.Rulesets.Objects.Drawables; using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Objects.Types; using osu.Game.Rulesets.Objects.Types;
using osuTK; using osuTK;
@ -19,7 +16,6 @@ namespace osu.Game.Rulesets.UI.Scrolling
{ {
private readonly IBindable<double> timeRange = new BindableDouble(); private readonly IBindable<double> timeRange = new BindableDouble();
private readonly IBindable<ScrollingDirection> direction = new Bindable<ScrollingDirection>(); private readonly IBindable<ScrollingDirection> direction = new Bindable<ScrollingDirection>();
private readonly Dictionary<DrawableHitObject, InitialState> hitObjectInitialStateCache = new Dictionary<DrawableHitObject, InitialState>();
[Resolved] [Resolved]
private IScrollingInfo scrollingInfo { get; set; } private IScrollingInfo scrollingInfo { get; set; }
@ -27,9 +23,7 @@ namespace osu.Game.Rulesets.UI.Scrolling
// Responds to changes in the layout. When the layout changes, all hit object states must be recomputed. // Responds to changes in the layout. When the layout changes, all hit object states must be recomputed.
private readonly LayoutValue layoutCache = new LayoutValue(Invalidation.RequiredParentSizeToFit | Invalidation.DrawInfo); private readonly LayoutValue layoutCache = new LayoutValue(Invalidation.RequiredParentSizeToFit | Invalidation.DrawInfo);
// A combined cache across all hit object states to reduce per-update iterations. private readonly HashSet<DrawableHitObject> invalidHitObjects = new HashSet<DrawableHitObject>();
// When invalidated, one or more (but not necessarily all) hitobject states must be re-validated.
private readonly Cached combinedObjCache = new Cached();
public ScrollingHitObjectContainer() public ScrollingHitObjectContainer()
{ {
@ -52,8 +46,7 @@ namespace osu.Game.Rulesets.UI.Scrolling
{ {
base.Clear(disposeChildren); base.Clear(disposeChildren);
combinedObjCache.Invalidate(); invalidHitObjects.Clear();
hitObjectInitialStateCache.Clear();
} }
/// <summary> /// <summary>
@ -150,17 +143,18 @@ namespace osu.Game.Rulesets.UI.Scrolling
/// <summary> /// <summary>
/// Invalidate the cache of the layout of this hit object. /// Invalidate the cache of the layout of this hit object.
/// A hit object should be invalidated after all its nested hit objects are invalidated.
/// </summary> /// </summary>
public void InvalidateDrawableHitObject(DrawableHitObject drawableObject) public void InvalidateDrawableHitObject(DrawableHitObject drawableObject)
{ {
if (hitObjectInitialStateCache.TryGetValue(drawableObject, out var state)) invalidHitObjects.Add(drawableObject);
state.Cache.Invalidate();
combinedObjCache.Invalidate(); // Remove children as nested hit objects will be recursively updated.
foreach (var nested in drawableObject.NestedHitObjects)
invalidHitObjects.Remove(nested);
} }
// Use a nonzero value to prevent infinite results private float scrollLength;
private float scrollLength = 1;
protected override void Update() protected override void Update()
{ {
@ -168,17 +162,11 @@ namespace osu.Game.Rulesets.UI.Scrolling
if (!layoutCache.IsValid) if (!layoutCache.IsValid)
{ {
foreach (var state in hitObjectInitialStateCache.Values) foreach (var obj in Objects)
state.Cache.Invalidate(); invalidHitObjects.Add(obj);
combinedObjCache.Invalidate();
scrollingInfo.Algorithm.Reset(); scrollingInfo.Algorithm.Reset();
layoutCache.Validate();
}
if (!combinedObjCache.IsValid)
{
switch (direction.Value) switch (direction.Value)
{ {
case ScrollingDirection.Up: case ScrollingDirection.Up:
@ -191,24 +179,16 @@ namespace osu.Game.Rulesets.UI.Scrolling
break; break;
} }
foreach (var obj in Objects) layoutCache.Validate();
}
foreach (var obj in invalidHitObjects)
{ {
if (!hitObjectInitialStateCache.TryGetValue(obj, out var state)) computeInitialStateRecursive(obj);
state = hitObjectInitialStateCache[obj] = new InitialState(new Cached());
if (state.Cache.IsValid)
continue;
state.ScheduledComputation?.Cancel();
state.ScheduledComputation = computeInitialStateRecursive(obj);
computeLifetimeStartRecursive(obj); computeLifetimeStartRecursive(obj);
state.Cache.Validate();
} }
combinedObjCache.Validate(); invalidHitObjects.Clear();
}
} }
private void computeLifetimeStartRecursive(DrawableHitObject hitObject) private void computeLifetimeStartRecursive(DrawableHitObject hitObject)
@ -247,7 +227,7 @@ namespace osu.Game.Rulesets.UI.Scrolling
return scrollingInfo.Algorithm.GetDisplayStartTime(hitObject.HitObject.StartTime, originAdjustment, timeRange.Value, scrollLength); return scrollingInfo.Algorithm.GetDisplayStartTime(hitObject.HitObject.StartTime, originAdjustment, timeRange.Value, scrollLength);
} }
private ScheduledDelegate computeInitialStateRecursive(DrawableHitObject hitObject) => hitObject.Schedule(() => private void computeInitialStateRecursive(DrawableHitObject hitObject)
{ {
if (hitObject.HitObject is IHasDuration e) if (hitObject.HitObject is IHasDuration e)
{ {
@ -272,7 +252,7 @@ namespace osu.Game.Rulesets.UI.Scrolling
// Nested hitobjects don't need to scroll, but they do need accurate positions // Nested hitobjects don't need to scroll, but they do need accurate positions
updatePosition(obj, hitObject.HitObject.StartTime); updatePosition(obj, hitObject.HitObject.StartTime);
} }
}); }
protected override void UpdateAfterChildrenLife() protected override void UpdateAfterChildrenLife()
{ {
@ -304,19 +284,5 @@ namespace osu.Game.Rulesets.UI.Scrolling
break; break;
} }
} }
private class InitialState
{
[NotNull]
public readonly Cached Cache;
[CanBeNull]
public ScheduledDelegate ScheduledComputation;
public InitialState(Cached cache)
{
Cache = cache;
}
}
} }
} }