2019-01-24 16:43:03 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2018-07-17 14:48:51 +08:00
using System ;
2018-01-15 19:22:41 +08:00
using System.Collections.Generic ;
2022-03-14 16:19:23 +08:00
using System.Diagnostics ;
2018-07-17 14:48:51 +08:00
using System.Linq ;
2020-11-13 23:54:57 +08:00
using JetBrains.Annotations ;
2017-03-21 13:53:55 +08:00
using osu.Framework.Allocation ;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables ;
2018-07-17 14:48:51 +08:00
using osu.Framework.Extensions.IEnumerableExtensions ;
2022-03-14 16:19:23 +08:00
using osu.Framework.Graphics ;
2020-11-13 23:54:57 +08:00
using osu.Framework.Graphics.Containers ;
using osu.Framework.Graphics.Pooling ;
2020-11-19 18:51:09 +08:00
using osu.Game.Audio ;
2020-11-10 22:32:30 +08:00
using osu.Game.Rulesets.Judgements ;
2018-08-03 20:03:11 +08:00
using osu.Game.Rulesets.Mods ;
2020-11-10 22:32:30 +08:00
using osu.Game.Rulesets.Objects ;
2022-03-14 16:19:23 +08:00
using osu.Game.Rulesets.Objects.Drawables ;
2020-11-19 18:51:09 +08:00
using osu.Game.Skinning ;
2018-11-20 15:51:59 +08:00
using osuTK ;
2021-06-15 18:41:37 +08:00
using osu.Game.Rulesets.Objects.Pooling ;
2023-01-19 18:43:23 +08:00
using osu.Framework.Extensions.ObjectExtensions ;
2023-07-28 14:47:57 +08:00
using osu.Framework.Graphics.Primitives ;
2018-04-13 17:19:50 +08:00
2017-04-18 15:05:58 +08:00
namespace osu.Game.Rulesets.UI
2016-09-02 19:30:27 +08:00
{
2020-11-13 23:54:57 +08:00
[Cached(typeof(IPooledHitObjectProvider))]
2020-11-19 18:51:09 +08:00
[Cached(typeof(IPooledSampleProvider))]
2023-07-05 17:41:32 +08:00
[Cached]
2020-11-19 18:51:09 +08:00
public abstract partial class Playfield : CompositeDrawable , IPooledHitObjectProvider , IPooledSampleProvider
2016-09-02 19:30:27 +08:00
{
2020-11-10 22:32:30 +08:00
/// <summary>
/// Invoked when a <see cref="DrawableHitObject"/> is judged.
/// </summary>
public event Action < DrawableHitObject , JudgementResult > NewResult ;
/// <summary>
2023-01-19 18:43:23 +08:00
/// Invoked when a judgement result is reverted.
2020-11-10 22:32:30 +08:00
/// </summary>
2023-01-19 18:43:23 +08:00
public event Action < JudgementResult > RevertResult ;
2020-11-10 22:32:30 +08:00
2017-03-15 17:58:41 +08:00
/// <summary>
2018-07-17 14:51:10 +08:00
/// The <see cref="DrawableHitObject"/> contained in this Playfield.
2017-03-15 17:58:41 +08:00
/// </summary>
2018-09-21 13:35:50 +08:00
public HitObjectContainer HitObjectContainer = > hitObjectContainerLazy . Value ;
private readonly Lazy < HitObjectContainer > hitObjectContainerLazy ;
2018-04-13 17:19:50 +08:00
2018-09-21 13:02:32 +08:00
/// <summary>
/// A function that converts gamefield coordinates to screen space.
/// </summary>
public Func < Vector2 , Vector2 > GamefieldToScreenSpace = > HitObjectContainer . ToScreenSpace ;
2020-03-23 18:18:56 +08:00
/// <summary>
/// A function that converts screen space coordinates to gamefield.
/// </summary>
public Func < Vector2 , Vector2 > ScreenSpaceToGamefield = > HitObjectContainer . ToLocalSpace ;
2018-01-15 19:22:41 +08:00
/// <summary>
2018-07-17 14:51:10 +08:00
/// All the <see cref="DrawableHitObject"/>s contained in this <see cref="Playfield"/> and all <see cref="NestedPlayfields"/>.
2018-01-15 19:22:41 +08:00
/// </summary>
2020-09-22 17:17:04 +08:00
public IEnumerable < DrawableHitObject > AllHitObjects
{
get
{
if ( HitObjectContainer = = null )
return Enumerable . Empty < DrawableHitObject > ( ) ;
var enumerable = HitObjectContainer . Objects ;
2021-04-15 17:11:47 +08:00
if ( nestedPlayfields . Count ! = 0 )
2020-09-22 17:17:04 +08:00
enumerable = enumerable . Concat ( NestedPlayfields . SelectMany ( p = > p . AllHitObjects ) ) ;
return enumerable ;
}
}
2018-07-17 14:51:10 +08:00
/// <summary>
/// All <see cref="Playfield"/>s nested inside this <see cref="Playfield"/>.
/// </summary>
2021-04-15 17:11:47 +08:00
public IEnumerable < Playfield > NestedPlayfields = > nestedPlayfields ;
2018-04-13 17:19:50 +08:00
2021-04-15 17:11:47 +08:00
private readonly List < Playfield > nestedPlayfields = new List < Playfield > ( ) ;
2018-04-13 17:19:50 +08:00
2022-03-03 14:37:39 +08:00
/// <summary>
/// Whether this <see cref="Playfield"/> is nested in another <see cref="Playfield"/>.
/// </summary>
public bool IsNested { get ; private set ; }
2018-07-20 16:04:33 +08:00
/// <summary>
/// Whether judgements should be displayed by this and and all nested <see cref="Playfield"/>s.
/// </summary>
public readonly BindableBool DisplayJudgements = new BindableBool ( true ) ;
2023-07-28 14:47:57 +08:00
/// <summary>
/// A screen space draw quad which resembles the edges of the playfield for skinning purposes.
/// This will allow users / components to snap objects to the "edge" of the playfield.
/// </summary>
/// <remarks>
/// Rulesets which reduce the visible area further than the full relative playfield space itself
/// should retarget this to the ScreenSpaceDrawQuad of the appropriate container.
/// </remarks>
public virtual Quad SkinnableComponentScreenSpaceDrawQuad = > ScreenSpaceDrawQuad ;
2020-11-19 18:51:09 +08:00
[Resolved(CanBeNull = true)]
2022-12-01 03:00:25 +08:00
[CanBeNull]
2022-11-30 17:13:53 +08:00
protected IReadOnlyList < Mod > Mods { get ; private set ; }
2020-11-19 18:51:09 +08:00
2021-06-16 14:15:33 +08:00
private readonly HitObjectEntryManager entryManager = new HitObjectEntryManager ( ) ;
2021-06-15 18:41:37 +08:00
2023-01-27 18:35:44 +08:00
private readonly Stack < HitObjectLifetimeEntry > judgedEntries ;
2023-01-19 18:43:23 +08:00
2017-03-07 14:43:44 +08:00
/// <summary>
2018-09-21 13:02:32 +08:00
/// Creates a new <see cref="Playfield"/>.
2017-03-07 14:43:44 +08:00
/// </summary>
2018-09-21 13:02:32 +08:00
protected Playfield ( )
2016-11-19 18:07:57 +08:00
{
2018-01-15 18:44:42 +08:00
RelativeSizeAxes = Axes . Both ;
2018-09-21 13:35:50 +08:00
2020-11-10 22:32:30 +08:00
hitObjectContainerLazy = new Lazy < HitObjectContainer > ( ( ) = > CreateHitObjectContainer ( ) . With ( h = >
{
2023-01-19 18:43:23 +08:00
h . NewResult + = onNewResult ;
2020-11-26 13:01:46 +08:00
h . HitObjectUsageBegan + = o = > HitObjectUsageBegan ? . Invoke ( o ) ;
h . HitObjectUsageFinished + = o = > HitObjectUsageFinished ? . Invoke ( o ) ;
2020-11-10 22:32:30 +08:00
} ) ) ;
2021-06-15 18:41:37 +08:00
entryManager . OnEntryAdded + = onEntryAdded ;
entryManager . OnEntryRemoved + = onEntryRemoved ;
2023-01-19 18:43:23 +08:00
2023-01-27 18:35:44 +08:00
judgedEntries = new Stack < HitObjectLifetimeEntry > ( ) ;
2017-03-21 13:53:55 +08:00
}
2018-04-13 17:19:50 +08:00
2017-03-21 13:53:55 +08:00
[BackgroundDependencyLoader]
2019-04-08 17:32:05 +08:00
private void load ( )
2017-03-21 13:53:55 +08:00
{
2019-03-08 14:01:45 +08:00
Cursor = CreateCursor ( ) ;
2019-05-07 12:23:09 +08:00
2019-03-08 14:01:45 +08:00
if ( Cursor ! = null )
2019-04-22 16:06:01 +08:00
{
// initial showing of the cursor will be handed by MenuCursorContainer (via DrawableRuleset's IProvideCursor implementation).
Cursor . Hide ( ) ;
2019-03-25 18:21:47 +08:00
AddInternal ( Cursor ) ;
2019-04-22 16:06:01 +08:00
}
2016-11-19 18:07:57 +08:00
}
2018-04-13 17:19:50 +08:00
2020-11-21 10:19:52 +08:00
private void onNewDrawableHitObject ( DrawableHitObject d )
{
d . OnNestedDrawableCreated + = onNewDrawableHitObject ;
2020-11-21 14:20:33 +08:00
OnNewDrawableHitObject ( d ) ;
2020-11-21 10:19:52 +08:00
2020-11-22 17:47:35 +08:00
Debug . Assert ( ! d . IsInitialized ) ;
d . IsInitialized = true ;
2020-11-21 10:19:52 +08:00
}
2017-03-15 17:58:41 +08:00
/// <summary>
/// Performs post-processing tasks (if any) after all DrawableHitObjects are loaded into this Playfield.
/// </summary>
2018-07-17 14:48:51 +08:00
public virtual void PostProcess ( ) = > NestedPlayfields . ForEach ( p = > p . PostProcess ( ) ) ;
2018-04-13 17:19:50 +08:00
2017-03-15 17:58:41 +08:00
/// <summary>
/// Adds a DrawableHitObject to this Playfield.
/// </summary>
/// <param name="h">The DrawableHitObject to add.</param>
2020-11-10 22:32:30 +08:00
public virtual void Add ( DrawableHitObject h )
{
2020-11-26 18:07:09 +08:00
if ( ! h . IsInitialized )
onNewDrawableHitObject ( h ) ;
2020-11-20 23:27:19 +08:00
2020-11-10 22:32:30 +08:00
HitObjectContainer . Add ( h ) ;
OnHitObjectAdded ( h . HitObject ) ;
}
2018-04-13 17:19:50 +08:00
2017-08-08 09:53:59 +08:00
/// <summary>
/// Remove a DrawableHitObject from this Playfield.
/// </summary>
/// <param name="h">The DrawableHitObject to remove.</param>
2020-11-10 22:32:30 +08:00
public virtual bool Remove ( DrawableHitObject h )
{
if ( ! HitObjectContainer . Remove ( h ) )
return false ;
OnHitObjectRemoved ( h . HitObject ) ;
return false ;
}
/// <summary>
/// Invoked when a <see cref="HitObject"/> is added to this <see cref="Playfield"/>.
/// </summary>
/// <param name="hitObject">The added <see cref="HitObject"/>.</param>
protected virtual void OnHitObjectAdded ( HitObject hitObject )
{
2021-06-15 18:41:37 +08:00
preloadSamples ( hitObject ) ;
2020-11-10 22:32:30 +08:00
}
/// <summary>
/// Invoked when a <see cref="HitObject"/> is removed from this <see cref="Playfield"/>.
/// </summary>
/// <param name="hitObject">The removed <see cref="HitObject"/>.</param>
protected virtual void OnHitObjectRemoved ( HitObject hitObject )
{
}
2020-11-21 14:20:33 +08:00
/// <summary>
/// Invoked before a new <see cref="DrawableHitObject"/> is added to this <see cref="Playfield"/>.
2020-11-22 17:47:35 +08:00
/// It is invoked only once even if the drawable is pooled and used multiple times for different <see cref="HitObject"/>s.
2020-11-21 14:20:33 +08:00
/// </summary>
/// <remarks>
/// This is also invoked for nested <see cref="DrawableHitObject"/>s.
/// </remarks>
protected virtual void OnNewDrawableHitObject ( DrawableHitObject drawableHitObject )
{
}
2020-11-13 17:54:49 +08:00
/// <summary>
/// The cursor currently being used by this <see cref="Playfield"/>. May be null if no cursor is provided.
/// </summary>
2022-10-13 05:16:42 +08:00
[CanBeNull]
2020-11-13 17:54:49 +08:00
public GameplayCursorContainer Cursor { get ; private set ; }
/// <summary>
/// Provide a cursor which is to be used for gameplay.
/// </summary>
/// <returns>The cursor, or null to show the menu cursor.</returns>
2022-10-12 22:50:47 +08:00
protected virtual GameplayCursorContainer CreateCursor ( ) = > null ;
2020-11-13 17:54:49 +08:00
/// <summary>
/// Registers a <see cref="Playfield"/> as a nested <see cref="Playfield"/>.
/// This does not add the <see cref="Playfield"/> to the draw hierarchy.
/// </summary>
/// <param name="otherPlayfield">The <see cref="Playfield"/> to add.</param>
protected void AddNested ( Playfield otherPlayfield )
{
2022-03-03 14:37:39 +08:00
otherPlayfield . IsNested = true ;
2020-11-13 17:54:49 +08:00
otherPlayfield . DisplayJudgements . BindTo ( DisplayJudgements ) ;
otherPlayfield . NewResult + = ( d , r ) = > NewResult ? . Invoke ( d , r ) ;
2023-01-19 18:43:23 +08:00
otherPlayfield . RevertResult + = r = > RevertResult ? . Invoke ( r ) ;
2020-11-13 17:54:49 +08:00
otherPlayfield . HitObjectUsageBegan + = h = > HitObjectUsageBegan ? . Invoke ( h ) ;
otherPlayfield . HitObjectUsageFinished + = h = > HitObjectUsageFinished ? . Invoke ( h ) ;
2021-04-15 17:11:47 +08:00
nestedPlayfields . Add ( otherPlayfield ) ;
2020-11-13 17:54:49 +08:00
}
2024-02-02 09:53:48 +08:00
private Mod [ ] mods ;
2020-11-13 17:54:49 +08:00
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
2024-02-02 09:53:48 +08:00
mods = Mods ? . ToArray ( ) ;
2020-11-13 17:54:49 +08:00
// 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 ( ) ;
2024-02-02 09:53:48 +08:00
if ( ! IsNested & & mods ! = null )
2020-11-13 17:54:49 +08:00
{
2024-02-02 09:53:48 +08:00
foreach ( Mod mod in mods )
2020-11-13 17:54:49 +08:00
{
if ( mod is IUpdatableByPlayfield updatable )
updatable . Update ( this ) ;
}
}
2023-01-19 18:43:23 +08:00
// When rewinding, revert future judgements in the reverse order.
2023-02-09 16:15:37 +08:00
while ( judgedEntries . Count > 0 )
{
var result = judgedEntries . Peek ( ) . Result ;
Debug . Assert ( result ? . RawTime ! = null ) ;
if ( Time . Current > = result . RawTime . Value )
break ;
2023-01-27 18:35:44 +08:00
revertResult ( judgedEntries . Pop ( ) ) ;
2023-02-09 16:15:37 +08:00
}
2020-11-13 17:54:49 +08:00
}
/// <summary>
/// Creates the container that will be used to contain the <see cref="DrawableHitObject"/>s.
/// </summary>
protected virtual HitObjectContainer CreateHitObjectContainer ( ) = > new HitObjectContainer ( ) ;
2020-11-13 23:54:57 +08:00
#region Pooling support
private readonly Dictionary < Type , IDrawablePool > pools = new Dictionary < Type , IDrawablePool > ( ) ;
/// <summary>
/// Adds a <see cref="HitObjectLifetimeEntry"/> for a pooled <see cref="HitObject"/> to this <see cref="Playfield"/>.
/// </summary>
/// <param name="hitObject"></param>
public virtual void Add ( HitObject hitObject )
{
2021-06-16 14:15:33 +08:00
var entry = CreateLifetimeEntry ( hitObject ) ;
entryManager . Add ( entry , null ) ;
2020-11-13 23:54:57 +08:00
}
2022-03-14 16:19:23 +08:00
private void preloadSamples ( HitObject hitObject )
{
// prepare sample pools ahead of time so we're not initialising at runtime.
foreach ( var sample in hitObject . Samples )
2023-04-25 22:01:43 +08:00
prepareSamplePool ( sample ) ;
2022-03-14 16:19:23 +08:00
foreach ( var sample in hitObject . AuxiliarySamples )
2023-04-25 22:01:43 +08:00
prepareSamplePool ( sample ) ;
2022-03-14 16:19:23 +08:00
foreach ( var nestedObject in hitObject . NestedHitObjects )
preloadSamples ( nestedObject ) ;
}
2020-11-13 23:54:57 +08:00
/// <summary>
/// Removes a <see cref="HitObjectLifetimeEntry"/> for a pooled <see cref="HitObject"/> from this <see cref="Playfield"/>.
/// </summary>
/// <param name="hitObject"></param>
/// <returns>Whether the <see cref="HitObject"/> was successfully removed.</returns>
public virtual bool Remove ( HitObject hitObject )
{
2021-06-16 14:15:33 +08:00
if ( entryManager . TryGet ( hitObject , out var entry ) )
{
entryManager . Remove ( entry ) ;
return true ;
}
return nestedPlayfields . Any ( p = > p . Remove ( hitObject ) ) ;
2021-06-15 18:41:37 +08:00
}
private void onEntryAdded ( HitObjectLifetimeEntry entry , [ CanBeNull ] HitObject parentHitObject )
{
if ( parentHitObject ! = null ) return ;
HitObjectContainer . Add ( entry ) ;
OnHitObjectAdded ( entry . HitObject ) ;
}
2020-11-13 23:54:57 +08:00
2021-06-15 18:41:37 +08:00
private void onEntryRemoved ( HitObjectLifetimeEntry entry , [ CanBeNull ] HitObject parentHitObject )
{
if ( parentHitObject ! = null ) return ;
HitObjectContainer . Remove ( entry ) ;
OnHitObjectRemoved ( entry . HitObject ) ;
2020-11-13 23:54:57 +08:00
}
/// <summary>
/// Creates the <see cref="HitObjectLifetimeEntry"/> for a given <see cref="HitObject"/>.
/// </summary>
/// <remarks>
/// This may be overridden to provide custom lifetime control (e.g. via <see cref="HitObjectLifetimeEntry.InitialLifetimeOffset"/>.
/// </remarks>
/// <param name="hitObject">The <see cref="HitObject"/> to create the entry for.</param>
/// <returns>The <see cref="HitObjectLifetimeEntry"/>.</returns>
[NotNull]
protected virtual HitObjectLifetimeEntry CreateLifetimeEntry ( [ NotNull ] HitObject hitObject ) = > new HitObjectLifetimeEntry ( hitObject ) ;
/// <summary>
/// Registers a default <see cref="DrawableHitObject"/> pool with this <see cref="DrawableRuleset"/> which is to be used whenever
/// <see cref="DrawableHitObject"/> representations are requested for the given <typeparamref name="TObject"/> type.
/// </summary>
/// <param name="initialSize">The number of <see cref="DrawableHitObject"/>s to be initially stored in the pool.</param>
/// <param name="maximumSize">
/// The maximum number of <see cref="DrawableHitObject"/>s that can be stored in the pool.
/// If this limit is exceeded, every subsequent <see cref="DrawableHitObject"/> will be created anew instead of being retrieved from the pool,
/// until some of the existing <see cref="DrawableHitObject"/>s are returned to the pool.
/// </param>
/// <typeparam name="TObject">The <see cref="HitObject"/> type.</typeparam>
/// <typeparam name="TDrawable">The <see cref="DrawableHitObject"/> receiver for <typeparamref name="TObject"/>s.</typeparam>
2022-03-20 02:29:44 +08:00
public void RegisterPool < TObject , TDrawable > ( int initialSize , int? maximumSize = null )
2020-11-13 23:54:57 +08:00
where TObject : HitObject
where TDrawable : DrawableHitObject , new ( )
= > RegisterPool < TObject , TDrawable > ( new DrawablePool < TDrawable > ( initialSize , maximumSize ) ) ;
/// <summary>
/// Registers a custom <see cref="DrawableHitObject"/> pool with this <see cref="DrawableRuleset"/> which is to be used whenever
/// <see cref="DrawableHitObject"/> representations are requested for the given <typeparamref name="TObject"/> type.
/// </summary>
/// <param name="pool">The <see cref="DrawablePool{T}"/> to register.</param>
/// <typeparam name="TObject">The <see cref="HitObject"/> type.</typeparam>
/// <typeparam name="TDrawable">The <see cref="DrawableHitObject"/> receiver for <typeparamref name="TObject"/>s.</typeparam>
protected void RegisterPool < TObject , TDrawable > ( [ NotNull ] DrawablePool < TDrawable > pool )
where TObject : HitObject
where TDrawable : DrawableHitObject , new ( )
{
pools [ typeof ( TObject ) ] = pool ;
AddInternal ( pool ) ;
}
2020-12-03 18:46:42 +08:00
DrawableHitObject IPooledHitObjectProvider . GetPooledDrawableRepresentation ( HitObject hitObject , DrawableHitObject parent )
2020-11-13 23:54:57 +08:00
{
2022-03-14 16:19:23 +08:00
var pool = prepareDrawableHitObjectPool ( hitObject ) ;
2020-11-13 23:54:57 +08:00
2020-11-16 22:30:24 +08:00
return ( DrawableHitObject ) pool ? . Get ( d = >
2020-11-13 23:54:57 +08:00
{
var dho = ( DrawableHitObject ) d ;
2020-11-22 17:47:35 +08:00
if ( ! dho . IsInitialized )
2020-11-13 23:54:57 +08:00
{
2020-11-21 10:19:52 +08:00
onNewDrawableHitObject ( dho ) ;
2020-11-20 23:27:19 +08:00
2020-11-21 10:19:52 +08:00
// If this is the first time this DHO is being used, then apply the DHO mods.
// This is done before Apply() so that the state is updated once when the hitobject is applied.
2024-02-02 09:53:48 +08:00
if ( mods ! = null )
2021-05-18 17:49:05 +08:00
{
2024-02-02 09:53:48 +08:00
foreach ( Mod mod in mods )
{
if ( mod is IApplicableToDrawableHitObject applicable )
applicable . ApplyToDrawableHitObject ( dho ) ;
}
2021-05-18 17:49:05 +08:00
}
2020-11-13 23:54:57 +08:00
}
2021-06-15 18:41:37 +08:00
if ( ! entryManager . TryGet ( hitObject , out var entry ) )
2021-06-16 14:15:33 +08:00
{
entry = CreateLifetimeEntry ( hitObject ) ;
entryManager . Add ( entry , parent ? . HitObject ) ;
}
2020-11-13 23:54:57 +08:00
2020-12-03 19:03:39 +08:00
dho . ParentHitObject = parent ;
2021-04-21 13:32:37 +08:00
dho . Apply ( entry ) ;
2020-11-13 23:54:57 +08:00
} ) ;
}
2022-03-14 16:19:23 +08:00
private IDrawablePool prepareDrawableHitObjectPool ( 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 pool ;
}
2020-11-19 18:51:09 +08:00
private readonly Dictionary < ISampleInfo , DrawablePool < PoolableSkinnableSample > > samplePools = new Dictionary < ISampleInfo , DrawablePool < PoolableSkinnableSample > > ( ) ;
2022-03-14 16:19:23 +08:00
public PoolableSkinnableSample GetPooledSample ( ISampleInfo sampleInfo ) = > prepareSamplePool ( sampleInfo ) . Get ( ) ;
private DrawablePool < PoolableSkinnableSample > prepareSamplePool ( ISampleInfo sampleInfo )
2020-11-19 18:51:09 +08:00
{
2022-03-14 16:19:23 +08:00
if ( samplePools . TryGetValue ( sampleInfo , out var pool ) ) return pool ;
AddInternal ( samplePools [ sampleInfo ] = pool = new DrawableSamplePool ( sampleInfo , 1 ) ) ;
2020-11-19 18:51:09 +08:00
2022-03-14 16:19:23 +08:00
return pool ;
2020-11-19 18:51:09 +08:00
}
private partial class DrawableSamplePool : DrawablePool < PoolableSkinnableSample >
{
private readonly ISampleInfo sampleInfo ;
public DrawableSamplePool ( ISampleInfo sampleInfo , int initialSize , int? maximumSize = null )
: base ( initialSize , maximumSize )
{
this . sampleInfo = sampleInfo ;
}
protected override PoolableSkinnableSample CreateNewDrawable ( ) = > base . CreateNewDrawable ( ) . With ( d = > d . Apply ( sampleInfo ) ) ;
}
2020-11-13 23:54:57 +08:00
#endregion
2023-01-19 18:43:23 +08:00
private void onNewResult ( DrawableHitObject drawable , JudgementResult result )
{
2023-02-09 16:15:37 +08:00
Debug . Assert ( result ! = null & & drawable . Entry ? . Result = = result & & result . RawTime ! = null ) ;
2023-01-27 18:35:44 +08:00
judgedEntries . Push ( drawable . Entry . AsNonNull ( ) ) ;
2023-01-19 18:43:23 +08:00
NewResult ? . Invoke ( drawable , result ) ;
}
2023-01-27 18:35:44 +08:00
private void revertResult ( HitObjectLifetimeEntry entry )
2023-01-19 18:43:23 +08:00
{
var result = entry . Result ;
2023-01-27 18:35:44 +08:00
Debug . Assert ( result ! = null ) ;
2023-01-19 18:43:23 +08:00
RevertResult ? . Invoke ( result ) ;
2023-01-27 18:35:44 +08:00
entry . OnRevertResult ( ) ;
2023-01-19 18:43:23 +08:00
2023-01-24 14:35:06 +08:00
result . Reset ( ) ;
2023-01-19 18:43:23 +08:00
}
2020-11-13 17:53:37 +08:00
#region Editor logic
2020-11-13 17:54:49 +08:00
/// <summary>
/// Invoked when a <see cref="HitObject"/> becomes used by a <see cref="DrawableHitObject"/>.
/// </summary>
/// <remarks>
/// If this <see cref="HitObjectContainer"/> uses pooled objects, this represents the time when the <see cref="HitObject"/>s become alive.
/// </remarks>
internal event Action < HitObject > HitObjectUsageBegan ;
/// <summary>
/// Invoked when a <see cref="HitObject"/> becomes unused by a <see cref="DrawableHitObject"/>.
/// </summary>
/// <remarks>
/// If this <see cref="HitObjectContainer"/> uses pooled objects, this represents the time when the <see cref="HitObject"/>s become dead.
/// </remarks>
internal event Action < HitObject > HitObjectUsageFinished ;
2020-11-12 17:30:32 +08:00
/// <summary>
/// Sets whether to keep a given <see cref="HitObject"/> always alive within this or any nested <see cref="Playfield"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to set.</param>
/// <param name="keepAlive">Whether to keep <paramref name="hitObject"/> always alive.</param>
2020-11-13 17:52:53 +08:00
internal void SetKeepAlive ( HitObject hitObject , bool keepAlive )
2020-11-12 17:30:32 +08:00
{
2021-06-15 18:41:37 +08:00
if ( entryManager . TryGet ( hitObject , out var entry ) )
2020-11-12 17:30:32 +08:00
{
entry . KeepAlive = keepAlive ;
return ;
}
2021-04-15 17:11:47 +08:00
foreach ( var p in nestedPlayfields )
2020-11-12 17:30:32 +08:00
p . SetKeepAlive ( hitObject , keepAlive ) ;
}
/// <summary>
/// Keeps all <see cref="HitObject"/>s alive within this and all nested <see cref="Playfield"/>s.
/// </summary>
2020-11-13 17:52:53 +08:00
internal void KeepAllAlive ( )
2020-11-12 17:30:32 +08:00
{
2021-06-15 18:41:37 +08:00
foreach ( var entry in entryManager . AllEntries )
2020-11-12 17:30:32 +08:00
entry . KeepAlive = true ;
2021-04-15 17:11:47 +08:00
foreach ( var p in nestedPlayfields )
2020-11-12 17:30:32 +08:00
p . KeepAllAlive ( ) ;
}
2020-11-12 17:34:50 +08:00
/// <summary>
/// The amount of time prior to the current time within which <see cref="HitObject"/>s should be considered alive.
/// </summary>
2020-11-13 17:54:49 +08:00
internal double PastLifetimeExtension
2020-11-12 17:34:50 +08:00
{
get = > HitObjectContainer . PastLifetimeExtension ;
set
{
HitObjectContainer . PastLifetimeExtension = value ;
2021-04-15 17:11:47 +08:00
foreach ( var nested in nestedPlayfields )
2020-11-12 17:34:50 +08:00
nested . PastLifetimeExtension = value ;
}
}
/// <summary>
/// The amount of time after the current time within which <see cref="HitObject"/>s should be considered alive.
/// </summary>
2020-11-13 17:54:49 +08:00
internal double FutureLifetimeExtension
2020-11-12 17:34:50 +08:00
{
get = > HitObjectContainer . FutureLifetimeExtension ;
set
{
HitObjectContainer . FutureLifetimeExtension = value ;
2021-04-15 17:11:47 +08:00
foreach ( var nested in nestedPlayfields )
2020-11-12 17:34:50 +08:00
nested . FutureLifetimeExtension = value ;
}
}
2020-11-13 17:53:37 +08:00
#endregion
2016-09-02 19:30:27 +08:00
}
}