Expose basic values from ISpeedChangeVisualiser

This commit is contained in:
smoogipoo 2018-10-30 17:31:43 +09:00
parent 8583fd1380
commit 0bdeebbce2
5 changed files with 125 additions and 86 deletions

View File

@ -95,9 +95,22 @@ namespace osu.Game.Rulesets.UI.Scrolling
{ {
base.Update(); base.Update();
speedChangeVisualiser.TimeRange = TimeRange.Value;
switch (Direction.Value)
{
case ScrollingDirection.Up:
case ScrollingDirection.Down:
speedChangeVisualiser.ScrollLength = DrawSize.Y;
break;
default:
speedChangeVisualiser.ScrollLength = DrawSize.X;
break;
}
if (!initialStateCache.IsValid) if (!initialStateCache.IsValid)
{ {
speedChangeVisualiser.ComputeInitialStates(Objects, Direction, TimeRange, DrawSize); speedChangeVisualiser.ComputeInitialStates(Objects, Direction);
initialStateCache.Validate(); initialStateCache.Validate();
} }
} }
@ -107,7 +120,7 @@ namespace osu.Game.Rulesets.UI.Scrolling
base.UpdateAfterChildrenLife(); base.UpdateAfterChildrenLife();
// We need to calculate this as soon as possible after lifetimes so that hitobjects get the final say in their positions // We need to calculate this as soon as possible after lifetimes so that hitobjects get the final say in their positions
speedChangeVisualiser.UpdatePositions(AliveObjects, Direction, Time.Current, TimeRange, DrawSize); speedChangeVisualiser.UpdatePositions(AliveObjects, Direction, Time.Current);
} }
} }
} }

View File

@ -4,64 +4,74 @@
using System.Collections.Generic; using System.Collections.Generic;
using osu.Game.Rulesets.Objects.Drawables; using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Objects.Types; using osu.Game.Rulesets.Objects.Types;
using OpenTK;
namespace osu.Game.Rulesets.UI.Scrolling.Visualisers namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
{ {
public class ConstantSpeedChangeVisualiser : ISpeedChangeVisualiser public class ConstantSpeedChangeVisualiser : ISpeedChangeVisualiser
{ {
public void ComputeInitialStates(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction, double timeRange, Vector2 length) public double TimeRange { get; set; }
public float ScrollLength { get; set; }
public void ComputeInitialStates(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction)
{ {
foreach (var obj in hitObjects) foreach (var obj in hitObjects)
{ {
obj.LifetimeStart = obj.HitObject.StartTime - timeRange; obj.LifetimeStart = GetDisplayStartTime(obj.HitObject.StartTime);
if (obj.HitObject is IHasEndTime endTime) if (obj.HitObject is IHasEndTime endTime)
{ {
var hitObjectLength = (endTime.EndTime - obj.HitObject.StartTime) / timeRange;
switch (direction) switch (direction)
{ {
case ScrollingDirection.Up: case ScrollingDirection.Up:
case ScrollingDirection.Down: case ScrollingDirection.Down:
obj.Height = (float)(hitObjectLength * length.Y); obj.Height = GetLength(obj.HitObject.StartTime, endTime.EndTime);
break; break;
case ScrollingDirection.Left: case ScrollingDirection.Left:
case ScrollingDirection.Right: case ScrollingDirection.Right:
obj.Width = (float)(hitObjectLength * length.X); obj.Height = GetLength(obj.HitObject.StartTime, endTime.EndTime);
break; break;
} }
} }
ComputeInitialStates(obj.NestedHitObjects, direction, timeRange, length); ComputeInitialStates(obj.NestedHitObjects, direction);
// Nested hitobjects don't need to scroll, but they do need accurate positions // Nested hitobjects don't need to scroll, but they do need accurate positions
UpdatePositions(obj.NestedHitObjects, direction, obj.HitObject.StartTime, timeRange, length); UpdatePositions(obj.NestedHitObjects, direction, obj.HitObject.StartTime);
} }
} }
public void UpdatePositions(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction, double currentTime, double timeRange, Vector2 length) public void UpdatePositions(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction, double currentTime)
{ {
foreach (var obj in hitObjects) foreach (var obj in hitObjects)
{ {
var position = (obj.HitObject.StartTime - currentTime) / timeRange;
switch (direction) switch (direction)
{ {
case ScrollingDirection.Up: case ScrollingDirection.Up:
obj.Y = (float)(position * length.Y); obj.Y = PositionAt(currentTime, obj.HitObject.StartTime);
break; break;
case ScrollingDirection.Down: case ScrollingDirection.Down:
obj.Y = (float)(-position * length.Y); obj.Y = -PositionAt(currentTime, obj.HitObject.StartTime);
break; break;
case ScrollingDirection.Left: case ScrollingDirection.Left:
obj.X = (float)(position * length.X); obj.X = PositionAt(currentTime, obj.HitObject.StartTime);
break; break;
case ScrollingDirection.Right: case ScrollingDirection.Right:
obj.X = (float)(-position * length.X); obj.X = -PositionAt(currentTime, obj.HitObject.StartTime);
break; break;
} }
} }
} }
public double GetDisplayStartTime(double startTime) => startTime - TimeRange;
public float GetLength(double startTime, double endTime)
{
// At the hitobject's end time, the hitobject will be positioned such that its end rests at the origin.
// This results in a negative-position value, and the absolute of it indicates the length of the hitobject.
return -PositionAt(endTime, startTime);
}
public float PositionAt(double currentTime, double startTime) => (float)((startTime - currentTime) / TimeRange * ScrollLength);
} }
} }

View File

@ -3,21 +3,22 @@
using System.Collections.Generic; using System.Collections.Generic;
using osu.Game.Rulesets.Objects.Drawables; using osu.Game.Rulesets.Objects.Drawables;
using OpenTK;
namespace osu.Game.Rulesets.UI.Scrolling.Visualisers namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
{ {
public interface ISpeedChangeVisualiser public interface ISpeedChangeVisualiser
{ {
double TimeRange { get; set; }
float ScrollLength { get; set; }
/// <summary> /// <summary>
/// Computes the states of <see cref="DrawableHitObject"/>s that remain constant while scrolling, such as lifetime and spatial length. /// Computes the states of <see cref="DrawableHitObject"/>s that remain constant while scrolling, such as lifetime and spatial length.
/// This is invoked once whenever <paramref name="timeRange"/> or <paramref name="length"/> changes. /// This is invoked once whenever <paramref name="timeRange"/> or <paramref name="length"/> changes.
/// </summary> /// </summary>
/// <param name="hitObjects">The <see cref="DrawableHitObject"/>s whose states should be computed.</param> /// <param name="hitObjects">The <see cref="DrawableHitObject"/>s whose states should be computed.</param>
/// <param name="direction">The scrolling direction.</param> /// <param name="direction">The scrolling direction.</param>
/// <param name="timeRange">The duration required to scroll through one length of the screen before any speed adjustments.</param> void ComputeInitialStates(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction);
/// <param name="length">The length of the screen that is scrolled through.</param>
void ComputeInitialStates(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction, double timeRange, Vector2 length);
/// <summary> /// <summary>
/// Updates the positions of <see cref="DrawableHitObject"/>s, depending on the current time. This is invoked once per frame. /// Updates the positions of <see cref="DrawableHitObject"/>s, depending on the current time. This is invoked once per frame.
@ -25,8 +26,12 @@ namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
/// <param name="hitObjects">The <see cref="DrawableHitObject"/>s whose positions should be computed.</param> /// <param name="hitObjects">The <see cref="DrawableHitObject"/>s whose positions should be computed.</param>
/// <param name="direction">The scrolling direction.</param> /// <param name="direction">The scrolling direction.</param>
/// <param name="currentTime">The current time.</param> /// <param name="currentTime">The current time.</param>
/// <param name="timeRange">The duration required to scroll through one length of the screen before any speed adjustments.</param> void UpdatePositions(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction, double currentTime);
/// <param name="length">The length of the screen that is scrolled through.</param>
void UpdatePositions(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction, double currentTime, double timeRange, Vector2 length); double GetDisplayStartTime(double startTime);
float GetLength(double startTime, double endTime);
float PositionAt(double currentTime, double startTime);
} }
} }

View File

@ -6,12 +6,15 @@ using osu.Framework.Lists;
using osu.Game.Rulesets.Objects.Drawables; using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Objects.Types; using osu.Game.Rulesets.Objects.Types;
using osu.Game.Rulesets.Timing; using osu.Game.Rulesets.Timing;
using OpenTK;
namespace osu.Game.Rulesets.UI.Scrolling.Visualisers namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
{ {
public class OverlappingSpeedChangeVisualiser : ISpeedChangeVisualiser public class OverlappingSpeedChangeVisualiser : ISpeedChangeVisualiser
{ {
public double TimeRange { get; set; }
public float ScrollLength { get; set; }
private readonly SortedList<MultiplierControlPoint> controlPoints; private readonly SortedList<MultiplierControlPoint> controlPoints;
public OverlappingSpeedChangeVisualiser(SortedList<MultiplierControlPoint> controlPoints) public OverlappingSpeedChangeVisualiser(SortedList<MultiplierControlPoint> controlPoints)
@ -19,79 +22,72 @@ namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
this.controlPoints = controlPoints; this.controlPoints = controlPoints;
} }
public void ComputeInitialStates(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction, double timeRange, Vector2 length) public void ComputeInitialStates(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction)
{ {
foreach (var obj in hitObjects) foreach (var obj in hitObjects)
{ {
// The total amount of time that the hitobject will remain visible within the timeRange, which decreases as the speed multiplier increases obj.LifetimeStart = GetDisplayStartTime(obj.HitObject.StartTime);
double visibleDuration = timeRange / controlPointAt(obj.HitObject.StartTime).Multiplier;
obj.LifetimeStart = obj.HitObject.StartTime - visibleDuration;
if (obj.HitObject is IHasEndTime endTime) if (obj.HitObject is IHasEndTime endTime)
{ {
// At the hitobject's end time, the hitobject will be positioned such that its end rests at the origin.
// This results in a negative-position value, and the absolute of it indicates the length of the hitobject.
var hitObjectLength = -hitObjectPositionAt(obj, endTime.EndTime, timeRange);
switch (direction) switch (direction)
{ {
case ScrollingDirection.Up: case ScrollingDirection.Up:
case ScrollingDirection.Down: case ScrollingDirection.Down:
obj.Height = (float)(hitObjectLength * length.Y); obj.Height = GetLength(obj.HitObject.StartTime, endTime.EndTime);
break; break;
case ScrollingDirection.Left: case ScrollingDirection.Left:
case ScrollingDirection.Right: case ScrollingDirection.Right:
obj.Width = (float)(hitObjectLength * length.X); obj.Width = GetLength(obj.HitObject.StartTime, endTime.EndTime);
break; break;
} }
} }
ComputeInitialStates(obj.NestedHitObjects, direction, timeRange, length); ComputeInitialStates(obj.NestedHitObjects, direction);
// Nested hitobjects don't need to scroll, but they do need accurate positions // Nested hitobjects don't need to scroll, but they do need accurate positions
UpdatePositions(obj.NestedHitObjects, direction, obj.HitObject.StartTime, timeRange, length); UpdatePositions(obj.NestedHitObjects, direction, obj.HitObject.StartTime);
} }
} }
public void UpdatePositions(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction, double currentTime, double timeRange, Vector2 length) public void UpdatePositions(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction, double currentTime)
{ {
foreach (var obj in hitObjects) foreach (var obj in hitObjects)
{ {
var position = hitObjectPositionAt(obj, currentTime, timeRange);
switch (direction) switch (direction)
{ {
case ScrollingDirection.Up: case ScrollingDirection.Up:
obj.Y = (float)(position * length.Y); obj.Y = PositionAt(currentTime, obj.HitObject.StartTime);
break; break;
case ScrollingDirection.Down: case ScrollingDirection.Down:
obj.Y = (float)(-position * length.Y); obj.Y = -PositionAt(currentTime, obj.HitObject.StartTime);
break; break;
case ScrollingDirection.Left: case ScrollingDirection.Left:
obj.X = (float)(position * length.X); obj.X = PositionAt(currentTime, obj.HitObject.StartTime);
break; break;
case ScrollingDirection.Right: case ScrollingDirection.Right:
obj.X = (float)(-position * length.X); obj.X = -PositionAt(currentTime, obj.HitObject.StartTime);
break; break;
} }
} }
} }
/// <summary> public double GetDisplayStartTime(double startTime)
/// Computes the position of a <see cref="DrawableHitObject"/> at a point in time. {
/// <para> // The total amount of time that the hitobject will remain visible within the timeRange, which decreases as the speed multiplier increases
/// At t &lt; startTime, position &gt; 0. <br /> double visibleDuration = TimeRange / controlPointAt(startTime).Multiplier;
/// At t = startTime, position = 0. <br /> return startTime - visibleDuration;
/// At t &gt; startTime, position &lt; 0. }
/// </para>
/// </summary> public float GetLength(double startTime, double endTime)
/// <param name="obj">The <see cref="DrawableHitObject"/>.</param> {
/// <param name="time">The time to find the position of <paramref name="obj"/> at.</param> // At the hitobject's end time, the hitobject will be positioned such that its end rests at the origin.
/// <param name="timeRange">The amount of time visualised by the scrolling area.</param> // This results in a negative-position value, and the absolute of it indicates the length of the hitobject.
/// <returns>The position of <paramref name="obj"/> in the scrolling area at time = <paramref name="time"/>.</returns> return -PositionAt(endTime, startTime);
private double hitObjectPositionAt(DrawableHitObject obj, double time, double timeRange) }
=> (obj.HitObject.StartTime - time) / timeRange * controlPointAt(obj.HitObject.StartTime).Multiplier;
public float PositionAt(double currentTime, double startTime)
=> (float)((startTime - currentTime) / TimeRange * controlPointAt(startTime).Multiplier * ScrollLength);
private readonly MultiplierControlPoint searchPoint = new MultiplierControlPoint(); private readonly MultiplierControlPoint searchPoint = new MultiplierControlPoint();

View File

@ -6,13 +6,16 @@ using System.Collections.Generic;
using osu.Game.Rulesets.Objects.Drawables; using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Objects.Types; using osu.Game.Rulesets.Objects.Types;
using osu.Game.Rulesets.Timing; using osu.Game.Rulesets.Timing;
using OpenTK;
namespace osu.Game.Rulesets.UI.Scrolling.Visualisers namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
{ {
public class SequentialSpeedChangeVisualiser : ISpeedChangeVisualiser public class SequentialSpeedChangeVisualiser : ISpeedChangeVisualiser
{ {
private readonly Dictionary<DrawableHitObject, double> hitObjectPositions = new Dictionary<DrawableHitObject, double>(); public double TimeRange { get; set; }
public float ScrollLength { get; set; }
private readonly Dictionary<double, double> positionCache = new Dictionary<double, double>();
private readonly IReadOnlyList<MultiplierControlPoint> controlPoints; private readonly IReadOnlyList<MultiplierControlPoint> controlPoints;
@ -21,66 +24,78 @@ namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
this.controlPoints = controlPoints; this.controlPoints = controlPoints;
} }
public void ComputeInitialStates(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction, double timeRange, Vector2 length) public void ComputeInitialStates(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction)
{ {
foreach (var obj in hitObjects) foreach (var obj in hitObjects)
{ {
// To reduce iterations when updating hitobject positions later on, their initial positions are cached obj.LifetimeStart = GetDisplayStartTime(obj.HitObject.StartTime);
var startPosition = hitObjectPositions[obj] = positionAt(obj.HitObject.StartTime, timeRange);
// Todo: This is approximate and will be incorrect in the case of extreme speed changes
obj.LifetimeStart = obj.HitObject.StartTime - timeRange - 1000;
if (obj.HitObject is IHasEndTime endTime) if (obj.HitObject is IHasEndTime endTime)
{ {
var hitObjectLength = positionAt(endTime.EndTime, timeRange) - startPosition;
switch (direction) switch (direction)
{ {
case ScrollingDirection.Up: case ScrollingDirection.Up:
case ScrollingDirection.Down: case ScrollingDirection.Down:
obj.Height = (float)(hitObjectLength * length.Y); obj.Height = GetLength(obj.HitObject.StartTime, endTime.EndTime);
break; break;
case ScrollingDirection.Left: case ScrollingDirection.Left:
case ScrollingDirection.Right: case ScrollingDirection.Right:
obj.Width = (float)(hitObjectLength * length.X); obj.Width = GetLength(obj.HitObject.StartTime, endTime.EndTime);
break; break;
} }
} }
ComputeInitialStates(obj.NestedHitObjects, direction, timeRange, length); ComputeInitialStates(obj.NestedHitObjects, direction);
// Nested hitobjects don't need to scroll, but they do need accurate positions // Nested hitobjects don't need to scroll, but they do need accurate positions
UpdatePositions(obj.NestedHitObjects, direction, obj.HitObject.StartTime, timeRange, length); UpdatePositions(obj.NestedHitObjects, direction, obj.HitObject.StartTime);
} }
} }
public void UpdatePositions(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction, double currentTime, double timeRange, Vector2 length) public void UpdatePositions(IEnumerable<DrawableHitObject> hitObjects, ScrollingDirection direction, double currentTime)
{ {
var timelinePosition = positionAt(currentTime, timeRange);
foreach (var obj in hitObjects) foreach (var obj in hitObjects)
{ {
var finalPosition = hitObjectPositions[obj] - timelinePosition;
switch (direction) switch (direction)
{ {
case ScrollingDirection.Up: case ScrollingDirection.Up:
obj.Y = (float)(finalPosition * length.Y); obj.Y = PositionAt(currentTime, obj.HitObject.StartTime);
break; break;
case ScrollingDirection.Down: case ScrollingDirection.Down:
obj.Y = (float)(-finalPosition * length.Y); obj.Y = -PositionAt(currentTime, obj.HitObject.StartTime);
break; break;
case ScrollingDirection.Left: case ScrollingDirection.Left:
obj.X = (float)(finalPosition * length.X); obj.X = PositionAt(currentTime, obj.HitObject.StartTime);
break; break;
case ScrollingDirection.Right: case ScrollingDirection.Right:
obj.X = (float)(-finalPosition * length.X); obj.X = -PositionAt(currentTime, obj.HitObject.StartTime);
break; break;
} }
} }
} }
public double GetDisplayStartTime(double startTime) => startTime - TimeRange - 1000;
public float GetLength(double startTime, double endTime)
{
var objectLength = relativePositionAtCached(endTime) - relativePositionAtCached(startTime);
return (float)(objectLength * ScrollLength);
}
public float PositionAt(double currentTime, double startTime)
{
// Caching is not used here as currentTime is unlikely to have been previously cached
double timelinePosition = relativePositionAt(currentTime);
return (float)((relativePositionAtCached(startTime) - timelinePosition) * ScrollLength);
}
private double relativePositionAtCached(double time)
{
if (!positionCache.TryGetValue(time, out double existing))
positionCache[time] = existing = relativePositionAt(time);
return existing;
}
/// <summary> /// <summary>
/// Finds the position which corresponds to a point in time. /// Finds the position which corresponds to a point in time.
/// This is a non-linear operation that depends on all the control points up to and including the one active at the time value. /// This is a non-linear operation that depends on all the control points up to and including the one active at the time value.
@ -88,10 +103,10 @@ namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
/// <param name="time">The time to find the position at.</param> /// <param name="time">The time to find the position at.</param>
/// <param name="timeRange">The amount of time visualised by the scrolling area.</param> /// <param name="timeRange">The amount of time visualised by the scrolling area.</param>
/// <returns>A positive value indicating the position at <paramref name="time"/>.</returns> /// <returns>A positive value indicating the position at <paramref name="time"/>.</returns>
private double positionAt(double time, double timeRange) private double relativePositionAt(double time)
{ {
if (controlPoints.Count == 0) if (controlPoints.Count == 0)
return time / timeRange; return time / TimeRange;
double length = 0; double length = 0;
@ -115,7 +130,7 @@ namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
var durationInCurrent = Math.Min(currentDuration, time - current.StartTime); var durationInCurrent = Math.Min(currentDuration, time - current.StartTime);
// Figure out how much of the time range the duration represents, and adjust it by the speed multiplier // Figure out how much of the time range the duration represents, and adjust it by the speed multiplier
length += durationInCurrent / timeRange * current.Multiplier; length += durationInCurrent / TimeRange * current.Multiplier;
} }
return length; return length;