// Copyright (c) 2007-2017 ppy Pty Ltd . // 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; using osu.Game.Beatmaps.Timing; namespace osu.Game.Rulesets.Mania.Timing { /// /// A container in which the Y-relative coordinate space is spanned by a length of time. /// /// This container contains s which scroll inside this container. /// Drawables added to this container are moved inside the relevant , /// and as such, will scroll along with the s. /// /// public class ControlPointContainer : Container { /// /// The amount of time which the height of this container spans. /// public double TimeSpan { get; set; } private readonly List drawableTimingSections; public ControlPointContainer(IEnumerable timingChanges) { drawableTimingSections = timingChanges.Select(t => new DrawableTimingSection(t)).ToList(); Children = drawableTimingSections; } /// /// Adds a drawable to this container. Note that the drawable added must have a /// Y-position as a time relative to this container. /// /// The drawable to add. public override void Add(Drawable drawable) { // Always add timing sections to ourselves if (drawable is DrawableTimingSection) { base.Add(drawable); return; } var section = drawableTimingSections.LastOrDefault(t => t.CanContain(drawable)) ?? drawableTimingSections.FirstOrDefault(); if (section == null) throw new Exception("Could not find suitable timing section to add object to."); section.Add(drawable); } /// /// A container that contains drawables within the time span of a timing section. /// /// The content of this container will scroll relative to the current time. /// /// private class DrawableTimingSection : Container { private readonly ControlPoint timingChange; protected override Container Content => content; private readonly Container content; /// /// Creates a drawable timing section. The height of this container will be proportional /// to the beat length of the timing section and the timespan of its parent at all times. /// /// This is so that, e.g. a beat length of 500ms results in this container being twice as high as its parent, /// which means that the content container will scroll at twice the normal rate. /// /// /// The timing change to create the drawable timing section for. public DrawableTimingSection(ControlPoint timingChange) { this.timingChange = timingChange; RelativeSizeAxes = Axes.Both; AddInternal(content = new AutoTimeRelativeContainer { RelativeSizeAxes = Axes.Both, RelativePositionAxes = Axes.Both, Y = (float)timingChange.Time }); } protected override void Update() { var parent = (ControlPointContainer)Parent; // Adjust our height to account for the speed changes Height = (float)(1000 / timingChange.BeatLength / timingChange.SpeedMultiplier); RelativeCoordinateSpace = new Vector2(1, (float)parent.TimeSpan); // Scroll the content content.Y = (float)(timingChange.Time - Time.Current); } public override void Add(Drawable drawable) { // The previously relatively-positioned drawable will now become relative to us, but since the drawable has no knowledge of us, // we need to offset it back by our position so that it becomes correctly relatively-positioned to us // This can be removed if hit objects were stored such that either their StartTime or their "beat offset" was relative to the timing section // they belonged to, but this requires a radical change to the beatmap format which we're not ready to do just yet drawable.Y -= (float)timingChange.Time; base.Add(drawable); } /// /// Whether this timing section can contain a drawable. A timing section can contain a drawable if the drawable /// can be placed within the timing section's bounds (in this case, from the start of the timing section up to infinity). /// /// The drawable to check. public bool CanContain(Drawable drawable) => content.Y <= drawable.Y; /// /// A container which always keeps its height and relative coordinate space "auto-sized" to its children. /// private class AutoTimeRelativeContainer : Container { public override void InvalidateFromChild(Invalidation invalidation) { if ((invalidation & Invalidation.Geometry) == 0) { base.InvalidateFromChild(invalidation); return; } float height = 0; foreach (Drawable child in Children) { float childEndPos = child.Y + child.Height; if (childEndPos > height) height = childEndPos; } Height = height; RelativeCoordinateSpace = new Vector2(1, height); base.InvalidateFromChild(invalidation); } } } } }