Give visualiser methods range+length params again

This commit is contained in:
smoogipoo 2018-10-30 18:33:24 +09:00
parent f41bfd14ca
commit 195f82fa96
5 changed files with 86 additions and 85 deletions

View File

@ -30,19 +30,30 @@ namespace osu.Game.Rulesets.UI.Scrolling
protected readonly SortedList<MultiplierControlPoint> ControlPoints = new SortedList<MultiplierControlPoint>(); protected readonly SortedList<MultiplierControlPoint> ControlPoints = new SortedList<MultiplierControlPoint>();
public readonly Bindable<ScrollingDirection> Direction = new Bindable<ScrollingDirection>(); public readonly Bindable<ScrollingDirection> Direction = new Bindable<ScrollingDirection>();
private readonly SpeedChangeVisualisationMethod visualisationMethod;
private readonly ISpeedChangeVisualiser visualiser;
private Cached initialStateCache = new Cached(); private Cached initialStateCache = new Cached();
private ISpeedChangeVisualiser visualiser;
public ScrollingHitObjectContainer(SpeedChangeVisualisationMethod visualisationMethod) public ScrollingHitObjectContainer(SpeedChangeVisualisationMethod visualisationMethod)
{ {
this.visualisationMethod = visualisationMethod;
RelativeSizeAxes = Axes.Both; RelativeSizeAxes = Axes.Both;
TimeRange.ValueChanged += _ => initialStateCache.Invalidate(); TimeRange.ValueChanged += _ => initialStateCache.Invalidate();
Direction.ValueChanged += _ => initialStateCache.Invalidate(); Direction.ValueChanged += _ => initialStateCache.Invalidate();
switch (visualisationMethod)
{
case SpeedChangeVisualisationMethod.Sequential:
visualiser = new SequentialSpeedChangeVisualiser(ControlPoints);
break;
case SpeedChangeVisualisationMethod.Overlapping:
visualiser = new OverlappingSpeedChangeVisualiser(ControlPoints);
break;
case SpeedChangeVisualisationMethod.Constant:
visualiser = new ConstantSpeedChangeVisualiser();
break;
}
} }
public override void Add(DrawableHitObject hitObject) public override void Add(DrawableHitObject hitObject)
@ -81,13 +92,26 @@ namespace osu.Game.Rulesets.UI.Scrolling
return base.Invalidate(invalidation, source, shallPropagate); return base.Invalidate(invalidation, source, shallPropagate);
} }
private float scrollLength;
protected override void Update() protected override void Update()
{ {
base.Update(); base.Update();
if (!initialStateCache.IsValid) if (!initialStateCache.IsValid)
{ {
visualiser = createVisualiser(); switch (Direction.Value)
{
case ScrollingDirection.Up:
case ScrollingDirection.Down:
scrollLength = DrawSize.Y;
break;
default:
scrollLength = DrawSize.X;
break;
}
visualiser.Reset();
foreach (var obj in Objects) foreach (var obj in Objects)
computeInitialStateRecursive(obj); computeInitialStateRecursive(obj);
@ -95,36 +119,9 @@ namespace osu.Game.Rulesets.UI.Scrolling
} }
} }
private ISpeedChangeVisualiser createVisualiser()
{
float scrollLength;
switch (Direction.Value)
{
case ScrollingDirection.Up:
case ScrollingDirection.Down:
scrollLength = DrawSize.Y;
break;
default:
scrollLength = DrawSize.X;
break;
}
switch (visualisationMethod)
{
default:
case SpeedChangeVisualisationMethod.Constant:
return new ConstantSpeedChangeVisualiser(TimeRange, scrollLength);
case SpeedChangeVisualisationMethod.Overlapping:
return new OverlappingSpeedChangeVisualiser(ControlPoints, TimeRange, scrollLength);
case SpeedChangeVisualisationMethod.Sequential:
return new SequentialSpeedChangeVisualiser(ControlPoints, TimeRange, scrollLength);
}
}
private void computeInitialStateRecursive(DrawableHitObject hitObject) private void computeInitialStateRecursive(DrawableHitObject hitObject)
{ {
hitObject.LifetimeStart = visualiser.GetDisplayStartTime(hitObject.HitObject.StartTime); hitObject.LifetimeStart = visualiser.GetDisplayStartTime(hitObject.HitObject.StartTime, TimeRange);
if (hitObject.HitObject is IHasEndTime endTime) if (hitObject.HitObject is IHasEndTime endTime)
{ {
@ -132,11 +129,11 @@ namespace osu.Game.Rulesets.UI.Scrolling
{ {
case ScrollingDirection.Up: case ScrollingDirection.Up:
case ScrollingDirection.Down: case ScrollingDirection.Down:
hitObject.Height = visualiser.GetLength(hitObject.HitObject.StartTime, endTime.EndTime); hitObject.Height = visualiser.GetLength(hitObject.HitObject.StartTime, endTime.EndTime, TimeRange, scrollLength);
break; break;
case ScrollingDirection.Left: case ScrollingDirection.Left:
case ScrollingDirection.Right: case ScrollingDirection.Right:
hitObject.Height = visualiser.GetLength(hitObject.HitObject.StartTime, endTime.EndTime); hitObject.Height = visualiser.GetLength(hitObject.HitObject.StartTime, endTime.EndTime, TimeRange, scrollLength);
break; break;
} }
} }
@ -164,16 +161,16 @@ namespace osu.Game.Rulesets.UI.Scrolling
switch (Direction.Value) switch (Direction.Value)
{ {
case ScrollingDirection.Up: case ScrollingDirection.Up:
hitObject.Y = visualiser.PositionAt(hitObject.HitObject.StartTime, currentTime); hitObject.Y = visualiser.PositionAt(hitObject.HitObject.StartTime, currentTime, TimeRange, scrollLength);
break; break;
case ScrollingDirection.Down: case ScrollingDirection.Down:
hitObject.Y = -visualiser.PositionAt(hitObject.HitObject.StartTime, currentTime); hitObject.Y = -visualiser.PositionAt(hitObject.HitObject.StartTime, currentTime, TimeRange, scrollLength);
break; break;
case ScrollingDirection.Left: case ScrollingDirection.Left:
hitObject.X = visualiser.PositionAt(hitObject.HitObject.StartTime, currentTime); hitObject.X = visualiser.PositionAt(hitObject.HitObject.StartTime, currentTime, TimeRange, scrollLength);
break; break;
case ScrollingDirection.Right: case ScrollingDirection.Right:
hitObject.X = -visualiser.PositionAt(hitObject.HitObject.StartTime, currentTime); hitObject.X = -visualiser.PositionAt(hitObject.HitObject.StartTime, currentTime, TimeRange, scrollLength);
break; break;
} }
} }

View File

@ -3,26 +3,22 @@
namespace osu.Game.Rulesets.UI.Scrolling.Visualisers namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
{ {
public readonly struct ConstantSpeedChangeVisualiser : ISpeedChangeVisualiser public class ConstantSpeedChangeVisualiser : ISpeedChangeVisualiser
{ {
private readonly double timeRange; public double GetDisplayStartTime(double time, double timeRange) => time - timeRange;
private readonly float scrollLength;
public ConstantSpeedChangeVisualiser(double timeRange, float scrollLength) public float GetLength(double startTime, double endTime, double timeRange, float scrollLength)
{
this.timeRange = timeRange;
this.scrollLength = scrollLength;
}
public double GetDisplayStartTime(double time) => time - 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. // 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. // This results in a negative-position value, and the absolute of it indicates the length of the hitobject.
return -PositionAt(startTime, endTime); return -PositionAt(startTime, endTime, timeRange, scrollLength);
} }
public float PositionAt(double time, double currentTime) => (float)((time - currentTime) / timeRange * scrollLength); public float PositionAt(double time, double currentTime, double timeRange, float scrollLength)
=> (float)((time - currentTime) / timeRange * scrollLength);
public void Reset()
{
}
} }
} }

View File

@ -6,29 +6,39 @@ namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
public interface ISpeedChangeVisualiser public interface ISpeedChangeVisualiser
{ {
/// <summary> /// <summary>
/// Given a point in time, computes the time at which the point enters the visible time range of this <see cref="ISpeedChangeVisualiser"/>. /// Given a point in time, computes the time at which it enters the time range.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// E.g. For a constant visible time range of 5000ms, the time at which t=7000ms enters the visible time range is 2000ms. /// E.g. For a constant time range of 5000ms, the time at which t=7000ms enters the time range is 2000ms.
/// </remarks> /// </remarks>
/// <param name="time">The time value.</param> /// <param name="time">The point in time.</param>
/// <returns>The time at which <paramref name="time"/> enters the visible time range of this <see cref="ISpeedChangeVisualiser"/>.</returns> /// <param name="timeRange">The amount of visible time.</param>
double GetDisplayStartTime(double time); /// <returns>The time at which <paramref name="time"/> enters <see cref="timeRange"/>.</returns>
double GetDisplayStartTime(double time, double timeRange);
/// <summary> /// <summary>
/// Computes the spatial length within a start and end time. /// Computes the spatial length within a start and end time.
/// </summary> /// </summary>
/// <param name="startTime">The start time.</param> /// <param name="startTime">The start time.</param>
/// <param name="endTime">The end time.</param> /// <param name="endTime">The end time.</param>
/// <param name="timeRange">The amount of visible time.</param>
/// <param name="scrollLength">The absolute spatial length through <see cref="timeRange"/>.</param>
/// <returns>The absolute spatial length.</returns> /// <returns>The absolute spatial length.</returns>
float GetLength(double startTime, double endTime); float GetLength(double startTime, double endTime, double timeRange, float scrollLength);
/// <summary> /// <summary>
/// Given the current time, computes the spatial position of a point in time. /// Given the current time, computes the spatial position of a point in time.
/// </summary> /// </summary>
/// <param name="time">The time to compute the spatial position of.</param> /// <param name="time">The time to compute the spatial position of.</param>
/// <param name="currentTime">The current time.</param> /// <param name="currentTime">The current time.</param>
/// <param name="timeRange">The amount of visible time.</param>
/// <param name="scrollLength">The absolute spatial length through <see cref="timeRange"/>.</param>
/// <returns>The absolute spatial position.</returns> /// <returns>The absolute spatial position.</returns>
float PositionAt(double time, double currentTime); float PositionAt(double time, double currentTime, double timeRange, float scrollLength);
/// <summary>
/// Resets this <see cref="ISpeedChangeVisualiser"/> to a default state.
/// </summary>
void Reset();
} }
} }

View File

@ -6,40 +6,40 @@ using osu.Game.Rulesets.Timing;
namespace osu.Game.Rulesets.UI.Scrolling.Visualisers namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
{ {
public readonly struct OverlappingSpeedChangeVisualiser : ISpeedChangeVisualiser public class OverlappingSpeedChangeVisualiser : ISpeedChangeVisualiser
{ {
private readonly MultiplierControlPoint searchPoint; private readonly MultiplierControlPoint searchPoint;
private readonly SortedList<MultiplierControlPoint> controlPoints; private readonly SortedList<MultiplierControlPoint> controlPoints;
private readonly double timeRange;
private readonly float scrollLength;
public OverlappingSpeedChangeVisualiser(SortedList<MultiplierControlPoint> controlPoints, double timeRange, float scrollLength) public OverlappingSpeedChangeVisualiser(SortedList<MultiplierControlPoint> controlPoints)
{ {
this.controlPoints = controlPoints; this.controlPoints = controlPoints;
this.timeRange = timeRange;
this.scrollLength = scrollLength;
searchPoint = new MultiplierControlPoint(); searchPoint = new MultiplierControlPoint();
} }
public double GetDisplayStartTime(double time) public double GetDisplayStartTime(double time, double timeRange)
{ {
// The total amount of time that the hitobject will remain visible within the timeRange, which decreases as the speed multiplier increases // The total amount of time that the hitobject will remain visible within the timeRange, which decreases as the speed multiplier increases
double visibleDuration = timeRange / controlPointAt(time).Multiplier; double visibleDuration = timeRange / controlPointAt(time).Multiplier;
return time - visibleDuration; return time - visibleDuration;
} }
public float GetLength(double startTime, double endTime) public float GetLength(double startTime, double endTime, double timeRange, float scrollLength)
{ {
// At the hitobject's end time, the hitobject will be positioned such that its end rests at the origin. // 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. // This results in a negative-position value, and the absolute of it indicates the length of the hitobject.
return -PositionAt(startTime, endTime); return -PositionAt(startTime, endTime, timeRange, scrollLength);
} }
public float PositionAt(double time, double currentTime) public float PositionAt(double time, double currentTime, double timeRange, float scrollLength)
=> (float)((time - currentTime) / timeRange * controlPointAt(time).Multiplier * scrollLength); => (float)((time - currentTime) / timeRange * controlPointAt(time).Multiplier * scrollLength);
public void Reset()
{
}
/// <summary> /// <summary>
/// Finds the <see cref="MultiplierControlPoint"/> which affects the speed of hitobjects at a specific time. /// Finds the <see cref="MultiplierControlPoint"/> which affects the speed of hitobjects at a specific time.
/// </summary> /// </summary>

View File

@ -7,45 +7,43 @@ using osu.Game.Rulesets.Timing;
namespace osu.Game.Rulesets.UI.Scrolling.Visualisers namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
{ {
public readonly struct SequentialSpeedChangeVisualiser : ISpeedChangeVisualiser public class SequentialSpeedChangeVisualiser : ISpeedChangeVisualiser
{ {
private readonly Dictionary<double, double> positionCache; private readonly Dictionary<double, double> positionCache;
private readonly IReadOnlyList<MultiplierControlPoint> controlPoints; private readonly IReadOnlyList<MultiplierControlPoint> controlPoints;
private readonly double timeRange;
private readonly float scrollLength;
public SequentialSpeedChangeVisualiser(IReadOnlyList<MultiplierControlPoint> controlPoints, double timeRange, float scrollLength) public SequentialSpeedChangeVisualiser(IReadOnlyList<MultiplierControlPoint> controlPoints)
{ {
this.controlPoints = controlPoints; this.controlPoints = controlPoints;
this.timeRange = timeRange;
this.scrollLength = scrollLength;
positionCache = new Dictionary<double, double>(); positionCache = new Dictionary<double, double>();
} }
public double GetDisplayStartTime(double time) => time - timeRange - 1000; public double GetDisplayStartTime(double time, double timeRange) => time - timeRange - 1000;
public float GetLength(double startTime, double endTime) public float GetLength(double startTime, double endTime, double timeRange, float scrollLength)
{ {
var objectLength = relativePositionAtCached(endTime) - relativePositionAtCached(startTime); var objectLength = relativePositionAtCached(endTime, timeRange) - relativePositionAtCached(startTime, timeRange);
return (float)(objectLength * scrollLength); return (float)(objectLength * scrollLength);
} }
public float PositionAt(double time, double currentTime) public float PositionAt(double time, double currentTime, double timeRange, float scrollLength)
{ {
// Caching is not used here as currentTime is unlikely to have been previously cached // Caching is not used here as currentTime is unlikely to have been previously cached
double timelinePosition = relativePositionAt(currentTime); double timelinePosition = relativePositionAt(currentTime, timeRange);
return (float)((relativePositionAtCached(time) - timelinePosition) * scrollLength); return (float)((relativePositionAtCached(time, timeRange) - timelinePosition) * scrollLength);
} }
private double relativePositionAtCached(double time) private double relativePositionAtCached(double time, double timeRange)
{ {
if (!positionCache.TryGetValue(time, out double existing)) if (!positionCache.TryGetValue(time, out double existing))
positionCache[time] = existing = relativePositionAt(time); positionCache[time] = existing = relativePositionAt(time, timeRange);
return existing; return existing;
} }
public void Reset() => positionCache.Clear();
/// <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.
@ -53,7 +51,7 @@ 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 relativePositionAt(double time) private double relativePositionAt(double time, double timeRange)
{ {
if (controlPoints.Count == 0) if (controlPoints.Count == 0)
return time / timeRange; return time / timeRange;