// 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 System.Diagnostics; using System.Linq; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Extensions.TypeExtensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Performance; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Objects.Drawables; namespace osu.Game.Rulesets.UI { public class HitObjectContainer : LifetimeManagementContainer { /// /// All currently in-use s. /// public IEnumerable Objects => InternalChildren.Cast().OrderBy(h => h.HitObject.StartTime); /// /// All currently in-use s that are alive. /// /// /// If this uses pooled objects, this is equivalent to . /// public IEnumerable AliveObjects => AliveInternalChildren.Cast().OrderBy(h => h.HitObject.StartTime); /// /// Invoked when a is judged. /// public event Action NewResult; /// /// Invoked when a judgement is reverted. /// public event Action RevertResult; /// /// Invoked when a becomes used by a . /// /// /// If this uses pooled objects, this represents the time when the s become alive. /// public event Action HitObjectUsageBegan; /// /// Invoked when a becomes unused by a . /// /// /// If this uses pooled objects, this represents the time when the s become dead. /// public event Action HitObjectUsageFinished; private readonly Dictionary startTimeMap = new Dictionary(); private readonly Dictionary drawableMap = new Dictionary(); private readonly LifetimeEntryManager lifetimeManager = new LifetimeEntryManager(); [Resolved(CanBeNull = true)] private DrawableRuleset drawableRuleset { get; set; } public HitObjectContainer() { RelativeSizeAxes = Axes.Both; lifetimeManager.EntryBecameAlive += entryBecameAlive; lifetimeManager.EntryBecameDead += entryBecameDead; } protected override void LoadAsyncComplete() { base.LoadAsyncComplete(); // Application of hitobjects during load() may have changed their start times, so ensure the correct sorting order. SortInternal(); } #region Pooling support public void Add(HitObjectLifetimeEntry entry) => lifetimeManager.AddEntry(entry); public bool Remove(HitObjectLifetimeEntry entry) => lifetimeManager.RemoveEntry(entry); private void entryBecameAlive(LifetimeEntry entry) => addDrawable((HitObjectLifetimeEntry)entry); private void entryBecameDead(LifetimeEntry entry) => removeDrawable((HitObjectLifetimeEntry)entry); private void addDrawable(HitObjectLifetimeEntry entry) { Debug.Assert(!drawableMap.ContainsKey(entry)); var drawable = drawableRuleset.GetPooledDrawableRepresentation(entry.HitObject); if (drawable == null) throw new InvalidOperationException($"A drawable representation could not be retrieved for hitobject type: {entry.HitObject.GetType().ReadableName()}."); drawable.OnNewResult += onNewResult; drawable.OnRevertResult += onRevertResult; bindStartTime(drawable); AddInternal(drawableMap[entry] = drawable, false); HitObjectUsageBegan?.Invoke(entry.HitObject); } private void removeDrawable(HitObjectLifetimeEntry entry) { Debug.Assert(drawableMap.ContainsKey(entry)); var drawable = drawableMap[entry]; drawable.OnNewResult -= onNewResult; drawable.OnRevertResult -= onRevertResult; drawable.OnKilled(); drawableMap.Remove(entry); unbindStartTime(drawable); RemoveInternal(drawable); HitObjectUsageFinished?.Invoke(entry.HitObject); } #endregion #region Non-pooling support public virtual void Add(DrawableHitObject hitObject) { bindStartTime(hitObject); hitObject.OnNewResult += onNewResult; hitObject.OnRevertResult += onRevertResult; AddInternal(hitObject); } public virtual bool Remove(DrawableHitObject hitObject) { if (!RemoveInternal(hitObject)) return false; hitObject.OnNewResult -= onNewResult; hitObject.OnRevertResult -= onRevertResult; unbindStartTime(hitObject); return true; } public int IndexOf(DrawableHitObject hitObject) => IndexOfInternal(hitObject); protected override void OnChildLifetimeBoundaryCrossed(LifetimeBoundaryCrossedEvent e) { if (!(e.Child is DrawableHitObject hitObject)) return; if ((e.Kind == LifetimeBoundaryKind.End && e.Direction == LifetimeBoundaryCrossingDirection.Forward) || (e.Kind == LifetimeBoundaryKind.Start && e.Direction == LifetimeBoundaryCrossingDirection.Backward)) { hitObject.OnKilled(); } } #endregion public virtual void Clear(bool disposeChildren = true) { lifetimeManager.ClearEntries(); ClearInternal(disposeChildren); unbindAllStartTimes(); } protected override bool CheckChildrenLife() { bool aliveChanged = base.CheckChildrenLife(); aliveChanged |= lifetimeManager.Update(Time.Current, Time.Current); return aliveChanged; } private void onNewResult(DrawableHitObject d, JudgementResult r) => NewResult?.Invoke(d, r); private void onRevertResult(DrawableHitObject d, JudgementResult r) => RevertResult?.Invoke(d, r); #region Comparator + StartTime tracking private void bindStartTime(DrawableHitObject hitObject) { var bindable = hitObject.StartTimeBindable.GetBoundCopy(); bindable.BindValueChanged(_ => { if (LoadState >= LoadState.Ready) SortInternal(); }); startTimeMap[hitObject] = bindable; } private void unbindStartTime(DrawableHitObject hitObject) { startTimeMap[hitObject].UnbindAll(); startTimeMap.Remove(hitObject); } private void unbindAllStartTimes() { foreach (var kvp in startTimeMap) kvp.Value.UnbindAll(); startTimeMap.Clear(); } protected override int Compare(Drawable x, Drawable y) { if (!(x is DrawableHitObject xObj) || !(y is DrawableHitObject yObj)) return base.Compare(x, y); // Put earlier hitobjects towards the end of the list, so they handle input first int i = yObj.HitObject.StartTime.CompareTo(xObj.HitObject.StartTime); return i == 0 ? CompareReverseChildID(x, y) : i; } #endregion protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); unbindAllStartTimes(); } } }