Merge pull request #4537 from peppy/stable-storyboard-ordering

Add stable storyboard element ordering

Co-authored-by: Dan Balasescu <1329837+smoogipoo@users.noreply.github.com>
This commit is contained in:
Dean Herbert 2019-03-29 14:15:26 +09:00 committed by GitHub
commit bda8ce2dbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 18 deletions

View File

@ -29,28 +29,28 @@ public void TestDecodeStoryboardEvents()
StoryboardLayer background = storyboard.Layers.FirstOrDefault(l => l.Depth == 3);
Assert.IsNotNull(background);
Assert.AreEqual(16, background.Elements.Count());
Assert.AreEqual(16, background.Elements.Count);
Assert.IsTrue(background.EnabledWhenFailing);
Assert.IsTrue(background.EnabledWhenPassing);
Assert.AreEqual("Background", background.Name);
StoryboardLayer fail = storyboard.Layers.FirstOrDefault(l => l.Depth == 2);
Assert.IsNotNull(fail);
Assert.AreEqual(0, fail.Elements.Count());
Assert.AreEqual(0, fail.Elements.Count);
Assert.IsTrue(fail.EnabledWhenFailing);
Assert.IsFalse(fail.EnabledWhenPassing);
Assert.AreEqual("Fail", fail.Name);
StoryboardLayer pass = storyboard.Layers.FirstOrDefault(l => l.Depth == 1);
Assert.IsNotNull(pass);
Assert.AreEqual(0, pass.Elements.Count());
Assert.AreEqual(0, pass.Elements.Count);
Assert.IsFalse(pass.EnabledWhenFailing);
Assert.IsTrue(pass.EnabledWhenPassing);
Assert.AreEqual("Pass", pass.Name);
StoryboardLayer foreground = storyboard.Layers.FirstOrDefault(l => l.Depth == 0);
Assert.IsNotNull(foreground);
Assert.AreEqual(151, foreground.Elements.Count());
Assert.AreEqual(151, foreground.Elements.Count);
Assert.IsTrue(foreground.EnabledWhenFailing);
Assert.IsTrue(foreground.EnabledWhenPassing);
Assert.AreEqual("Foreground", foreground.Name);
@ -62,7 +62,7 @@ public void TestDecodeStoryboardEvents()
Assert.AreEqual(15, spriteCount);
Assert.AreEqual(1, animationCount);
Assert.AreEqual(0, sampleCount);
Assert.AreEqual(background.Elements.Count(), spriteCount + animationCount + sampleCount);
Assert.AreEqual(background.Elements.Count, spriteCount + animationCount + sampleCount);
var sprite = background.Elements.ElementAt(0) as StoryboardSprite;
Assert.NotNull(sprite);
@ -70,9 +70,9 @@ public void TestDecodeStoryboardEvents()
Assert.AreEqual(new Vector2(320, 240), sprite.InitialPosition);
Assert.IsTrue(sprite.IsDrawable);
Assert.AreEqual(Anchor.Centre, sprite.Origin);
Assert.AreEqual("SB/lyric/ja-21.png", sprite.Path);
Assert.AreEqual("SB/black.jpg", sprite.Path);
var animation = background.Elements.ElementAt(12) as StoryboardAnimation;
var animation = background.Elements.OfType<StoryboardAnimation>().First();
Assert.NotNull(animation);
Assert.AreEqual(141175, animation.EndTime);
Assert.AreEqual(10, animation.FrameCount);

View File

@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using osuTK;
using osuTK.Graphics;
using osu.Framework.Graphics;
@ -38,6 +39,10 @@ protected override void ParseStreamInto(StreamReader stream, Storyboard storyboa
{
this.storyboard = storyboard;
base.ParseStreamInto(stream, storyboard);
// OrderBy is used to guarantee that the parsing order of elements with equal start times is maintained (stably-sorted)
foreach (StoryboardLayer layer in storyboard.Layers)
layer.Elements = layer.Elements.OrderBy(h => h.StartTime).ToList();
}
protected override void ParseLine(Storyboard storyboard, Section section, string line)

View File

@ -25,7 +25,7 @@ public class DrawableStoryboardSample : Component
public DrawableStoryboardSample(StoryboardSample sample)
{
this.sample = sample;
LifetimeStart = sample.Time;
LifetimeStart = sample.StartTime;
}
[BackgroundDependencyLoader]
@ -43,27 +43,27 @@ protected override void Update()
base.Update();
// TODO: this logic will need to be consolidated with other game samples like hit sounds.
if (Time.Current < sample.Time)
if (Time.Current < sample.StartTime)
{
// We've rewound before the start time of the sample
channel?.Stop();
// In the case that the user fast-forwards to a point far beyond the start time of the sample,
// we want to be able to fall into the if-conditional below (therefore we must not have a life time end)
LifetimeStart = sample.Time;
LifetimeStart = sample.StartTime;
LifetimeEnd = double.MaxValue;
}
else if (Time.Current - Time.Elapsed < sample.Time)
else if (Time.Current - Time.Elapsed < sample.StartTime)
{
// We've passed the start time of the sample. We only play the sample if we're within an allowable range
// from the sample's start, to reduce layering if we've been fast-forwarded far into the future
if (Time.Current - sample.Time < allowable_late_start)
if (Time.Current - sample.StartTime < allowable_late_start)
channel?.Play();
// In the case that the user rewinds to a point far behind the start time of the sample,
// we want to be able to fall into the if-conditional above (therefore we must not have a life time start)
LifetimeStart = double.MinValue;
LifetimeEnd = sample.Time;
LifetimeEnd = sample.StartTime;
}
}
}

View File

@ -10,6 +10,8 @@ public interface IStoryboardElement
string Path { get; }
bool IsDrawable { get; }
double StartTime { get; }
Drawable CreateDrawable();
}
}

View File

@ -13,8 +13,7 @@ public class StoryboardLayer
public bool EnabledWhenPassing = true;
public bool EnabledWhenFailing = true;
private readonly List<IStoryboardElement> elements = new List<IStoryboardElement>();
public IEnumerable<IStoryboardElement> Elements => elements;
public List<IStoryboardElement> Elements = new List<IStoryboardElement>();
public StoryboardLayer(string name, int depth)
{
@ -24,7 +23,7 @@ public StoryboardLayer(string name, int depth)
public void Add(IStoryboardElement element)
{
elements.Add(element);
Elements.Add(element);
}
public DrawableStoryboardLayer CreateDrawable()

View File

@ -11,13 +11,14 @@ public class StoryboardSample : IStoryboardElement
public string Path { get; set; }
public bool IsDrawable => true;
public double Time;
public double StartTime { get; }
public float Volume;
public StoryboardSample(string path, double time, float volume)
{
Path = path;
Time = time;
StartTime = time;
Volume = volume;
}