// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using osu.Framework.Bindables; using osu.Framework.Graphics.Performance; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Objects.Drawables; namespace osu.Game.Rulesets.Objects { /// /// A that stores the lifetime for a . /// public class HitObjectLifetimeEntry : LifetimeEntry { /// /// The . /// public readonly HitObject HitObject; /// /// The list of for the 's nested objects (if any). /// public readonly List NestedEntries = new List(); /// /// The result that was judged with. /// This is set by the accompanying , and reused when required for rewinding. /// internal JudgementResult? Result; private readonly IBindable startTimeBindable = new BindableDouble(); internal event Action? RevertResult; /// /// Creates a new . /// /// The to store the lifetime of. public HitObjectLifetimeEntry(HitObject hitObject) { HitObject = hitObject; startTimeBindable.BindTo(HitObject.StartTimeBindable); startTimeBindable.BindValueChanged(_ => SetInitialLifetime(), true); // Subscribe to this event before the DrawableHitObject so that the local callback is invoked before the entry is re-applied as a result of DefaultsApplied. // This way, the DrawableHitObject can use OnApply() to overwrite the LifetimeStart that was set inside setInitialLifetime(). HitObject.DefaultsApplied += _ => SetInitialLifetime(); } // The lifetime, as set by the hitobject. private double realLifetimeStart = double.MinValue; private double realLifetimeEnd = double.MaxValue; // This method is called even if `start == LifetimeStart` when `KeepAlive` is true (necessary to update `realLifetimeStart`). protected override void SetLifetimeStart(double start) { realLifetimeStart = start; if (!keepAlive) base.SetLifetimeStart(start); } protected override void SetLifetimeEnd(double end) { realLifetimeEnd = end; if (!keepAlive) base.SetLifetimeEnd(end); } private bool keepAlive; /// /// Whether the should be kept always alive. /// internal bool KeepAlive { set { if (keepAlive == value) return; keepAlive = value; if (keepAlive) SetLifetime(double.MinValue, double.MaxValue); else SetLifetime(realLifetimeStart, realLifetimeEnd); } } /// /// A safe offset prior to the start time of at which it may begin displaying contents. /// By default, s are assumed to display their contents within 10 seconds prior to their start time. /// /// /// This is only used as an optimisation to delay the initial application of the to a . /// A more accurate should be set on the hit object application, for further optimisation. /// protected virtual double InitialLifetimeOffset => 10000; /// /// Set using . /// internal void SetInitialLifetime() => LifetimeStart = HitObject.StartTime - InitialLifetimeOffset; internal void OnRevertResult() => RevertResult?.Invoke(); } }