Move beat snapping to its own interface

This commit is contained in:
Dean Herbert
2020-01-23 13:33:55 +09:00
parent cb09c2e144
commit 56c044c44a
9 changed files with 90 additions and 63 deletions

View File

@ -8,11 +8,12 @@ using osu.Framework.Bindables;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Beatmaps.Timing;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Objects;
namespace osu.Game.Screens.Edit
{
public class EditorBeatmap : IBeatmap
public class EditorBeatmap : IBeatmap, IBeatSnapProvider
{
/// <summary>
/// Invoked when a <see cref="HitObject"/> is added to this <see cref="EditorBeatmap"/>.
@ -33,11 +34,14 @@ namespace osu.Game.Screens.Edit
public readonly IBeatmap PlayableBeatmap;
private readonly BindableBeatDivisor beatDivisor;
private readonly Dictionary<HitObject, Bindable<double>> startTimeBindables = new Dictionary<HitObject, Bindable<double>>();
public EditorBeatmap(IBeatmap playableBeatmap)
public EditorBeatmap(IBeatmap playableBeatmap, BindableBeatDivisor beatDivisor = null)
{
PlayableBeatmap = playableBeatmap;
this.beatDivisor = beatDivisor;
foreach (var obj in HitObjects)
trackStartTime(obj);
@ -123,5 +127,17 @@ namespace osu.Game.Screens.Edit
return list.Count - 1;
}
public double SnapTime(double referenceTime, double duration, int beatDivisor)
{
double beatLength = GetBeatLengthAtTime(referenceTime, beatDivisor);
// A 1ms offset prevents rounding errors due to minute variations in duration
return (int)((duration + 1) / beatLength) * beatLength;
}
public double GetBeatLengthAtTime(double referenceTime, int beatDivisor) => ControlPointInfo.TimingPointAt(referenceTime).BeatLength / BeatDivisor;
public int BeatDivisor => beatDivisor?.Value ?? 1;
}
}