RotateHandle.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /// <summary>
  7. /// Handle that allows an object to be rotated around the three primary axes, as well as a free axis currently
  8. /// facing the camera.
  9. /// </summary>
  10. public sealed class RotateHandle : DefaultHandle
  11. {
  12. private Quaternion delta;
  13. private HandleSliderDisc xAxis;
  14. private HandleSliderDisc yAxis;
  15. private HandleSliderDisc zAxis;
  16. private HandleSliderDisc freeAxis;
  17. private bool isDragged;
  18. private Quaternion dragStartRotation;
  19. /// <summary>
  20. /// Amount of rotation applied since the last frame. Only valid while the handle is being dragged.
  21. /// </summary>
  22. public Quaternion Delta
  23. {
  24. get { return delta; }
  25. }
  26. /// <inheritdoc/>
  27. internal override bool IsDragged()
  28. {
  29. return xAxis.State == HandleSlider.StateType.Active ||
  30. yAxis.State == HandleSlider.StateType.Active ||
  31. zAxis.State == HandleSlider.StateType.Active ||
  32. freeAxis.State == HandleSlider.StateType.Active;
  33. }
  34. /// <summary>
  35. /// Creates a new rotation handle.
  36. /// </summary>
  37. public RotateHandle()
  38. {
  39. xAxis = new HandleSliderDisc(this, Vector3.XAxis, 1.0f);
  40. yAxis = new HandleSliderDisc(this, Vector3.YAxis, 1.0f);
  41. zAxis = new HandleSliderDisc(this, Vector3.ZAxis, 1.0f);
  42. freeAxis = new HandleSliderDisc(this, -Vector3.ZAxis, 1.2f);
  43. }
  44. /// <inheritdoc/>
  45. protected internal override void PreInput()
  46. {
  47. xAxis.Position = position;
  48. yAxis.Position = position;
  49. zAxis.Position = position;
  50. freeAxis.Position = position;
  51. Quaternion handleRotation = isDragged ? dragStartRotation : Rotation;
  52. xAxis.Rotation = handleRotation;
  53. yAxis.Rotation = handleRotation;
  54. zAxis.Rotation = handleRotation;
  55. freeAxis.Rotation = EditorApplication.SceneViewCamera.SceneObject.Rotation;
  56. xAxis.SetCutoffPlane(GetXStartAngle(isDragged), true);
  57. yAxis.SetCutoffPlane(GetYStartAngle(isDragged), true);
  58. zAxis.SetCutoffPlane(GetZStartAngle(isDragged), true);
  59. }
  60. /// <inheritdoc/>
  61. protected internal override void PostInput()
  62. {
  63. if (IsDragged())
  64. {
  65. if (!isDragged)
  66. {
  67. isDragged = true;
  68. dragStartRotation = Rotation;
  69. }
  70. }
  71. else
  72. {
  73. isDragged = false;
  74. dragStartRotation = Quaternion.Identity;
  75. }
  76. Degree xValue = (Degree)0.0f;
  77. Degree yValue = (Degree)0.0f;
  78. Degree zValue = (Degree)0.0f;
  79. Degree freeAxisValue = (Degree)0.0f;
  80. if (Handles.RotateHandleSnapActive)
  81. {
  82. xValue = Handles.SnapValue(xAxis.Delta, Handles.RotateSnapAmount);
  83. yValue = Handles.SnapValue(yAxis.Delta, Handles.RotateSnapAmount);
  84. zValue = Handles.SnapValue(zAxis.Delta, Handles.RotateSnapAmount);
  85. freeAxisValue = Handles.SnapValue(freeAxis.Delta, Handles.RotateSnapAmount);
  86. }
  87. else
  88. {
  89. xValue = xAxis.Delta;
  90. yValue = yAxis.Delta;
  91. zValue = zAxis.Delta;
  92. freeAxisValue = freeAxis.Delta;
  93. }
  94. Vector3 cameraForward = -(dragStartRotation.Inverse * EditorApplication.SceneViewCamera.SceneObject.Rotation).Forward;
  95. delta = Quaternion.FromEuler(xValue, yValue, zValue);
  96. delta *= Quaternion.FromAxisAngle(cameraForward, freeAxisValue);
  97. }
  98. /// <inheritdoc/>
  99. protected internal override void Draw()
  100. {
  101. HandleDrawing.Layer = 1;
  102. HandleDrawing.Transform = Matrix4.TRS(Position, Rotation, Vector3.One);
  103. float handleSize = Handles.GetHandleSize(EditorApplication.SceneViewCamera, position);
  104. // Draw arcs
  105. if (xAxis.State == HandleSlider.StateType.Active)
  106. HandleDrawing.Color = Color.White;
  107. else if(xAxis.State == HandleSlider.StateType.Hover)
  108. HandleDrawing.Color = Color.BansheeOrange;
  109. else
  110. HandleDrawing.Color = Color.Red;
  111. HandleDrawing.DrawWireArc(Vector3.Zero, Vector3.XAxis, 1.0f, GetXStartAngle(false), new Degree(180.0f), handleSize);
  112. if (yAxis.State == HandleSlider.StateType.Active)
  113. HandleDrawing.Color = Color.White;
  114. else if (yAxis.State == HandleSlider.StateType.Hover)
  115. HandleDrawing.Color = Color.BansheeOrange;
  116. else
  117. HandleDrawing.Color = Color.Green;
  118. HandleDrawing.DrawWireArc(Vector3.Zero, Vector3.YAxis, 1.0f, GetYStartAngle(false), new Degree(180.0f), handleSize);
  119. if (zAxis.State == HandleSlider.StateType.Active)
  120. HandleDrawing.Color = Color.White;
  121. else if (zAxis.State == HandleSlider.StateType.Hover)
  122. HandleDrawing.Color = Color.BansheeOrange;
  123. else
  124. HandleDrawing.Color = Color.Blue;
  125. HandleDrawing.DrawWireArc(Vector3.Zero, Vector3.ZAxis, 1.0f, GetZStartAngle(false), new Degree(180.0f), handleSize);
  126. // Draw "bounds" and free handle
  127. Color gray = new Color(1.0f, 1.0f, 1.0f, 0.3f);
  128. Vector3 cameraNormal = EditorApplication.SceneViewCamera.SceneObject.Rotation.Rotate(Vector3.ZAxis);
  129. HandleDrawing.Transform = Matrix4.TRS(Position, Quaternion.Identity, Vector3.One);
  130. HandleDrawing.Color = gray;
  131. HandleDrawing.DrawWireDisc(cameraNormal * 0.1f, cameraNormal, 1.0f, handleSize);
  132. if (freeAxis.State == HandleSlider.StateType.Active)
  133. HandleDrawing.Color = Color.White;
  134. else if (freeAxis.State == HandleSlider.StateType.Hover)
  135. HandleDrawing.Color = Color.BansheeOrange;
  136. else
  137. HandleDrawing.Color = gray;
  138. HandleDrawing.DrawWireDisc(Vector3.Zero, cameraNormal, 1.2f, handleSize);
  139. // Draw active rotation pie
  140. HandleDrawing.Color = gray;
  141. HandleDrawing.Transform = Matrix4.TRS(Position, EditorApplication.SceneViewCamera.SceneObject.Rotation, Vector3.One);
  142. if (freeAxis.State == HandleSlider.StateType.Active)
  143. HandleDrawing.DrawArc(Vector3.Zero, -Vector3.ZAxis, 1.2f, freeAxis.StartAngle, -freeAxis.Delta, handleSize);
  144. HandleDrawing.Transform = Matrix4.TRS(Position, dragStartRotation, Vector3.One);
  145. if (xAxis.State == HandleSlider.StateType.Active)
  146. HandleDrawing.DrawArc(Vector3.Zero, Vector3.XAxis, 1.0f, xAxis.StartAngle, -xAxis.Delta, handleSize);
  147. else if (yAxis.State == HandleSlider.StateType.Active)
  148. HandleDrawing.DrawArc(Vector3.Zero, Vector3.YAxis, 1.0f, yAxis.StartAngle, -yAxis.Delta, handleSize);
  149. else if (zAxis.State == HandleSlider.StateType.Active)
  150. HandleDrawing.DrawArc(Vector3.Zero, Vector3.ZAxis, 1.0f, zAxis.StartAngle, -zAxis.Delta, handleSize);
  151. }
  152. /// <summary>
  153. /// The rotate handle only displays the 180 degree arc facing the camera and this method returns the angle at which
  154. /// the arc starts for the X axis.
  155. /// </summary>
  156. /// <param name="frozen">Determines should the local handle rotation be taken into account, or should it be frozen
  157. /// to the value when handle drag started. This is useful because we do not want the visible
  158. /// arc to change while the user is in the process of rotating the handle.</param>
  159. /// <returns>Angle at which to display the visible arc for the X axis rotations.</returns>
  160. private Degree GetXStartAngle(bool frozen)
  161. {
  162. Quaternion handleRotation = frozen ? dragStartRotation : Rotation;
  163. Vector3 xStartDir = Vector3.Cross(handleRotation.Inverse.Rotate(EditorApplication.SceneViewCamera.SceneObject.Forward), Vector3.XAxis);
  164. return PointOnCircleToAngle(Vector3.XAxis, xStartDir);
  165. }
  166. /// <summary>
  167. /// The rotate handle only displays the 180 degree arc facing the camera and this method returns the angle at which
  168. /// the arc starts for the Y axis.
  169. /// </summary>
  170. /// <param name="frozen">Determines should the local handle rotation be taken into account, or should it be frozen
  171. /// to the value when handle drag started. This is useful because we do not want the visible
  172. /// arc to change while the user is in the process of rotating the handle.</param>
  173. /// <returns>Angle at which to display the visible arc for the Y axis rotations.</returns>
  174. private Degree GetYStartAngle(bool frozen)
  175. {
  176. Quaternion handleRotation = frozen ? dragStartRotation : Rotation;
  177. Vector3 yStartDir = Vector3.Cross(handleRotation.Inverse.Rotate(EditorApplication.SceneViewCamera.SceneObject.Forward), Vector3.YAxis);
  178. return PointOnCircleToAngle(Vector3.YAxis, yStartDir);
  179. }
  180. /// <summary>
  181. /// The rotate handle only displays the 180 degree arc facing the camera and this method returns the angle at which
  182. /// the arc starts for the Z axis.
  183. /// </summary>
  184. /// <param name="frozen">Determines should the local handle rotation be taken into account, or should it be frozen
  185. /// to the value when handle drag started. This is useful because we do not want the visible
  186. /// arc to change while the user is in the process of rotating the handle.</param>
  187. /// <returns>Angle at which to display the visible arc for the Z axis rotations.</returns>
  188. private Degree GetZStartAngle(bool frozen)
  189. {
  190. Quaternion handleRotation = frozen ? dragStartRotation : Rotation;
  191. Vector3 zStartDir = Vector3.Cross(handleRotation.Inverse.Rotate(EditorApplication.SceneViewCamera.SceneObject.Forward), Vector3.ZAxis);
  192. return PointOnCircleToAngle(Vector3.ZAxis, zStartDir);
  193. }
  194. /// <summary>
  195. /// Converts a point on the circle to an angle on the circle.
  196. /// </summary>
  197. /// <param name="up">Up vector determining the orientation of the circle.</param>
  198. /// <param name="point">Point along a unit circle centered around the origin.</param>
  199. /// <returns>Angle at which the provided point is located on the circle.</returns>
  200. private Degree PointOnCircleToAngle(Vector3 up, Vector3 point)
  201. {
  202. Quaternion rot = Quaternion.FromToRotation(up, Vector3.YAxis);
  203. Matrix4 worldToPlane = Matrix4.TRS(Vector3.Zero, rot, Vector3.One);
  204. point = worldToPlane.MultiplyDirection(point);
  205. return (MathEx.Atan2(-point.z, -point.x) + MathEx.Pi);
  206. }
  207. }
  208. }