// Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using System.Linq; using osu.Framework.Caching; using osu.Framework.Configuration; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Rulesets.Objects.Drawables; using OpenTK; using osu.Game.Rulesets.Objects.Types; namespace osu.Game.Rulesets.Timing { /// /// A container that scrolls relative to the current time. Will autosize to the total duration of all contained hit objects along the scrolling axes. /// public abstract class ScrollingContainer : Container { /// /// Gets or sets the range of time that is visible by the length of the scrolling axes. /// public readonly BindableDouble VisibleTimeRange = new BindableDouble { Default = 1000 }; /// /// The axes through which this scrolls. This is set by the . /// internal Axes ScrollingAxes; public override bool RemoveWhenNotAlive => false; protected override bool RequiresChildrenUpdate => true; /// /// The control point that defines the speed adjustments for this container. This is set by the . /// internal MultiplierControlPoint ControlPoint; private Cached durationBacking; /// /// Creates a new . /// protected ScrollingContainer() { RelativeSizeAxes = Axes.Both; RelativePositionAxes = Axes.Both; } protected override int Compare(Drawable x, Drawable y) { var hX = (DrawableHitObject)x; var hY = (DrawableHitObject)y; int result = hY.HitObject.StartTime.CompareTo(hX.HitObject.StartTime); if (result != 0) return result; return base.Compare(y, x); } public override void Add(DrawableHitObject drawable) { durationBacking.Invalidate(); base.Add(drawable); } public override bool Remove(DrawableHitObject drawable) { durationBacking.Invalidate(); return base.Remove(drawable); } // Todo: This may underestimate the size of the hit object in some cases, but won't be too much of a problem for now private double computeDuration() => Math.Max(0, Children.Select(c => (c.HitObject as IHasEndTime)?.EndTime ?? c.HitObject.StartTime).DefaultIfEmpty().Max() - ControlPoint.StartTime) + 1000; /// /// An approximate total duration of this scrolling container. /// public double Duration => durationBacking.IsValid ? durationBacking : (durationBacking.Value = computeDuration()); protected override void Update() { base.Update(); RelativeChildOffset = new Vector2((ScrollingAxes & Axes.X) > 0 ? (float)ControlPoint.StartTime : 0, (ScrollingAxes & Axes.Y) > 0 ? (float)ControlPoint.StartTime : 0); // We want our size and position-space along the scrolling axes to span our duration to completely enclose all the hit objects Size = new Vector2((ScrollingAxes & Axes.X) > 0 ? (float)Duration : Size.X, (ScrollingAxes & Axes.Y) > 0 ? (float)Duration : Size.Y); // And we need to make sure the hit object's position-space doesn't change due to our resizing RelativeChildSize = Size; } } }