2018-04-13 17:19:50 +08:00
|
|
|
|
// Copyright (c) 2007-2018 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 osu.Game.Rulesets.Timing;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.UI.Scrolling.Visualisers
|
|
|
|
|
{
|
2018-10-30 17:00:55 +08:00
|
|
|
|
public readonly struct SequentialSpeedChangeVisualiser : ISpeedChangeVisualiser
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2018-10-30 17:00:55 +08:00
|
|
|
|
private readonly Dictionary<double, double> positionCache;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
private readonly IReadOnlyList<MultiplierControlPoint> controlPoints;
|
2018-10-30 17:00:55 +08:00
|
|
|
|
private readonly double timeRange;
|
|
|
|
|
private readonly float scrollLength;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-10-30 17:00:55 +08:00
|
|
|
|
public SequentialSpeedChangeVisualiser(IReadOnlyList<MultiplierControlPoint> controlPoints, double timeRange, float scrollLength)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
this.controlPoints = controlPoints;
|
2018-10-30 17:00:55 +08:00
|
|
|
|
this.timeRange = timeRange;
|
|
|
|
|
this.scrollLength = scrollLength;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-10-30 17:00:55 +08:00
|
|
|
|
positionCache = new Dictionary<double, double>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-30 17:00:55 +08:00
|
|
|
|
public double GetDisplayStartTime(double startTime) => startTime - timeRange - 1000;
|
2018-10-30 16:31:43 +08:00
|
|
|
|
|
|
|
|
|
public float GetLength(double startTime, double endTime)
|
|
|
|
|
{
|
|
|
|
|
var objectLength = relativePositionAtCached(endTime) - relativePositionAtCached(startTime);
|
2018-10-30 17:00:55 +08:00
|
|
|
|
return (float)(objectLength * scrollLength);
|
2018-10-30 16:31:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2018-10-30 17:00:55 +08:00
|
|
|
|
return (float)((relativePositionAtCached(startTime) - timelinePosition) * scrollLength);
|
2018-10-30 16:31:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double relativePositionAtCached(double time)
|
|
|
|
|
{
|
|
|
|
|
if (!positionCache.TryGetValue(time, out double existing))
|
|
|
|
|
positionCache[time] = existing = relativePositionAt(time);
|
|
|
|
|
return existing;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-20 13:16:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 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.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="time">The time to find the position at.</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>
|
2018-10-30 16:31:43 +08:00
|
|
|
|
private double relativePositionAt(double time)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2018-06-08 13:49:45 +08:00
|
|
|
|
if (controlPoints.Count == 0)
|
2018-10-30 17:00:55 +08:00
|
|
|
|
return time / timeRange;
|
2018-06-08 13:49:45 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
double length = 0;
|
2018-04-20 13:16:30 +08:00
|
|
|
|
|
|
|
|
|
// We need to consider all timing points until the specified time and not just the currently-active one,
|
|
|
|
|
// since each timing point individually affects the positions of _all_ hitobjects after its start time
|
2018-04-13 17:19:50 +08:00
|
|
|
|
for (int i = 0; i < controlPoints.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var current = controlPoints[i];
|
|
|
|
|
var next = i < controlPoints.Count - 1 ? controlPoints[i + 1] : null;
|
|
|
|
|
|
2018-04-20 13:16:30 +08:00
|
|
|
|
// We don't need to consider any control points beyond the current time, since it will not yet
|
|
|
|
|
// affect any hitobjects
|
2018-04-13 17:19:50 +08:00
|
|
|
|
if (i > 0 && current.StartTime > time)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// Duration of the current control point
|
|
|
|
|
var currentDuration = (next?.StartTime ?? double.PositiveInfinity) - current.StartTime;
|
|
|
|
|
|
2018-04-20 13:16:30 +08:00
|
|
|
|
// We want to consider the minimal amount of time that this control point has affected,
|
|
|
|
|
// which may be either its duration, or the amount of time that has passed within it
|
|
|
|
|
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
|
2018-10-30 17:00:55 +08:00
|
|
|
|
length += durationInCurrent / timeRange * current.Multiplier;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|