1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-16 03:42:55 +08:00

Merge pull request #7588 from peppy/i-beat-snapping

Move beat snapping to its own interface
This commit is contained in:
Dan Balasescu 2020-01-23 16:04:34 +09:00 committed by GitHub
commit a9e4bdff33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 76 additions and 25 deletions

View File

@ -5,6 +5,7 @@ using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Testing; using osu.Framework.Testing;
using osu.Game.Beatmaps.ControlPoints; using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Osu; using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Beatmaps; using osu.Game.Rulesets.Osu.Beatmaps;
using osu.Game.Rulesets.Osu.Edit; using osu.Game.Rulesets.Osu.Edit;
@ -19,7 +20,13 @@ namespace osu.Game.Tests.Editor
private TestHitObjectComposer composer; private TestHitObjectComposer composer;
[Cached(typeof(EditorBeatmap))] [Cached(typeof(EditorBeatmap))]
private readonly EditorBeatmap editorBeatmap = new EditorBeatmap(new OsuBeatmap()); [Cached(typeof(IBeatSnapProvider))]
private readonly EditorBeatmap editorBeatmap;
public TestSceneHitObjectComposerDistanceSnapping()
{
editorBeatmap = new EditorBeatmap(new OsuBeatmap(), BeatDivisor);
}
[SetUp] [SetUp]
public void Setup() => Schedule(() => public void Setup() => Schedule(() =>

View File

@ -3,6 +3,7 @@
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Osu; using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Beatmaps; using osu.Game.Rulesets.Osu.Beatmaps;
using osu.Game.Screens.Edit; using osu.Game.Screens.Edit;
@ -14,6 +15,7 @@ namespace osu.Game.Tests.Visual.Editor
public class TestSceneComposeScreen : EditorClockTestScene public class TestSceneComposeScreen : EditorClockTestScene
{ {
[Cached(typeof(EditorBeatmap))] [Cached(typeof(EditorBeatmap))]
[Cached(typeof(IBeatSnapProvider))]
private readonly EditorBeatmap editorBeatmap = private readonly EditorBeatmap editorBeatmap =
new EditorBeatmap(new OsuBeatmap new EditorBeatmap(new OsuBeatmap
{ {

View File

@ -66,6 +66,7 @@ namespace osu.Game.Tests.Visual.Editor
Dependencies.CacheAs<IAdjustableClock>(clock); Dependencies.CacheAs<IAdjustableClock>(clock);
Dependencies.CacheAs<IFrameBasedClock>(clock); Dependencies.CacheAs<IFrameBasedClock>(clock);
Dependencies.CacheAs(editorBeatmap); Dependencies.CacheAs(editorBeatmap);
Dependencies.CacheAs<IBeatSnapProvider>(editorBeatmap);
Child = new OsuHitObjectComposer(new OsuRuleset()); Child = new OsuHitObjectComposer(new OsuRuleset());
} }

View File

@ -46,7 +46,7 @@ namespace osu.Game.Rulesets.Edit
private IAdjustableClock adjustableClock { get; set; } private IAdjustableClock adjustableClock { get; set; }
[Resolved] [Resolved]
private BindableBeatDivisor beatDivisor { get; set; } private IBeatSnapProvider beatSnapProvider { get; set; }
private IBeatmapProcessor beatmapProcessor; private IBeatmapProcessor beatmapProcessor;
@ -257,40 +257,26 @@ namespace osu.Game.Rulesets.Edit
public override float GetBeatSnapDistanceAt(double referenceTime) public override float GetBeatSnapDistanceAt(double referenceTime)
{ {
DifficultyControlPoint difficultyPoint = EditorBeatmap.ControlPointInfo.DifficultyPointAt(referenceTime); DifficultyControlPoint difficultyPoint = EditorBeatmap.ControlPointInfo.DifficultyPointAt(referenceTime);
return (float)(100 * EditorBeatmap.BeatmapInfo.BaseDifficulty.SliderMultiplier * difficultyPoint.SpeedMultiplier / beatDivisor.Value); return (float)(100 * EditorBeatmap.BeatmapInfo.BaseDifficulty.SliderMultiplier * difficultyPoint.SpeedMultiplier / beatSnapProvider.BeatDivisor);
} }
public override float DurationToDistance(double referenceTime, double duration) public override float DurationToDistance(double referenceTime, double duration)
{ {
double beatLength = EditorBeatmap.ControlPointInfo.TimingPointAt(referenceTime).BeatLength / beatDivisor.Value; double beatLength = beatSnapProvider.GetBeatLengthAtTime(referenceTime);
return (float)(duration / beatLength * GetBeatSnapDistanceAt(referenceTime)); return (float)(duration / beatLength * GetBeatSnapDistanceAt(referenceTime));
} }
public override double DistanceToDuration(double referenceTime, float distance) public override double DistanceToDuration(double referenceTime, float distance)
{ {
double beatLength = EditorBeatmap.ControlPointInfo.TimingPointAt(referenceTime).BeatLength / beatDivisor.Value; double beatLength = beatSnapProvider.GetBeatLengthAtTime(referenceTime);
return distance / GetBeatSnapDistanceAt(referenceTime) * beatLength; return distance / GetBeatSnapDistanceAt(referenceTime) * beatLength;
} }
public override double GetSnappedDurationFromDistance(double referenceTime, float distance) public override double GetSnappedDurationFromDistance(double referenceTime, float distance)
=> beatSnap(referenceTime, DistanceToDuration(referenceTime, distance)); => beatSnapProvider.SnapTime(referenceTime, DistanceToDuration(referenceTime, distance));
public override float GetSnappedDistanceFromDistance(double referenceTime, float distance) public override float GetSnappedDistanceFromDistance(double referenceTime, float distance)
=> DurationToDistance(referenceTime, beatSnap(referenceTime, DistanceToDuration(referenceTime, distance))); => DurationToDistance(referenceTime, beatSnapProvider.SnapTime(referenceTime, DistanceToDuration(referenceTime, distance)));
/// <summary>
/// Snaps a duration to the closest beat of a timing point applicable at the reference time.
/// </summary>
/// <param name="referenceTime">The time of the timing point which <paramref name="duration"/> resides in.</param>
/// <param name="duration">The duration to snap.</param>
/// <returns>A value that represents <paramref name="duration"/> snapped to the closest beat of the timing point.</returns>
private double beatSnap(double referenceTime, double duration)
{
double beatLength = EditorBeatmap.ControlPointInfo.TimingPointAt(referenceTime).BeatLength / beatDivisor.Value;
// A 1ms offset prevents rounding errors due to minute variations in duration
return (int)((duration + 1) / beatLength) * beatLength;
}
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {

View File

@ -0,0 +1,28 @@
// 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.
namespace osu.Game.Rulesets.Edit
{
public interface IBeatSnapProvider
{
/// <summary>
/// Snaps a duration to the closest beat of a timing point applicable at the reference time.
/// </summary>
/// <param name="referenceTime">The time of the timing point which <paramref name="duration"/> resides in.</param>
/// <param name="duration">The duration to snap.</param>
/// <returns>A value that represents <paramref name="duration"/> snapped to the closest beat of the timing point.</returns>
double SnapTime(double referenceTime, double duration);
/// <summary>
/// Get the most appropriate beat length at a given time.
/// </summary>
/// <param name="referenceTime">A reference time used for lookup.</param>
/// <returns>The most appropriate beat length.</returns>
double GetBeatLengthAtTime(double referenceTime);
/// <summary>
/// Returns the current beat divisor.
/// </summary>
int BeatDivisor { get; }
}
}

View File

@ -26,6 +26,7 @@ using osu.Framework.Input.Bindings;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Graphics.Cursor; using osu.Game.Graphics.Cursor;
using osu.Game.Input.Bindings; using osu.Game.Input.Bindings;
using osu.Game.Rulesets.Edit;
using osu.Game.Screens.Edit.Compose; using osu.Game.Screens.Edit.Compose;
using osu.Game.Screens.Edit.Setup; using osu.Game.Screens.Edit.Setup;
using osu.Game.Screens.Edit.Timing; using osu.Game.Screens.Edit.Timing;
@ -34,7 +35,8 @@ using osu.Game.Users;
namespace osu.Game.Screens.Edit namespace osu.Game.Screens.Edit
{ {
public class Editor : ScreenWithBeatmapBackground, IKeyBindingHandler<GlobalAction> [Cached(typeof(IBeatSnapProvider))]
public class Editor : ScreenWithBeatmapBackground, IKeyBindingHandler<GlobalAction>, IBeatSnapProvider
{ {
public override float BackgroundParallaxAmount => 0.1f; public override float BackgroundParallaxAmount => 0.1f;
@ -77,11 +79,14 @@ namespace osu.Game.Screens.Edit
clock.ChangeSource(sourceClock); clock.ChangeSource(sourceClock);
playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Beatmap.Value.BeatmapInfo.Ruleset); playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Beatmap.Value.BeatmapInfo.Ruleset);
editorBeatmap = new EditorBeatmap(playableBeatmap); editorBeatmap = new EditorBeatmap(playableBeatmap, beatDivisor);
dependencies.CacheAs<IFrameBasedClock>(clock); dependencies.CacheAs<IFrameBasedClock>(clock);
dependencies.CacheAs<IAdjustableClock>(clock); dependencies.CacheAs<IAdjustableClock>(clock);
// todo: remove caching of this and consume via editorBeatmap?
dependencies.Cache(beatDivisor); dependencies.Cache(beatDivisor);
dependencies.CacheAs(editorBeatmap); dependencies.CacheAs(editorBeatmap);
EditorMenuBar menuBar; EditorMenuBar menuBar;
@ -347,5 +352,11 @@ namespace osu.Game.Screens.Edit
saveBeatmap(); saveBeatmap();
beatmapManager.Export(Beatmap.Value.BeatmapSetInfo); beatmapManager.Export(Beatmap.Value.BeatmapSetInfo);
} }
public double SnapTime(double referenceTime, double duration) => editorBeatmap.SnapTime(referenceTime, duration);
public double GetBeatLengthAtTime(double referenceTime) => editorBeatmap.GetBeatLengthAtTime(referenceTime);
public int BeatDivisor => beatDivisor.Value;
} }
} }

View File

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