MoveHandle.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using bs;
  4. namespace bs.Editor
  5. {
  6. /** @addtogroup Handles
  7. * @{
  8. */
  9. /// <summary>
  10. /// Handle that allows an object to be translated along the three primary axes.
  11. /// </summary>
  12. public sealed class MoveHandle : DefaultHandle
  13. {
  14. private const float CONE_HEIGHT = 0.25f;
  15. private const float CONE_RADIUS = 0.175f;
  16. private Vector3 delta;
  17. private HandleSliderLine xAxis;
  18. private HandleSliderLine yAxis;
  19. private HandleSliderLine zAxis;
  20. private HandleSliderPlane xyPlane;
  21. private HandleSliderPlane yzPlane;
  22. private HandleSliderPlane zxPlane;
  23. /// <summary>
  24. /// Returns the amount of translation since last frame. Only valid while the handle is being dragged.
  25. /// </summary>
  26. public Vector3 Delta
  27. {
  28. get { return delta; }
  29. }
  30. /// <inheritdoc/>
  31. internal override bool IsDragged()
  32. {
  33. return xAxis.State == HandleSlider.StateType.Active ||
  34. yAxis.State == HandleSlider.StateType.Active ||
  35. zAxis.State == HandleSlider.StateType.Active ||
  36. xyPlane.State == HandleSlider.StateType.Active ||
  37. yzPlane.State == HandleSlider.StateType.Active ||
  38. zxPlane.State == HandleSlider.StateType.Active;
  39. }
  40. /// <summary>
  41. /// Creates a new move handle.
  42. /// </summary>
  43. public MoveHandle()
  44. {
  45. xAxis = new HandleSliderLine(this, Vector3.XAxis, 1.0f);
  46. yAxis = new HandleSliderLine(this, Vector3.YAxis, 1.0f);
  47. zAxis = new HandleSliderLine(this, Vector3.ZAxis, 1.0f);
  48. xyPlane = new HandleSliderPlane(this, Vector3.XAxis, Vector3.YAxis, 0.3f);
  49. yzPlane = new HandleSliderPlane(this, Vector3.YAxis, Vector3.ZAxis, 0.3f);
  50. zxPlane = new HandleSliderPlane(this, Vector3.ZAxis, Vector3.XAxis, 0.3f);
  51. }
  52. /// <inheritdoc/>
  53. protected internal override void PreInput()
  54. {
  55. xAxis.Position = position;
  56. yAxis.Position = position;
  57. zAxis.Position = position;
  58. xyPlane.Position = position;
  59. yzPlane.Position = position;
  60. zxPlane.Position = position;
  61. xAxis.Rotation = rotation;
  62. yAxis.Rotation = rotation;
  63. zAxis.Rotation = rotation;
  64. xyPlane.Rotation = rotation;
  65. yzPlane.Rotation = rotation;
  66. zxPlane.Rotation = rotation;
  67. }
  68. /// <inheritdoc/>
  69. protected internal override void PostInput()
  70. {
  71. delta = Vector3.Zero;
  72. if (Handles.MoveHandleSnapActive)
  73. {
  74. delta += Handles.SnapValue(xAxis.Delta, Handles.MoveSnapAmount) * GetXDir();
  75. delta += Handles.SnapValue(yAxis.Delta, Handles.MoveSnapAmount) * GetYDir();
  76. delta += Handles.SnapValue(zAxis.Delta, Handles.MoveSnapAmount) * GetZDir();
  77. delta += Handles.SnapValue(xyPlane.Delta.x, Handles.MoveSnapAmount) * GetXDir();
  78. delta += Handles.SnapValue(xyPlane.Delta.y, Handles.MoveSnapAmount) * GetYDir();
  79. delta += Handles.SnapValue(yzPlane.Delta.x, Handles.MoveSnapAmount) * GetYDir();
  80. delta += Handles.SnapValue(yzPlane.Delta.y, Handles.MoveSnapAmount) * GetZDir();
  81. delta += Handles.SnapValue(zxPlane.Delta.x, Handles.MoveSnapAmount) * GetZDir();
  82. delta += Handles.SnapValue(zxPlane.Delta.y, Handles.MoveSnapAmount) * GetXDir();
  83. }
  84. else
  85. {
  86. delta += xAxis.Delta * GetXDir();
  87. delta += yAxis.Delta * GetYDir();
  88. delta += zAxis.Delta * GetZDir();
  89. delta += xyPlane.Delta.x * GetXDir();
  90. delta += xyPlane.Delta.y * GetYDir();
  91. delta += yzPlane.Delta.x * GetYDir();
  92. delta += yzPlane.Delta.y * GetZDir();
  93. delta += zxPlane.Delta.x * GetZDir();
  94. delta += zxPlane.Delta.y * GetXDir();
  95. }
  96. }
  97. /// <inheritdoc/>
  98. protected internal override void Draw()
  99. {
  100. HandleDrawing.Layer = 1;
  101. HandleDrawing.Transform = Matrix4.TRS(Position, Rotation, Vector3.One);
  102. float handleSize = Handles.GetHandleSize(EditorApplication.SceneViewCamera, position);
  103. // Draw 1D arrows
  104. if (xAxis.State == HandleSlider.StateType.Active)
  105. HandleDrawing.Color = Color.White;
  106. else if(xAxis.State == HandleSlider.StateType.Hover)
  107. HandleDrawing.Color = Color.BansheeOrange;
  108. else
  109. HandleDrawing.Color = Color.Red;
  110. Vector3 xConeStart = Vector3.XAxis*(1.0f - CONE_HEIGHT);
  111. HandleDrawing.DrawLine(Vector3.Zero, xConeStart, handleSize);
  112. HandleDrawing.DrawCone(xConeStart, Vector3.XAxis, CONE_HEIGHT, CONE_RADIUS, handleSize);
  113. if (yAxis.State == HandleSlider.StateType.Active)
  114. HandleDrawing.Color = Color.White;
  115. else if (yAxis.State == HandleSlider.StateType.Hover)
  116. HandleDrawing.Color = Color.BansheeOrange;
  117. else
  118. HandleDrawing.Color = Color.Green;
  119. Vector3 yConeStart = Vector3.YAxis * (1.0f - CONE_HEIGHT);
  120. HandleDrawing.DrawLine(Vector3.Zero, yConeStart, handleSize);
  121. HandleDrawing.DrawCone(yConeStart, Vector3.YAxis, CONE_HEIGHT, CONE_RADIUS, handleSize);
  122. if (zAxis.State == HandleSlider.StateType.Active)
  123. HandleDrawing.Color = Color.White;
  124. else if (zAxis.State == HandleSlider.StateType.Hover)
  125. HandleDrawing.Color = Color.BansheeOrange;
  126. else
  127. HandleDrawing.Color = Color.Blue;
  128. Vector3 zConeStart = Vector3.ZAxis * (1.0f - CONE_HEIGHT);
  129. HandleDrawing.DrawLine(Vector3.Zero, zConeStart, handleSize);
  130. HandleDrawing.DrawCone(zConeStart, Vector3.ZAxis, CONE_HEIGHT, CONE_RADIUS, handleSize);
  131. // Draw 2D planes
  132. Color planeNormal = new Color(1.0f, 1.0f, 1.0f, 0.2f);
  133. Color planeHover = new Color(1.0f, 1.0f, 1.0f, 0.4f);
  134. Color planeActive = new Color(1.0f, 1.0f, 1.0f, 0.6f);
  135. Vector3 planeXOffset = Vector3.XAxis * 0.3f;
  136. Vector3 planeYOffset = Vector3.YAxis * 0.3f;
  137. Vector3 planeZOffset = Vector3.ZAxis * 0.3f;
  138. //// XY plane
  139. HandleDrawing.Color = Color.Blue;
  140. HandleDrawing.DrawLine(planeXOffset, planeXOffset + planeYOffset, handleSize);
  141. HandleDrawing.DrawLine(planeYOffset, planeYOffset + planeXOffset, handleSize);
  142. if (xyPlane.State == HandleSlider.StateType.Active)
  143. HandleDrawing.Color = Color.Blue * planeActive;
  144. else if (xyPlane.State == HandleSlider.StateType.Hover)
  145. HandleDrawing.Color = Color.Blue * planeHover;
  146. else
  147. HandleDrawing.Color = Color.Blue * planeNormal;
  148. Rect3 xyPlaneArea = new Rect3(
  149. (planeXOffset + planeYOffset) * 0.5f,
  150. new Vector3[] { Vector3.XAxis, Vector3.YAxis},
  151. new float[] { 0.15f, 0.15f});
  152. HandleDrawing.DrawRect(xyPlaneArea, handleSize);
  153. //// YZ plane
  154. HandleDrawing.Color = Color.Red;
  155. HandleDrawing.DrawLine(planeYOffset, planeYOffset + planeZOffset, handleSize);
  156. HandleDrawing.DrawLine(planeZOffset, planeZOffset + planeYOffset, handleSize);
  157. if (yzPlane.State == HandleSlider.StateType.Active)
  158. HandleDrawing.Color = Color.Red * planeActive;
  159. else if (yzPlane.State == HandleSlider.StateType.Hover)
  160. HandleDrawing.Color = Color.Red * planeHover;
  161. else
  162. HandleDrawing.Color = Color.Red * planeNormal;
  163. Rect3 yzPlaneArea = new Rect3(
  164. (planeYOffset + planeZOffset) * 0.5f,
  165. new Vector3[] { Vector3.YAxis, Vector3.ZAxis },
  166. new float[] { 0.15f, 0.15f });
  167. HandleDrawing.DrawRect(yzPlaneArea, handleSize);
  168. //// ZX plane
  169. HandleDrawing.Color = Color.Green;
  170. HandleDrawing.DrawLine(planeZOffset, planeZOffset + planeXOffset, handleSize);
  171. HandleDrawing.DrawLine(planeXOffset, planeXOffset + planeZOffset, handleSize);
  172. if (zxPlane.State == HandleSlider.StateType.Active)
  173. HandleDrawing.Color = Color.Green * planeActive;
  174. else if (zxPlane.State == HandleSlider.StateType.Hover)
  175. HandleDrawing.Color = Color.Green * planeHover;
  176. else
  177. HandleDrawing.Color = Color.Green * planeNormal;
  178. Rect3 zxPlaneArea = new Rect3(
  179. (planeZOffset + planeXOffset) * 0.5f,
  180. new Vector3[] { Vector3.ZAxis, Vector3.XAxis },
  181. new float[] { 0.15f, 0.15f });
  182. HandleDrawing.DrawRect(zxPlaneArea, handleSize);
  183. }
  184. /// <summary>
  185. /// Returns the direction of the handle's x axis in world space.
  186. /// </summary>
  187. /// <returns>Direction of the handle's x axis in world space</returns>
  188. private Vector3 GetXDir()
  189. {
  190. return rotation.Rotate(Vector3.XAxis);
  191. }
  192. /// <summary>
  193. /// Returns the direction of the handle's y axis in world space.
  194. /// </summary>
  195. /// <returns>Direction of the handle's y axis in world space</returns>
  196. private Vector3 GetYDir()
  197. {
  198. return rotation.Rotate(Vector3.YAxis);
  199. }
  200. /// <summary>
  201. /// Returns the direction of the handle's z axis in world space.
  202. /// </summary>
  203. /// <returns>Direction of the handle's z axis in world space</returns>
  204. private Vector3 GetZDir()
  205. {
  206. return rotation.Rotate(Vector3.ZAxis);
  207. }
  208. }
  209. /** @} */
  210. }