AnimationSet.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 j = 0;
  165. if (m_defaultAnimation.AnimID != UUID.Zero)
  166. ++j;
  167. int defaultSize = m_animations.Count + j;
  168. animIDs = new UUID[defaultSize];
  169. sequenceNums = new int[defaultSize];
  170. objectIDs = new UUID[defaultSize];
  171. if (m_defaultAnimation.AnimID != UUID.Zero)
  172. {
  173. animIDs[0] = m_defaultAnimation.AnimID;
  174. sequenceNums[0] = m_defaultAnimation.SequenceNum;
  175. objectIDs[0] = m_defaultAnimation.ObjectID;
  176. }
  177. for (int i = 0; i < m_animations.Count; ++i,++j)
  178. {
  179. animIDs[j] = m_animations[i].AnimID;
  180. sequenceNums[j] = m_animations[i].SequenceNum;
  181. objectIDs[j] = m_animations[i].ObjectID;
  182. }
  183. }
  184. }
  185. public OpenSim.Framework.Animation[] ToArray()
  186. {
  187. OpenSim.Framework.Animation[] theArray = null;
  188. try
  189. {
  190. theArray = m_animations.ToArray();
  191. }
  192. catch
  193. {
  194. return new OpenSim.Framework.Animation[0];
  195. }
  196. return theArray;
  197. }
  198. public int FromArray(OpenSim.Framework.Animation[] theArray)
  199. {
  200. int ret = 0;
  201. foreach (OpenSim.Framework.Animation anim in theArray)
  202. {
  203. m_animations.Add(anim);
  204. if(anim.SequenceNum > ret)
  205. ret = anim.SequenceNum;
  206. }
  207. return ret;
  208. }
  209. // Create representation of this AnimationSet as an OSDArray.
  210. // First two entries in the array are the default and implicitDefault animations
  211. // followed by the other animations.
  212. public OSDArray ToOSDArray()
  213. {
  214. OSDArray ret = new OSDArray();
  215. ret.Add(DefaultAnimation.PackUpdateMessage());
  216. ret.Add(ImplicitDefaultAnimation.PackUpdateMessage());
  217. foreach (OpenSim.Framework.Animation anim in m_animations)
  218. ret.Add(anim.PackUpdateMessage());
  219. return ret;
  220. }
  221. public void FromOSDArray(OSDArray pArray)
  222. {
  223. this.Clear();
  224. if (pArray.Count >= 1)
  225. {
  226. m_defaultAnimation = new OpenSim.Framework.Animation((OSDMap)pArray[0]);
  227. }
  228. if (pArray.Count >= 2)
  229. {
  230. m_implicitDefaultAnimation = new OpenSim.Framework.Animation((OSDMap)pArray[1]);
  231. }
  232. for (int ii = 2; ii < pArray.Count; ii++)
  233. {
  234. m_animations.Add(new OpenSim.Framework.Animation((OSDMap)pArray[ii]));
  235. }
  236. }
  237. // Compare two AnimationSets and return 'true' if the default animations are the same
  238. // and all of the animations in the list are equal.
  239. public override bool Equals(object obj)
  240. {
  241. AnimationSet other = obj as AnimationSet;
  242. if (other != null)
  243. {
  244. if (this.DefaultAnimation.Equals(other.DefaultAnimation)
  245. && this.ImplicitDefaultAnimation.Equals(other.ImplicitDefaultAnimation))
  246. {
  247. // The defaults are the same. Is the list of animations the same?
  248. OpenSim.Framework.Animation[] thisAnims = this.ToArray();
  249. OpenSim.Framework.Animation[] otherAnims = other.ToArray();
  250. if (thisAnims.Length == 0 && otherAnims.Length == 0)
  251. return true; // the common case
  252. if (thisAnims.Length == otherAnims.Length)
  253. {
  254. // Do this the hard way but since the list is usually short this won't take long.
  255. foreach (OpenSim.Framework.Animation thisAnim in thisAnims)
  256. {
  257. bool found = false;
  258. foreach (OpenSim.Framework.Animation otherAnim in otherAnims)
  259. {
  260. if (thisAnim.Equals(otherAnim))
  261. {
  262. found = true;
  263. break;
  264. }
  265. }
  266. if (!found)
  267. {
  268. // If anything is not in the other list, these are not equal
  269. return false;
  270. }
  271. }
  272. // Found everything in the other list. Since lists are equal length, they must be equal.
  273. return true;
  274. }
  275. }
  276. return false;
  277. }
  278. // Don't know what was passed, but the base system will figure it out for me.
  279. return base.Equals(obj);
  280. }
  281. public override int GetHashCode()
  282. {
  283. return base.GetHashCode();
  284. }
  285. public override string ToString()
  286. {
  287. StringBuilder buff = new StringBuilder();
  288. buff.Append("dflt=");
  289. buff.Append(DefaultAnimation.ToString());
  290. buff.Append(",iDflt=");
  291. if (DefaultAnimation.Equals(ImplicitDefaultAnimation))
  292. buff.Append("same");
  293. else
  294. buff.Append(ImplicitDefaultAnimation.ToString());
  295. if (m_animations.Count > 0)
  296. {
  297. buff.Append(",anims=");
  298. bool firstTime = true;
  299. foreach (OpenSim.Framework.Animation anim in m_animations)
  300. {
  301. if (!firstTime)
  302. buff.Append(",");
  303. buff.Append("<");
  304. buff.Append(anim.ToString());
  305. buff.Append(">");
  306. firstTime = false;
  307. }
  308. }
  309. return buff.ToString();
  310. }
  311. }
  312. }