AnimationStateData.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System;
  30. using System.Collections.Generic;
  31. namespace Spine {
  32. /// <summary>Stores mix (crossfade) durations to be applied when AnimationState animations are changed.</summary>
  33. public class AnimationStateData {
  34. internal SkeletonData skeletonData;
  35. readonly Dictionary<AnimationPair, float> animationToMixTime = new Dictionary<AnimationPair, float>(AnimationPairComparer.Instance);
  36. internal float defaultMix;
  37. /// <summary>The SkeletonData to look up animations when they are specified by name.</summary>
  38. public SkeletonData SkeletonData { get { return skeletonData; } }
  39. /// <summary>
  40. /// The mix duration to use when no mix duration has been specifically defined between two animations.</summary>
  41. public float DefaultMix { get { return defaultMix; } set { defaultMix = value; } }
  42. public AnimationStateData (SkeletonData skeletonData) {
  43. if (skeletonData == null) throw new ArgumentException("skeletonData cannot be null.", "skeletonData");
  44. this.skeletonData = skeletonData;
  45. }
  46. /// <summary>Sets a mix duration by animation names.</summary>
  47. public void SetMix (string fromName, string toName, float duration) {
  48. Animation from = skeletonData.FindAnimation(fromName);
  49. if (from == null) throw new ArgumentException("Animation not found: " + fromName, "fromName");
  50. Animation to = skeletonData.FindAnimation(toName);
  51. if (to == null) throw new ArgumentException("Animation not found: " + toName, "toName");
  52. SetMix(from, to, duration);
  53. }
  54. /// <summary>Sets a mix duration when changing from the specified animation to the other.
  55. /// See TrackEntry.MixDuration.</summary>
  56. public void SetMix (Animation from, Animation to, float duration) {
  57. if (from == null) throw new ArgumentNullException("from", "from cannot be null.");
  58. if (to == null) throw new ArgumentNullException("to", "to cannot be null.");
  59. AnimationPair key = new AnimationPair(from, to);
  60. animationToMixTime.Remove(key);
  61. animationToMixTime.Add(key, duration);
  62. }
  63. /// <summary>
  64. /// The mix duration to use when changing from the specified animation to the other,
  65. /// or the DefaultMix if no mix duration has been set.
  66. /// </summary>
  67. public float GetMix (Animation from, Animation to) {
  68. if (from == null) throw new ArgumentNullException("from", "from cannot be null.");
  69. if (to == null) throw new ArgumentNullException("to", "to cannot be null.");
  70. AnimationPair key = new AnimationPair(from, to);
  71. float duration;
  72. if (animationToMixTime.TryGetValue(key, out duration)) return duration;
  73. return defaultMix;
  74. }
  75. public struct AnimationPair {
  76. public readonly Animation a1;
  77. public readonly Animation a2;
  78. public AnimationPair (Animation a1, Animation a2) {
  79. this.a1 = a1;
  80. this.a2 = a2;
  81. }
  82. public override string ToString () {
  83. return a1.name + "->" + a2.name;
  84. }
  85. }
  86. // Avoids boxing in the dictionary.
  87. public class AnimationPairComparer : IEqualityComparer<AnimationPair> {
  88. public static readonly AnimationPairComparer Instance = new AnimationPairComparer();
  89. bool IEqualityComparer<AnimationPair>.Equals (AnimationPair x, AnimationPair y) {
  90. return ReferenceEquals(x.a1, y.a1) && ReferenceEquals(x.a2, y.a2);
  91. }
  92. int IEqualityComparer<AnimationPair>.GetHashCode (AnimationPair obj) {
  93. // from Tuple.CombineHashCodes // return (((h1 << 5) + h1) ^ h2);
  94. int h1 = obj.a1.GetHashCode();
  95. return (((h1 << 5) + h1) ^ obj.a2.GetHashCode());
  96. }
  97. }
  98. }
  99. }