2017-06-01 13:26:21 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System ;
using System.Collections.Generic ;
using System.Linq ;
2017-06-09 15:11:31 +08:00
using osu.Framework.Configuration ;
2017-06-01 13:26:21 +08:00
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
using osu.Game.Rulesets.Objects.Drawables ;
2017-06-02 19:17:44 +08:00
namespace osu.Game.Rulesets.Timing
2017-06-01 13:26:21 +08:00
{
2017-06-09 02:36:15 +08:00
/// <summary>
2017-06-09 15:11:31 +08:00
/// A collection of <see cref="SpeedAdjustmentContainer"/>s.
///
2017-06-09 02:36:15 +08:00
/// <para>
2017-06-12 14:20:34 +08:00
/// This container redirects any <see cref="DrawableHitObject"/>'s added to it to the <see cref="SpeedAdjustmentContainer"/>
/// which provides the speed adjustment active at the start time of the hit object. Furthermore, this container provides the
/// necessary <see cref="VisibleTimeRange"/> for the contained <see cref="SpeedAdjustmentContainer"/>s.
2017-06-09 02:36:15 +08:00
/// </para>
/// </summary>
2017-06-09 15:11:31 +08:00
public class SpeedAdjustmentCollection : Container < SpeedAdjustmentContainer >
2017-06-01 13:26:21 +08:00
{
2017-06-09 15:11:31 +08:00
private readonly BindableDouble visibleTimeRange = new BindableDouble ( ) ;
2017-06-09 00:14:14 +08:00
/// <summary>
2017-06-12 14:20:34 +08:00
/// Gets or sets the range of time that is visible by the length of this container.
2017-06-09 15:11:31 +08:00
/// For example, only hit objects with start time less than or equal to 1000 will be visible with <see cref="VisibleTimeRange"/> = 1000.
2017-06-09 00:14:14 +08:00
/// </summary>
2017-06-09 15:11:31 +08:00
public Bindable < double > VisibleTimeRange
{
get { return visibleTimeRange ; }
set { visibleTimeRange . BindTo ( value ) ; }
}
2017-06-01 13:26:21 +08:00
2017-07-12 02:21:58 +08:00
protected override int Compare ( Drawable x , Drawable y )
{
var xSpeedAdjust = x as SpeedAdjustmentContainer ;
var ySpeedAdjust = y as SpeedAdjustmentContainer ;
// If either of the two drawables are not hit objects, fall back to the base comparer
if ( xSpeedAdjust ? . ControlPoint = = null | | ySpeedAdjust ? . ControlPoint = = null )
return CompareReverseChildID ( x , y ) ;
// Compare by start time
int i = ySpeedAdjust . ControlPoint . StartTime . CompareTo ( xSpeedAdjust . ControlPoint . StartTime ) ;
return i ! = 0 ? i : CompareReverseChildID ( x , y ) ;
}
2017-06-16 08:38:06 +08:00
/// <summary>
2017-06-16 09:56:33 +08:00
/// Hit objects that are to be re-processed on the next update.
2017-06-16 08:38:06 +08:00
/// </summary>
private readonly Queue < DrawableHitObject > queuedHitObjects = new Queue < DrawableHitObject > ( ) ;
2017-06-16 12:00:08 +08:00
private readonly Axes scrollingAxes ;
/// <summary>
/// Creates a new <see cref="SpeedAdjustmentCollection"/>.
/// </summary>
/// <param name="scrollingAxes">The axes upon which hit objects should appear to scroll inside this container.</param>
public SpeedAdjustmentCollection ( Axes scrollingAxes )
{
this . scrollingAxes = scrollingAxes ;
}
2017-06-19 09:54:23 +08:00
public override void Add ( SpeedAdjustmentContainer speedAdjustment )
{
speedAdjustment . VisibleTimeRange . BindTo ( VisibleTimeRange ) ;
speedAdjustment . ScrollingAxes = scrollingAxes ;
base . Add ( speedAdjustment ) ;
}
2017-06-01 13:26:21 +08:00
/// <summary>
2017-06-16 08:38:06 +08:00
/// Adds a hit object to this <see cref="SpeedAdjustmentCollection"/>. The hit objects will be kept in a queue
/// and will be processed when new <see cref="SpeedAdjustmentContainer"/>s are added to this <see cref="SpeedAdjustmentCollection"/>.
2017-06-01 13:26:21 +08:00
/// </summary>
/// <param name="hitObject">The hit object to add.</param>
public void Add ( DrawableHitObject hitObject )
{
2017-06-19 09:54:23 +08:00
if ( ! ( hitObject is IScrollingHitObject ) )
throw new InvalidOperationException ( $"Hit objects added to a {nameof(SpeedAdjustmentCollection)} must implement {nameof(IScrollingHitObject)}." ) ;
2017-06-01 13:26:21 +08:00
2017-06-19 09:54:23 +08:00
queuedHitObjects . Enqueue ( hitObject ) ;
2017-06-09 15:11:31 +08:00
}
2017-06-16 08:38:06 +08:00
protected override void Update ( )
{
base . Update ( ) ;
2017-06-16 09:56:33 +08:00
// Todo: At the moment this is going to re-process every single Update, however this will only be a null-op
// when there are no SpeedAdjustmentContainers available. This should probably error or something, but it's okay for now.
2017-06-16 09:53:02 +08:00
// An external count is kept because hit objects that can't be added are re-queued
int count = queuedHitObjects . Count ;
while ( count - - > 0 )
2017-06-16 08:38:06 +08:00
{
2017-06-16 09:53:02 +08:00
var hitObject = queuedHitObjects . Dequeue ( ) ;
2017-06-16 08:38:06 +08:00
2017-06-16 09:53:02 +08:00
var target = adjustmentContainerFor ( hitObject ) ;
if ( target = = null )
{
// We can't add this hit object to a speed adjustment container yet, so re-queue it
// for re-processing when the layout next invalidated
queuedHitObjects . Enqueue ( hitObject ) ;
continue ;
}
2017-06-16 08:38:06 +08:00
2017-06-16 09:53:02 +08:00
if ( hitObject . RelativePositionAxes ! = target . ScrollingAxes )
throw new InvalidOperationException ( $"Make sure to set all {nameof(DrawableHitObject)}'s {nameof(RelativePositionAxes)} are equal to the correct axes of scrolling ({target.ScrollingAxes})." ) ;
2017-06-16 08:38:06 +08:00
2017-06-16 09:53:02 +08:00
target . Add ( hitObject ) ;
2017-06-16 08:38:06 +08:00
}
}
2017-06-02 14:28:30 +08:00
2017-06-01 13:26:21 +08:00
/// <summary>
2017-06-12 14:20:34 +08:00
/// Finds the <see cref="SpeedAdjustmentContainer"/> which provides the speed adjustment active at the start time
/// of a hit object. If there is no <see cref="SpeedAdjustmentContainer"/> active at the start time of the hit object,
/// then the first (time-wise) speed adjustment is returned.
2017-06-01 13:26:21 +08:00
/// </summary>
2017-06-12 14:20:34 +08:00
/// <param name="hitObject">The hit object to find the active <see cref="SpeedAdjustmentContainer"/> for.</param>
/// <returns>The <see cref="SpeedAdjustmentContainer"/> active at <paramref name="hitObject"/>'s start time. Null if there are no speed adjustments.</returns>
private SpeedAdjustmentContainer adjustmentContainerFor ( DrawableHitObject hitObject ) = > Children . FirstOrDefault ( c = > c . CanContain ( hitObject ) ) ? ? Children . LastOrDefault ( ) ;
2017-06-02 14:28:30 +08:00
2017-06-16 08:38:06 +08:00
/// <summary>
/// Finds the <see cref="SpeedAdjustmentContainer"/> which provides the speed adjustment active at a time.
/// If there is no <see cref="SpeedAdjustmentContainer"/> active at the time, then the first (time-wise) speed adjustment is returned.
/// </summary>
/// <param name="time">The time to find the active <see cref="SpeedAdjustmentContainer"/> at.</param>
/// <returns>The <see cref="SpeedAdjustmentContainer"/> active at <paramref name="time"/>. Null if there are no speed adjustments.</returns>
private SpeedAdjustmentContainer adjustmentContainerAt ( double time ) = > Children . FirstOrDefault ( c = > c . CanContain ( time ) ) ? ? Children . LastOrDefault ( ) ;
2017-06-01 13:26:21 +08:00
}
2017-06-16 09:56:33 +08:00
}