1
0

AnimationSet.cs 6.7 KB

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