1
0

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