1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-24 18:47:32 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Objects/Drawables/Pieces/SpinnerDisc.cs

178 lines
5.3 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 17:19:50 +08:00
using System;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
using osu.Framework.Utils;
using osu.Game.Skinning;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces
{
public class SpinnerDisc : CircularContainer, IHasAccentColour
{
private readonly Spinner spinner;
private Color4 accentColour;
2018-04-13 17:19:50 +08:00
public Color4 AccentColour
{
get => accentColour;
set
{
accentColour = value;
if (background.Drawable is IHasAccentColour accent)
accent.AccentColour = value;
}
2018-04-13 17:19:50 +08:00
}
private readonly SkinnableDrawable background;
2018-04-13 17:19:50 +08:00
private const float idle_alpha = 0.2f;
private const float tracking_alpha = 0.4f;
public override bool IsPresent => true; // handle input when hidden
public SpinnerDisc(Spinner s)
{
spinner = s;
RelativeSizeAxes = Axes.Both;
Children = new Drawable[]
{
background = new SkinnableDrawable(new OsuSkinComponent(OsuSkinComponents.SpinnerDisc), _ => new SpinnerFill { Alpha = idle_alpha }),
2018-04-13 17:19:50 +08:00
};
}
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => true;
2018-04-13 17:19:50 +08:00
private bool tracking;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public bool Tracking
{
get => tracking;
2018-04-13 17:19:50 +08:00
set
{
if (value == tracking) return;
2019-02-28 12:31:40 +08:00
tracking = value;
2018-04-13 17:19:50 +08:00
// todo: new default only
background.Drawable.FadeTo(tracking ? tracking_alpha : idle_alpha, 100);
2018-04-13 17:19:50 +08:00
}
}
private bool complete;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
public bool Complete
{
get => complete;
2018-04-13 17:19:50 +08:00
set
{
if (value == complete) return;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
complete = value;
updateCompleteTick();
}
}
/// <summary>
/// The total rotation performed on the spinner disc, disregarding the spin direction.
/// </summary>
/// <remarks>
/// 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).
/// </remarks>
/// <example>
/// 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 <see cref="Drawable.Rotation"/>).
/// </example>
public float CumulativeRotation;
/// <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
{
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;
private int completeTick;
private bool updateCompleteTick() => completeTick != (completeTick = (int)(CumulativeRotation / 360));
2018-04-13 17:19:50 +08:00
private bool rotationTransferred;
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
if (tracking)
2020-02-08 09:51:32 +08:00
Rotate(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
if (Complete && updateCompleteTick())
{
// todo: new default only
background.Drawable.FinishTransforms(false, nameof(Alpha));
background.Drawable
.FadeTo(tracking_alpha + 0.2f, 60, Easing.OutExpo)
.Then()
.FadeTo(tracking_alpha, 250, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
Rotation = (float)Interpolation.Lerp(Rotation, currentRotation / 2, Math.Clamp(Math.Abs(Time.Elapsed) / 40, 0, 1));
2018-04-13 17:19:50 +08:00
}
2020-02-05 14:23:59 +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-02-05 14:23:59 +08:00
public void Rotate(float angle)
{
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;
CumulativeRotation += Math.Abs(angle) * Math.Sign(Clock.ElapsedFrameTime);
2020-02-05 14:23:59 +08:00
}
2018-04-13 17:19:50 +08:00
}
}