AnimationSet.cs 6.4 KB

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