Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve loop sequence logic #882

Merged
merged 8 commits into from
Jul 9, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

using System;
using System.Threading;
using NUnit.Framework;
using osu.Framework.Desktop.Platform;
using osu.Framework.Platform;
using NUnit.Framework;

namespace osu.Framework.Desktop.Tests.Platform
{
Expand Down
2 changes: 1 addition & 1 deletion osu.Framework.Tests/Lists/TestSortedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Lists;
using NUnit.Framework;

namespace osu.Framework.Tests.Lists
{
Expand Down
57 changes: 20 additions & 37 deletions osu.Framework.VisualTests/Tests/TestCaseContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Transforms;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Testing;

Expand All @@ -20,6 +19,8 @@ internal class TestCaseContextMenu : TestCase
private const int start_time = 0;
private const int duration = 1000;

private readonly ContextMenuBox movingBox;

private ContextMenuBox makeBox(Anchor anchor)
{
return new ContextMenuBox
Expand All @@ -40,7 +41,6 @@ private ContextMenuBox makeBox(Anchor anchor)

public TestCaseContextMenu()
{
ContextMenuBox movingBox;
Add(new ContextMenuContainer
{
RelativeSizeAxes = Axes.Both,
Expand All @@ -53,43 +53,26 @@ public TestCaseContextMenu()
movingBox = makeBox(Anchor.Centre),
}
});
}

movingBox.Transforms.Add(new TransformPosition
{
StartValue = Vector2.Zero,
EndValue = new Vector2(0, 100),
StartTime = start_time,
EndTime = start_time + duration,
LoopCount = -1,
LoopDelay = duration * 3
});
movingBox.Transforms.Add(new TransformPosition
{
StartValue = new Vector2(0, 100),
EndValue = new Vector2(100, 100),
StartTime = start_time + duration,
EndTime = start_time + duration * 2,
LoopCount = -1,
LoopDelay = duration * 3
});
movingBox.Transforms.Add(new TransformPosition
{
StartValue = new Vector2(100, 100),
EndValue = new Vector2(100, 0),
StartTime = start_time + duration * 2,
EndTime = start_time + duration * 3,
LoopCount = -1,
LoopDelay = duration * 3
});
movingBox.Transforms.Add(new TransformPosition
protected override void LoadComplete()
{
base.LoadComplete();

using (movingBox.BeginLoopedSequece())
{
StartValue = new Vector2(100, 0),
EndValue = Vector2.Zero,
StartTime = start_time + duration * 3,
EndTime = start_time + duration * 4,
LoopCount = -1,
LoopDelay = duration * 3
});
movingBox.MoveTo(new Vector2(0, 100), duration);
using (movingBox.BeginDelayedSequence(duration))
{
movingBox.MoveTo(new Vector2(100, 100), duration);
using (movingBox.BeginDelayedSequence(duration))
{
movingBox.MoveTo(new Vector2(100, 0), duration);
using (movingBox.BeginDelayedSequence(duration))
movingBox.MoveTo(Vector2.Zero, duration);
}
}
}
}

private class ContextMenuBox : Container, IHasContextMenu
Expand Down
51 changes: 19 additions & 32 deletions osu.Framework.VisualTests/Tests/TestCaseSizing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,13 @@ private void loadTest(int testType)
});
}

shrinkContainer.ScaleTo(new Vector2(1.5f, 1), 1000);
using (shrinkContainer.BeginDelayedSequence(1000))
using (shrinkContainer.BeginLoopedSequece())
{
shrinkContainer.ScaleTo(new Vector2(1f, 1), 1000);
shrinkContainer.ScaleTo(new Vector2(1.5f, 1), 1000);
using (shrinkContainer.BeginDelayedSequence(1000))
{
shrinkContainer.Loop();
}
shrinkContainer.ScaleTo(new Vector2(1f, 1), 1000);
}

break;

case 8:
Expand Down Expand Up @@ -480,14 +478,11 @@ private void loadTest(int testType)

foreach (Container b in new[] { box1, box2, box3 })
{
b.ScaleTo(new Vector2(2, 2), 1000);
using (b.BeginDelayedSequence(1000))
using (b.BeginLoopedSequece())
{
b.ScaleTo(new Vector2(1, 1), 1000);
b.ScaleTo(new Vector2(2, 2), 1000);
using (b.BeginDelayedSequence(1000))
{
b.Loop();
}
b.ScaleTo(new Vector2(1, 1), 1000);
}
}

Expand Down Expand Up @@ -612,14 +607,11 @@ private void loadTest(int testType)

foreach (Container b in new[] { box1, box2, box3 })
{
b.ScaleTo(new Vector2(2, 2), 1000);
using (b.BeginDelayedSequence(1000))
using (b.BeginLoopedSequece())
{
b.ScaleTo(new Vector2(1, 1), 1000);
b.ScaleTo(new Vector2(2, 2), 1000);
using (b.BeginDelayedSequence(1000))
{
b.Loop();
}
b.ScaleTo(new Vector2(1, 1), 1000);
}
}

Expand Down Expand Up @@ -744,14 +736,11 @@ private void loadTest(int testType)

foreach (Container b in new[] { box1, box2, box3 })
{
b.ScaleTo(new Vector2(2, 2), 1000);
using (b.BeginDelayedSequence(1000))
using (b.BeginLoopedSequece())
{
b.ScaleTo(new Vector2(1, 1), 1000);
b.ScaleTo(new Vector2(2, 2), 1000);
using (b.BeginDelayedSequence(1000))
{
b.Loop();
}
b.ScaleTo(new Vector2(1, 1), 1000);
}
}

Expand Down Expand Up @@ -876,12 +865,11 @@ private void loadTest(int testType)

foreach (Drawable b in new[] { box1, box2, box3 })
{
b.ScaleTo(new Vector2(2, 2), 1000);
using (b.BeginDelayedSequence(1000))
using (b.BeginLoopedSequece())
{
b.ScaleTo(new Vector2(1, 1), 1000);
b.ScaleTo(new Vector2(2, 2), 1000);
using (b.BeginDelayedSequence(1000))
b.Loop();
b.ScaleTo(new Vector2(1, 1), 1000);
}
}

Expand Down Expand Up @@ -934,12 +922,11 @@ private void loadTest(int testType)
}
});

sizedBox.ScaleTo(new Vector2(2, 2), 1000, EasingTypes.Out);
using (sizedBox.BeginDelayedSequence(1000))
using (sizedBox.BeginLoopedSequece())
{
sizedBox.ScaleTo(new Vector2(1, 1), 1000, EasingTypes.In);
sizedBox.ScaleTo(new Vector2(2, 2), 1000, EasingTypes.Out);
using (sizedBox.BeginDelayedSequence(1000))
sizedBox.Loop();
sizedBox.ScaleTo(new Vector2(1, 1), 1000, EasingTypes.In);
}

break;
Expand Down
74 changes: 74 additions & 0 deletions osu.Framework.VisualTests/Tests/TestCaseTransformSequence.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE

using OpenTK;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Testing;

namespace osu.Framework.VisualTests.Tests
{
internal class TestCaseTransformSequence : GridTestCase
{
public override string Description => @"Sequences (potentially looping) of transforms";

private readonly Drawable[] boxes;

public TestCaseTransformSequence() : base(2, 2)
{
string[] labels =
{
"Looped single transform with 3 iterations",
"Looped single transform with 1 sec pause",
"Looped double transforms",
"Looped double transforms with 1 sec pause"
};

boxes = new Drawable[Rows * Cols];
for (int i = 0; i < Rows * Cols; ++i)
{
Cell(i).Add(new[]
{
new SpriteText
{
Text = labels[i],
TextSize = 20,
},
boxes[i] = new Box
{
RelativeSizeAxes = Axes.Both,
Size = new Vector2(0.25f),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
}
});
}
}

protected override void LoadComplete()
{
base.LoadComplete();

using (boxes[0].BeginLoopedSequece(0, 3))
boxes[0].RotateTo(360, 1000);

using (boxes[1].BeginLoopedSequece(1000))
boxes[1].RotateTo(360, 1000);

using (boxes[2].BeginLoopedSequece())
{
boxes[2].RotateTo(360, 1000);
using (boxes[2].BeginDelayedSequence(1000))
boxes[2].RotateTo(0, 1000);
}

using (boxes[3].BeginLoopedSequece(1000))
{
boxes[3].RotateTo(360, 1000);
using (boxes[3].BeginDelayedSequence(1000))
boxes[3].RotateTo(0, 1000);
}
}
}
}
1 change: 1 addition & 0 deletions osu.Framework.VisualTests/osu.Framework.VisualTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Tests\TestCaseCheckboxes.cs" />
<Compile Include="Tests\TestCaseCircularProgress.cs" />
<Compile Include="Tests\TestCaseColourGradient.cs" />
<Compile Include="Tests\TestCaseTransformSequence.cs" />
<Compile Include="Tests\TestCaseNonAsyncContainer.cs" />
<Compile Include="Tests\TestCaseContextMenu.cs" />
<Compile Include="Tests\TestCaseCoordinateSpaces.cs" />
Expand Down
5 changes: 5 additions & 0 deletions osu.Framework/Graphics/Transforms/ITransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ public interface ITransform<in T> : IHasLifetime
ITransform<T> CloneReverse();

void Reverse();

void Loop(double delay, int loopCount = -1);

/// <summary>
/// Shift this transform by the specified time value.
/// </summary>
/// <param name="offset">Time in milliseconds to shift the transform.</param>
void Shift(double offset);

void NextIteration();

bool HasNextIteration { get; }
}

public class TransformTimeComparer<T> : IComparer<ITransform<T>>
Expand Down
30 changes: 17 additions & 13 deletions osu.Framework/Graphics/Transforms/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public abstract class Transform<TValue, T> : ITransform<T>
public abstract TValue CurrentValue { get; }
public TValue EndValue { get; set; }

public double LoopDelay;
public int LoopCount;
public int CurrentLoopCount;
private double loopDelay;
private int loopCount;
private int currentLoopCount;

public EasingTypes Easing;

Expand All @@ -37,7 +37,7 @@ public bool IsAlive
if (StartTime > Time?.Current)
return false;

return EndTime >= Time?.Current || LoopCount != CurrentLoopCount;
return EndTime >= Time?.Current || loopCount != currentLoopCount;
}
}

Expand All @@ -62,8 +62,8 @@ public virtual void Reverse()

public void Loop(double delay, int loopCount = -1)
{
LoopDelay = delay;
LoopCount = loopCount;
loopDelay = delay;
this.loopCount = loopCount;
}

public double LifetimeStart => StartTime;
Expand All @@ -76,17 +76,13 @@ public void Loop(double delay, int loopCount = -1)

public bool IsLoaded => true;

public bool HasNextIteration => Time?.Current > EndTime && loopCount != currentLoopCount;

public virtual void Apply(T d)
{
if (Time?.Current > EndTime && LoopCount != CurrentLoopCount)
{
CurrentLoopCount++;
double duration = Duration;
StartTime = EndTime + LoopDelay;
EndTime = StartTime + duration;
}
}


public override string ToString()
{
return string.Format("Transform({2}) {0}-{1}", StartTime, EndTime, typeof(TValue));
Expand All @@ -101,5 +97,13 @@ public void Shift(double offset)
StartTime += offset;
EndTime += offset;
}

public void NextIteration()
{
currentLoopCount++;
double duration = Duration;
StartTime = EndTime + loopDelay;
EndTime = StartTime + duration;
}
}
}
Loading