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
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2018-04-06 08:40:06 +00:00
|
|
|
|
using System;
|
2022-08-18 05:11:42 +00:00
|
|
|
|
using System.Diagnostics;
|
2018-04-06 08:40:06 +00:00
|
|
|
|
using System.Linq;
|
2020-09-24 09:55:49 +00:00
|
|
|
|
using osu.Framework.Audio.Track;
|
2020-09-24 10:01:28 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2020-05-22 07:40:52 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
2022-08-18 05:11:42 +00:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-05-22 07:40:52 +00:00
|
|
|
|
using osu.Framework.Graphics.Transforms;
|
2018-04-06 08:40:06 +00:00
|
|
|
|
using osu.Framework.Timing;
|
2020-09-28 09:16:19 +00:00
|
|
|
|
using osu.Framework.Utils;
|
2018-05-08 13:21:54 +00:00
|
|
|
|
using osu.Game.Beatmaps;
|
2018-04-06 08:40:06 +00:00
|
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-04-06 08:40:06 +00:00
|
|
|
|
namespace osu.Game.Screens.Edit
|
|
|
|
|
{
|
2018-04-12 12:04:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A decoupled clock which adds editor-specific functionality, such as snapping to a user-defined beat divisor.
|
|
|
|
|
/// </summary>
|
2022-08-18 05:11:42 +00:00
|
|
|
|
public class EditorClock : CompositeComponent, IFrameBasedClock, IAdjustableClock, ISourceChangeableClock
|
2018-04-06 08:40:06 +00:00
|
|
|
|
{
|
2020-09-24 10:01:28 +00:00
|
|
|
|
public IBindable<Track> Track => track;
|
|
|
|
|
|
|
|
|
|
private readonly Bindable<Track> track = new Bindable<Track>();
|
|
|
|
|
|
|
|
|
|
public double TrackLength => track.Value?.Length ?? 60000;
|
2018-05-08 13:21:54 +00:00
|
|
|
|
|
2021-09-14 14:56:57 +00:00
|
|
|
|
public ControlPointInfo ControlPointInfo => Beatmap.ControlPointInfo;
|
|
|
|
|
|
|
|
|
|
public IBeatmap Beatmap { get; set; }
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-04-06 08:40:06 +00:00
|
|
|
|
private readonly BindableBeatDivisor beatDivisor;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-08-18 05:11:42 +00:00
|
|
|
|
private readonly FramedBeatmapClock underlyingClock;
|
2020-05-22 07:40:52 +00:00
|
|
|
|
|
2020-03-13 23:42:05 +00:00
|
|
|
|
private bool playbackFinished;
|
2020-03-12 05:40:08 +00:00
|
|
|
|
|
2020-10-27 05:31:56 +00:00
|
|
|
|
public IBindable<bool> SeekingOrStopped => seekingOrStopped;
|
2020-09-29 03:45:20 +00:00
|
|
|
|
|
2020-10-27 05:31:56 +00:00
|
|
|
|
private readonly Bindable<bool> seekingOrStopped = new Bindable<bool>(true);
|
2020-09-29 03:45:20 +00:00
|
|
|
|
|
2021-01-15 07:14:38 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether a seek is currently in progress. True for the duration of a seek performed via <see cref="SeekSmoothlyTo"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool IsSeeking { get; private set; }
|
|
|
|
|
|
2021-09-14 14:56:57 +00:00
|
|
|
|
public EditorClock(IBeatmap beatmap = null, BindableBeatDivisor beatDivisor = null)
|
2018-04-06 08:40:06 +00:00
|
|
|
|
{
|
2021-09-14 14:56:57 +00:00
|
|
|
|
Beatmap = beatmap ?? new Beatmap();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-09-14 14:56:57 +00:00
|
|
|
|
this.beatDivisor = beatDivisor ?? new BindableBeatDivisor();
|
2020-05-22 07:40:52 +00:00
|
|
|
|
|
2022-08-26 09:08:39 +00:00
|
|
|
|
underlyingClock = new FramedBeatmapClock(applyOffsets: true) { IsCoupled = false };
|
2022-08-18 05:11:42 +00:00
|
|
|
|
AddInternal(underlyingClock);
|
2018-04-06 08:40:06 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-04-12 12:04:45 +00:00
|
|
|
|
/// <summary>
|
2018-04-12 12:17:17 +00:00
|
|
|
|
/// Seek to the closest snappable beat from a time.
|
2018-04-12 12:04:45 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="position">The raw position which should be seeked around.</param>
|
|
|
|
|
/// <returns>Whether the seek could be performed.</returns>
|
2018-04-06 08:40:06 +00:00
|
|
|
|
public bool SeekSnapped(double position)
|
|
|
|
|
{
|
2018-04-06 09:38:44 +00:00
|
|
|
|
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
|
|
|
|
|
2018-04-06 08:40:06 +00:00
|
|
|
|
// We will be snapping to beats within the timing point
|
|
|
|
|
position -= timingPoint.Time;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-04-06 08:40:06 +00:00
|
|
|
|
// 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;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-04-06 08:40:06 +00:00
|
|
|
|
// 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-06 08:40:06 +00:00
|
|
|
|
if (position > nextTimingPoint?.Time)
|
|
|
|
|
position = nextTimingPoint.Time;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-04-06 08:40:06 +00:00
|
|
|
|
return Seek(position);
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-04-06 08:40:06 +00:00
|
|
|
|
/// <summary>
|
2018-04-12 12:17:17 +00:00
|
|
|
|
/// Seeks backwards by one beat length.
|
2018-04-06 08:40:06 +00:00
|
|
|
|
/// </summary>
|
2018-04-12 12:17:17 +00:00
|
|
|
|
/// <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>
|
2020-10-06 08:49:12 +00:00
|
|
|
|
public void SeekBackward(bool snapped = false, double amount = 1) => seek(-1, snapped, amount + (IsRunning ? 1.5 : 0));
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-04-06 08:40:06 +00:00
|
|
|
|
/// <summary>
|
2018-04-12 12:17:17 +00:00
|
|
|
|
/// Seeks forwards by one beat length.
|
2018-04-06 08:40:06 +00:00
|
|
|
|
/// </summary>
|
2018-04-12 12:17:17 +00:00
|
|
|
|
/// <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-06 08:40:06 +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-06 08:40:06 +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
|
|
|
|
|
2018-04-06 09:38:44 +00:00
|
|
|
|
if (!snapped || ControlPointInfo.TimingPoints.Count == 0)
|
2018-04-06 08:40:06 +00:00
|
|
|
|
{
|
2021-01-15 07:14:21 +00:00
|
|
|
|
SeekSmoothlyTo(seekTime);
|
2018-04-06 08:40:06 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-04-06 08:40:06 +00:00
|
|
|
|
// We will be snapping to beats within timingPoint
|
|
|
|
|
seekTime -= timingPoint.Time;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-04-06 08:40:06 +00:00
|
|
|
|
// 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);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-04-06 08:40:06 +00:00
|
|
|
|
seekTime = timingPoint.Time + closestBeat * seekAmount;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
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-06 08:40:06 +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-06 08:40:06 +00:00
|
|
|
|
{
|
|
|
|
|
closestBeat += direction > 0 ? 1 : -1;
|
|
|
|
|
seekTime = timingPoint.Time + closestBeat * seekAmount;
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-06-20 07:53:03 +00:00
|
|
|
|
if (seekTime < timingPoint.Time && !ReferenceEquals(timingPoint, ControlPointInfo.TimingPoints.First()))
|
2018-04-06 08:40:06 +00:00
|
|
|
|
seekTime = timingPoint.Time;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-01-15 07:14:21 +00:00
|
|
|
|
SeekSmoothlyTo(seekTime);
|
2020-05-22 07:40:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-01-15 07:14:21 +00:00
|
|
|
|
/// The current time of this clock, include any active transform seeks performed via <see cref="SeekSmoothlyTo"/>.
|
2020-05-22 07:40:52 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public double CurrentTimeAccurate =>
|
|
|
|
|
Transforms.OfType<TransformSeek>().FirstOrDefault()?.EndValue ?? CurrentTime;
|
|
|
|
|
|
|
|
|
|
public double CurrentTime => underlyingClock.CurrentTime;
|
|
|
|
|
|
2022-08-29 07:42:50 +00:00
|
|
|
|
public double TotalAppliedOffset => underlyingClock.TotalAppliedOffset;
|
|
|
|
|
|
2020-05-22 07:40:52 +00:00
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
ClearTransforms();
|
|
|
|
|
underlyingClock.Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
|
|
|
|
ClearTransforms();
|
2021-03-17 22:31:16 +00:00
|
|
|
|
|
|
|
|
|
if (playbackFinished)
|
|
|
|
|
underlyingClock.Seek(0);
|
|
|
|
|
|
2020-05-22 07:40:52 +00:00
|
|
|
|
underlyingClock.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
2020-10-27 05:31:56 +00:00
|
|
|
|
seekingOrStopped.Value = true;
|
2020-05-22 07:40:52 +00:00
|
|
|
|
underlyingClock.Stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Seek(double position)
|
|
|
|
|
{
|
2021-01-15 07:14:38 +00:00
|
|
|
|
seekingOrStopped.Value = IsSeeking = true;
|
2020-09-29 03:45:20 +00:00
|
|
|
|
|
2020-05-22 07:40:52 +00:00
|
|
|
|
ClearTransforms();
|
2021-08-16 18:36:46 +00:00
|
|
|
|
|
|
|
|
|
// Ensure the sought point is within the boundaries
|
|
|
|
|
position = Math.Clamp(position, 0, TrackLength);
|
2020-05-22 07:40:52 +00:00
|
|
|
|
return underlyingClock.Seek(position);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-15 07:14:21 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Seek smoothly to the provided destination.
|
|
|
|
|
/// Use <see cref="Seek"/> to perform an immediate seek.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="seekDestination"></param>
|
|
|
|
|
public void SeekSmoothlyTo(double seekDestination)
|
|
|
|
|
{
|
|
|
|
|
seekingOrStopped.Value = true;
|
|
|
|
|
|
|
|
|
|
if (IsRunning)
|
|
|
|
|
Seek(seekDestination);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
transformSeekTo(seekDestination, transform_time, Easing.OutQuint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 07:40:52 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2021-03-17 17:18:19 +00:00
|
|
|
|
public void ProcessFrame()
|
|
|
|
|
{
|
2022-08-26 09:08:39 +00:00
|
|
|
|
// Noop to ensure an external consumer doesn't process the internal clock an extra time.
|
2021-03-17 17:18:19 +00:00
|
|
|
|
}
|
2020-05-22 07:40:52 +00:00
|
|
|
|
|
|
|
|
|
public double ElapsedFrameTime => underlyingClock.ElapsedFrameTime;
|
|
|
|
|
|
|
|
|
|
public double FramesPerSecond => underlyingClock.FramesPerSecond;
|
|
|
|
|
|
|
|
|
|
public FrameTimeInfo TimeInfo => underlyingClock.TimeInfo;
|
|
|
|
|
|
2020-09-24 09:55:49 +00:00
|
|
|
|
public void ChangeSource(IClock source)
|
|
|
|
|
{
|
2020-09-24 10:01:28 +00:00
|
|
|
|
track.Value = source as Track;
|
2020-09-24 09:55:49 +00:00
|
|
|
|
underlyingClock.ChangeSource(source);
|
|
|
|
|
}
|
2020-05-22 07:40:52 +00:00
|
|
|
|
|
|
|
|
|
public IClock Source => underlyingClock.Source;
|
|
|
|
|
|
|
|
|
|
private const double transform_time = 300;
|
|
|
|
|
|
2020-09-28 09:16:19 +00:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
2022-08-26 09:08:39 +00:00
|
|
|
|
// EditorClock wasn't being added in many places. This gives us more certainty that it is.
|
|
|
|
|
Debug.Assert(underlyingClock.LoadState > LoadState.NotLoaded);
|
|
|
|
|
|
|
|
|
|
playbackFinished = CurrentTime >= TrackLength;
|
|
|
|
|
|
|
|
|
|
if (playbackFinished)
|
|
|
|
|
{
|
|
|
|
|
if (IsRunning)
|
|
|
|
|
underlyingClock.Stop();
|
|
|
|
|
|
|
|
|
|
if (CurrentTime > TrackLength)
|
|
|
|
|
underlyingClock.Seek(TrackLength);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-29 03:45:20 +00:00
|
|
|
|
updateSeekingState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateSeekingState()
|
|
|
|
|
{
|
2020-10-27 05:31:56 +00:00
|
|
|
|
if (seekingOrStopped.Value)
|
2020-09-28 09:16:19 +00:00
|
|
|
|
{
|
2021-01-15 07:14:38 +00:00
|
|
|
|
IsSeeking &= Transforms.Any();
|
|
|
|
|
|
2020-09-29 03:45:20 +00:00
|
|
|
|
if (track.Value?.IsRunning != true)
|
|
|
|
|
{
|
|
|
|
|
// seeking in the editor can happen while the track isn't running.
|
|
|
|
|
// in this case we always want to expose ourselves as seeking (to avoid sample playback).
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-29 03:17:38 +00:00
|
|
|
|
|
2020-09-28 09:16:19 +00:00
|
|
|
|
// we are either running a seek tween or doing an immediate seek.
|
|
|
|
|
// in the case of an immediate seek the seeking bool will be set to false after one update.
|
|
|
|
|
// this allows for silencing hit sounds and the likes.
|
2021-01-15 07:14:38 +00:00
|
|
|
|
seekingOrStopped.Value = IsSeeking;
|
2020-09-28 09:16:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 07:40:52 +00:00
|
|
|
|
private void transformSeekTo(double seek, double duration = 0, Easing easing = Easing.None)
|
2021-08-16 18:36:46 +00:00
|
|
|
|
=> this.TransformTo(this.PopulateTransform(new TransformSeek(), Math.Clamp(seek, 0, TrackLength), duration, easing));
|
2020-05-22 07:40:52 +00:00
|
|
|
|
|
|
|
|
|
private double currentTime
|
|
|
|
|
{
|
|
|
|
|
get => underlyingClock.CurrentTime;
|
|
|
|
|
set => underlyingClock.Seek(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class TransformSeek : Transform<double, EditorClock>
|
|
|
|
|
{
|
|
|
|
|
public override string TargetMember => nameof(currentTime);
|
|
|
|
|
|
2020-11-02 12:08:58 +00:00
|
|
|
|
protected override void Apply(EditorClock clock, double time) => clock.currentTime = valueAt(time);
|
|
|
|
|
|
|
|
|
|
private double valueAt(double time)
|
|
|
|
|
{
|
|
|
|
|
if (time < StartTime) return StartValue;
|
|
|
|
|
if (time >= EndTime) return EndValue;
|
|
|
|
|
|
|
|
|
|
return Interpolation.ValueAt(time, StartValue, EndValue, StartTime, EndTime, Easing);
|
|
|
|
|
}
|
2020-05-22 07:40:52 +00:00
|
|
|
|
|
|
|
|
|
protected override void ReadIntoStartValue(EditorClock clock) => StartValue = clock.currentTime;
|
2018-04-06 08:40:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|