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