diff --git a/Intar.Tests/AnimationCurveTest.Unity.cs b/Intar.Tests/AnimationCurveTest.Unity.cs index 691c5341a..864424ce3 100644 --- a/Intar.Tests/AnimationCurveTest.Unity.cs +++ b/Intar.Tests/AnimationCurveTest.Unity.cs @@ -48,5 +48,31 @@ public static void TestAnimationCurveConstructor() { Assert.IsNotNull(curve.keys); Assert.AreEqual(0, curve.length); } + + [Test] + public static void TestSetKeys() { + var keys = new Keyframe[] { + new Keyframe(4, 5), + new Keyframe(4, 4), + new Keyframe(3, 4), + new Keyframe(3, 3), + new Keyframe(2, 3), + new Keyframe(2, 2), + new Keyframe(1, 2), + new Keyframe(1, 1), + }; + var curve = new AnimationCurve { keys = keys }; + + // keys 設定時, 値が安定ソートされることをテスト + Assert.AreEqual(8, curve.length); + Assert.AreEqual(keys[6], curve[0]); + Assert.AreEqual(keys[7], curve[1]); + Assert.AreEqual(keys[4], curve[2]); + Assert.AreEqual(keys[5], curve[3]); + Assert.AreEqual(keys[2], curve[4]); + Assert.AreEqual(keys[3], curve[5]); + Assert.AreEqual(keys[0], curve[6]); + Assert.AreEqual(keys[1], curve[7]); + } } } diff --git a/Intar.Tests/AnimationCurveTest.cs b/Intar.Tests/AnimationCurveTest.cs index 6cab910e8..a99516525 100644 --- a/Intar.Tests/AnimationCurveTest.cs +++ b/Intar.Tests/AnimationCurveTest.cs @@ -39,5 +39,32 @@ public static void TestAnimationCurveI17F15Constructor() { Assert.IsNotNull(curve.Keys); Assert.AreEqual(0, curve.Length); } + + [Test] + public static void TestSetKeysI17F15() { + var keys = new KeyframeI17F15[] { + new KeyframeI17F15(I17F15.FromBits(4000), I17F15.FromBits(5000)), + new KeyframeI17F15(I17F15.FromBits(4000), I17F15.FromBits(4000)), + new KeyframeI17F15(I17F15.FromBits(3000), I17F15.FromBits(4000)), + new KeyframeI17F15(I17F15.FromBits(3000), I17F15.FromBits(3000)), + new KeyframeI17F15(I17F15.FromBits(2000), I17F15.FromBits(3000)), + new KeyframeI17F15(I17F15.FromBits(2000), I17F15.FromBits(2000)), + new KeyframeI17F15(I17F15.FromBits(1000), I17F15.FromBits(2000)), + new KeyframeI17F15(I17F15.FromBits(1000), I17F15.FromBits(1000)), + }; + var curve = new AnimationCurveI17F15 { Keys = keys }; + + // Keys 設定時, 値が安定ソートされることをテスト + Assert.AreEqual(8, curve.Length); + Assert.AreEqual(keys[6], curve[0]); + Assert.AreEqual(keys[7], curve[1]); + Assert.AreEqual(keys[4], curve[2]); + Assert.AreEqual(keys[5], curve[3]); + Assert.AreEqual(keys[2], curve[4]); + Assert.AreEqual(keys[3], curve[5]); + Assert.AreEqual(keys[0], curve[6]); + Assert.AreEqual(keys[1], curve[7]); + } + } } diff --git a/Intar/AnimationCurveI17F15.cs b/Intar/AnimationCurveI17F15.cs index daf5384e8..e1a1a64a2 100644 --- a/Intar/AnimationCurveI17F15.cs +++ b/Intar/AnimationCurveI17F15.cs @@ -85,6 +85,7 @@ public KeyframeI17F15[] Keys { return; } keys = new List(value); + Utility.InsertionSort(keys, (a, b) => a.Time.CompareTo(b.Time)); } } public int Length { diff --git a/Intar/Utility.cs b/Intar/Utility.cs new file mode 100644 index 000000000..889151940 --- /dev/null +++ b/Intar/Utility.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; + +namespace Intar { + /// + /// 主に実装用のユーティリティクラス + /// + static class Utility { + internal static void InsertionSort(IList list, Comparison comparison) { + var count = list.Count; + for (var i = 1; i < count; i++) { + var key = list[i]; + var j = i - 1; + while (0 <= j && 0 < comparison(list[j], key)) { + list[j + 1] = list[j]; + j--; + } + list[j + 1] = key; + } + } + } +} diff --git a/Intar/Utility.cs.meta b/Intar/Utility.cs.meta new file mode 100644 index 000000000..bb110110c --- /dev/null +++ b/Intar/Utility.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: fe2eea8ac0eb02740bb044dcd32f2edf \ No newline at end of file