MoveHandle.cs 9.8 KB

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