osu/osu.Game.Rulesets.Osu/Skinning/Default/SpinnerRotationTracker.cs

132 lines
4.2 KiB
C#
Raw Normal View History

// 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.
2018-04-13 09:19:50 +00:00
using System;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
using osu.Framework.Utils;
using osu.Game.Rulesets.Objects.Drawables;
2020-12-04 11:21:53 +00:00
using osu.Game.Rulesets.Osu.Objects.Drawables;
using osu.Game.Screens.Play;
using osuTK;
2018-04-13 09:19:50 +00:00
2020-12-04 11:21:53 +00:00
namespace osu.Game.Rulesets.Osu.Skinning.Default
2018-04-13 09:19:50 +00:00
{
public class SpinnerRotationTracker : CircularContainer
2018-04-13 09:19:50 +00:00
{
public override bool IsPresent => true; // handle input when hidden
private readonly DrawableSpinner drawableSpinner;
public SpinnerRotationTracker(DrawableSpinner drawableSpinner)
2018-04-13 09:19:50 +00:00
{
this.drawableSpinner = drawableSpinner;
drawableSpinner.HitObjectApplied += resetState;
2018-04-13 09:19:50 +00:00
RelativeSizeAxes = Axes.Both;
}
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
2018-04-13 09:19:50 +00:00
public bool Tracking { get; set; }
2019-02-28 04:31:40 +00:00
2020-07-30 10:34:59 +00:00
/// <summary>
/// Whether the spinning is spinning at a reasonable speed to be considered visually spinning.
/// </summary>
public readonly BindableBool IsSpinning = new BindableBool();
/// <summary>
/// Whether currently in the correct time range to allow spinning.
/// </summary>
private bool isSpinnableTime => drawableSpinner.HitObject.StartTime <= Time.Current && drawableSpinner.HitObject.EndTime > Time.Current;
2020-02-08 00:59:35 +00:00
2018-10-02 03:02:47 +00:00
protected override bool OnMouseMove(MouseMoveEvent e)
2018-04-13 09:19:50 +00:00
{
mousePosition = Parent.ToLocalSpace(e.ScreenSpaceMousePosition);
2018-10-02 03:02:47 +00:00
return base.OnMouseMove(e);
2018-04-13 09:19:50 +00:00
}
private Vector2 mousePosition;
private float lastAngle;
private float currentRotation;
2018-04-13 09:19:50 +00:00
private bool rotationTransferred;
[Resolved(canBeNull: true)]
private GameplayClock gameplayClock { get; set; }
2018-04-13 09:19:50 +00:00
protected override void Update()
{
base.Update();
2020-02-08 01:51:32 +00:00
var thisAngle = -MathUtils.RadiansToDegrees(MathF.Atan2(mousePosition.X - DrawSize.X / 2, mousePosition.Y - DrawSize.Y / 2));
2018-04-13 09:19:50 +00:00
2020-02-08 01:51:32 +00:00
var delta = thisAngle - lastAngle;
2018-04-13 09:19:50 +00:00
if (Tracking)
AddRotation(delta);
2020-02-05 06:23:59 +00:00
2020-02-08 01:51:32 +00:00
lastAngle = thisAngle;
2018-04-13 09:19:50 +00:00
2020-07-30 10:34:59 +00:00
IsSpinning.Value = isSpinnableTime && Math.Abs(currentRotation / 2 - Rotation) > 5f;
Rotation = (float)Interpolation.Damp(Rotation, currentRotation / 2, 0.99, Math.Abs(Time.Elapsed));
2018-04-13 09:19:50 +00:00
}
2020-02-05 06:23:59 +00:00
/// <summary>
/// Rotate the disc by the provided angle (in addition to any existing rotation).
/// </summary>
/// <remarks>
/// Will be a no-op if not a valid time to spin.
/// </remarks>
/// <param name="angle">The delta angle.</param>
public void AddRotation(float angle)
2020-02-05 06:23:59 +00:00
{
if (!isSpinnableTime)
return;
2020-02-05 06:23:59 +00:00
if (!rotationTransferred)
{
currentRotation = Rotation * 2;
rotationTransferred = true;
}
if (angle > 180)
{
lastAngle += 360;
angle -= 360;
}
else if (-angle > 180)
{
lastAngle -= 360;
angle += 360;
}
currentRotation += angle;
// rate has to be applied each frame, because it's not guaranteed to be constant throughout playback
// (see: ModTimeRamp)
drawableSpinner.Result.RateAdjustedRotation += (float)(Math.Abs(angle) * (gameplayClock?.TrueGameplayRate ?? Clock.Rate));
2020-02-05 06:23:59 +00:00
}
private void resetState(DrawableHitObject obj)
{
Tracking = false;
IsSpinning.Value = false;
mousePosition = default;
lastAngle = currentRotation = Rotation = 0;
rotationTransferred = false;
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (drawableSpinner != null)
drawableSpinner.HitObjectApplied -= resetState;
}
2018-04-13 09:19:50 +00:00
}
}