1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 22:22:54 +08:00

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

View File

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