源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
5.2 KiB

  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using System;
  31. using System.Collections.Generic;
  32. namespace Spine {
  33. /// <summary>Stores mix (crossfade) durations to be applied when AnimationState animations are changed.</summary>
  34. public class AnimationStateData {
  35. internal SkeletonData skeletonData;
  36. readonly Dictionary<AnimationPair, float> animationToMixTime = new Dictionary<AnimationPair, float>(AnimationPairComparer.Instance);
  37. internal float defaultMix;
  38. /// <summary>The SkeletonData to look up animations when they are specified by name.</summary>
  39. public SkeletonData SkeletonData { get { return skeletonData; } }
  40. /// <summary>
  41. /// The mix duration to use when no mix duration has been specifically defined between two animations.</summary>
  42. public float DefaultMix { get { return defaultMix; } set { defaultMix = value; } }
  43. public AnimationStateData (SkeletonData skeletonData) {
  44. if (skeletonData == null) throw new ArgumentException("skeletonData cannot be null.", "skeletonData");
  45. this.skeletonData = skeletonData;
  46. }
  47. /// <summary>Sets a mix duration by animation names.</summary>
  48. public void SetMix (string fromName, string toName, float duration) {
  49. Animation from = skeletonData.FindAnimation(fromName);
  50. if (from == null) throw new ArgumentException("Animation not found: " + fromName, "fromName");
  51. Animation to = skeletonData.FindAnimation(toName);
  52. if (to == null) throw new ArgumentException("Animation not found: " + toName, "toName");
  53. SetMix(from, to, duration);
  54. }
  55. /// <summary>Sets a mix duration when changing from the specified animation to the other.
  56. /// See TrackEntry.MixDuration.</summary>
  57. public void SetMix (Animation from, Animation to, float duration) {
  58. if (from == null) throw new ArgumentNullException("from", "from cannot be null.");
  59. if (to == null) throw new ArgumentNullException("to", "to cannot be null.");
  60. AnimationPair key = new AnimationPair(from, to);
  61. animationToMixTime.Remove(key);
  62. animationToMixTime.Add(key, duration);
  63. }
  64. /// <summary>
  65. /// The mix duration to use when changing from the specified animation to the other,
  66. /// or the DefaultMix if no mix duration has been set.
  67. /// </summary>
  68. public float GetMix (Animation from, Animation to) {
  69. if (from == null) throw new ArgumentNullException("from", "from cannot be null.");
  70. if (to == null) throw new ArgumentNullException("to", "to cannot be null.");
  71. AnimationPair key = new AnimationPair(from, to);
  72. float duration;
  73. if (animationToMixTime.TryGetValue(key, out duration)) return duration;
  74. return defaultMix;
  75. }
  76. public struct AnimationPair {
  77. public readonly Animation a1;
  78. public readonly Animation a2;
  79. public AnimationPair (Animation a1, Animation a2) {
  80. this.a1 = a1;
  81. this.a2 = a2;
  82. }
  83. public override string ToString () {
  84. return a1.name + "->" + a2.name;
  85. }
  86. }
  87. // Avoids boxing in the dictionary.
  88. public class AnimationPairComparer : IEqualityComparer<AnimationPair> {
  89. public static readonly AnimationPairComparer Instance = new AnimationPairComparer();
  90. bool IEqualityComparer<AnimationPair>.Equals (AnimationPair x, AnimationPair y) {
  91. return ReferenceEquals(x.a1, y.a1) && ReferenceEquals(x.a2, y.a2);
  92. }
  93. int IEqualityComparer<AnimationPair>.GetHashCode (AnimationPair obj) {
  94. // from Tuple.CombineHashCodes // return (((h1 << 5) + h1) ^ h2);
  95. int h1 = obj.a1.GetHashCode();
  96. return (((h1 << 5) + h1) ^ obj.a2.GetHashCode());
  97. }
  98. }
  99. }
  100. }