UndoState.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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.Reflection;
  29. using System.Collections.Generic;
  30. using log4net;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Framework.Interfaces;
  34. namespace OpenSim.Region.Framework.Scenes
  35. {
  36. public class UndoState
  37. {
  38. const int UNDOEXPIRESECONDS = 300; // undo expire time (nice to have it came from a ini later)
  39. public ObjectChangeData data;
  40. public DateTime creationtime;
  41. /// <summary>
  42. /// Constructor.
  43. /// </summary>
  44. /// <param name="part"></param>
  45. /// <param name="change">bit field with what is changed</param>
  46. ///
  47. public UndoState(SceneObjectPart part, ObjectChangeType change)
  48. {
  49. data = new ObjectChangeData();
  50. data.change = change;
  51. creationtime = DateTime.UtcNow;
  52. if (part.ParentGroup.RootPart == part)
  53. {
  54. if ((change & ObjectChangeType.Position) != 0)
  55. data.position = part.ParentGroup.AbsolutePosition;
  56. if ((change & ObjectChangeType.Rotation) != 0)
  57. data.rotation = part.RotationOffset;
  58. if ((change & ObjectChangeType.Scale) != 0)
  59. data.scale = part.Shape.Scale;
  60. }
  61. else
  62. {
  63. if ((change & ObjectChangeType.Position) != 0)
  64. data.position = part.OffsetPosition;
  65. if ((change & ObjectChangeType.Rotation) != 0)
  66. data.rotation = part.RotationOffset;
  67. if ((change & ObjectChangeType.Scale) != 0)
  68. data.scale = part.Shape.Scale;
  69. }
  70. }
  71. /// <summary>
  72. /// check if undo or redo is too old
  73. /// </summary>
  74. public bool checkExpire()
  75. {
  76. TimeSpan t = DateTime.UtcNow - creationtime;
  77. if (t.Seconds > UNDOEXPIRESECONDS)
  78. return true;
  79. return false;
  80. }
  81. /// <summary>
  82. /// updates undo or redo creation time to now
  83. /// </summary>
  84. public void updateExpire()
  85. {
  86. creationtime = DateTime.UtcNow;
  87. }
  88. /// <summary>
  89. /// Compare the relevant state in the given part to this state.
  90. /// </summary>
  91. /// <param name="part"></param>
  92. /// <returns>true what fiels and related data are equal, False otherwise.</returns>
  93. ///
  94. public bool Compare(SceneObjectPart part, ObjectChangeType change)
  95. {
  96. if (data.change != change) // if diferent targets, then they are diferent
  97. return false;
  98. if (part != null)
  99. {
  100. if (part.ParentID == 0)
  101. {
  102. if ((change & ObjectChangeType.Position) != 0 && data.position != part.ParentGroup.AbsolutePosition)
  103. return false;
  104. }
  105. else
  106. {
  107. if ((change & ObjectChangeType.Position) != 0 && data.position != part.OffsetPosition)
  108. return false;
  109. }
  110. if ((change & ObjectChangeType.Rotation) != 0 && data.rotation != part.RotationOffset)
  111. return false;
  112. if ((change & ObjectChangeType.Rotation) != 0 && data.scale == part.Shape.Scale)
  113. return false;
  114. return true;
  115. }
  116. return false;
  117. }
  118. /// <summary>
  119. /// executes the undo or redo to a part or its group
  120. /// </summary>
  121. /// <param name="part"></param>
  122. ///
  123. public void PlayState(SceneObjectPart part)
  124. {
  125. part.Undoing = true;
  126. SceneObjectGroup grp = part.ParentGroup;
  127. if (grp != null)
  128. {
  129. grp.doChangeObject(part, data);
  130. }
  131. part.Undoing = false;
  132. }
  133. }
  134. public class UndoRedoState
  135. {
  136. int size;
  137. public LinkedList<UndoState> m_redo = new LinkedList<UndoState>();
  138. public LinkedList<UndoState> m_undo = new LinkedList<UndoState>();
  139. /// <summary>
  140. /// creates a new UndoRedoState with default states memory size
  141. /// </summary>
  142. public UndoRedoState()
  143. {
  144. size = 5;
  145. }
  146. /// <summary>
  147. /// creates a new UndoRedoState with states memory having indicated size
  148. /// </summary>
  149. /// <param name="size"></param>
  150. public UndoRedoState(int _size)
  151. {
  152. if (_size < 3)
  153. size = 3;
  154. else
  155. size = _size;
  156. }
  157. /// <summary>
  158. /// returns number of undo entries in memory
  159. /// </summary>
  160. public int Count
  161. {
  162. get { return m_undo.Count; }
  163. }
  164. /// <summary>
  165. /// clears all undo and redo entries
  166. /// </summary>
  167. public void Clear()
  168. {
  169. m_undo.Clear();
  170. m_redo.Clear();
  171. }
  172. /// <summary>
  173. /// adds a new state undo to part or its group, with changes indicated by what bits
  174. /// </summary>
  175. /// <param name="part"></param>
  176. /// <param name="change">bit field with what is changed</param>
  177. public void StoreUndo(SceneObjectPart part, ObjectChangeType change)
  178. {
  179. lock (m_undo)
  180. {
  181. UndoState last;
  182. if (m_redo.Count > 0) // last code seems to clear redo on every new undo
  183. {
  184. m_redo.Clear();
  185. }
  186. if (m_undo.Count > 0)
  187. {
  188. // check expired entry
  189. last = m_undo.First.Value;
  190. if (last != null && last.checkExpire())
  191. m_undo.Clear();
  192. else
  193. {
  194. // see if we actually have a change
  195. if (last != null)
  196. {
  197. if (last.Compare(part, change))
  198. return;
  199. }
  200. }
  201. }
  202. // limite size
  203. while (m_undo.Count >= size)
  204. m_undo.RemoveLast();
  205. UndoState nUndo = new UndoState(part, change);
  206. m_undo.AddFirst(nUndo);
  207. }
  208. }
  209. /// <summary>
  210. /// executes last state undo to part or its group
  211. /// current state is pushed into redo
  212. /// </summary>
  213. /// <param name="part"></param>
  214. public void Undo(SceneObjectPart part)
  215. {
  216. lock (m_undo)
  217. {
  218. UndoState nUndo;
  219. // expire redo
  220. if (m_redo.Count > 0)
  221. {
  222. nUndo = m_redo.First.Value;
  223. if (nUndo != null && nUndo.checkExpire())
  224. m_redo.Clear();
  225. }
  226. if (m_undo.Count > 0)
  227. {
  228. UndoState goback = m_undo.First.Value;
  229. // check expired
  230. if (goback != null && goback.checkExpire())
  231. {
  232. m_undo.Clear();
  233. return;
  234. }
  235. if (goback != null)
  236. {
  237. m_undo.RemoveFirst();
  238. // redo limite size
  239. while (m_redo.Count >= size)
  240. m_redo.RemoveLast();
  241. nUndo = new UndoState(part, goback.data.change); // new value in part should it be full goback copy?
  242. m_redo.AddFirst(nUndo);
  243. goback.PlayState(part);
  244. }
  245. }
  246. }
  247. }
  248. /// <summary>
  249. /// executes last state redo to part or its group
  250. /// current state is pushed into undo
  251. /// </summary>
  252. /// <param name="part"></param>
  253. public void Redo(SceneObjectPart part)
  254. {
  255. lock (m_undo)
  256. {
  257. UndoState nUndo;
  258. // expire undo
  259. if (m_undo.Count > 0)
  260. {
  261. nUndo = m_undo.First.Value;
  262. if (nUndo != null && nUndo.checkExpire())
  263. m_undo.Clear();
  264. }
  265. if (m_redo.Count > 0)
  266. {
  267. UndoState gofwd = m_redo.First.Value;
  268. // check expired
  269. if (gofwd != null && gofwd.checkExpire())
  270. {
  271. m_redo.Clear();
  272. return;
  273. }
  274. if (gofwd != null)
  275. {
  276. m_redo.RemoveFirst();
  277. // limite undo size
  278. while (m_undo.Count >= size)
  279. m_undo.RemoveLast();
  280. nUndo = new UndoState(part, gofwd.data.change); // new value in part should it be full gofwd copy?
  281. m_undo.AddFirst(nUndo);
  282. gofwd.PlayState(part);
  283. }
  284. }
  285. }
  286. }
  287. }
  288. public class LandUndoState
  289. {
  290. public ITerrainModule m_terrainModule;
  291. public ITerrainChannel m_terrainChannel;
  292. public LandUndoState(ITerrainModule terrainModule, ITerrainChannel terrainChannel)
  293. {
  294. m_terrainModule = terrainModule;
  295. m_terrainChannel = terrainChannel;
  296. }
  297. public bool Compare(ITerrainChannel terrainChannel)
  298. {
  299. return m_terrainChannel == terrainChannel;
  300. }
  301. public void PlaybackState()
  302. {
  303. m_terrainModule.UndoTerrain(m_terrainChannel);
  304. }
  305. }
  306. }