1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-30 03:27:24 +08:00
osu-lazer/osu.Game.Rulesets.Osu/UI/SmokeContainer.cs
Dean Herbert 6628ab5190 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.
2022-10-05 18:37:14 +09:00

66 lines
2.2 KiB
C#

// 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;
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;
namespace osu.Game.Rulesets.Osu.UI
{
/// <summary>
/// Manages smoke trails generated from user input.
/// </summary>
public class SmokeContainer : Container, IRequireHighFrequencyMousePosition, IKeyBindingHandler<OsuAction>
{
public Vector2 LastMousePosition;
private SkinnableDrawable? currentSegment;
public override bool ReceivePositionalInputAt(Vector2 _) => true;
public bool OnPressed(KeyBindingPressEvent<OsuAction> e)
{
if (e.Action == OsuAction.Smoke)
{
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)
{
(currentSegment?.Drawable as SmokeSegment)?.FinishDrawing(Time.Current);
currentSegment = null;
foreach (Drawable child in Children)
{
var skinnable = (SkinnableDrawable)child;
skinnable.LifetimeEnd = skinnable.Drawable.LifetimeEnd;
}
}
}
protected override bool OnMouseMove(MouseMoveEvent e)
{
if (currentSegment != null)
addPosition(e.MousePosition, Time.Current);
LastMousePosition = e.MousePosition;
return base.OnMouseMove(e);
}
}
}