1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 10:33:30 +08:00

Merge pull request #13257 from ekrctb/simplify-shoc

Multiple `ScrollingHitObjectContainer` logic simplification
This commit is contained in:
Dan Balasescu 2021-05-31 21:31:10 +09:00 committed by GitHub
commit bd7814b487
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 92 deletions

View File

@ -122,18 +122,20 @@ namespace osu.Game.Rulesets.UI
var entry = (HitObjectLifetimeEntry)lifetimeEntry;
Debug.Assert(!aliveDrawableMap.ContainsKey(entry));
bool isNonPooled = nonPooledDrawableMap.TryGetValue(entry, out var drawable);
bool isPooled = !nonPooledDrawableMap.TryGetValue(entry, out var drawable);
drawable ??= pooledObjectProvider?.GetPooledDrawableRepresentation(entry.HitObject, null);
if (drawable == null)
throw new InvalidOperationException($"A drawable representation could not be retrieved for hitobject type: {entry.HitObject.GetType().ReadableName()}.");
aliveDrawableMap[entry] = drawable;
if (isPooled)
{
addDrawable(drawable);
HitObjectUsageBegan?.Invoke(entry.HitObject);
}
OnAdd(drawable);
if (isNonPooled) return;
addDrawable(drawable);
HitObjectUsageBegan?.Invoke(entry.HitObject);
}
private void entryBecameDead(LifetimeEntry lifetimeEntry)
@ -142,17 +144,18 @@ namespace osu.Game.Rulesets.UI
Debug.Assert(aliveDrawableMap.ContainsKey(entry));
var drawable = aliveDrawableMap[entry];
bool isNonPooled = nonPooledDrawableMap.ContainsKey(entry);
bool isPooled = !nonPooledDrawableMap.ContainsKey(entry);
drawable.OnKilled();
aliveDrawableMap.Remove(entry);
if (isPooled)
{
removeDrawable(drawable);
HitObjectUsageFinished?.Invoke(entry.HitObject);
}
OnRemove(drawable);
if (isNonPooled) return;
removeDrawable(drawable);
// The hit object is not freed when the DHO was not pooled.
HitObjectUsageFinished?.Invoke(entry.HitObject);
}
private void addDrawable(DrawableHitObject drawable)
@ -211,21 +214,16 @@ namespace osu.Game.Rulesets.UI
#endregion
/// <summary>
/// Invoked when a <see cref="DrawableHitObject"/> is added to this container.
/// Invoked after a <see cref="DrawableHitObject"/> is added to this container.
/// </summary>
/// <remarks>
/// This method is not invoked for nested <see cref="DrawableHitObject"/>s.
/// </remarks>
protected virtual void OnAdd(DrawableHitObject drawableHitObject)
{
Debug.Assert(drawableHitObject.LoadState >= LoadState.Ready);
}
/// <summary>
/// Invoked when a <see cref="DrawableHitObject"/> is removed from this container.
/// Invoked after a <see cref="DrawableHitObject"/> is removed from this container.
/// </summary>
/// <remarks>
/// This method is not invoked for nested <see cref="DrawableHitObject"/>s.
/// </remarks>
protected virtual void OnRemove(DrawableHitObject drawableHitObject)
{
}

View File

@ -18,12 +18,7 @@ namespace osu.Game.Rulesets.UI.Scrolling
private readonly IBindable<ScrollingDirection> direction = new Bindable<ScrollingDirection>();
/// <summary>
/// Hit objects which require lifetime computation in the next update call.
/// </summary>
private readonly HashSet<DrawableHitObject> toComputeLifetime = new HashSet<DrawableHitObject>();
/// <summary>
/// A set containing all <see cref="HitObjectContainer.AliveObjects"/> which have an up-to-date layout.
/// A set of top-level <see cref="DrawableHitObject"/>s which have an up-to-date layout.
/// </summary>
private readonly HashSet<DrawableHitObject> layoutComputed = new HashSet<DrawableHitObject>();
@ -54,7 +49,6 @@ namespace osu.Game.Rulesets.UI.Scrolling
{
base.Clear();
toComputeLifetime.Clear();
layoutComputed.Clear();
}
@ -83,7 +77,7 @@ namespace osu.Game.Rulesets.UI.Scrolling
flipPositionIfRequired(ref position);
return scrollingInfo.Algorithm.TimeAt(position, Time.Current, scrollingInfo.TimeRange.Value, getLength());
return scrollingInfo.Algorithm.TimeAt(position, Time.Current, scrollingInfo.TimeRange.Value, scrollLength);
}
/// <summary>
@ -91,7 +85,7 @@ namespace osu.Game.Rulesets.UI.Scrolling
/// </summary>
public Vector2 ScreenSpacePositionAtTime(double time)
{
var pos = scrollingInfo.Algorithm.PositionAt(time, Time.Current, scrollingInfo.TimeRange.Value, getLength());
var pos = scrollingInfo.Algorithm.PositionAt(time, Time.Current, scrollingInfo.TimeRange.Value, scrollLength);
flipPositionIfRequired(ref pos);
@ -106,16 +100,19 @@ namespace osu.Game.Rulesets.UI.Scrolling
}
}
private float getLength()
private float scrollLength
{
switch (scrollingInfo.Direction.Value)
get
{
case ScrollingDirection.Left:
case ScrollingDirection.Right:
return DrawWidth;
switch (scrollingInfo.Direction.Value)
{
case ScrollingDirection.Left:
case ScrollingDirection.Right:
return DrawWidth;
default:
return DrawHeight;
default:
return DrawHeight;
}
}
}
@ -150,81 +147,40 @@ namespace osu.Game.Rulesets.UI.Scrolling
}
}
protected override void OnAdd(DrawableHitObject drawableHitObject) => onAddRecursive(drawableHitObject);
protected override void OnRemove(DrawableHitObject drawableHitObject) => onRemoveRecursive(drawableHitObject);
private void onAddRecursive(DrawableHitObject hitObject)
protected override void OnAdd(DrawableHitObject drawableHitObject)
{
invalidateHitObject(hitObject);
hitObject.DefaultsApplied += invalidateHitObject;
foreach (var nested in hitObject.NestedHitObjects)
onAddRecursive(nested);
invalidateHitObject(drawableHitObject);
drawableHitObject.DefaultsApplied += invalidateHitObject;
}
private void onRemoveRecursive(DrawableHitObject hitObject)
protected override void OnRemove(DrawableHitObject drawableHitObject)
{
toComputeLifetime.Remove(hitObject);
layoutComputed.Remove(hitObject);
layoutComputed.Remove(drawableHitObject);
hitObject.DefaultsApplied -= invalidateHitObject;
foreach (var nested in hitObject.NestedHitObjects)
onRemoveRecursive(nested);
drawableHitObject.DefaultsApplied -= invalidateHitObject;
}
/// <summary>
/// Make this <see cref="DrawableHitObject"/> lifetime and layout computed in next update.
/// </summary>
private void invalidateHitObject(DrawableHitObject hitObject)
{
// Lifetime computation is delayed until next update because
// when the hit object is not pooled this container is not loaded here and `scrollLength` cannot be computed.
toComputeLifetime.Add(hitObject);
hitObject.LifetimeStart = computeOriginAdjustedLifetimeStart(hitObject);
layoutComputed.Remove(hitObject);
}
private float scrollLength;
protected override void Update()
{
base.Update();
if (!layoutCache.IsValid)
if (layoutCache.IsValid) return;
foreach (var hitObject in Objects)
{
toComputeLifetime.Clear();
foreach (var hitObject in Objects)
{
if (hitObject.HitObject != null)
toComputeLifetime.Add(hitObject);
}
layoutComputed.Clear();
scrollingInfo.Algorithm.Reset();
switch (direction.Value)
{
case ScrollingDirection.Up:
case ScrollingDirection.Down:
scrollLength = DrawSize.Y;
break;
default:
scrollLength = DrawSize.X;
break;
}
layoutCache.Validate();
if (hitObject.HitObject != null)
invalidateHitObject(hitObject);
}
foreach (var hitObject in toComputeLifetime)
hitObject.LifetimeStart = computeOriginAdjustedLifetimeStart(hitObject);
scrollingInfo.Algorithm.Reset();
toComputeLifetime.Clear();
layoutCache.Validate();
}
protected override void UpdateAfterChildrenLife()