2019-01-24 16:43:03 +08:00
|
|
|
|
// 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 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
2020-09-21 18:39:54 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2020-07-29 19:01:01 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-10-02 11:02:47 +08:00
|
|
|
|
using osu.Framework.Input.Events;
|
2020-01-09 03:21:13 +08:00
|
|
|
|
using osu.Framework.Utils;
|
2020-09-21 18:39:54 +08:00
|
|
|
|
using osu.Game.Screens.Play;
|
2020-07-29 19:01:01 +08:00
|
|
|
|
using osuTK;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces
|
|
|
|
|
{
|
2020-07-29 19:01:01 +08:00
|
|
|
|
public class SpinnerRotationTracker : CircularContainer
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
private readonly Spinner spinner;
|
|
|
|
|
|
|
|
|
|
public override bool IsPresent => true; // handle input when hidden
|
|
|
|
|
|
2020-07-29 19:01:01 +08:00
|
|
|
|
public SpinnerRotationTracker(Spinner s)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
spinner = s;
|
|
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 13:01:15 +08:00
|
|
|
|
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-07-29 19:01:01 +08:00
|
|
|
|
public bool Tracking { get; set; }
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2020-07-29 19:01:01 +08:00
|
|
|
|
public readonly BindableBool Complete = new BindableBool();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-07-09 03:05:41 +08:00
|
|
|
|
/// <summary>
|
2020-08-11 04:17:47 +08:00
|
|
|
|
/// The total rotation performed on the spinner disc, disregarding the spin direction,
|
|
|
|
|
/// adjusted for the track's playback rate.
|
2020-07-09 03:05:41 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
2020-08-11 04:17:47 +08:00
|
|
|
|
/// <para>
|
2020-07-09 03:05:41 +08:00
|
|
|
|
/// This value is always non-negative and is monotonically increasing with time
|
|
|
|
|
/// (i.e. will only increase if time is passing forward, but can decrease during rewind).
|
2020-08-11 04:17:47 +08:00
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// The rotation from each frame is multiplied by the clock's current playback rate.
|
|
|
|
|
/// The reason this is done is to ensure that spinners give the same score and require the same number of spins
|
|
|
|
|
/// regardless of whether speed-modifying mods are applied.
|
|
|
|
|
/// </para>
|
2020-07-09 03:05:41 +08:00
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <example>
|
2020-08-11 04:17:47 +08:00
|
|
|
|
/// Assuming no speed-modifying mods are active,
|
|
|
|
|
/// if the spinner is spun 360 degrees clockwise and then 360 degrees counter-clockwise,
|
2020-07-09 03:05:41 +08:00
|
|
|
|
/// this property will return the value of 720 (as opposed to 0 for <see cref="Drawable.Rotation"/>).
|
2020-08-11 04:17:47 +08:00
|
|
|
|
/// If Double Time is active instead (with a speed multiplier of 1.5x),
|
|
|
|
|
/// in the same scenario the property will return 720 * 1.5 = 1080.
|
2020-07-09 03:05:41 +08:00
|
|
|
|
/// </example>
|
2020-08-11 04:17:47 +08:00
|
|
|
|
public float RateAdjustedRotation { get; private set; }
|
2020-07-09 03:05:41 +08:00
|
|
|
|
|
2020-07-30 18:34:59 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the spinning is spinning at a reasonable speed to be considered visually spinning.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public readonly BindableBool IsSpinning = new BindableBool();
|
|
|
|
|
|
2020-03-29 13:31:03 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether currently in the correct time range to allow spinning.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private bool isSpinnableTime => spinner.StartTime <= Time.Current && spinner.EndTime > Time.Current;
|
2020-02-08 08:59:35 +08:00
|
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
|
protected override bool OnMouseMove(MouseMoveEvent e)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2018-09-19 19:52:57 +08:00
|
|
|
|
mousePosition = Parent.ToLocalSpace(e.ScreenSpaceMousePosition);
|
2018-10-02 11:02:47 +08:00
|
|
|
|
return base.OnMouseMove(e);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Vector2 mousePosition;
|
|
|
|
|
|
|
|
|
|
private float lastAngle;
|
|
|
|
|
private float currentRotation;
|
2019-09-08 21:14:14 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
private bool rotationTransferred;
|
|
|
|
|
|
2020-09-21 21:30:14 +08:00
|
|
|
|
[Resolved(canBeNull: true)]
|
2020-09-21 18:39:54 +08:00
|
|
|
|
private GameplayClock gameplayClock { get; set; }
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
2020-02-08 09:51:32 +08:00
|
|
|
|
var thisAngle = -MathUtils.RadiansToDegrees(MathF.Atan2(mousePosition.X - DrawSize.X / 2, mousePosition.Y - DrawSize.Y / 2));
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-02-08 09:51:32 +08:00
|
|
|
|
var delta = thisAngle - lastAngle;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-07-29 19:01:01 +08:00
|
|
|
|
if (Tracking)
|
|
|
|
|
AddRotation(delta);
|
2020-02-05 14:23:59 +08:00
|
|
|
|
|
2020-02-08 09:51:32 +08:00
|
|
|
|
lastAngle = thisAngle;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-07-30 18:34:59 +08: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 17:19:50 +08:00
|
|
|
|
}
|
2020-02-05 14:23:59 +08:00
|
|
|
|
|
2020-03-29 13:31:03 +08: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>
|
2020-07-29 19:01:01 +08:00
|
|
|
|
public void AddRotation(float angle)
|
2020-02-05 14:23:59 +08:00
|
|
|
|
{
|
2020-03-29 13:31:03 +08:00
|
|
|
|
if (!isSpinnableTime)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-02-05 14:23:59 +08: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;
|
2020-08-11 04:17:47 +08:00
|
|
|
|
// rate has to be applied each frame, because it's not guaranteed to be constant throughout playback
|
|
|
|
|
// (see: ModTimeRamp)
|
2020-09-21 21:30:14 +08:00
|
|
|
|
RateAdjustedRotation += (float)(Math.Abs(angle) * (gameplayClock?.TrueGameplayRate ?? Clock.Rate));
|
2020-02-05 14:23:59 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|