AnimationSet.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 OpenSim 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 OpenMetaverse;
  30. namespace OpenSim.Region.Framework.Scenes
  31. {
  32. [Serializable]
  33. public class AnimationSet
  34. {
  35. public static AvatarAnimations Animations = new AvatarAnimations();
  36. private Animation m_defaultAnimation = new Animation();
  37. private List<Animation> m_animations = new List<Animation>();
  38. public Animation DefaultAnimation
  39. {
  40. get { return m_defaultAnimation; }
  41. }
  42. public AnimationSet()
  43. {
  44. ResetDefaultAnimation();
  45. }
  46. public bool HasAnimation(UUID animID)
  47. {
  48. if (m_defaultAnimation.AnimID == animID)
  49. return true;
  50. for (int i = 0; i < m_animations.Count; ++i)
  51. {
  52. if (m_animations[i].AnimID == animID)
  53. return true;
  54. }
  55. return false;
  56. }
  57. public bool Add(UUID animID, int sequenceNum, UUID objectID)
  58. {
  59. lock (m_animations)
  60. {
  61. if (!HasAnimation(animID))
  62. {
  63. m_animations.Add(new Animation(animID, sequenceNum, objectID));
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. public bool Remove(UUID animID)
  70. {
  71. lock (m_animations)
  72. {
  73. if (m_defaultAnimation.AnimID == animID)
  74. {
  75. ResetDefaultAnimation();
  76. }
  77. else if (HasAnimation(animID))
  78. {
  79. for (int i = 0; i < m_animations.Count; i++)
  80. {
  81. if (m_animations[i].AnimID == animID)
  82. {
  83. m_animations.RemoveAt(i);
  84. return true;
  85. }
  86. }
  87. }
  88. }
  89. return false;
  90. }
  91. public void Clear()
  92. {
  93. ResetDefaultAnimation();
  94. m_animations.Clear();
  95. }
  96. /// <summary>
  97. /// The default animation is reserved for "main" animations
  98. /// that are mutually exclusive, e.g. flying and sitting.
  99. /// </summary>
  100. public bool SetDefaultAnimation(UUID animID, int sequenceNum, UUID objectID)
  101. {
  102. if (m_defaultAnimation.AnimID != animID)
  103. {
  104. m_defaultAnimation = new Animation(animID, sequenceNum, objectID);
  105. return true;
  106. }
  107. return false;
  108. }
  109. protected bool ResetDefaultAnimation()
  110. {
  111. return TrySetDefaultAnimation("STAND", 1, UUID.Zero);
  112. }
  113. /// <summary>
  114. /// Set the animation as the default animation if it's known
  115. /// </summary>
  116. public bool TrySetDefaultAnimation(string anim, int sequenceNum, UUID objectID)
  117. {
  118. if (Animations.AnimsUUID.ContainsKey(anim))
  119. {
  120. return SetDefaultAnimation(Animations.AnimsUUID[anim], sequenceNum, objectID);
  121. }
  122. return false;
  123. }
  124. public void GetArrays(out UUID[] animIDs, out int[] sequenceNums, out UUID[] objectIDs)
  125. {
  126. lock (m_animations)
  127. {
  128. animIDs = new UUID[m_animations.Count + 1];
  129. sequenceNums = new int[m_animations.Count + 1];
  130. objectIDs = new UUID[m_animations.Count + 1];
  131. animIDs[0] = m_defaultAnimation.AnimID;
  132. sequenceNums[0] = m_defaultAnimation.SequenceNum;
  133. objectIDs[0] = m_defaultAnimation.ObjectID;
  134. for (int i = 0; i < m_animations.Count; ++i)
  135. {
  136. animIDs[i + 1] = m_animations[i].AnimID;
  137. sequenceNums[i + 1] = m_animations[i].SequenceNum;
  138. objectIDs[i + 1] = m_animations[i].ObjectID;
  139. }
  140. }
  141. }
  142. public Animation[] ToArray()
  143. {
  144. Animation[] theArray = new Animation[m_animations.Count];
  145. uint i = 0;
  146. try
  147. {
  148. foreach (Animation anim in m_animations)
  149. theArray[i++] = anim;
  150. }
  151. catch
  152. {
  153. /* S%^t happens. Ignore. */
  154. }
  155. return theArray;
  156. }
  157. public void FromArray(Animation[] theArray)
  158. {
  159. foreach (Animation anim in theArray)
  160. m_animations.Add(anim);
  161. }
  162. }
  163. }