// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using osu.Framework.Utils; using osu.Game.Beatmaps; using osu.Game.Beatmaps.ControlPoints; using osu.Game.Screens.Edit; namespace osu.Game.Rulesets.Objects { /// /// Used to find the lowest beat divisor that a aligns to in an . /// public class BeatDivisorFinder { private readonly IBeatmap beatmap; /// /// Creates a new instance. /// /// The beatmap to use when calculating beat divisor alignment. public BeatDivisorFinder(IBeatmap beatmap) { this.beatmap = beatmap; } /// /// Finds the lowest beat divisor that the given aligns to. /// Returns 0 if it does not align to any divisor. /// /// The to evaluate. public int FindDivisor(HitObject hitObject) { TimingControlPoint currentTimingPoint = beatmap.ControlPointInfo.TimingPointAt(hitObject.StartTime); double snapResult = hitObject.StartTime - currentTimingPoint.Time; foreach (var divisor in BindableBeatDivisor.VALID_DIVISORS) { if (almostDivisibleBy(snapResult, currentTimingPoint.BeatLength / divisor)) return divisor; } return 0; } private const double leniency_ms = 1.0; private static bool almostDivisibleBy(double dividend, double divisor) { double remainder = Math.Abs(dividend) % divisor; return Precision.AlmostEquals(remainder, 0, leniency_ms) || Precision.AlmostEquals(remainder - divisor, 0, leniency_ms); } } }