2017-06-09 15:11:31 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
2017-06-02 17:20:14 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2017-06-09 15:11:31 +08:00
using System.Collections.Generic ;
using System.Linq ;
using osu.Framework.Caching ;
2017-06-09 18:57:03 +08:00
using osu.Framework.Configuration ;
2017-06-01 13:26:21 +08:00
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
2017-06-09 15:11:31 +08:00
using osu.Game.Rulesets.Objects ;
2017-06-01 13:26:21 +08:00
using osu.Game.Rulesets.Objects.Drawables ;
2017-06-02 18:53:30 +08:00
using OpenTK ;
2017-06-16 18:21:54 +08:00
using osu.Game.Rulesets.Objects.Types ;
2017-06-01 13:26:21 +08:00
2017-06-09 18:57:03 +08:00
namespace osu.Game.Rulesets.Timing
2017-06-01 13:26:21 +08:00
{
2017-06-02 18:27:00 +08:00
/// <summary>
2017-06-09 15:11:31 +08:00
/// A collection of hit objects which scrolls within a <see cref="SpeedAdjustmentContainer"/>.
///
/// <para>
/// This container handles the conversion between time and position through <see cref="Container{T}.RelativeChildSize"/> and
/// <see cref="Container{T}.RelativeChildOffset"/> such that hit objects added to this container should have time values set as their
/// positions/sizes to make proper use of this container.
/// </para>
///
/// <para>
2017-06-12 14:20:34 +08:00
/// This container will auto-size to the total duration of the contained hit objects along the desired auto-sizing axes such that the resulting size
/// of this container will be a value representing the total duration of all contained hit objects.
2017-06-09 15:11:31 +08:00
/// </para>
///
/// <para>
2017-06-12 14:20:34 +08:00
/// This container is and must always be relatively-sized and positioned to its such that the parent can utilise <see cref="Container{T}.RelativeChildSize"/>
/// and <see cref="Container{T}.RelativeChildOffset"/> to apply further time offsets to this collection of hit objects.
2017-06-09 15:11:31 +08:00
/// </para>
2017-06-02 18:27:00 +08:00
/// </summary>
2017-06-08 22:40:24 +08:00
public abstract class DrawableTimingSection : Container < DrawableHitObject >
2017-06-01 13:26:21 +08:00
{
2017-06-09 15:11:31 +08:00
private readonly BindableDouble visibleTimeRange = new BindableDouble ( ) ;
2017-06-12 14:20:34 +08:00
/// <summary>
/// Gets or sets the range of time that is visible by the length of this container.
/// </summary>
2017-06-09 15:11:31 +08:00
public BindableDouble VisibleTimeRange
{
get { return visibleTimeRange ; }
set { visibleTimeRange . BindTo ( value ) ; }
}
2017-06-16 12:00:08 +08:00
/// <summary>
2017-06-16 18:21:54 +08:00
/// Axes through which this timing section scrolls. This is set by the <see cref="SpeedAdjustmentContainer"/>.
2017-06-16 12:00:08 +08:00
/// </summary>
internal Axes ScrollingAxes ;
2017-06-01 13:26:21 +08:00
2017-06-16 18:21:54 +08:00
/// <summary>
/// The control point that provides the speed adjustments for this container. This is set by the <see cref="SpeedAdjustmentContainer"/>.
/// </summary>
internal MultiplierControlPoint ControlPoint ;
protected override IComparer < Drawable > DepthComparer = > new HitObjectReverseStartTimeComparer ( ) ;
2017-06-02 18:27:00 +08:00
/// <summary>
2017-06-09 00:14:14 +08:00
/// Creates a new <see cref="DrawableTimingSection"/>.
2017-06-02 18:27:00 +08:00
/// </summary>
2017-06-16 12:00:08 +08:00
protected DrawableTimingSection ( )
2017-06-01 13:26:21 +08:00
{
2017-06-12 14:20:34 +08:00
RelativeSizeAxes = Axes . Both ;
2017-06-12 12:09:02 +08:00
RelativePositionAxes = Axes . Both ;
2017-06-02 17:11:36 +08:00
}
2017-06-09 15:11:31 +08:00
public override void InvalidateFromChild ( Invalidation invalidation )
{
// We only want to re-compute our size when a child's size or position has changed
if ( ( invalidation & Invalidation . RequiredParentSizeToFit ) = = 0 )
{
base . InvalidateFromChild ( invalidation ) ;
2017-06-02 18:27:00 +08:00
return ;
2017-06-09 15:11:31 +08:00
}
2017-06-02 17:11:36 +08:00
2017-06-16 18:21:54 +08:00
durationBacking . Invalidate ( ) ;
2017-06-02 15:39:31 +08:00
2017-06-09 15:11:31 +08:00
base . InvalidateFromChild ( invalidation ) ;
2017-06-01 13:26:21 +08:00
}
2017-06-16 18:21:54 +08:00
private Cached < double > durationBacking = new Cached < double > ( ) ;
2017-07-02 18:00:02 +08:00
private double computeDuration ( )
2017-06-09 15:11:31 +08:00
{
2017-06-16 18:21:54 +08:00
if ( ! Children . Any ( ) )
return 0 ;
2017-06-01 13:26:21 +08:00
2017-06-16 18:21:54 +08:00
double baseDuration = Children . Max ( c = > ( c . HitObject as IHasEndTime ) ? . EndTime ? ? c . HitObject . StartTime ) - ControlPoint . StartTime ;
2017-06-16 18:47:15 +08:00
// If we have a singular hit object at the timing section's start time, let's set a sane default duration
2017-06-16 18:21:54 +08:00
if ( baseDuration = = 0 )
2017-06-16 19:00:16 +08:00
baseDuration = 1 ;
2017-06-16 18:21:54 +08:00
2017-06-16 18:47:15 +08:00
// Scrolling ruleset hit objects typically have anchors+origins set to the hit object's start time, but if the hit object doesn't implement IHasEndTime and lies on the control point
// then the baseDuration above will be 0. This will cause problems with masking when it is further set as the value for Size in Update(). We _want_ the timing section bounds to
// completely enclose the hit object to avoid the masking optimisations.
2017-06-16 18:21:54 +08:00
//
2017-06-16 18:47:15 +08:00
// To do this we need to find a duration that corresponds to the absolute size of the element that extrudes beyond the timing section's bounds and add that to baseDuration.
// We can utilize the fact that the Size and RelativeChildSpace are 1:1, meaning that an change in duration for the timing section has no change to the hit object's positioning
// and simply find the largest absolutely-sized element in this timing section. This introduces a little bit of error, but will never under-estimate the duration.
// Find the largest element that is absolutely-sized along ScrollingAxes
2017-06-16 18:21:54 +08:00
float maxAbsoluteSize = Children . Where ( c = > ( c . RelativeSizeAxes & ScrollingAxes ) = = 0 )
. Select ( c = > ( ScrollingAxes & Axes . X ) > 0 ? c . Width : c . Height )
. DefaultIfEmpty ( ) . Max ( ) ;
float ourAbsoluteSize = ( ScrollingAxes & Axes . X ) > 0 ? DrawWidth : DrawHeight ;
// Add the extra duration to account for the absolute size
baseDuration * = 1 + maxAbsoluteSize / ourAbsoluteSize ;
return baseDuration ;
2017-07-02 18:00:02 +08:00
}
/// <summary>
/// The maximum duration of any one hit object inside this <see cref="DrawableTimingSection"/>. This is calculated as the maximum
/// end time between all hit objects relative to this <see cref="DrawableTimingSection"/>'s <see cref="MultiplierControlPoint.StartTime"/>.
/// </summary>
public double Duration = > durationBacking . IsValid ? durationBacking : ( durationBacking . Value = computeDuration ( ) ) ;
2017-06-16 18:21:54 +08:00
protected override void Update ( )
{
base . Update ( ) ;
2017-06-16 18:47:15 +08:00
// We want our size and position-space along ScrollingAxes to span our duration to completely enclose all the hit objects
2017-06-16 18:21:54 +08:00
Size = new Vector2 ( ( ScrollingAxes & Axes . X ) > 0 ? ( float ) Duration : Size . X , ( ScrollingAxes & Axes . Y ) > 0 ? ( float ) Duration : Size . Y ) ;
2017-06-16 18:47:15 +08:00
// And we need to make sure the hit object's position-space doesn't change due to our resizing
2017-06-16 18:21:54 +08:00
RelativeChildSize = Size ;
2017-06-09 15:11:31 +08:00
}
2017-06-01 13:26:21 +08:00
}
2017-06-09 15:11:31 +08:00
}