Refactor to avoid DI / event flow

There's always one active smoke segment and it's the direct child of
`SmokeContainer`. This can be simplified as such.
This commit is contained in:
Dean Herbert 2022-10-05 18:37:09 +09:00
parent 1e5ff2679b
commit 6628ab5190
2 changed files with 14 additions and 47 deletions

View File

@ -14,7 +14,6 @@
using osu.Framework.Graphics.Shaders; using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Textures; using osu.Framework.Graphics.Textures;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Rulesets.Osu.UI;
using osuTK; using osuTK;
using osuTK.Graphics; using osuTK.Graphics;
@ -54,7 +53,6 @@ protected float Radius
private float totalDistance; private float totalDistance;
private Vector2? lastPosition; private Vector2? lastPosition;
private SmokeContainer? smokeContainer;
private const int max_point_count = 18_000; private const int max_point_count = 18_000;
@ -84,9 +82,8 @@ protected float Radius
private const float max_rotation = 0.25f; private const float max_rotation = 0.25f;
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(SmokeContainer container, ShaderManager shaders) private void load(ShaderManager shaders)
{ {
smokeContainer = container;
RoundedTextureShader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE_ROUNDED); RoundedTextureShader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE_ROUNDED);
TextureShader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE); TextureShader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE);
} }
@ -100,14 +97,6 @@ protected override void LoadComplete()
SmokeStartTime = Time.Current; SmokeStartTime = Time.Current;
totalDistance = PointInterval; totalDistance = PointInterval;
if (smokeContainer != null)
{
smokeContainer.SmokeMoved += onSmokeMoved;
smokeContainer.SmokeEnded += onSmokeEnded;
onSmokeMoved(smokeContainer.LastMousePosition, Time.Current);
}
} }
private Vector2 nextPointDirection() private Vector2 nextPointDirection()
@ -116,7 +105,7 @@ private Vector2 nextPointDirection()
return new Vector2(MathF.Sin(angle), -MathF.Cos(angle)); return new Vector2(MathF.Sin(angle), -MathF.Cos(angle));
} }
private void onSmokeMoved(Vector2 position, double time) public void AddPosition(Vector2 position, double time)
{ {
lastPosition ??= position; lastPosition ??= position;
@ -158,17 +147,11 @@ private void onSmokeMoved(Vector2 position, double time)
lastPosition = position; lastPosition = position;
if (SmokePoints.Count >= max_point_count) if (SmokePoints.Count >= max_point_count)
onSmokeEnded(time); FinishDrawing(time);
} }
private void onSmokeEnded(double time) public void FinishDrawing(double time)
{ {
if (smokeContainer != null)
{
smokeContainer.SmokeMoved -= onSmokeMoved;
smokeContainer.SmokeEnded -= onSmokeEnded;
}
SmokeEndTime = time; SmokeEndTime = time;
} }
@ -181,17 +164,6 @@ protected override void Update()
Invalidate(Invalidation.DrawNode); Invalidate(Invalidation.DrawNode);
} }
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (smokeContainer != null)
{
smokeContainer.SmokeMoved -= onSmokeMoved;
smokeContainer.SmokeEnded -= onSmokeEnded;
}
}
protected struct SmokePoint protected struct SmokePoint
{ {
public Vector2 Position; public Vector2 Position;

View File

@ -1,13 +1,12 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Input; using osu.Framework.Input;
using osu.Framework.Input.Bindings; using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events; using osu.Framework.Input.Events;
using osu.Game.Rulesets.Osu.Skinning;
using osu.Game.Rulesets.Osu.Skinning.Default; using osu.Game.Rulesets.Osu.Skinning.Default;
using osu.Game.Skinning; using osu.Game.Skinning;
using osuTK; using osuTK;
@ -17,15 +16,11 @@ namespace osu.Game.Rulesets.Osu.UI
/// <summary> /// <summary>
/// Manages smoke trails generated from user input. /// Manages smoke trails generated from user input.
/// </summary> /// </summary>
[Cached]
public class SmokeContainer : Container, IRequireHighFrequencyMousePosition, IKeyBindingHandler<OsuAction> public class SmokeContainer : Container, IRequireHighFrequencyMousePosition, IKeyBindingHandler<OsuAction>
{ {
public event Action<Vector2, double>? SmokeMoved;
public event Action<double>? SmokeEnded;
public Vector2 LastMousePosition; public Vector2 LastMousePosition;
private bool isSmoking; private SkinnableDrawable? currentSegment;
public override bool ReceivePositionalInputAt(Vector2 _) => true; public override bool ReceivePositionalInputAt(Vector2 _) => true;
@ -33,21 +28,22 @@ public bool OnPressed(KeyBindingPressEvent<OsuAction> e)
{ {
if (e.Action == OsuAction.Smoke) if (e.Action == OsuAction.Smoke)
{ {
isSmoking = true; AddInternal(currentSegment = new SkinnableDrawable(new OsuSkinComponent(OsuSkinComponents.SmokeTrail), _ => new DefaultSmokeSegment()));
AddInternal(new SkinnableDrawable(new OsuSkinComponent(OsuSkinComponents.SmokeTrail), _ => new DefaultSmokeSegment())); addPosition(LastMousePosition, Time.Current);
return true; return true;
} }
return false; return false;
} }
private void addPosition(Vector2 position, double timeCurrent) => (currentSegment?.Drawable as SmokeSegment)?.AddPosition(position, timeCurrent);
public void OnReleased(KeyBindingReleaseEvent<OsuAction> e) public void OnReleased(KeyBindingReleaseEvent<OsuAction> e)
{ {
if (e.Action == OsuAction.Smoke) if (e.Action == OsuAction.Smoke)
{ {
isSmoking = false; (currentSegment?.Drawable as SmokeSegment)?.FinishDrawing(Time.Current);
SmokeEnded?.Invoke(Time.Current); currentSegment = null;
foreach (Drawable child in Children) foreach (Drawable child in Children)
{ {
@ -59,11 +55,10 @@ public void OnReleased(KeyBindingReleaseEvent<OsuAction> e)
protected override bool OnMouseMove(MouseMoveEvent e) protected override bool OnMouseMove(MouseMoveEvent e)
{ {
if (isSmoking) if (currentSegment != null)
SmokeMoved?.Invoke(e.MousePosition, Time.Current); addPosition(e.MousePosition, Time.Current);
LastMousePosition = e.MousePosition; LastMousePosition = e.MousePosition;
return base.OnMouseMove(e); return base.OnMouseMove(e);
} }
} }