Merge branch 'master' into more-global-scrollalgo

This commit is contained in:
Dean Herbert 2018-11-14 13:54:53 +09:00 committed by GitHub
commit 2ae139d260
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 256 additions and 0 deletions

View File

@ -0,0 +1,54 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using NUnit.Framework;
using osu.Game.Rulesets.UI.Scrolling.Algorithms;
namespace osu.Game.Tests.ScrollAlgorithms
{
[TestFixture]
public class ConstantScrollTest
{
private IScrollAlgorithm algorithm;
[SetUp]
public void Setup()
{
algorithm = new ConstantScrollAlgorithm();
}
[Test]
public void TestDisplayStartTime()
{
Assert.AreEqual(-8000, algorithm.GetDisplayStartTime(2000, 10000));
Assert.AreEqual(-3000, algorithm.GetDisplayStartTime(2000, 5000));
Assert.AreEqual(2000, algorithm.GetDisplayStartTime(7000, 5000));
Assert.AreEqual(7000, algorithm.GetDisplayStartTime(17000, 10000));
}
[Test]
public void TestLength()
{
Assert.AreEqual(1f / 5, algorithm.GetLength(0, 1000, 5000, 1));
Assert.AreEqual(1f / 5, algorithm.GetLength(6000, 7000, 5000, 1));
}
[Test]
public void TestPosition()
{
Assert.AreEqual(1f / 5, algorithm.PositionAt(1000, 0, 5000, 1));
Assert.AreEqual(1f / 5, algorithm.PositionAt(6000, 5000, 5000, 1));
}
[TestCase(1000)]
[TestCase(10000)]
[TestCase(15000)]
[TestCase(20000)]
[TestCase(25000)]
public void TestTime(double time)
{
Assert.AreEqual(time, algorithm.TimeAt(algorithm.PositionAt(time, 0, 5000, 1), 0, 5000, 1), 0.001);
Assert.AreEqual(time, algorithm.TimeAt(algorithm.PositionAt(time, 5000, 5000, 1), 5000, 5000, 1), 0.001);
}
}
}

View File

@ -0,0 +1,67 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using NUnit.Framework;
using osu.Framework.Lists;
using osu.Game.Rulesets.Timing;
using osu.Game.Rulesets.UI.Scrolling.Algorithms;
namespace osu.Game.Tests.ScrollAlgorithms
{
[TestFixture]
public class OverlappingScrollTest
{
private IScrollAlgorithm algorithm;
[SetUp]
public void Setup()
{
var controlPoints = new SortedList<MultiplierControlPoint>
{
new MultiplierControlPoint(0) { Velocity = 1 },
new MultiplierControlPoint(10000) { Velocity = 2f },
new MultiplierControlPoint(20000) { Velocity = 0.5f }
};
algorithm = new OverlappingScrollAlgorithm(controlPoints);
}
[Test]
public void TestDisplayStartTime()
{
Assert.AreEqual(1000, algorithm.GetDisplayStartTime(2000, 1000)); // Like constant
Assert.AreEqual(10000, algorithm.GetDisplayStartTime(10500, 1000)); // 10500 - (1000 * 0.5)
Assert.AreEqual(20000, algorithm.GetDisplayStartTime(22000, 1000)); // 23000 - (1000 / 0.5)
}
[Test]
public void TestLength()
{
Assert.AreEqual(1f / 5, algorithm.GetLength(0, 1000, 5000, 1)); // Like constant
Assert.AreEqual(1f / 5, algorithm.GetLength(10000, 10500, 5000, 1)); // (10500 - 10000) / 0.5 / 5000
Assert.AreEqual(1f / 5, algorithm.GetLength(20000, 22000, 5000, 1)); // (22000 - 20000) * 0.5 / 5000
}
[Test]
public void TestPosition()
{
// Basically same calculations as TestLength()
Assert.AreEqual(1f / 5, algorithm.PositionAt(1000, 0, 5000, 1));
Assert.AreEqual(1f / 5, algorithm.PositionAt(10500, 10000, 5000, 1));
Assert.AreEqual(1f / 5, algorithm.PositionAt(22000, 20000, 5000, 1));
}
[TestCase(1000)]
[TestCase(10000)]
[TestCase(15000)]
[TestCase(20000)]
[TestCase(25000)]
[Ignore("Disabled for now because overlapping control points have multiple time values under the same position."
+ "Ideally, scrolling should be changed to constant or sequential during editing of hitobjects.")]
public void TestTime(double time)
{
Assert.AreEqual(time, algorithm.TimeAt(algorithm.PositionAt(time, 0, 5000, 1), 0, 5000, 1), 0.001);
Assert.AreEqual(time, algorithm.TimeAt(algorithm.PositionAt(time, 5000, 5000, 1), 5000, 5000, 1), 0.001);
}
}
}

View File

@ -0,0 +1,64 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using NUnit.Framework;
using osu.Framework.Lists;
using osu.Game.Rulesets.Timing;
using osu.Game.Rulesets.UI.Scrolling.Algorithms;
namespace osu.Game.Tests.ScrollAlgorithms
{
[TestFixture]
public class SequentialScrollTest
{
private IScrollAlgorithm algorithm;
[SetUp]
public void Setup()
{
var controlPoints = new SortedList<MultiplierControlPoint>
{
new MultiplierControlPoint(0) { Velocity = 1 },
new MultiplierControlPoint(10000) { Velocity = 2f },
new MultiplierControlPoint(20000) { Velocity = 0.5f }
};
algorithm = new SequentialScrollAlgorithm(controlPoints);
}
[Test]
public void TestDisplayStartTime()
{
// Sequential scroll algorithm approximates the start time
// This should be fixed in the future
}
[Test]
public void TestLength()
{
Assert.AreEqual(1f / 5, algorithm.GetLength(0, 1000, 5000, 1)); // Like constant
Assert.AreEqual(1f / 5, algorithm.GetLength(10000, 10500, 5000, 1)); // (10500 - 10000) / 0.5 / 5000
Assert.AreEqual(1f / 5, algorithm.GetLength(20000, 22000, 5000, 1)); // (22000 - 20000) * 0.5 / 5000
}
[Test]
public void TestPosition()
{
// Basically same calculations as TestLength()
Assert.AreEqual(1f / 5, algorithm.PositionAt(1000, 0, 5000, 1));
Assert.AreEqual(1f / 5, algorithm.PositionAt(10500, 10000, 5000, 1));
Assert.AreEqual(1f / 5, algorithm.PositionAt(22000, 20000, 5000, 1));
}
[TestCase(1000)]
[TestCase(10000)]
[TestCase(15000)]
[TestCase(20000)]
[TestCase(25000)]
public void TestTime(double time)
{
Assert.AreEqual(time, algorithm.TimeAt(algorithm.PositionAt(time, 0, 5000, 1), 0, 5000, 1), 0.001);
Assert.AreEqual(time, algorithm.TimeAt(algorithm.PositionAt(time, 5000, 5000, 1), 5000, 5000, 1), 0.001);
}
}
}

View File

@ -17,6 +17,9 @@ public float GetLength(double startTime, double endTime, double timeRange, float
public float PositionAt(double time, double currentTime, double timeRange, float scrollLength)
=> (float)((time - currentTime) / timeRange * scrollLength);
public double TimeAt(float position, double currentTime, double timeRange, float scrollLength)
=> position * timeRange / scrollLength + currentTime;
public void Reset()
{
}

View File

@ -36,6 +36,16 @@ public interface IScrollAlgorithm
/// <returns>The absolute spatial position.</returns>
float PositionAt(double time, double currentTime, double timeRange, float scrollLength);
/// <summary>
/// Computes the time which brings a point to a provided spatial position given the current time.
/// </summary>
/// <param name="position">The absolute spatial position.</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 time at which <see cref="PositionAt(t)"/> == <paramref name="position"/>.</returns>
double TimeAt(float position, double currentTime, double timeRange, float scrollLength);
/// <summary>
/// Resets this <see cref="IScrollAlgorithm"/> to a default state.
/// </summary>

View File

@ -3,6 +3,7 @@
using osu.Framework.Lists;
using osu.Game.Rulesets.Timing;
using OpenTK;
namespace osu.Game.Rulesets.UI.Scrolling.Algorithms
{
@ -36,6 +37,33 @@ public float GetLength(double startTime, double endTime, double timeRange, float
public float PositionAt(double time, double currentTime, double timeRange, float scrollLength)
=> (float)((time - currentTime) / timeRange * controlPointAt(time).Multiplier * scrollLength);
public double TimeAt(float position, double currentTime, double timeRange, float scrollLength)
{
// Find the control point relating to the position.
// Note: Due to velocity adjustments, overlapping control points will provide multiple valid time values for a single position
// As such, this operation provides unexpected results by using the latter of the control points.
int i = 0;
float pos = 0;
for (; i < controlPoints.Count; i++)
{
float lastPos = pos;
pos = PositionAt(controlPoints[i].StartTime, currentTime, timeRange, scrollLength);
if (pos > position)
{
i--;
pos = lastPos;
break;
}
}
i = MathHelper.Clamp(i, 0, controlPoints.Count - 1);
return controlPoints[i].StartTime + (position - pos) * timeRange / controlPoints[i].Multiplier / scrollLength;
}
public void Reset()
{
}

View File

@ -35,6 +35,36 @@ public float PositionAt(double time, double currentTime, double timeRange, float
return (float)((relativePositionAtCached(time, timeRange) - timelinePosition) * scrollLength);
}
public double TimeAt(float position, double currentTime, double timeRange, float scrollLength)
{
// Convert the position to a length relative to time = 0
double length = position / scrollLength + relativePositionAt(currentTime, timeRange);
// 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
for (int i = 0; i < controlPoints.Count; i++)
{
var current = controlPoints[i];
var next = i < controlPoints.Count - 1 ? controlPoints[i + 1] : null;
// Duration of the current control point
var currentDuration = (next?.StartTime ?? double.PositiveInfinity) - current.StartTime;
// Figure out the length of control point
var currentLength = currentDuration / timeRange * current.Multiplier;
if (currentLength > length)
{
// The point is within this control point
return current.StartTime + length * timeRange / current.Multiplier;
}
length -= currentLength;
}
return 0; // Should never occur
}
private double relativePositionAtCached(double time, double timeRange)
{
if (!positionCache.TryGetValue(time, out double existing))