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.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 ;
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-08-07 16:25:40 +08:00
/// A container that scrolls relative to the current time. Will autosize to the total duration of all contained hit objects along the scrolling axes.
2017-06-02 18:27:00 +08:00
/// </summary>
2017-08-04 19:22:53 +08:00
public abstract class ScrollingContainer : Container < DrawableHitObject >
2017-06-01 13:26:21 +08:00
{
2017-08-04 22:07:08 +08:00
private readonly BindableDouble visibleTimeRange = new BindableDouble { Default = 1000 } ;
2017-06-12 14:20:34 +08:00
/// <summary>
2017-08-07 16:25:40 +08:00
/// Gets or sets the range of time that is visible by the length of the scrolling axes.
2017-06-12 14:20:34 +08:00
/// </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-08-07 16:25:40 +08:00
/// The axes through which this <see cref="ScrollingContainer"/> 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>
2017-08-07 16:25:40 +08:00
/// The control point that defines the speed adjustments for this container. This is set by the <see cref="SpeedAdjustmentContainer"/>.
2017-06-16 18:21:54 +08:00
/// </summary>
internal MultiplierControlPoint ControlPoint ;
2017-08-07 15:02:38 +08:00
private Cached < double > durationBacking ;
2017-06-02 18:27:00 +08:00
/// <summary>
2017-08-04 19:22:53 +08:00
/// Creates a new <see cref="ScrollingContainer"/>.
2017-06-02 18:27:00 +08:00
/// </summary>
2017-08-04 19:22:53 +08:00
protected ScrollingContainer ( )
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-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-08-07 16:25:40 +08:00
// This container needs to resize such that it completely encloses the hit objects to avoid masking optimisations. This is done by converting the largest
// absolutely-sized element along the scrolling axes and adding a corresponding duration value. This introduces a bit of error, but will never under-estimate.ion.
2017-06-16 18:47:15 +08:00
// 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>
2017-08-04 19:22:53 +08:00
/// The maximum duration of any one hit object inside this <see cref="ScrollingContainer"/>. This is calculated as the maximum
2017-08-07 16:25:40 +08:00
/// duration of all hit objects relative to this <see cref="ScrollingContainer"/>'s <see cref="MultiplierControlPoint.StartTime"/>.
2017-07-02 18:00:02 +08:00
/// </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-08-07 16:25:40 +08:00
// We want our size and position-space along the scrolling axes 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
}