1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 14:37:30 +08:00
osu-lazer/osu.Game/Screens/Edit/EditorClock.cs

133 lines
5.6 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 System.Linq;
using osu.Framework.MathUtils;
using osu.Framework.Timing;
using osu.Game.Beatmaps;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps.ControlPoints;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Edit
{
/// <summary>
/// A decoupled clock which adds editor-specific functionality, such as snapping to a user-defined beat divisor.
/// </summary>
public class EditorClock : DecoupleableInterpolatingFramedClock
{
2018-05-21 18:23:39 +08:00
public readonly double TrackLength;
2018-04-13 17:19:50 +08:00
public ControlPointInfo ControlPointInfo;
private readonly BindableBeatDivisor beatDivisor;
public EditorClock(WorkingBeatmap beatmap, BindableBeatDivisor beatDivisor)
{
this.beatDivisor = beatDivisor;
ControlPointInfo = beatmap.Beatmap.ControlPointInfo;
TrackLength = beatmap.Track.Length;
}
2018-05-21 18:23:39 +08:00
2018-05-08 21:37:06 +08:00
public EditorClock(ControlPointInfo controlPointInfo, double trackLength, BindableBeatDivisor beatDivisor)
2018-04-13 17:19:50 +08:00
{
this.beatDivisor = beatDivisor;
ControlPointInfo = controlPointInfo;
2018-05-08 21:37:06 +08:00
TrackLength = trackLength;
2018-04-13 17:19:50 +08: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 17:56:34 +08:00
double beatSnapLength = timingPoint.BeatLength / beatDivisor.Value;
2018-04-13 17:19:50 +08: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 18:48:01 +08:00
var nextTimingPoint = ControlPointInfo.TimingPoints.FirstOrDefault(t => t.Time > timingPoint.Time);
2018-04-13 17:19:50 +08: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>
/// <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 17:19:50 +08:00
/// <summary>
/// Seeks forwards by one beat length.
/// </summary>
/// <param name="snapped">Whether to snap to the closest beat after seeking.</param>
/// <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 17:19:50 +08:00
private void seek(int direction, bool snapped, double amount = 1)
2018-04-13 17:19:50 +08:00
{
if (amount <= 0) throw new ArgumentException("Value should be greater than zero", nameof(amount));
2018-04-13 17:19:50 +08:00
var timingPoint = ControlPointInfo.TimingPointAt(CurrentTime);
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
if (direction < 0 && timingPoint.Time == CurrentTime)
// 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
2019-10-30 18:09:46 +08:00
timingPoint = ControlPointInfo.TimingPointAt(CurrentTime - 1);
2018-04-13 17:19:50 +08:00
2019-02-21 17:56:34 +08:00
double seekAmount = timingPoint.BeatLength / beatDivisor.Value * amount;
2018-04-13 17:19:50 +08:00
double seekTime = CurrentTime + seekAmount * direction;
if (!snapped || ControlPointInfo.TimingPoints.Count == 0)
{
Seek(seekTime);
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;
// 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
if (Precision.AlmostEquals(CurrentTime, seekTime))
{
closestBeat += direction > 0 ? 1 : -1;
seekTime = timingPoint.Time + closestBeat * seekAmount;
}
if (seekTime < timingPoint.Time && timingPoint != ControlPointInfo.TimingPoints.First())
seekTime = timingPoint.Time;
2019-10-25 18:48:01 +08:00
var nextTimingPoint = ControlPointInfo.TimingPoints.FirstOrDefault(t => t.Time > timingPoint.Time);
2018-04-13 17:19:50 +08:00
if (seekTime > nextTimingPoint?.Time)
seekTime = nextTimingPoint.Time;
2018-05-08 21:37:06 +08:00
// Ensure the sought point is within the boundaries
2018-05-12 20:09:53 +08:00
seekTime = MathHelper.Clamp(seekTime, 0, TrackLength);
2018-04-13 17:19:50 +08:00
Seek(seekTime);
}
}
}