1
0

AnimationSet.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using OpenSim.Framework;
  30. using OpenMetaverse;
  31. namespace OpenSim.Region.Framework.Scenes
  32. {
  33. [Serializable]
  34. public class AnimationSet
  35. {
  36. public static AvatarAnimations Animations = new AvatarAnimations();
  37. private Animation m_defaultAnimation = new Animation();
  38. private List<Animation> m_animations = new List<Animation>();
  39. public Animation DefaultAnimation
  40. {
  41. get { return m_defaultAnimation; }
  42. }
  43. public AnimationSet()
  44. {
  45. ResetDefaultAnimation();
  46. }
  47. public bool HasAnimation(UUID animID)
  48. {
  49. if (m_defaultAnimation.AnimID == animID)
  50. return true;
  51. for (int i = 0; i < m_animations.Count; ++i)
  52. {
  53. if (m_animations[i].AnimID == animID)
  54. return true;
  55. }
  56. return false;
  57. }
  58. public bool Add(UUID animID, int sequenceNum, UUID objectID)
  59. {
  60. lock (m_animations)
  61. {
  62. if (!HasAnimation(animID))
  63. {
  64. m_animations.Add(new Animation(animID, sequenceNum, objectID));
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. public bool Remove(UUID animID)
  71. {
  72. lock (m_animations)
  73. {
  74. if (m_defaultAnimation.AnimID == animID)
  75. {
  76. ResetDefaultAnimation();
  77. }
  78. else if (HasAnimation(animID))
  79. {
  80. for (int i = 0; i < m_animations.Count; i++)
  81. {
  82. if (m_animations[i].AnimID == animID)
  83. {
  84. m_animations.RemoveAt(i);
  85. return true;
  86. }
  87. }
  88. }
  89. }
  90. return false;
  91. }
  92. public void Clear()
  93. {
  94. ResetDefaultAnimation();
  95. m_animations.Clear();
  96. }
  97. /// <summary>
  98. /// The default animation is reserved for "main" animations
  99. /// that are mutually exclusive, e.g. flying and sitting.
  100. /// </summary>
  101. public bool SetDefaultAnimation(UUID animID, int sequenceNum, UUID objectID)
  102. {
  103. if (m_defaultAnimation.AnimID != animID)
  104. {
  105. m_defaultAnimation = new Animation(animID, sequenceNum, objectID);
  106. return true;
  107. }
  108. return false;
  109. }
  110. protected bool ResetDefaultAnimation()
  111. {
  112. return TrySetDefaultAnimation("STAND", 1, UUID.Zero);
  113. }
  114. /// <summary>
  115. /// Set the animation as the default animation if it's known
  116. /// </summary>
  117. public bool TrySetDefaultAnimation(string anim, int sequenceNum, UUID objectID)
  118. {
  119. if (Animations.AnimsUUID.ContainsKey(anim))
  120. {
  121. return SetDefaultAnimation(Animations.AnimsUUID[anim], sequenceNum, objectID);
  122. }
  123. return false;
  124. }
  125. public void GetArrays(out UUID[] animIDs, out int[] sequenceNums, out UUID[] objectIDs)
  126. {
  127. lock (m_animations)
  128. {
  129. animIDs = new UUID[m_animations.Count + 1];
  130. sequenceNums = new int[m_animations.Count + 1];
  131. objectIDs = new UUID[m_animations.Count + 1];
  132. animIDs[0] = m_defaultAnimation.AnimID;
  133. sequenceNums[0] = m_defaultAnimation.SequenceNum;
  134. objectIDs[0] = m_defaultAnimation.ObjectID;
  135. for (int i = 0; i < m_animations.Count; ++i)
  136. {
  137. animIDs[i + 1] = m_animations[i].AnimID;
  138. sequenceNums[i + 1] = m_animations[i].SequenceNum;
  139. objectIDs[i + 1] = m_animations[i].ObjectID;
  140. }
  141. }
  142. }
  143. public Animation[] ToArray()
  144. {
  145. Animation[] theArray = new Animation[m_animations.Count];
  146. uint i = 0;
  147. try
  148. {
  149. foreach (Animation anim in m_animations)
  150. theArray[i++] = anim;
  151. }
  152. catch
  153. {
  154. /* S%^t happens. Ignore. */
  155. }
  156. return theArray;
  157. }
  158. public void FromArray(Animation[] theArray)
  159. {
  160. foreach (Animation anim in theArray)
  161. m_animations.Add(anim);
  162. }
  163. }
  164. }