2019-09-10 04:29:50 +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.
|
|
|
|
|
2020-05-17 20:08:49 +00:00
|
|
|
using System;
|
2019-09-10 04:29:50 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-01-09 04:43:44 +00:00
|
|
|
using osu.Framework.Utils;
|
2019-09-10 04:29:50 +00:00
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Objects
|
|
|
|
{
|
2019-09-24 21:03:55 +00:00
|
|
|
public class BarLineGenerator<TBarLine>
|
|
|
|
where TBarLine : class, IBarLine, new()
|
2019-09-10 04:29:50 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The generated bar lines.
|
|
|
|
/// </summary>
|
2019-09-24 21:03:55 +00:00
|
|
|
public readonly List<TBarLine> BarLines = new List<TBarLine>();
|
2019-09-10 04:29:50 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Constructs and generates bar lines for provided beatmap.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="beatmap">The beatmap to generate bar lines for.</param>
|
|
|
|
public BarLineGenerator(IBeatmap beatmap)
|
|
|
|
{
|
|
|
|
if (beatmap.HitObjects.Count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
HitObject lastObject = beatmap.HitObjects.Last();
|
2019-11-25 13:37:07 +00:00
|
|
|
double lastHitTime = 1 + lastObject.GetEndTime();
|
2019-09-10 04:29:50 +00:00
|
|
|
|
|
|
|
var timingPoints = beatmap.ControlPointInfo.TimingPoints;
|
|
|
|
|
|
|
|
if (timingPoints.Count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (int i = 0; i < timingPoints.Count; i++)
|
|
|
|
{
|
|
|
|
TimingControlPoint currentTimingPoint = timingPoints[i];
|
|
|
|
int currentBeat = 0;
|
|
|
|
|
|
|
|
// Stop on the beat before the next timing point, or if there is no next timing point stop slightly past the last object
|
|
|
|
double endTime = i < timingPoints.Count - 1 ? timingPoints[i + 1].Time - currentTimingPoint.BeatLength : lastHitTime + currentTimingPoint.BeatLength * (int)currentTimingPoint.TimeSignature;
|
|
|
|
|
|
|
|
double barLength = currentTimingPoint.BeatLength * (int)currentTimingPoint.TimeSignature;
|
|
|
|
|
|
|
|
for (double t = currentTimingPoint.Time; Precision.DefinitelyBigger(endTime, t); t += barLength, currentBeat++)
|
|
|
|
{
|
2020-05-17 20:08:49 +00:00
|
|
|
var roundedTime = Math.Round(t, MidpointRounding.AwayFromZero);
|
|
|
|
|
|
|
|
// in the case of some bar lengths, rounding errors can cause t to be slightly less than
|
|
|
|
// the expected whole number value due to floating point inaccuracies.
|
|
|
|
// if this is the case, apply rounding.
|
|
|
|
if (Precision.AlmostEquals(t, roundedTime))
|
|
|
|
{
|
|
|
|
t = roundedTime;
|
|
|
|
}
|
|
|
|
|
2019-09-24 21:03:55 +00:00
|
|
|
BarLines.Add(new TBarLine
|
2019-09-10 04:29:50 +00:00
|
|
|
{
|
|
|
|
StartTime = t,
|
|
|
|
Major = currentBeat % (int)currentTimingPoint.TimeSignature == 0
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|