1
0

AnimationSet.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 System.Text;
  31. using log4net;
  32. using OpenMetaverse;
  33. using OpenMetaverse.StructuredData;
  34. using OpenSim.Framework;
  35. using Animation = OpenSim.Framework.Animation;
  36. namespace OpenSim.Region.Framework.Scenes.Animation
  37. {
  38. [Serializable]
  39. public class AnimationSet
  40. {
  41. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private OpenSim.Framework.Animation m_implicitDefaultAnimation = new OpenSim.Framework.Animation();
  43. private OpenSim.Framework.Animation m_defaultAnimation = new OpenSim.Framework.Animation();
  44. private List<OpenSim.Framework.Animation> m_animations = new List<OpenSim.Framework.Animation>();
  45. public OpenSim.Framework.Animation DefaultAnimation
  46. {
  47. get { return m_defaultAnimation; }
  48. }
  49. public OpenSim.Framework.Animation ImplicitDefaultAnimation
  50. {
  51. get { return m_implicitDefaultAnimation; }
  52. }
  53. public AnimationSet()
  54. {
  55. ResetDefaultAnimation();
  56. }
  57. public AnimationSet(OSDArray pArray)
  58. {
  59. ResetDefaultAnimation();
  60. FromOSDArray(pArray);
  61. }
  62. public bool HasAnimation(UUID animID)
  63. {
  64. if (m_defaultAnimation.AnimID == animID)
  65. return true;
  66. for (int i = 0; i < m_animations.Count; ++i)
  67. {
  68. if (m_animations[i].AnimID == animID)
  69. return true;
  70. }
  71. return false;
  72. }
  73. public bool Add(UUID animID, int sequenceNum, UUID objectID)
  74. {
  75. lock (m_animations)
  76. {
  77. if (!HasAnimation(animID))
  78. {
  79. m_animations.Add(new OpenSim.Framework.Animation(animID, sequenceNum, objectID));
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. /// <summary>
  86. /// Remove the specified animation
  87. /// </summary>
  88. /// <param name='animID'></param>
  89. /// <param name='allowNoDefault'>
  90. /// If true, then the default animation can be entirely removed.
  91. /// If false, then removing the default animation will reset it to the simulator default (currently STAND).
  92. /// </param>
  93. public bool Remove(UUID animID, bool allowNoDefault)
  94. {
  95. lock (m_animations)
  96. {
  97. if (m_defaultAnimation.AnimID == animID)
  98. {
  99. if (allowNoDefault)
  100. m_defaultAnimation = new OpenSim.Framework.Animation(UUID.Zero, 1, UUID.Zero);
  101. else
  102. ResetDefaultAnimation();
  103. }
  104. else if (HasAnimation(animID))
  105. {
  106. for (int i = 0; i < m_animations.Count; i++)
  107. {
  108. if (m_animations[i].AnimID == animID)
  109. {
  110. m_animations.RemoveAt(i);
  111. return true;
  112. }
  113. }
  114. }
  115. }
  116. return false;
  117. }
  118. public void Clear()
  119. {
  120. ResetDefaultAnimation();
  121. m_animations.Clear();
  122. }
  123. /// <summary>
  124. /// The default animation is reserved for "main" animations
  125. /// that are mutually exclusive, e.g. flying and sitting.
  126. /// </summary>
  127. public bool SetDefaultAnimation(UUID animID, int sequenceNum, UUID objectID)
  128. {
  129. if (m_defaultAnimation.AnimID != animID)
  130. {
  131. m_defaultAnimation = new OpenSim.Framework.Animation(animID, sequenceNum, objectID);
  132. m_implicitDefaultAnimation = m_defaultAnimation;
  133. return true;
  134. }
  135. return false;
  136. }
  137. // Called from serialization only
  138. public void SetImplicitDefaultAnimation(UUID animID, int sequenceNum, UUID objectID)
  139. {
  140. m_implicitDefaultAnimation = new OpenSim.Framework.Animation(animID, sequenceNum, objectID);
  141. }
  142. protected bool ResetDefaultAnimation()
  143. {
  144. return TrySetDefaultAnimation("STAND", 1, UUID.Zero);
  145. }
  146. /// <summary>
  147. /// Set the animation as the default animation if it's known
  148. /// </summary>
  149. public bool TrySetDefaultAnimation(string anim, int sequenceNum, UUID objectID)
  150. {
  151. // m_log.DebugFormat(
  152. // "[ANIMATION SET]: Setting default animation {0}, sequence number {1}, object id {2}",
  153. // anim, sequenceNum, objectID);
  154. if (DefaultAvatarAnimations.AnimsUUID.ContainsKey(anim))
  155. {
  156. return SetDefaultAnimation(DefaultAvatarAnimations.AnimsUUID[anim], sequenceNum, objectID);
  157. }
  158. return false;
  159. }
  160. public void GetArrays(out UUID[] animIDs, out int[] sequenceNums, out UUID[] objectIDs)
  161. {
  162. lock (m_animations)
  163. {
  164. int defaultSize = 0;
  165. if (m_defaultAnimation.AnimID != UUID.Zero)
  166. defaultSize++;
  167. animIDs = new UUID[m_animations.Count + defaultSize];
  168. sequenceNums = new int[m_animations.Count + defaultSize];
  169. objectIDs = new UUID[m_animations.Count + defaultSize];
  170. if (m_defaultAnimation.AnimID != UUID.Zero)
  171. {
  172. animIDs[0] = m_defaultAnimation.AnimID;
  173. sequenceNums[0] = m_defaultAnimation.SequenceNum;
  174. objectIDs[0] = m_defaultAnimation.ObjectID;
  175. }
  176. for (int i = 0; i < m_animations.Count; ++i)
  177. {
  178. animIDs[i + defaultSize] = m_animations[i].AnimID;
  179. sequenceNums[i + defaultSize] = m_animations[i].SequenceNum;
  180. objectIDs[i + defaultSize] = m_animations[i].ObjectID;
  181. }
  182. }
  183. }
  184. public OpenSim.Framework.Animation[] ToArray()
  185. {
  186. OpenSim.Framework.Animation[] theArray = new OpenSim.Framework.Animation[m_animations.Count];
  187. uint i = 0;
  188. try
  189. {
  190. foreach (OpenSim.Framework.Animation anim in m_animations)
  191. theArray[i++] = anim;
  192. }
  193. catch
  194. {
  195. /* S%^t happens. Ignore. */
  196. }
  197. return theArray;
  198. }
  199. public void FromArray(OpenSim.Framework.Animation[] theArray)
  200. {
  201. foreach (OpenSim.Framework.Animation anim in theArray)
  202. m_animations.Add(anim);
  203. }
  204. // Create representation of this AnimationSet as an OSDArray.
  205. // First two entries in the array are the default and implicitDefault animations
  206. // followed by the other animations.
  207. public OSDArray ToOSDArray()
  208. {
  209. OSDArray ret = new OSDArray();
  210. ret.Add(DefaultAnimation.PackUpdateMessage());
  211. ret.Add(ImplicitDefaultAnimation.PackUpdateMessage());
  212. foreach (OpenSim.Framework.Animation anim in m_animations)
  213. ret.Add(anim.PackUpdateMessage());
  214. return ret;
  215. }
  216. public void FromOSDArray(OSDArray pArray)
  217. {
  218. this.Clear();
  219. if (pArray.Count >= 1)
  220. {
  221. m_defaultAnimation = new OpenSim.Framework.Animation((OSDMap)pArray[0]);
  222. }
  223. if (pArray.Count >= 2)
  224. {
  225. m_implicitDefaultAnimation = new OpenSim.Framework.Animation((OSDMap)pArray[1]);
  226. }
  227. for (int ii = 2; ii < pArray.Count; ii++)
  228. {
  229. m_animations.Add(new OpenSim.Framework.Animation((OSDMap)pArray[ii]));
  230. }
  231. }
  232. // Compare two AnimationSets and return 'true' if the default animations are the same
  233. // and all of the animations in the list are equal.
  234. public override bool Equals(object obj)
  235. {
  236. AnimationSet other = obj as AnimationSet;
  237. if (other != null)
  238. {
  239. if (this.DefaultAnimation.Equals(other.DefaultAnimation)
  240. && this.ImplicitDefaultAnimation.Equals(other.ImplicitDefaultAnimation))
  241. {
  242. // The defaults are the same. Is the list of animations the same?
  243. OpenSim.Framework.Animation[] thisAnims = this.ToArray();
  244. OpenSim.Framework.Animation[] otherAnims = other.ToArray();
  245. if (thisAnims.Length == 0 && otherAnims.Length == 0)
  246. return true; // the common case
  247. if (thisAnims.Length == otherAnims.Length)
  248. {
  249. // Do this the hard way but since the list is usually short this won't take long.
  250. foreach (OpenSim.Framework.Animation thisAnim in thisAnims)
  251. {
  252. bool found = false;
  253. foreach (OpenSim.Framework.Animation otherAnim in otherAnims)
  254. {
  255. if (thisAnim.Equals(otherAnim))
  256. {
  257. found = true;
  258. break;
  259. }
  260. }
  261. if (!found)
  262. {
  263. // If anything is not in the other list, these are not equal
  264. return false;
  265. }
  266. }
  267. // Found everything in the other list. Since lists are equal length, they must be equal.
  268. return true;
  269. }
  270. }
  271. return false;
  272. }
  273. // Don't know what was passed, but the base system will figure it out for me.
  274. return base.Equals(obj);
  275. }
  276. public override int GetHashCode()
  277. {
  278. return base.GetHashCode();
  279. }
  280. public override string ToString()
  281. {
  282. StringBuilder buff = new StringBuilder();
  283. buff.Append("dflt=");
  284. buff.Append(DefaultAnimation.ToString());
  285. buff.Append(",iDflt=");
  286. if (DefaultAnimation.Equals(ImplicitDefaultAnimation))
  287. buff.Append("same");
  288. else
  289. buff.Append(ImplicitDefaultAnimation.ToString());
  290. if (m_animations.Count > 0)
  291. {
  292. buff.Append(",anims=");
  293. bool firstTime = true;
  294. foreach (OpenSim.Framework.Animation anim in m_animations)
  295. {
  296. if (!firstTime)
  297. buff.Append(",");
  298. buff.Append("<");
  299. buff.Append(anim.ToString());
  300. buff.Append(">");
  301. firstTime = false;
  302. }
  303. }
  304. return buff.ToString();
  305. }
  306. }
  307. }