// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Input.Events; using osu.Framework.Utils; using osuTK; namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces { public class SpinnerRotationTracker : CircularContainer { private readonly Spinner spinner; public override bool IsPresent => true; // handle input when hidden public SpinnerRotationTracker(Spinner s) { spinner = s; RelativeSizeAxes = Axes.Both; } public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true; public bool Tracking { get; set; } public readonly BindableBool Complete = new BindableBool(); /// /// The total rotation performed on the spinner disc, disregarding the spin direction. /// /// /// 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). /// /// /// If the spinner is spun 360 degrees clockwise and then 360 degrees counter-clockwise, /// this property will return the value of 720 (as opposed to 0 for ). /// public float CumulativeRotation { get; private set; } /// /// Whether currently in the correct time range to allow spinning. /// private bool isSpinnableTime => spinner.StartTime <= Time.Current && spinner.EndTime > Time.Current; protected override bool OnMouseMove(MouseMoveEvent e) { mousePosition = Parent.ToLocalSpace(e.ScreenSpaceMousePosition); return base.OnMouseMove(e); } private Vector2 mousePosition; private float lastAngle; private float currentRotation; private bool rotationTransferred; protected override void Update() { base.Update(); var thisAngle = -MathUtils.RadiansToDegrees(MathF.Atan2(mousePosition.X - DrawSize.X / 2, mousePosition.Y - DrawSize.Y / 2)); var delta = thisAngle - lastAngle; if (Tracking) AddRotation(delta); lastAngle = thisAngle; Rotation = (float)Interpolation.Lerp(Rotation, currentRotation / 2, Math.Clamp(Math.Abs(Time.Elapsed) / 40, 0, 1)); } /// /// Rotate the disc by the provided angle (in addition to any existing rotation). /// /// /// Will be a no-op if not a valid time to spin. /// /// The delta angle. public void AddRotation(float angle) { if (!isSpinnableTime) return; if (!rotationTransferred) { currentRotation = Rotation * 2; rotationTransferred = true; } if (angle > 180) { lastAngle += 360; angle -= 360; } else if (-angle > 180) { lastAngle -= 360; angle += 360; } currentRotation += angle; CumulativeRotation += Math.Abs(angle) * Math.Sign(Clock.ElapsedFrameTime); } } }