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

@ -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);
}
}