2019-01-24 16:43:03 +08: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.
2018-04-13 17:19:50 +08:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2021-06-16 15:24:30 +08:00
using JetBrains.Annotations ;
2018-04-13 17:19:50 +08:00
using Newtonsoft.Json ;
2019-10-28 10:39:53 +08:00
using osu.Framework.Bindables ;
2018-04-13 17:19:50 +08:00
using osu.Framework.Lists ;
2021-05-01 14:13:42 +08:00
using osu.Framework.Utils ;
2021-04-28 15:47:30 +08:00
using osu.Game.Screens.Edit ;
2021-07-19 11:38:22 +08:00
using osu.Game.Utils ;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Beatmaps.ControlPoints
{
[Serializable]
2021-07-19 11:38:22 +08:00
public class ControlPointInfo : IDeepCloneable < ControlPointInfo >
2018-04-13 17:19:50 +08:00
{
2019-10-25 18:48:01 +08:00
/// <summary>
2019-10-28 10:40:33 +08:00
/// All control points grouped by time.
2019-10-25 18:48:01 +08:00
/// </summary>
[JsonProperty]
2019-10-28 10:39:53 +08:00
public IBindableList < ControlPointGroup > Groups = > groups ;
2019-10-25 18:48:01 +08:00
2019-10-28 10:39:53 +08:00
private readonly BindableList < ControlPointGroup > groups = new BindableList < ControlPointGroup > ( ) ;
2019-10-25 18:48:01 +08:00
2018-04-13 17:19:50 +08:00
/// <summary>
/// All timing points.
/// </summary>
[JsonProperty]
2019-10-25 18:48:01 +08:00
public IReadOnlyList < TimingControlPoint > TimingPoints = > timingPoints ;
private readonly SortedList < TimingControlPoint > timingPoints = new SortedList < TimingControlPoint > ( Comparer < TimingControlPoint > . Default ) ;
2018-04-13 17:19:50 +08:00
/// <summary>
/// All difficulty points.
/// </summary>
[JsonProperty]
2019-10-25 18:48:01 +08:00
public IReadOnlyList < DifficultyControlPoint > DifficultyPoints = > difficultyPoints ;
private readonly SortedList < DifficultyControlPoint > difficultyPoints = new SortedList < DifficultyControlPoint > ( Comparer < DifficultyControlPoint > . Default ) ;
2018-04-13 17:19:50 +08:00
/// <summary>
/// All sound points.
/// </summary>
[JsonProperty]
2020-11-02 13:39:01 +08:00
public IBindableList < SampleControlPoint > SamplePoints = > samplePoints ;
2019-10-25 18:48:01 +08:00
2020-11-02 13:39:01 +08:00
private readonly BindableList < SampleControlPoint > samplePoints = new BindableList < SampleControlPoint > ( ) ;
2018-04-13 17:19:50 +08:00
/// <summary>
/// All effect points.
/// </summary>
[JsonProperty]
2019-10-25 18:48:01 +08:00
public IReadOnlyList < EffectControlPoint > EffectPoints = > effectPoints ;
private readonly SortedList < EffectControlPoint > effectPoints = new SortedList < EffectControlPoint > ( Comparer < EffectControlPoint > . Default ) ;
/// <summary>
/// All control points, of all types.
/// </summary>
2020-04-21 12:58:23 +08:00
[JsonIgnore]
2019-10-25 18:48:01 +08:00
public IEnumerable < ControlPoint > AllControlPoints = > Groups . SelectMany ( g = > g . ControlPoints ) . ToArray ( ) ;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Finds the difficulty control point that is active at <paramref name="time"/>.
/// </summary>
/// <param name="time">The time to find the difficulty control point at.</param>
/// <returns>The difficulty control point.</returns>
2021-06-16 15:24:30 +08:00
[NotNull]
2020-07-18 10:53:04 +08:00
public DifficultyControlPoint DifficultyPointAt ( double time ) = > binarySearchWithFallback ( DifficultyPoints , time , DifficultyControlPoint . DEFAULT ) ;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Finds the effect control point that is active at <paramref name="time"/>.
/// </summary>
/// <param name="time">The time to find the effect control point at.</param>
/// <returns>The effect control point.</returns>
2021-06-16 15:24:30 +08:00
[NotNull]
2020-07-18 10:53:04 +08:00
public EffectControlPoint EffectPointAt ( double time ) = > binarySearchWithFallback ( EffectPoints , time , EffectControlPoint . DEFAULT ) ;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Finds the sound control point that is active at <paramref name="time"/>.
/// </summary>
/// <param name="time">The time to find the sound control point at.</param>
/// <returns>The sound control point.</returns>
2021-06-16 15:24:30 +08:00
[NotNull]
2020-07-19 13:11:21 +08:00
public SampleControlPoint SamplePointAt ( double time ) = > binarySearchWithFallback ( SamplePoints , time , SamplePoints . Count > 0 ? SamplePoints [ 0 ] : SampleControlPoint . DEFAULT ) ;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Finds the timing control point that is active at <paramref name="time"/>.
/// </summary>
/// <param name="time">The time to find the timing control point at.</param>
/// <returns>The timing control point.</returns>
2021-06-16 15:24:30 +08:00
[NotNull]
2020-07-19 13:11:21 +08:00
public TimingControlPoint TimingPointAt ( double time ) = > binarySearchWithFallback ( TimingPoints , time , TimingPoints . Count > 0 ? TimingPoints [ 0 ] : TimingControlPoint . DEFAULT ) ;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Finds the maximum BPM represented by any timing control point.
/// </summary>
[JsonIgnore]
public double BPMMaximum = >
2020-07-18 10:53:04 +08:00
60000 / ( TimingPoints . OrderBy ( c = > c . BeatLength ) . FirstOrDefault ( ) ? ? TimingControlPoint . DEFAULT ) . BeatLength ;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Finds the minimum BPM represented by any timing control point.
/// </summary>
[JsonIgnore]
public double BPMMinimum = >
2020-07-18 10:53:04 +08:00
60000 / ( TimingPoints . OrderByDescending ( c = > c . BeatLength ) . FirstOrDefault ( ) ? ? TimingControlPoint . DEFAULT ) . BeatLength ;
2018-04-13 17:19:50 +08:00
2019-10-28 10:40:33 +08:00
/// <summary>
/// Remove all <see cref="ControlPointGroup"/>s and return to a pristine state.
/// </summary>
public void Clear ( )
{
groups . Clear ( ) ;
timingPoints . Clear ( ) ;
difficultyPoints . Clear ( ) ;
samplePoints . Clear ( ) ;
effectPoints . Clear ( ) ;
}
/// <summary>
/// Add a new <see cref="ControlPoint"/>. Note that the provided control point may not be added if the correct state is already present at the provided time.
/// </summary>
/// <param name="time">The time at which the control point should be added.</param>
/// <param name="controlPoint">The control point to add.</param>
/// <returns>Whether the control point was added.</returns>
public bool Add ( double time , ControlPoint controlPoint )
{
if ( checkAlreadyExisting ( time , controlPoint ) )
return false ;
GroupAt ( time , true ) . Add ( controlPoint ) ;
return true ;
}
public ControlPointGroup GroupAt ( double time , bool addIfNotExisting = false )
{
var newGroup = new ControlPointGroup ( time ) ;
int i = groups . BinarySearch ( newGroup ) ;
if ( i > = 0 )
return groups [ i ] ;
if ( addIfNotExisting )
{
newGroup . ItemAdded + = groupItemAdded ;
newGroup . ItemRemoved + = groupItemRemoved ;
groups . Insert ( ~ i , newGroup ) ;
return newGroup ;
}
return null ;
}
public void RemoveGroup ( ControlPointGroup group )
{
2020-10-02 14:25:35 +08:00
foreach ( var item in group . ControlPoints . ToArray ( ) )
group . Remove ( item ) ;
2019-10-28 10:40:33 +08:00
group . ItemAdded - = groupItemAdded ;
group . ItemRemoved - = groupItemRemoved ;
groups . Remove ( group ) ;
}
2021-04-28 15:47:30 +08:00
/// <summary>
/// Returns the time on the given beat divisor closest to the given time.
/// </summary>
/// <param name="time">The time to find the closest snapped time to.</param>
/// <param name="beatDivisor">The beat divisor to snap to.</param>
/// <param name="referenceTime">An optional reference point to use for timing point lookup.</param>
2021-04-28 15:59:49 +08:00
public double GetClosestSnappedTime ( double time , int beatDivisor , double? referenceTime = null )
2021-04-28 15:47:30 +08:00
{
var timingPoint = TimingPointAt ( referenceTime ? ? time ) ;
2021-04-28 16:16:05 +08:00
return getClosestSnappedTime ( timingPoint , time , beatDivisor ) ;
2021-04-28 15:47:30 +08:00
}
/// <summary>
2021-04-28 16:16:05 +08:00
/// Returns the time on *ANY* valid beat divisor, favouring the divisor closest to the given time.
2021-04-28 15:47:30 +08:00
/// </summary>
/// <param name="time">The time to find the closest snapped time to.</param>
2021-04-28 16:16:05 +08:00
public double GetClosestSnappedTime ( double time ) = > GetClosestSnappedTime ( time , GetClosestBeatDivisor ( time ) ) ;
2021-04-28 15:47:30 +08:00
/// <summary>
2021-04-28 16:16:05 +08:00
/// Returns the beat snap divisor closest to the given time. If two are equally close, the smallest divisor is returned.
2021-04-28 15:47:30 +08:00
/// </summary>
/// <param name="time">The time to find the closest beat snap divisor to.</param>
/// <param name="referenceTime">An optional reference point to use for timing point lookup.</param>
2021-04-28 15:57:52 +08:00
public int GetClosestBeatDivisor ( double time , double? referenceTime = null )
2021-04-28 15:47:30 +08:00
{
2021-04-28 16:16:05 +08:00
TimingControlPoint timingPoint = TimingPointAt ( referenceTime ? ? time ) ;
int closestDivisor = 0 ;
double closestTime = double . MaxValue ;
foreach ( int divisor in BindableBeatDivisor . VALID_DIVISORS )
{
double distanceFromSnap = Math . Abs ( time - getClosestSnappedTime ( timingPoint , time , divisor ) ) ;
2021-04-28 15:47:30 +08:00
2021-05-01 14:13:42 +08:00
if ( Precision . DefinitelyBigger ( closestTime , distanceFromSnap ) )
2021-04-28 16:16:05 +08:00
{
closestDivisor = divisor ;
closestTime = distanceFromSnap ;
}
}
2021-04-28 15:47:30 +08:00
2021-04-28 16:16:05 +08:00
return closestDivisor ;
}
private static double getClosestSnappedTime ( TimingControlPoint timingPoint , double time , int beatDivisor )
{
var beatLength = timingPoint . BeatLength / beatDivisor ;
var beatLengths = ( int ) Math . Round ( ( time - timingPoint . Time ) / beatLength , MidpointRounding . AwayFromZero ) ;
return timingPoint . Time + beatLengths * beatLength ;
2021-04-28 15:47:30 +08:00
}
2018-04-13 17:19:50 +08:00
/// <summary>
/// Binary searches one of the control point lists to find the active control point at <paramref name="time"/>.
2019-10-26 07:31:41 +08:00
/// Includes logic for returning a specific point when no matching point is found.
2018-04-13 17:19:50 +08:00
/// </summary>
/// <param name="list">The list to search.</param>
/// <param name="time">The time to find the control point at.</param>
2020-07-18 10:53:04 +08:00
/// <param name="fallback">The control point to use when <paramref name="time"/> is before any control points.</param>
2019-10-26 07:31:41 +08:00
/// <returns>The active control point at <paramref name="time"/>, or a fallback <see cref="ControlPoint"/> if none found.</returns>
2020-07-18 10:53:04 +08:00
private T binarySearchWithFallback < T > ( IReadOnlyList < T > list , double time , T fallback )
2020-07-18 11:06:41 +08:00
where T : ControlPoint
2019-10-26 07:31:41 +08:00
{
2020-07-18 10:53:04 +08:00
return binarySearch ( list , time ) ? ? fallback ;
2019-10-26 07:31:41 +08:00
}
/// <summary>
/// Binary searches one of the control point lists to find the active control point at <paramref name="time"/>.
/// </summary>
/// <param name="list">The list to search.</param>
/// <param name="time">The time to find the control point at.</param>
/// <returns>The active control point at <paramref name="time"/>.</returns>
private T binarySearch < T > ( IReadOnlyList < T > list , double time )
where T : ControlPoint
2018-04-13 17:19:50 +08:00
{
if ( list = = null )
throw new ArgumentNullException ( nameof ( list ) ) ;
if ( list . Count = = 0 )
2019-10-26 07:31:41 +08:00
return null ;
2018-04-13 17:19:50 +08:00
if ( time < list [ 0 ] . Time )
2019-10-26 07:31:41 +08:00
return null ;
2018-04-13 17:19:50 +08:00
2019-12-14 20:54:22 +08:00
if ( time > = list [ ^ 1 ] . Time )
return list [ ^ 1 ] ;
2018-10-17 10:39:29 +08:00
2018-10-17 10:23:18 +08:00
int l = 0 ;
2018-10-17 10:39:29 +08:00
int r = list . Count - 2 ;
2018-04-13 17:19:50 +08:00
2018-10-17 10:23:18 +08:00
while ( l < = r )
{
int pivot = l + ( ( r - l ) > > 1 ) ;
2018-04-13 17:19:50 +08:00
2018-10-17 10:23:18 +08:00
if ( list [ pivot ] . Time < time )
l = pivot + 1 ;
else if ( list [ pivot ] . Time > time )
r = pivot - 1 ;
else
return list [ pivot ] ;
}
2018-04-13 17:19:50 +08:00
2018-10-17 10:23:18 +08:00
// l will be the first control point with Time > time, but we want the one before it
return list [ l - 1 ] ;
2018-04-13 17:19:50 +08:00
}
2019-10-25 18:48:01 +08:00
2019-10-26 10:38:05 +08:00
/// <summary>
2019-11-17 20:49:36 +08:00
/// Check whether <paramref name="newPoint"/> should be added.
2019-10-26 10:38:05 +08:00
/// </summary>
/// <param name="time">The time to find the timing control point at.</param>
/// <param name="newPoint">A point to be added.</param>
/// <returns>Whether the new point should be added.</returns>
private bool checkAlreadyExisting ( double time , ControlPoint newPoint )
{
ControlPoint existing = null ;
switch ( newPoint )
{
case TimingControlPoint _ :
// Timing points are a special case and need to be added regardless of fallback availability.
existing = binarySearch ( TimingPoints , time ) ;
break ;
case EffectControlPoint _ :
existing = EffectPointAt ( time ) ;
break ;
case SampleControlPoint _ :
2019-12-13 17:12:44 +08:00
existing = binarySearch ( SamplePoints , time ) ;
2019-10-26 10:38:05 +08:00
break ;
case DifficultyControlPoint _ :
existing = DifficultyPointAt ( time ) ;
break ;
}
2020-04-17 16:10:13 +08:00
return newPoint ? . IsRedundant ( existing ) = = true ;
2019-10-26 10:38:05 +08:00
}
2019-10-28 10:40:33 +08:00
private void groupItemAdded ( ControlPoint controlPoint )
2019-10-25 18:48:01 +08:00
{
2019-10-28 10:40:33 +08:00
switch ( controlPoint )
2019-10-25 18:48:01 +08:00
{
case TimingControlPoint typed :
2019-10-28 10:40:33 +08:00
timingPoints . Add ( typed ) ;
2019-10-25 18:48:01 +08:00
break ;
case EffectControlPoint typed :
2019-10-28 10:40:33 +08:00
effectPoints . Add ( typed ) ;
2019-10-25 18:48:01 +08:00
break ;
case SampleControlPoint typed :
2019-10-28 10:40:33 +08:00
samplePoints . Add ( typed ) ;
2019-10-25 18:48:01 +08:00
break ;
case DifficultyControlPoint typed :
2019-10-28 10:40:33 +08:00
difficultyPoints . Add ( typed ) ;
2019-10-25 18:48:01 +08:00
break ;
}
}
2019-10-28 10:40:33 +08:00
private void groupItemRemoved ( ControlPoint controlPoint )
2019-10-25 18:48:01 +08:00
{
2019-10-28 10:40:33 +08:00
switch ( controlPoint )
2019-10-25 18:48:01 +08:00
{
case TimingControlPoint typed :
2019-10-28 10:40:33 +08:00
timingPoints . Remove ( typed ) ;
2019-10-25 18:48:01 +08:00
break ;
case EffectControlPoint typed :
2019-10-28 10:40:33 +08:00
effectPoints . Remove ( typed ) ;
2019-10-25 18:48:01 +08:00
break ;
case SampleControlPoint typed :
2019-10-28 10:40:33 +08:00
samplePoints . Remove ( typed ) ;
2019-10-25 18:48:01 +08:00
break ;
case DifficultyControlPoint typed :
2019-10-28 10:40:33 +08:00
difficultyPoints . Remove ( typed ) ;
2019-10-25 18:48:01 +08:00
break ;
}
}
2021-01-04 15:37:07 +08:00
2021-07-19 11:38:22 +08:00
public ControlPointInfo DeepClone ( )
2021-01-04 15:37:07 +08:00
{
var controlPointInfo = new ControlPointInfo ( ) ;
foreach ( var point in AllControlPoints )
2021-07-19 11:38:22 +08:00
controlPointInfo . Add ( point . Time , point . DeepClone ( ) ) ;
2021-01-04 15:37:07 +08:00
return controlPointInfo ;
}
2018-04-13 17:19:50 +08:00
}
}