1
0

UndoState.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 log4net;
  30. using OpenMetaverse;
  31. using OpenSim.Region.Framework.Interfaces;
  32. namespace OpenSim.Region.Framework.Scenes
  33. {
  34. public class UndoState
  35. {
  36. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  37. public Vector3 Position = Vector3.Zero;
  38. public Vector3 Scale = Vector3.Zero;
  39. public Quaternion Rotation = Quaternion.Identity;
  40. /// <summary>
  41. /// Is this undo state for an entire group?
  42. /// </summary>
  43. public bool ForGroup;
  44. /// <summary>
  45. /// Constructor.
  46. /// </summary>
  47. /// <param name="part"></param>
  48. /// <param name="forGroup">True if the undo is for an entire group</param>
  49. public UndoState(SceneObjectPart part, bool forGroup)
  50. {
  51. if (part.ParentID == 0)
  52. {
  53. ForGroup = forGroup;
  54. // if (ForGroup)
  55. Position = part.ParentGroup.AbsolutePosition;
  56. // else
  57. // Position = part.OffsetPosition;
  58. // m_log.DebugFormat(
  59. // "[UNDO STATE]: Storing undo position {0} for root part", Position);
  60. Rotation = part.RotationOffset;
  61. // m_log.DebugFormat(
  62. // "[UNDO STATE]: Storing undo rotation {0} for root part", Rotation);
  63. Scale = part.Shape.Scale;
  64. // m_log.DebugFormat(
  65. // "[UNDO STATE]: Storing undo scale {0} for root part", Scale);
  66. }
  67. else
  68. {
  69. Position = part.OffsetPosition;
  70. // m_log.DebugFormat(
  71. // "[UNDO STATE]: Storing undo position {0} for child part", Position);
  72. Rotation = part.RotationOffset;
  73. // m_log.DebugFormat(
  74. // "[UNDO STATE]: Storing undo rotation {0} for child part", Rotation);
  75. Scale = part.Shape.Scale;
  76. // m_log.DebugFormat(
  77. // "[UNDO STATE]: Storing undo scale {0} for child part", Scale);
  78. }
  79. }
  80. /// <summary>
  81. /// Compare the relevant state in the given part to this state.
  82. /// </summary>
  83. /// <param name="part"></param>
  84. /// <returns>true if both the part's position, rotation and scale match those in this undo state. False otherwise.</returns>
  85. public bool Compare(SceneObjectPart part)
  86. {
  87. if (part != null)
  88. {
  89. if (part.ParentID == 0)
  90. return
  91. Position == part.ParentGroup.AbsolutePosition
  92. && Rotation == part.RotationOffset
  93. && Scale == part.Shape.Scale;
  94. else
  95. return
  96. Position == part.OffsetPosition
  97. && Rotation == part.RotationOffset
  98. && Scale == part.Shape.Scale;
  99. }
  100. return false;
  101. }
  102. public void PlaybackState(SceneObjectPart part)
  103. {
  104. part.Undoing = true;
  105. if (part.ParentID == 0)
  106. {
  107. // m_log.DebugFormat(
  108. // "[UNDO STATE]: Undoing position to {0} for root part {1} {2}",
  109. // Position, part.Name, part.LocalId);
  110. if (Position != Vector3.Zero)
  111. {
  112. if (ForGroup)
  113. part.ParentGroup.AbsolutePosition = Position;
  114. else
  115. part.ParentGroup.UpdateRootPosition(Position);
  116. }
  117. // m_log.DebugFormat(
  118. // "[UNDO STATE]: Undoing rotation {0} to {1} for root part {2} {3}",
  119. // part.RotationOffset, Rotation, part.Name, part.LocalId);
  120. if (ForGroup)
  121. part.UpdateRotation(Rotation);
  122. else
  123. part.ParentGroup.UpdateRootRotation(Rotation);
  124. if (Scale != Vector3.Zero)
  125. {
  126. // m_log.DebugFormat(
  127. // "[UNDO STATE]: Undoing scale {0} to {1} for root part {2} {3}",
  128. // part.Shape.Scale, Scale, part.Name, part.LocalId);
  129. if (ForGroup)
  130. part.ParentGroup.GroupResize(Scale);
  131. else
  132. part.Resize(Scale);
  133. }
  134. part.ParentGroup.ScheduleGroupForTerseUpdate();
  135. }
  136. else
  137. {
  138. if (Position != Vector3.Zero)
  139. {
  140. // m_log.DebugFormat(
  141. // "[UNDO STATE]: Undoing position {0} to {1} for child part {2} {3}",
  142. // part.OffsetPosition, Position, part.Name, part.LocalId);
  143. part.OffsetPosition = Position;
  144. }
  145. // m_log.DebugFormat(
  146. // "[UNDO STATE]: Undoing rotation {0} to {1} for child part {2} {3}",
  147. // part.RotationOffset, Rotation, part.Name, part.LocalId);
  148. part.UpdateRotation(Rotation);
  149. if (Scale != Vector3.Zero)
  150. {
  151. // m_log.DebugFormat(
  152. // "[UNDO STATE]: Undoing scale {0} to {1} for child part {2} {3}",
  153. // part.Shape.Scale, Scale, part.Name, part.LocalId);
  154. part.Resize(Scale);
  155. }
  156. part.ScheduleTerseUpdate();
  157. }
  158. part.Undoing = false;
  159. }
  160. public void PlayfwdState(SceneObjectPart part)
  161. {
  162. part.Undoing = true;
  163. if (part.ParentID == 0)
  164. {
  165. if (Position != Vector3.Zero)
  166. part.ParentGroup.AbsolutePosition = Position;
  167. if (Rotation != Quaternion.Identity)
  168. part.UpdateRotation(Rotation);
  169. if (Scale != Vector3.Zero)
  170. {
  171. if (ForGroup)
  172. part.ParentGroup.GroupResize(Scale);
  173. else
  174. part.Resize(Scale);
  175. }
  176. part.ParentGroup.ScheduleGroupForTerseUpdate();
  177. }
  178. else
  179. {
  180. if (Position != Vector3.Zero)
  181. part.OffsetPosition = Position;
  182. if (Rotation != Quaternion.Identity)
  183. part.UpdateRotation(Rotation);
  184. if (Scale != Vector3.Zero)
  185. part.Resize(Scale);
  186. part.ScheduleTerseUpdate();
  187. }
  188. part.Undoing = false;
  189. }
  190. }
  191. public class LandUndoState
  192. {
  193. public ITerrainModule m_terrainModule;
  194. public ITerrainChannel m_terrainChannel;
  195. public LandUndoState(ITerrainModule terrainModule, ITerrainChannel terrainChannel)
  196. {
  197. m_terrainModule = terrainModule;
  198. m_terrainChannel = terrainChannel;
  199. }
  200. public bool Compare(ITerrainChannel terrainChannel)
  201. {
  202. return m_terrainChannel == terrainChannel;
  203. }
  204. public void PlaybackState()
  205. {
  206. m_terrainModule.UndoTerrain(m_terrainChannel);
  207. }
  208. }
  209. }