1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-18 20:13:21 +08:00

implement pooling

Uses pooling for all analysis objects and creates the lifetime entries from replay data when the analysis container is constructed.
This commit is contained in:
Sheepposu 2024-02-23 23:32:35 -05:00
parent 2a1fa8ccac
commit 4d669c56a2
7 changed files with 284 additions and 13 deletions

View File

@ -0,0 +1,242 @@
// 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.
using System;
using System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Lines;
using osu.Framework.Graphics.Performance;
using osu.Framework.Graphics.Pooling;
using osu.Game.Replays;
using osu.Game.Rulesets.Objects.Pooling;
using osu.Game.Rulesets.Osu.Replays;
using osu.Game.Rulesets.Osu.Skinning;
using osu.Game.Rulesets.Osu.Skinning.Default;
using osu.Game.Rulesets.UI;
using osu.Game.Skinning;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Rulesets.Osu.UI
{
public partial class OsuAnalysisContainer : AnalysisContainer
{
public Bindable<bool> HitMarkerEnabled = new BindableBool();
public Bindable<bool> AimMarkersEnabled = new BindableBool();
public Bindable<bool> AimLinesEnabled = new BindableBool();
private HitMarkersContainer hitMarkersContainer;
private AimMarkersContainer aimMarkersContainer;
private AimLinesContainer aimLinesContainer;
public OsuAnalysisContainer(Replay replay)
: base(replay)
{
InternalChildren = new Drawable[]
{
hitMarkersContainer = new HitMarkersContainer(),
aimMarkersContainer = new AimMarkersContainer() { Depth = float.MinValue },
aimLinesContainer = new AimLinesContainer() { Depth = float.MaxValue }
};
HitMarkerEnabled.ValueChanged += e => hitMarkersContainer.FadeTo(e.NewValue ? 1 : 0);
AimMarkersEnabled.ValueChanged += e => aimMarkersContainer.FadeTo(e.NewValue ? 1 : 0);
AimLinesEnabled.ValueChanged += e => aimLinesContainer.FadeTo(e.NewValue ? 1 : 0);
}
[BackgroundDependencyLoader]
private void load(ISkinSource skin)
{
var aimLineColor = skin.GetConfig<OsuSkinColour, Color4>(OsuSkinColour.ReplayAimLine)?.Value ?? Color4.White;
aimLineColor.A = 127;
hitMarkersContainer.Hide();
aimMarkersContainer.Hide();
aimLinesContainer.Hide();
bool leftHeld = false;
bool rightHeld = false;
foreach (var frame in Replay.Frames)
{
var osuFrame = (OsuReplayFrame)frame;
aimMarkersContainer.Add(new AimPointEntry(osuFrame.Time, osuFrame.Position));
aimLinesContainer.Add(new AimPointEntry(osuFrame.Time, osuFrame.Position));
bool leftButton = osuFrame.Actions.Contains(OsuAction.LeftButton);
bool rightButton = osuFrame.Actions.Contains(OsuAction.RightButton);
if (leftHeld && !leftButton)
leftHeld = false;
else if (!leftHeld && leftButton)
{
hitMarkersContainer.Add(new HitMarkerEntry(osuFrame.Time, osuFrame.Position, true));
leftHeld = true;
}
if (rightHeld && !rightButton)
rightHeld = false;
else if (!rightHeld && rightButton)
{
hitMarkersContainer.Add(new HitMarkerEntry(osuFrame.Time, osuFrame.Position, false));
rightHeld = true;
}
}
}
private partial class HitMarkersContainer : PooledDrawableWithLifetimeContainer<HitMarkerEntry, HitMarkerDrawable>
{
private readonly HitMarkerPool leftPool;
private readonly HitMarkerPool rightPool;
public HitMarkersContainer()
{
AddInternal(leftPool = new HitMarkerPool(OsuSkinComponents.HitMarkerLeft, OsuAction.LeftButton, 15));
AddInternal(rightPool = new HitMarkerPool(OsuSkinComponents.HitMarkerRight, OsuAction.RightButton, 15));
}
protected override HitMarkerDrawable GetDrawable(HitMarkerEntry entry) => (entry.IsLeftMarker ? leftPool : rightPool).Get(d => d.Apply(entry));
}
private partial class AimMarkersContainer : PooledDrawableWithLifetimeContainer<AimPointEntry, HitMarkerDrawable>
{
private readonly HitMarkerPool pool;
public AimMarkersContainer()
{
AddInternal(pool = new HitMarkerPool(OsuSkinComponents.AimMarker, null, 80));
}
protected override HitMarkerDrawable GetDrawable(AimPointEntry entry) => pool.Get(d => d.Apply(entry));
}
private partial class AimLinesContainer : Path
{
private LifetimeEntryManager lifetimeManager = new LifetimeEntryManager();
private SortedSet<AimPointEntry> aliveEntries = new SortedSet<AimPointEntry>(new AimLinePointComparator());
public AimLinesContainer()
{
lifetimeManager.EntryBecameAlive += entryBecameAlive;
lifetimeManager.EntryBecameDead += entryBecameDead;
lifetimeManager.EntryCrossedBoundary += entryCrossedBoundary;
PathRadius = 1f;
Colour = new Color4(255, 255, 255, 127);
}
protected override void Update()
{
base.Update();
lifetimeManager.Update(Time.Current);
}
public void Add(AimPointEntry entry) => lifetimeManager.AddEntry(entry);
private void entryBecameAlive(LifetimeEntry entry)
{
aliveEntries.Add((AimPointEntry)entry);
updateVertices();
}
private void entryBecameDead(LifetimeEntry entry)
{
aliveEntries.Remove((AimPointEntry)entry);
updateVertices();
}
private void updateVertices()
{
ClearVertices();
foreach (var entry in aliveEntries)
{
AddVertex(entry.Position);
}
}
private void entryCrossedBoundary(LifetimeEntry entry, LifetimeBoundaryKind kind, LifetimeBoundaryCrossingDirection direction)
{
}
private sealed class AimLinePointComparator : IComparer<AimPointEntry>
{
public int Compare(AimPointEntry? x, AimPointEntry? y)
{
ArgumentNullException.ThrowIfNull(x);
ArgumentNullException.ThrowIfNull(y);
return x.LifetimeStart.CompareTo(y.LifetimeStart);
}
}
}
private partial class HitMarkerDrawable : PoolableDrawableWithLifetime<AimPointEntry>
{
/// <summary>
/// This constructor only exists to meet the <c>new()</c> type constraint of <see cref="DrawablePool{T}"/>.
/// </summary>
public HitMarkerDrawable()
{
}
public HitMarkerDrawable(OsuSkinComponents component, OsuAction? action)
{
Origin = Anchor.Centre;
InternalChild = new SkinnableDrawable(new OsuSkinComponentLookup(component), _ => new DefaultHitMarker(action));
}
protected override void OnApply(AimPointEntry entry)
{
Position = entry.Position;
using (BeginAbsoluteSequence(LifetimeStart))
Show();
using (BeginAbsoluteSequence(LifetimeEnd - 200))
this.FadeOut(200);
}
}
private partial class HitMarkerPool : DrawablePool<HitMarkerDrawable>
{
private readonly OsuSkinComponents component;
private readonly OsuAction? action;
public HitMarkerPool(OsuSkinComponents component, OsuAction? action, int initialSize)
: base(initialSize)
{
this.component = component;
this.action = action;
}
protected override HitMarkerDrawable CreateNewDrawable() => new HitMarkerDrawable(component, action);
}
private partial class AimPointEntry : LifetimeEntry
{
public Vector2 Position { get; }
public AimPointEntry(double time, Vector2 position)
{
LifetimeStart = time;
LifetimeEnd = time + 1_000;
Position = position;
}
}
private partial class HitMarkerEntry : AimPointEntry
{
public bool IsLeftMarker { get; }
public HitMarkerEntry(double lifetimeStart, Vector2 position, bool isLeftMarker)
: base(lifetimeStart, position)
{
IsLeftMarker = isLeftMarker;
}
}
}
}

View File

@ -1,10 +1,10 @@
// 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.
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Localisation;
using osu.Game.Replays;
using osu.Game.Rulesets.UI;
using osu.Game.Screens.Play.PlayerSettings;
@ -29,14 +29,7 @@ namespace osu.Game.Rulesets.Osu.UI
aimLinesToggle = new PlayerCheckbox { LabelText = PlayerSettingsOverlayStrings.AimLines },
hideCursorToggle = new PlayerCheckbox { LabelText = PlayerSettingsOverlayStrings.HideCursor }
};
}
[BackgroundDependencyLoader]
private void load()
{
DrawableRuleset.Playfield.MarkersContainer.HitMarkerEnabled.BindTo(hitMarkerToggle.Current);
DrawableRuleset.Playfield.MarkersContainer.AimMarkersEnabled.BindTo(aimMarkerToggle.Current);
DrawableRuleset.Playfield.MarkersContainer.AimLinesEnabled.BindTo(aimLinesToggle.Current);
hideCursorToggle.Current.BindValueChanged(onCursorToggle);
}
@ -52,5 +45,14 @@ namespace osu.Game.Rulesets.Osu.UI
DrawableRuleset.Playfield.Cursor.FadeIn();
}
}
public override AnalysisContainer CreateAnalysisContainer(Replay replay)
{
var analysisContainer = new OsuAnalysisContainer(replay);
analysisContainer.HitMarkerEnabled.BindTo(hitMarkerToggle.Current);
analysisContainer.AimMarkersEnabled.BindTo(aimMarkerToggle.Current);
analysisContainer.AimLinesEnabled.BindTo(aimLinesToggle.Current);
return analysisContainer;
}
}
}

View File

@ -37,8 +37,6 @@ namespace osu.Game.Rulesets.Osu.UI
public SmokeContainer Smoke { get; }
public HitMarkerContainer MarkersContainer { get; }
public FollowPointRenderer FollowPoints { get; }
public static readonly Vector2 BASE_SIZE = new Vector2(512, 384);
@ -61,8 +59,7 @@ namespace osu.Game.Rulesets.Osu.UI
judgementLayer = new JudgementContainer<DrawableOsuJudgement> { RelativeSizeAxes = Axes.Both },
HitObjectContainer,
judgementAboveHitObjectLayer = new Container { RelativeSizeAxes = Axes.Both },
approachCircles = new ProxyContainer { RelativeSizeAxes = Axes.Both },
MarkersContainer = new HitMarkerContainer(HitObjectContainer) { RelativeSizeAxes = Axes.Both }
approachCircles = new ProxyContainer { RelativeSizeAxes = Axes.Both }
};
HitPolicy = new StartTimeOrderedHitPolicy();

View File

@ -0,0 +1,18 @@
// 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.
using osu.Framework.Graphics.Containers;
using osu.Game.Replays;
namespace osu.Game.Rulesets.UI
{
public partial class AnalysisContainer : Container
{
protected Replay Replay;
public AnalysisContainer(Replay replay)
{
Replay = replay;
}
}
}

View File

@ -291,6 +291,12 @@ namespace osu.Game.Rulesets.UI
/// </summary>
protected virtual HitObjectContainer CreateHitObjectContainer() => new HitObjectContainer();
/// <summary>
/// Adds an analysis container to internal children for replays.
/// </summary>
/// <param name="analysisContainer"></param>
public virtual void AddAnalysisContainer(AnalysisContainer analysisContainer) => AddInternal(analysisContainer);
#region Pooling support
private readonly Dictionary<Type, IDrawablePool> pools = new Dictionary<Type, IDrawablePool>();

View File

@ -1,11 +1,12 @@
// 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.
using osu.Game.Replays;
using osu.Game.Rulesets.UI;
namespace osu.Game.Screens.Play.PlayerSettings
{
public partial class AnalysisSettings : PlayerSettingsGroup
public abstract partial class AnalysisSettings : PlayerSettingsGroup
{
protected DrawableRuleset DrawableRuleset;
@ -14,5 +15,7 @@ namespace osu.Game.Screens.Play.PlayerSettings
{
DrawableRuleset = drawableRuleset;
}
public abstract AnalysisContainer CreateAnalysisContainer(Replay replay);
}
}

View File

@ -74,7 +74,10 @@ namespace osu.Game.Screens.Play
var analysisSettings = DrawableRuleset.Ruleset.CreateAnalysisSettings(DrawableRuleset);
if (analysisSettings != null)
{
HUDOverlay.PlayerSettingsOverlay.AddAtStart(analysisSettings);
DrawableRuleset.Playfield.AddAnalysisContainer(analysisSettings.CreateAnalysisContainer(GameplayState.Score.Replay));
}
}
protected override void PrepareReplay()