// 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.Linq; using JetBrains.Annotations; using osu.Framework.Graphics; using osu.Game.Rulesets.Objects.Drawables; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Extensions.IEnumerableExtensions; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Pooling; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Mods; using osu.Game.Rulesets.Objects; using osuTK; namespace osu.Game.Rulesets.UI { [Cached(typeof(IPooledHitObjectProvider))] public abstract class Playfield : CompositeDrawable, IPooledHitObjectProvider { /// /// Invoked when a is judged. /// public event Action NewResult; /// /// Invoked when a judgement is reverted. /// public event Action RevertResult; /// /// The contained in this Playfield. /// public HitObjectContainer HitObjectContainer => hitObjectContainerLazy.Value; private readonly Lazy hitObjectContainerLazy; /// /// A function that converts gamefield coordinates to screen space. /// public Func GamefieldToScreenSpace => HitObjectContainer.ToScreenSpace; /// /// A function that converts screen space coordinates to gamefield. /// public Func ScreenSpaceToGamefield => HitObjectContainer.ToLocalSpace; /// /// All the s contained in this and all . /// public IEnumerable AllHitObjects { get { if (HitObjectContainer == null) return Enumerable.Empty(); var enumerable = HitObjectContainer.Objects; if (nestedPlayfields.IsValueCreated) enumerable = enumerable.Concat(NestedPlayfields.SelectMany(p => p.AllHitObjects)); return enumerable; } } /// /// All s nested inside this . /// public IEnumerable NestedPlayfields => nestedPlayfields.IsValueCreated ? nestedPlayfields.Value : Enumerable.Empty(); private readonly Lazy> nestedPlayfields = new Lazy>(); /// /// Whether judgements should be displayed by this and and all nested s. /// public readonly BindableBool DisplayJudgements = new BindableBool(true); /// /// Creates a new . /// protected Playfield() { RelativeSizeAxes = Axes.Both; hitObjectContainerLazy = new Lazy(() => CreateHitObjectContainer().With(h => { h.NewResult += (d, r) => NewResult?.Invoke(d, r); h.RevertResult += (d, r) => RevertResult?.Invoke(d, r); h.HitObjectUsageBegan += o => HitObjectUsageBegan?.Invoke(o); h.HitObjectUsageFinished += o => HitObjectUsageFinished?.Invoke(o); })); } [Resolved(CanBeNull = true)] private IReadOnlyList mods { get; set; } [BackgroundDependencyLoader] private void load() { Cursor = CreateCursor(); if (Cursor != null) { // initial showing of the cursor will be handed by MenuCursorContainer (via DrawableRuleset's IProvideCursor implementation). Cursor.Hide(); AddInternal(Cursor); } } /// /// Performs post-processing tasks (if any) after all DrawableHitObjects are loaded into this Playfield. /// public virtual void PostProcess() => NestedPlayfields.ForEach(p => p.PostProcess()); /// /// Adds a DrawableHitObject to this Playfield. /// /// The DrawableHitObject to add. public virtual void Add(DrawableHitObject h) { HitObjectContainer.Add(h); OnHitObjectAdded(h.HitObject); } /// /// Remove a DrawableHitObject from this Playfield. /// /// The DrawableHitObject to remove. public virtual bool Remove(DrawableHitObject h) { if (!HitObjectContainer.Remove(h)) return false; OnHitObjectRemoved(h.HitObject); return false; } /// /// Invoked when a is added to this . /// /// The added . protected virtual void OnHitObjectAdded(HitObject hitObject) { } /// /// Invoked when a is removed from this . /// /// The removed . protected virtual void OnHitObjectRemoved(HitObject hitObject) { } /// /// The cursor currently being used by this . May be null if no cursor is provided. /// public GameplayCursorContainer Cursor { get; private set; } /// /// Provide a cursor which is to be used for gameplay. /// /// /// The default provided cursor is invisible when inside the bounds of the . /// /// The cursor, or null to show the menu cursor. protected virtual GameplayCursorContainer CreateCursor() => new InvisibleCursorContainer(); /// /// Registers a as a nested . /// This does not add the to the draw hierarchy. /// /// The to add. protected void AddNested(Playfield otherPlayfield) { otherPlayfield.DisplayJudgements.BindTo(DisplayJudgements); otherPlayfield.NewResult += (d, r) => NewResult?.Invoke(d, r); otherPlayfield.RevertResult += (d, r) => RevertResult?.Invoke(d, r); otherPlayfield.HitObjectUsageBegan += h => HitObjectUsageBegan?.Invoke(h); otherPlayfield.HitObjectUsageFinished += h => HitObjectUsageFinished?.Invoke(h); nestedPlayfields.Value.Add(otherPlayfield); } protected override void LoadComplete() { base.LoadComplete(); // in the case a consumer forgets to add the HitObjectContainer, we will add it here. if (HitObjectContainer.Parent == null) AddInternal(HitObjectContainer); } protected override void Update() { base.Update(); if (mods != null) { foreach (var mod in mods) { if (mod is IUpdatableByPlayfield updatable) updatable.Update(this); } } } /// /// Creates the container that will be used to contain the s. /// protected virtual HitObjectContainer CreateHitObjectContainer() => new HitObjectContainer(); #region Pooling support private readonly Dictionary pools = new Dictionary(); /// /// Adds a for a pooled to this . /// /// public virtual void Add(HitObject hitObject) { var entry = CreateLifetimeEntry(hitObject); lifetimeEntryMap[entry.HitObject] = entry; HitObjectContainer.Add(entry); OnHitObjectAdded(entry.HitObject); } /// /// Removes a for a pooled from this . /// /// /// Whether the was successfully removed. public virtual bool Remove(HitObject hitObject) { if (lifetimeEntryMap.Remove(hitObject, out var entry)) { HitObjectContainer.Remove(entry); OnHitObjectRemoved(hitObject); return true; } bool removedFromNested = false; if (nestedPlayfields.IsValueCreated) removedFromNested = nestedPlayfields.Value.Any(p => p.Remove(hitObject)); return removedFromNested; } /// /// Creates the for a given . /// /// /// This may be overridden to provide custom lifetime control (e.g. via . /// /// The to create the entry for. /// The . [NotNull] protected virtual HitObjectLifetimeEntry CreateLifetimeEntry([NotNull] HitObject hitObject) => new HitObjectLifetimeEntry(hitObject); /// /// Registers a default pool with this which is to be used whenever /// representations are requested for the given type. /// /// The number of s to be initially stored in the pool. /// /// The maximum number of s that can be stored in the pool. /// If this limit is exceeded, every subsequent will be created anew instead of being retrieved from the pool, /// until some of the existing s are returned to the pool. /// /// The type. /// The receiver for s. protected void RegisterPool(int initialSize, int? maximumSize = null) where TObject : HitObject where TDrawable : DrawableHitObject, new() => RegisterPool(new DrawablePool(initialSize, maximumSize)); /// /// Registers a custom pool with this which is to be used whenever /// representations are requested for the given type. /// /// The to register. /// The type. /// The receiver for s. protected void RegisterPool([NotNull] DrawablePool pool) where TObject : HitObject where TDrawable : DrawableHitObject, new() { pools[typeof(TObject)] = pool; AddInternal(pool); } DrawableHitObject IPooledHitObjectProvider.GetPooledDrawableRepresentation(HitObject hitObject) { var lookupType = hitObject.GetType(); IDrawablePool pool; // Tests may add derived hitobject instances for which pools don't exist. Try to find any applicable pool and dynamically assign the type if the pool exists. if (!pools.TryGetValue(lookupType, out pool)) { foreach (var (t, p) in pools) { if (!t.IsInstanceOfType(hitObject)) continue; pools[lookupType] = pool = p; break; } } return (DrawableHitObject)pool?.Get(d => { var dho = (DrawableHitObject)d; // If this is the first time this DHO is being used (not loaded), then apply the DHO mods. // This is done before Apply() so that the state is updated once when the hitobject is applied. if (!dho.IsLoaded) { foreach (var m in mods.OfType()) m.ApplyToDrawableHitObjects(dho.Yield()); } if (!lifetimeEntryMap.TryGetValue(hitObject, out var entry)) lifetimeEntryMap[hitObject] = entry = CreateLifetimeEntry(hitObject); dho.Apply(hitObject, entry); }); } #endregion #region Editor logic /// /// Invoked when a becomes used by a . /// /// /// If this uses pooled objects, this represents the time when the s become alive. /// internal event Action HitObjectUsageBegan; /// /// Invoked when a becomes unused by a . /// /// /// If this uses pooled objects, this represents the time when the s become dead. /// internal event Action HitObjectUsageFinished; private readonly Dictionary lifetimeEntryMap = new Dictionary(); /// /// Sets whether to keep a given always alive within this or any nested . /// /// The to set. /// Whether to keep always alive. internal void SetKeepAlive(HitObject hitObject, bool keepAlive) { if (lifetimeEntryMap.TryGetValue(hitObject, out var entry)) { entry.KeepAlive = keepAlive; return; } if (!nestedPlayfields.IsValueCreated) return; foreach (var p in nestedPlayfields.Value) p.SetKeepAlive(hitObject, keepAlive); } /// /// Keeps all s alive within this and all nested s. /// internal void KeepAllAlive() { foreach (var (_, entry) in lifetimeEntryMap) entry.KeepAlive = true; if (!nestedPlayfields.IsValueCreated) return; foreach (var p in nestedPlayfields.Value) p.KeepAllAlive(); } /// /// The amount of time prior to the current time within which s should be considered alive. /// internal double PastLifetimeExtension { get => HitObjectContainer.PastLifetimeExtension; set { HitObjectContainer.PastLifetimeExtension = value; if (!nestedPlayfields.IsValueCreated) return; foreach (var nested in nestedPlayfields.Value) nested.PastLifetimeExtension = value; } } /// /// The amount of time after the current time within which s should be considered alive. /// internal double FutureLifetimeExtension { get => HitObjectContainer.FutureLifetimeExtension; set { HitObjectContainer.FutureLifetimeExtension = value; if (!nestedPlayfields.IsValueCreated) return; foreach (var nested in nestedPlayfields.Value) nested.FutureLifetimeExtension = value; } } #endregion public class InvisibleCursorContainer : GameplayCursorContainer { protected override Drawable CreateCursor() => new InvisibleCursor(); private class InvisibleCursor : Drawable { } } } }