AnimationSet.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 readonly 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.Equals(animID))
  65. return true;
  66. for (int i = 0; i < m_animations.Count; ++i)
  67. {
  68. if (m_animations[i].AnimID.Equals(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.Equals(animID))
  98. {
  99. if (allowNoDefault)
  100. m_defaultAnimation = new OpenSim.Framework.Animation(UUID.Zero, 1, UUID.Zero);
  101. else
  102. ResetDefaultAnimation();
  103. }
  104. else
  105. {
  106. for (int i = 0; i < m_animations.Count; i++)
  107. {
  108. if (m_animations[i].AnimID.Equals(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.NotEqual(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. return true;
  137. }
  138. // Called from serialization only
  139. public void SetImplicitDefaultAnimation(UUID animID, int sequenceNum, UUID objectID)
  140. {
  141. m_implicitDefaultAnimation = new OpenSim.Framework.Animation(animID, sequenceNum, objectID);
  142. }
  143. protected bool ResetDefaultAnimation()
  144. {
  145. return TrySetDefaultAnimation("STAND", 1, UUID.Zero);
  146. }
  147. /// <summary>
  148. /// Set the animation as the default animation if it's known
  149. /// </summary>
  150. public bool TrySetDefaultAnimation(string anim, int sequenceNum, UUID objectID)
  151. {
  152. //m_log.DebugFormat(
  153. // "[ANIMATION SET]: Setting default animation {0}, sequence number {1}, object id {2}",
  154. // anim, sequenceNum, objectID);
  155. if (DefaultAvatarAnimations.AnimsUUIDbyName.TryGetValue(anim, out UUID id))
  156. {
  157. return SetDefaultAnimation(id, sequenceNum, objectID);
  158. }
  159. return false;
  160. }
  161. public void GetAnimationIDsArray(out UUID[] animIDs)
  162. {
  163. lock (m_animations)
  164. {
  165. int j = m_defaultAnimation.AnimID.IsZero() ? 0 : 1;
  166. int defaultSize = m_animations.Count + j;
  167. animIDs = new UUID[defaultSize];
  168. if (j > 0)
  169. {
  170. animIDs[0] = m_defaultAnimation.AnimID;
  171. }
  172. for (int i = 0; i < m_animations.Count; ++i, ++j)
  173. {
  174. animIDs[j] = m_animations[i].AnimID;
  175. }
  176. }
  177. }
  178. public void GetArrays(out UUID[] animIDs, out int[] sequenceNums, out UUID[] objectIDs)
  179. {
  180. lock (m_animations)
  181. {
  182. int j = m_defaultAnimation.AnimID.IsZero() ? 0 : 1;
  183. int defaultSize = m_animations.Count + j;
  184. animIDs = new UUID[defaultSize];
  185. sequenceNums = new int[defaultSize];
  186. objectIDs = new UUID[defaultSize];
  187. if (j > 0)
  188. {
  189. animIDs[0] = m_defaultAnimation.AnimID;
  190. sequenceNums[0] = m_defaultAnimation.SequenceNum;
  191. objectIDs[0] = m_defaultAnimation.ObjectID;
  192. }
  193. for (int i = 0; i < m_animations.Count; ++i,++j)
  194. {
  195. animIDs[j] = m_animations[i].AnimID;
  196. sequenceNums[j] = m_animations[i].SequenceNum;
  197. objectIDs[j] = m_animations[i].ObjectID;
  198. }
  199. }
  200. }
  201. public OpenSim.Framework.Animation[] ToArray()
  202. {
  203. OpenSim.Framework.Animation[] theArray = null;
  204. try
  205. {
  206. theArray = m_animations.ToArray();
  207. }
  208. catch
  209. {
  210. return new OpenSim.Framework.Animation[0];
  211. }
  212. return theArray;
  213. }
  214. public int FromArray(OpenSim.Framework.Animation[] theArray)
  215. {
  216. int ret = 0;
  217. foreach (OpenSim.Framework.Animation anim in theArray)
  218. {
  219. m_animations.Add(anim);
  220. if(anim.SequenceNum > ret)
  221. ret = anim.SequenceNum;
  222. }
  223. return ret;
  224. }
  225. // Create representation of this AnimationSet as an OSDArray.
  226. // First two entries in the array are the default and implicitDefault animations
  227. // followed by the other animations.
  228. public OSDArray ToOSDArray()
  229. {
  230. OSDArray ret = new OSDArray();
  231. ret.Add(DefaultAnimation.PackUpdateMessage());
  232. ret.Add(ImplicitDefaultAnimation.PackUpdateMessage());
  233. foreach (OpenSim.Framework.Animation anim in m_animations)
  234. ret.Add(anim.PackUpdateMessage());
  235. return ret;
  236. }
  237. public void FromOSDArray(OSDArray pArray)
  238. {
  239. this.Clear();
  240. if (pArray.Count >= 1)
  241. {
  242. m_defaultAnimation = new OpenSim.Framework.Animation((OSDMap)pArray[0]);
  243. }
  244. if (pArray.Count >= 2)
  245. {
  246. m_implicitDefaultAnimation = new OpenSim.Framework.Animation((OSDMap)pArray[1]);
  247. }
  248. for (int ii = 2; ii < pArray.Count; ii++)
  249. {
  250. m_animations.Add(new OpenSim.Framework.Animation((OSDMap)pArray[ii]));
  251. }
  252. }
  253. // Compare two AnimationSets and return 'true' if the default animations are the same
  254. // and all of the animations in the list are equal.
  255. public override bool Equals(object obj)
  256. {
  257. AnimationSet other = obj as AnimationSet;
  258. if (other != null)
  259. {
  260. if (this.DefaultAnimation.Equals(other.DefaultAnimation)
  261. && this.ImplicitDefaultAnimation.Equals(other.ImplicitDefaultAnimation))
  262. {
  263. // The defaults are the same. Is the list of animations the same?
  264. OpenSim.Framework.Animation[] thisAnims = this.ToArray();
  265. OpenSim.Framework.Animation[] otherAnims = other.ToArray();
  266. if (thisAnims.Length == 0 && otherAnims.Length == 0)
  267. return true; // the common case
  268. if (thisAnims.Length == otherAnims.Length)
  269. {
  270. // Do this the hard way but since the list is usually short this won't take long.
  271. foreach (OpenSim.Framework.Animation thisAnim in thisAnims)
  272. {
  273. bool found = false;
  274. foreach (OpenSim.Framework.Animation otherAnim in otherAnims)
  275. {
  276. if (thisAnim.Equals(otherAnim))
  277. {
  278. found = true;
  279. break;
  280. }
  281. }
  282. if (!found)
  283. {
  284. // If anything is not in the other list, these are not equal
  285. return false;
  286. }
  287. }
  288. // Found everything in the other list. Since lists are equal length, they must be equal.
  289. return true;
  290. }
  291. }
  292. return false;
  293. }
  294. // Don't know what was passed, but the base system will figure it out for me.
  295. return base.Equals(obj);
  296. }
  297. public override int GetHashCode()
  298. {
  299. return base.GetHashCode();
  300. }
  301. public override string ToString()
  302. {
  303. StringBuilder buff = new StringBuilder();
  304. buff.Append("dflt=");
  305. buff.Append(DefaultAnimation.ToString());
  306. buff.Append(",iDflt=");
  307. if (DefaultAnimation.Equals(ImplicitDefaultAnimation))
  308. buff.Append("same");
  309. else
  310. buff.Append(ImplicitDefaultAnimation.ToString());
  311. if (m_animations.Count > 0)
  312. {
  313. buff.Append(",anims=");
  314. bool firstTime = true;
  315. foreach (OpenSim.Framework.Animation anim in m_animations)
  316. {
  317. if (!firstTime)
  318. buff.Append(",");
  319. buff.Append("<");
  320. buff.Append(anim.ToString());
  321. buff.Append(">");
  322. firstTime = false;
  323. }
  324. }
  325. return buff.ToString();
  326. }
  327. }
  328. }