2019-01-24 08:43:03 +00: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 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2020-05-22 07:40:52 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Transforms;
|
2020-01-09 04:43:44 +00:00
|
|
|
|
using osu.Framework.Utils;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Timing;
|
2018-05-08 13:21:54 +00:00
|
|
|
|
using osu.Game.Beatmaps;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A decoupled clock which adds editor-specific functionality, such as snapping to a user-defined beat divisor.
|
|
|
|
|
/// </summary>
|
2020-05-22 07:40:52 +00:00
|
|
|
|
public class EditorClock : Component, IFrameBasedClock, IAdjustableClock, ISourceChangeableClock
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2018-05-21 10:23:39 +00:00
|
|
|
|
public readonly double TrackLength;
|
2018-05-08 13:21:54 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public ControlPointInfo ControlPointInfo;
|
|
|
|
|
|
|
|
|
|
private readonly BindableBeatDivisor beatDivisor;
|
|
|
|
|
|
2020-05-22 07:40:52 +00:00
|
|
|
|
private readonly DecoupleableInterpolatingFramedClock underlyingClock;
|
|
|
|
|
|
2018-05-28 09:01:15 +00:00
|
|
|
|
public EditorClock(WorkingBeatmap beatmap, BindableBeatDivisor beatDivisor)
|
2020-05-22 07:40:52 +00:00
|
|
|
|
: this(beatmap.Beatmap.ControlPointInfo, beatmap.Track.Length, beatDivisor)
|
2018-05-08 13:21:54 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
2018-05-21 10:23:39 +00:00
|
|
|
|
|
2018-05-08 13:37:06 +00:00
|
|
|
|
public EditorClock(ControlPointInfo controlPointInfo, double trackLength, BindableBeatDivisor beatDivisor)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
this.beatDivisor = beatDivisor;
|
|
|
|
|
|
|
|
|
|
ControlPointInfo = controlPointInfo;
|
2018-05-08 13:37:06 +00:00
|
|
|
|
TrackLength = trackLength;
|
2020-05-22 07:40:52 +00:00
|
|
|
|
|
|
|
|
|
underlyingClock = new DecoupleableInterpolatingFramedClock();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 09:23:24 +00:00
|
|
|
|
public EditorClock()
|
|
|
|
|
: this(new ControlPointInfo(), 1000, new BindableBeatDivisor())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Seek to the closest snappable beat from a time.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position">The raw position which should be seeked around.</param>
|
|
|
|
|
/// <returns>Whether the seek could be performed.</returns>
|
|
|
|
|
public bool SeekSnapped(double position)
|
|
|
|
|
{
|
|
|
|
|
var timingPoint = ControlPointInfo.TimingPointAt(position);
|
2019-02-21 09:56:34 +00:00
|
|
|
|
double beatSnapLength = timingPoint.BeatLength / beatDivisor.Value;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
// We will be snapping to beats within the timing point
|
|
|
|
|
position -= timingPoint.Time;
|
|
|
|
|
|
|
|
|
|
// Determine the index from the current timing point of the closest beat to position
|
|
|
|
|
int closestBeat = (int)Math.Round(position / beatSnapLength);
|
|
|
|
|
position = timingPoint.Time + closestBeat * beatSnapLength;
|
|
|
|
|
|
|
|
|
|
// Depending on beatSnapLength, we may snap to a beat that is beyond timingPoint's end time, but we want to instead snap to
|
|
|
|
|
// the next timing point's start time
|
2019-10-25 10:48:01 +00:00
|
|
|
|
var nextTimingPoint = ControlPointInfo.TimingPoints.FirstOrDefault(t => t.Time > timingPoint.Time);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
if (position > nextTimingPoint?.Time)
|
|
|
|
|
position = nextTimingPoint.Time;
|
|
|
|
|
|
|
|
|
|
return Seek(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Seeks backwards by one beat length.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="snapped">Whether to snap to the closest beat after seeking.</param>
|
2018-11-30 05:57:25 +00:00
|
|
|
|
/// <param name="amount">The relative amount (magnitude) which should be seeked.</param>
|
|
|
|
|
public void SeekBackward(bool snapped = false, double amount = 1) => seek(-1, snapped, amount);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Seeks forwards by one beat length.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="snapped">Whether to snap to the closest beat after seeking.</param>
|
2018-11-30 05:57:25 +00:00
|
|
|
|
/// <param name="amount">The relative amount (magnitude) which should be seeked.</param>
|
|
|
|
|
public void SeekForward(bool snapped = false, double amount = 1) => seek(1, snapped, amount);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-11-30 05:57:25 +00:00
|
|
|
|
private void seek(int direction, bool snapped, double amount = 1)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2020-05-22 13:11:55 +00:00
|
|
|
|
double current = CurrentTimeAccurate;
|
2020-05-22 07:40:52 +00:00
|
|
|
|
|
2018-11-30 05:57:25 +00:00
|
|
|
|
if (amount <= 0) throw new ArgumentException("Value should be greater than zero", nameof(amount));
|
|
|
|
|
|
2020-05-22 07:40:52 +00:00
|
|
|
|
var timingPoint = ControlPointInfo.TimingPointAt(current);
|
2019-04-01 03:16:05 +00:00
|
|
|
|
|
2020-05-22 07:40:52 +00:00
|
|
|
|
if (direction < 0 && timingPoint.Time == current)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
// When going backwards and we're at the boundary of two timing points, we compute the seek distance with the timing point which we are seeking into
|
2020-05-22 07:40:52 +00:00
|
|
|
|
timingPoint = ControlPointInfo.TimingPointAt(current - 1);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-02-21 09:56:34 +00:00
|
|
|
|
double seekAmount = timingPoint.BeatLength / beatDivisor.Value * amount;
|
2020-05-22 07:40:52 +00:00
|
|
|
|
double seekTime = current + seekAmount * direction;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
if (!snapped || ControlPointInfo.TimingPoints.Count == 0)
|
|
|
|
|
{
|
2020-05-22 07:40:52 +00:00
|
|
|
|
SeekTo(seekTime);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We will be snapping to beats within timingPoint
|
|
|
|
|
seekTime -= timingPoint.Time;
|
|
|
|
|
|
|
|
|
|
// Determine the index from timingPoint of the closest beat to seekTime, accounting for scrolling direction
|
|
|
|
|
int closestBeat;
|
|
|
|
|
if (direction > 0)
|
|
|
|
|
closestBeat = (int)Math.Floor(seekTime / seekAmount);
|
|
|
|
|
else
|
|
|
|
|
closestBeat = (int)Math.Ceiling(seekTime / seekAmount);
|
|
|
|
|
|
|
|
|
|
seekTime = timingPoint.Time + closestBeat * seekAmount;
|
|
|
|
|
|
2020-07-17 07:40:02 +00:00
|
|
|
|
// limit forward seeking to only up to the next timing point's start time.
|
|
|
|
|
var nextTimingPoint = ControlPointInfo.TimingPoints.FirstOrDefault(t => t.Time > timingPoint.Time);
|
|
|
|
|
if (seekTime > nextTimingPoint?.Time)
|
|
|
|
|
seekTime = nextTimingPoint.Time;
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
// Due to the rounding above, we may end up on the current beat. This will effectively cause 0 seeking to happen, but we don't want this.
|
|
|
|
|
// Instead, we'll go to the next beat in the direction when this is the case
|
2020-07-17 07:40:02 +00:00
|
|
|
|
if (Precision.AlmostEquals(current, seekTime, 0.5f))
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
closestBeat += direction > 0 ? 1 : -1;
|
|
|
|
|
seekTime = timingPoint.Time + closestBeat * seekAmount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (seekTime < timingPoint.Time && timingPoint != ControlPointInfo.TimingPoints.First())
|
|
|
|
|
seekTime = timingPoint.Time;
|
|
|
|
|
|
2018-05-08 13:37:06 +00:00
|
|
|
|
// Ensure the sought point is within the boundaries
|
2019-11-20 12:19:49 +00:00
|
|
|
|
seekTime = Math.Clamp(seekTime, 0, TrackLength);
|
2020-05-22 07:40:52 +00:00
|
|
|
|
SeekTo(seekTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The current time of this clock, include any active transform seeks performed via <see cref="SeekTo"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public double CurrentTimeAccurate =>
|
|
|
|
|
Transforms.OfType<TransformSeek>().FirstOrDefault()?.EndValue ?? CurrentTime;
|
|
|
|
|
|
|
|
|
|
public double CurrentTime => underlyingClock.CurrentTime;
|
|
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
ClearTransforms();
|
|
|
|
|
underlyingClock.Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
ClearTransforms();
|
|
|
|
|
underlyingClock.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
underlyingClock.Stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Seek(double position)
|
|
|
|
|
{
|
|
|
|
|
ClearTransforms();
|
|
|
|
|
return underlyingClock.Seek(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetSpeedAdjustments() => underlyingClock.ResetSpeedAdjustments();
|
|
|
|
|
|
|
|
|
|
double IAdjustableClock.Rate
|
|
|
|
|
{
|
|
|
|
|
get => underlyingClock.Rate;
|
|
|
|
|
set => underlyingClock.Rate = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double IClock.Rate => underlyingClock.Rate;
|
|
|
|
|
|
|
|
|
|
public bool IsRunning => underlyingClock.IsRunning;
|
|
|
|
|
|
|
|
|
|
public void ProcessFrame() => underlyingClock.ProcessFrame();
|
|
|
|
|
|
|
|
|
|
public double ElapsedFrameTime => underlyingClock.ElapsedFrameTime;
|
|
|
|
|
|
|
|
|
|
public double FramesPerSecond => underlyingClock.FramesPerSecond;
|
|
|
|
|
|
|
|
|
|
public FrameTimeInfo TimeInfo => underlyingClock.TimeInfo;
|
|
|
|
|
|
|
|
|
|
public void ChangeSource(IClock source) => underlyingClock.ChangeSource(source);
|
|
|
|
|
|
|
|
|
|
public IClock Source => underlyingClock.Source;
|
|
|
|
|
|
|
|
|
|
public bool IsCoupled
|
|
|
|
|
{
|
|
|
|
|
get => underlyingClock.IsCoupled;
|
|
|
|
|
set => underlyingClock.IsCoupled = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const double transform_time = 300;
|
|
|
|
|
|
|
|
|
|
public void SeekTo(double seekDestination)
|
|
|
|
|
{
|
|
|
|
|
if (IsRunning)
|
|
|
|
|
Seek(seekDestination);
|
|
|
|
|
else
|
|
|
|
|
transformSeekTo(seekDestination, transform_time, Easing.OutQuint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void transformSeekTo(double seek, double duration = 0, Easing easing = Easing.None)
|
|
|
|
|
=> this.TransformTo(this.PopulateTransform(new TransformSeek(), seek, duration, easing));
|
|
|
|
|
|
|
|
|
|
private double currentTime
|
|
|
|
|
{
|
|
|
|
|
get => underlyingClock.CurrentTime;
|
|
|
|
|
set => underlyingClock.Seek(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class TransformSeek : Transform<double, EditorClock>
|
|
|
|
|
{
|
|
|
|
|
public override string TargetMember => nameof(currentTime);
|
|
|
|
|
|
|
|
|
|
protected override void Apply(EditorClock clock, double time) =>
|
|
|
|
|
clock.currentTime = Interpolation.ValueAt(time, StartValue, EndValue, StartTime, EndTime, Easing);
|
|
|
|
|
|
|
|
|
|
protected override void ReadIntoStartValue(EditorClock clock) => StartValue = clock.currentTime;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|