2017-05-11 16:41:00 +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 ;
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
using OpenTK ;
2017-05-12 21:23:32 +08:00
using osu.Game.Beatmaps.Timing ;
2017-05-11 16:41:00 +08:00
namespace osu.Game.Rulesets.Mania.Timing
2017-05-11 15:09:48 +08:00
{
/// <summary>
2017-05-16 17:17:34 +08:00
/// A container in which added drawables are put into a relative coordinate space spanned by a length of time.
2017-05-11 16:41:00 +08:00
/// <para>
2017-05-12 21:23:32 +08:00
/// This container contains <see cref="ControlPoint"/>s which scroll inside this container.
/// Drawables added to this container are moved inside the relevant <see cref="ControlPoint"/>,
/// and as such, will scroll along with the <see cref="ControlPoint"/>s.
2017-05-11 15:09:48 +08:00
/// </para>
/// </summary>
2017-05-16 16:50:09 +08:00
public class ControlPointContainer : Container < Drawable >
2017-05-11 16:41:00 +08:00
{
/// <summary>
2017-05-16 17:17:34 +08:00
/// The amount of time which this container spans.
2017-05-11 16:41:00 +08:00
/// </summary>
2017-05-16 16:50:09 +08:00
public double TimeSpan { get ; set ; }
2017-05-11 16:41:00 +08:00
2017-05-16 17:17:34 +08:00
private readonly List < DrawableControlPoint > drawableControlPoints ;
2017-05-11 18:57:24 +08:00
2017-05-16 16:50:09 +08:00
public ControlPointContainer ( IEnumerable < ControlPoint > timingChanges )
2017-05-11 15:09:48 +08:00
{
2017-05-16 17:17:34 +08:00
drawableControlPoints = timingChanges . Select ( t = > new DrawableControlPoint ( t ) ) . ToList ( ) ;
Children = drawableControlPoints ;
2017-05-11 15:09:48 +08:00
}
2017-05-11 16:41:00 +08:00
/// <summary>
2017-05-16 17:17:34 +08:00
/// Adds a drawable to this container. Note that the drawable added must have its Y-position be
/// an absolute unit of time that is _not_ relative to <see cref="TimeSpan"/>.
2017-05-11 16:41:00 +08:00
/// </summary>
/// <param name="drawable">The drawable to add.</param>
public override void Add ( Drawable drawable )
2017-05-11 15:09:48 +08:00
{
// Always add timing sections to ourselves
2017-05-16 17:17:34 +08:00
if ( drawable is DrawableControlPoint )
2017-05-11 16:41:00 +08:00
{
2017-05-11 15:09:48 +08:00
base . Add ( drawable ) ;
2017-05-11 16:41:00 +08:00
return ;
2017-05-11 15:09:48 +08:00
}
2017-05-11 16:41:00 +08:00
2017-05-16 17:17:34 +08:00
var controlPoint = drawableControlPoints . LastOrDefault ( t = > t . CanContain ( drawable ) ) ? ? drawableControlPoints . FirstOrDefault ( ) ;
2017-05-11 16:41:00 +08:00
2017-05-16 17:17:34 +08:00
if ( controlPoint = = null )
2017-05-11 15:09:48 +08:00
throw new Exception ( "Could not find suitable timing section to add object to." ) ;
2017-05-11 16:41:00 +08:00
2017-05-16 17:17:34 +08:00
controlPoint . Add ( drawable ) ;
2017-05-11 15:09:48 +08:00
}
2017-05-11 16:41:00 +08:00
/// <summary>
2017-05-11 15:09:48 +08:00
/// A container that contains drawables within the time span of a timing section.
/// <para>
2017-05-11 18:57:24 +08:00
/// The content of this container will scroll relative to the current time.
2017-05-11 16:41:00 +08:00
/// </para>
/// </summary>
2017-05-16 17:17:34 +08:00
private class DrawableControlPoint : Container
2017-05-11 15:09:48 +08:00
{
2017-05-12 21:23:32 +08:00
private readonly ControlPoint timingChange ;
2017-05-11 15:09:48 +08:00
2017-05-16 16:03:43 +08:00
protected override Container < Drawable > Content = > content ;
private readonly Container content ;
2017-05-11 18:57:24 +08:00
/// <summary>
2017-05-16 17:17:34 +08:00
/// Creates a drawable control point. The height of this container will be proportional
/// to the beat length of the control point it is initialized with such that, e.g. a beat length
/// of 500ms results in this container being twice as high as its parent, which further means that
/// the content container will scroll at twice the normal rate.
2017-05-11 18:57:24 +08:00
/// </summary>
2017-05-16 17:17:34 +08:00
/// <param name="timingChange">The control point to create the drawable control point for.</param>
public DrawableControlPoint ( ControlPoint timingChange )
2017-05-11 15:09:48 +08:00
{
2017-05-12 21:23:32 +08:00
this . timingChange = timingChange ;
2017-05-11 15:09:48 +08:00
RelativeSizeAxes = Axes . Both ;
2017-05-16 16:03:43 +08:00
AddInternal ( content = new AutoTimeRelativeContainer
{
RelativeSizeAxes = Axes . Both ,
RelativePositionAxes = Axes . Both ,
2017-05-16 16:34:41 +08:00
Y = ( float ) timingChange . Time
2017-05-16 16:03:43 +08:00
} ) ;
2017-05-11 15:09:48 +08:00
}
protected override void Update ( )
{
2017-05-16 16:50:09 +08:00
var parent = ( ControlPointContainer ) Parent ;
2017-05-11 18:57:24 +08:00
// Adjust our height to account for the speed changes
2017-05-16 16:50:09 +08:00
Height = ( float ) ( 1000 / timingChange . BeatLength / timingChange . SpeedMultiplier ) ;
2017-05-11 18:57:24 +08:00
RelativeCoordinateSpace = new Vector2 ( 1 , ( float ) parent . TimeSpan ) ;
// Scroll the content
2017-05-16 16:34:41 +08:00
content . Y = ( float ) ( timingChange . Time - Time . Current ) ;
2017-05-11 15:09:48 +08:00
}
public override void Add ( Drawable drawable )
{
2017-05-16 17:17:34 +08:00
// The previously relatively-positioned drawable will now become relative to content, but since the drawable has no knowledge of content,
// we need to offset it back by content's position position so that it becomes correctly relatively-positioned to content
// This can be removed if hit objects were stored such that either their StartTime or their "beat offset" was relative to the timing change
2017-05-11 15:09:48 +08:00
// they belonged to, but this requires a radical change to the beatmap format which we're not ready to do just yet
2017-05-16 16:34:41 +08:00
drawable . Y - = ( float ) timingChange . Time ;
2017-05-11 16:41:00 +08:00
base . Add ( drawable ) ;
2017-05-11 15:09:48 +08:00
}
2017-05-11 18:57:24 +08:00
2017-05-11 21:01:37 +08:00
/// <summary>
2017-05-16 17:17:34 +08:00
/// Whether this control point can contain a drawable. This control point can contain a drawable if the drawable is positioned "after" this control point.
2017-05-11 21:01:37 +08:00
/// </summary>
/// <param name="drawable">The drawable to check.</param>
2017-05-16 16:34:41 +08:00
public bool CanContain ( Drawable drawable ) = > content . Y < = drawable . Y ;
2017-05-16 16:03:43 +08:00
2017-05-16 16:50:09 +08:00
/// <summary>
/// A container which always keeps its height and relative coordinate space "auto-sized" to its children.
/// </summary>
2017-05-16 16:03:43 +08:00
private class AutoTimeRelativeContainer : Container
{
2017-05-16 16:50:09 +08:00
public override void InvalidateFromChild ( Invalidation invalidation )
2017-05-16 16:03:43 +08:00
{
2017-05-16 16:50:09 +08:00
if ( ( invalidation & Invalidation . Geometry ) = = 0 )
{
base . InvalidateFromChild ( invalidation ) ;
return ;
}
2017-05-16 17:26:49 +08:00
if ( ! Children . Any ( ) )
return ;
2017-05-16 17:17:34 +08:00
float height = Children . Select ( child = > child . Y + child . Height ) . Max ( ) ;
2017-05-16 16:03:43 +08:00
Height = height ;
RelativeCoordinateSpace = new Vector2 ( 1 , height ) ;
2017-05-16 16:50:09 +08:00
base . InvalidateFromChild ( invalidation ) ;
2017-05-16 16:03:43 +08:00
}
}
2017-05-11 16:41:00 +08:00
}
}
2017-05-11 15:09:48 +08:00
}