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;
|
2023-10-16 18:23:35 +08:00
|
|
|
|
using System.Diagnostics;
|
2020-09-21 18:39:54 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2020-07-29 19:01:01 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2022-11-09 12:36:52 +08:00
|
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
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-11-15 06:23:11 +08:00
|
|
|
|
using osu.Game.Rulesets.Objects.Drawables;
|
2020-12-04 19:21:53 +08:00
|
|
|
|
using osu.Game.Rulesets.Osu.Objects.Drawables;
|
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
|
|
|
|
|
2020-12-04 19:21:53 +08:00
|
|
|
|
namespace osu.Game.Rulesets.Osu.Skinning.Default
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2022-11-24 13:32:20 +08:00
|
|
|
|
public partial class SpinnerRotationTracker : CircularContainer
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
public override bool IsPresent => true; // handle input when hidden
|
|
|
|
|
|
2020-11-05 13:40:48 +08:00
|
|
|
|
private readonly DrawableSpinner drawableSpinner;
|
|
|
|
|
|
2023-09-30 01:19:11 +08:00
|
|
|
|
private Vector2? mousePosition;
|
|
|
|
|
private float? lastAngle;
|
2022-11-09 12:36:52 +08:00
|
|
|
|
|
|
|
|
|
private float currentRotation;
|
|
|
|
|
private bool rotationTransferred;
|
|
|
|
|
|
|
|
|
|
[Resolved(canBeNull: true)]
|
|
|
|
|
private IGameplayClock? gameplayClock { get; set; }
|
|
|
|
|
|
2020-11-05 13:40:48 +08:00
|
|
|
|
public SpinnerRotationTracker(DrawableSpinner drawableSpinner)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2020-11-05 13:40:48 +08:00
|
|
|
|
this.drawableSpinner = drawableSpinner;
|
2020-11-15 06:23:11 +08:00
|
|
|
|
drawableSpinner.HitObjectApplied += resetState;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
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-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>
|
2020-11-05 13:40:48 +08:00
|
|
|
|
private bool isSpinnableTime => drawableSpinner.HitObject.StartTime <= Time.Current && drawableSpinner.HitObject.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
|
|
|
|
{
|
2023-10-17 16:40:44 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
2023-09-30 01:19:11 +08:00
|
|
|
|
if (mousePosition is Vector2 pos)
|
|
|
|
|
{
|
|
|
|
|
float thisAngle = -MathUtils.RadiansToDegrees(MathF.Atan2(pos.X - DrawSize.X / 2, pos.Y - DrawSize.Y / 2));
|
|
|
|
|
float delta = lastAngle == null ? 0 : thisAngle - lastAngle.Value;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2023-10-16 18:23:35 +08:00
|
|
|
|
// Normalise the delta to -180 .. 180
|
|
|
|
|
if (delta > 180) delta -= 360;
|
|
|
|
|
if (delta < -180) delta += 360;
|
|
|
|
|
|
2023-09-30 01:19:11 +08:00
|
|
|
|
if (Tracking)
|
|
|
|
|
AddRotation(delta);
|
2020-02-05 14:23:59 +08:00
|
|
|
|
|
2023-09-30 01:19:11 +08:00
|
|
|
|
lastAngle = thisAngle;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2023-07-25 18:21:20 +08:00
|
|
|
|
IsSpinning.Value = isSpinnableTime && Math.Abs(currentRotation - Rotation) > 10f;
|
|
|
|
|
Rotation = (float)Interpolation.Damp(Rotation, currentRotation, 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>
|
2023-10-16 18:23:35 +08:00
|
|
|
|
/// <param name="delta">The delta angle.</param>
|
|
|
|
|
public void AddRotation(float delta)
|
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)
|
|
|
|
|
{
|
2023-10-16 18:34:55 +08:00
|
|
|
|
currentRotation = Rotation;
|
2020-02-05 14:23:59 +08:00
|
|
|
|
rotationTransferred = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-31 04:15:40 +08:00
|
|
|
|
Debug.Assert(Math.Abs(delta) <= 180);
|
|
|
|
|
|
2023-10-16 18:23:35 +08:00
|
|
|
|
double rate = gameplayClock?.GetTrueGameplayRate() ?? Clock.Rate;
|
2023-10-16 18:07:03 +08:00
|
|
|
|
delta = (float)(delta * Math.Abs(rate));
|
2023-10-16 18:23:35 +08:00
|
|
|
|
|
2023-10-16 18:07:03 +08:00
|
|
|
|
currentRotation += delta;
|
|
|
|
|
drawableSpinner.Result.History.ReportDelta(Time.Current, delta);
|
2020-02-05 14:23:59 +08:00
|
|
|
|
}
|
2020-11-15 06:23:11 +08:00
|
|
|
|
|
|
|
|
|
private void resetState(DrawableHitObject obj)
|
|
|
|
|
{
|
|
|
|
|
Tracking = false;
|
|
|
|
|
IsSpinning.Value = false;
|
2023-09-30 01:19:11 +08:00
|
|
|
|
mousePosition = null;
|
|
|
|
|
lastAngle = null;
|
|
|
|
|
currentRotation = 0;
|
|
|
|
|
Rotation = 0;
|
2020-11-15 06:23:11 +08:00
|
|
|
|
rotationTransferred = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
2022-11-09 12:36:52 +08:00
|
|
|
|
if (drawableSpinner.IsNotNull())
|
2020-11-15 06:23:11 +08:00
|
|
|
|
drawableSpinner.HitObjectApplied -= resetState;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|