ScaleHandle.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using BansheeEngine;
  2. namespace BansheeEditor
  3. {
  4. /// <summary>
  5. /// Handle that allows an object to be scaled along the three primary axes, as well as a free axis currently
  6. /// facing the camera.
  7. /// </summary>
  8. public sealed class ScaleHandle : DefaultHandle
  9. {
  10. private const float SMALL_CUBE_SIZE = 0.175f;
  11. private const float CENTER_CUBE_SIZE = 0.33f;
  12. private Vector3 delta;
  13. private HandleSliderLine xAxis;
  14. private HandleSliderLine yAxis;
  15. private HandleSliderLine zAxis;
  16. private HandleSliderPlane freeAxis;
  17. /// <summary>
  18. /// Returns the amount of scaling applied since the last frame. Only valid while the handle is being dragged.
  19. /// </summary>
  20. public Vector3 Delta
  21. {
  22. get { return delta; }
  23. }
  24. /// <inheritdoc/>
  25. internal override bool IsDragged()
  26. {
  27. return xAxis.State == HandleSlider.StateType.Active ||
  28. yAxis.State == HandleSlider.StateType.Active ||
  29. zAxis.State == HandleSlider.StateType.Active ||
  30. freeAxis.State == HandleSlider.StateType.Active;
  31. }
  32. /// <summary>
  33. /// Creates a new scale handle.
  34. /// </summary>
  35. public ScaleHandle()
  36. {
  37. xAxis = new HandleSliderLine(this, Vector3.XAxis, 1.0f);
  38. yAxis = new HandleSliderLine(this, Vector3.YAxis, 1.0f);
  39. zAxis = new HandleSliderLine(this, Vector3.ZAxis, 1.0f);
  40. freeAxis = new HandleSliderPlane(this, Vector3.XAxis, Vector3.YAxis, 0.4f);
  41. }
  42. /// <inheritdoc/>
  43. protected internal override void PreInput()
  44. {
  45. xAxis.Position = position;
  46. yAxis.Position = position;
  47. zAxis.Position = position;
  48. xAxis.Rotation = rotation;
  49. yAxis.Rotation = rotation;
  50. zAxis.Rotation = rotation;
  51. float handleSize = Handles.GetHandleSize(EditorApplication.SceneViewCamera, position);
  52. Vector3 freeAxisOffset = (Vector3.XAxis * -0.2f + Vector3.YAxis * -0.2f) * handleSize;
  53. freeAxis.Rotation = EditorApplication.SceneViewCamera.SceneObject.Rotation;
  54. freeAxis.Position = position + freeAxis.Rotation.Rotate(freeAxisOffset);
  55. }
  56. /// <inheritdoc/>
  57. protected internal override void PostInput()
  58. {
  59. delta = Vector3.Zero;
  60. delta += xAxis.Delta * GetXDir() * 0.1f;
  61. delta += yAxis.Delta * GetYDir() * 0.1f;
  62. delta += zAxis.Delta * GetZDir() * 0.1f;
  63. delta += (freeAxis.Delta.x + freeAxis.Delta.y) * Vector3.One * 0.1f;
  64. }
  65. /// <inheritdoc/>
  66. protected internal override void Draw()
  67. {
  68. HandleDrawing.Layer = 1;
  69. HandleDrawing.Transform = Matrix4.TRS(Position, Rotation, Vector3.One);
  70. float handleSize = Handles.GetHandleSize(EditorApplication.SceneViewCamera, position);
  71. // Draw 1D sliders
  72. Vector3 smallCubeExtents = new Vector3(SMALL_CUBE_SIZE*0.5f, SMALL_CUBE_SIZE*0.5f, SMALL_CUBE_SIZE*0.5f);
  73. if (xAxis.State == HandleSlider.StateType.Active)
  74. HandleDrawing.Color = Color.White;
  75. else if (xAxis.State == HandleSlider.StateType.Hover)
  76. HandleDrawing.Color = Color.BansheeOrange;
  77. else
  78. HandleDrawing.Color = Color.Red;
  79. Vector3 xCubeOffset = Vector3.XAxis * SMALL_CUBE_SIZE * 0.5f;
  80. Vector3 xCubeStart = Vector3.XAxis - xCubeOffset;
  81. HandleDrawing.DrawLine(Vector3.Zero, xCubeStart, handleSize);
  82. HandleDrawing.DrawCube(xCubeStart + xCubeOffset, smallCubeExtents, handleSize);
  83. if (yAxis.State == HandleSlider.StateType.Active)
  84. HandleDrawing.Color = Color.White;
  85. else if (yAxis.State == HandleSlider.StateType.Hover)
  86. HandleDrawing.Color = Color.BansheeOrange;
  87. else
  88. HandleDrawing.Color = Color.Green;
  89. Vector3 yCubeOffset = Vector3.YAxis * SMALL_CUBE_SIZE * 0.5f;
  90. Vector3 yCubeStart = Vector3.YAxis - yCubeOffset;
  91. HandleDrawing.DrawLine(Vector3.Zero, yCubeStart, handleSize);
  92. HandleDrawing.DrawCube(yCubeStart + yCubeOffset, smallCubeExtents, handleSize);
  93. if (zAxis.State == HandleSlider.StateType.Active)
  94. HandleDrawing.Color = Color.White;
  95. else if (zAxis.State == HandleSlider.StateType.Hover)
  96. HandleDrawing.Color = Color.BansheeOrange;
  97. else
  98. HandleDrawing.Color = Color.Blue;
  99. Vector3 zCubeOffset = Vector3.ZAxis * SMALL_CUBE_SIZE * 0.5f;
  100. Vector3 zCubeStart = Vector3.ZAxis - zCubeOffset;
  101. HandleDrawing.DrawLine(Vector3.Zero, zCubeStart, handleSize);
  102. HandleDrawing.DrawCube(zCubeStart + zCubeOffset, smallCubeExtents, handleSize);
  103. // Draw free scale handle
  104. if (freeAxis.State == HandleSlider.StateType.Active)
  105. HandleDrawing.Color = Color.White;
  106. else if (freeAxis.State == HandleSlider.StateType.Hover)
  107. HandleDrawing.Color = Color.BansheeOrange;
  108. else
  109. HandleDrawing.Color = Color.White;
  110. //// Rotate it so it always faces the camera, and move it forward a bit to always render in front
  111. Vector3 bottomLeft = -Vector3.XAxis * 0.2f - Vector3.YAxis * 0.2f;
  112. Vector3 topLeft = -Vector3.XAxis * 0.2f + Vector3.YAxis * 0.2f;
  113. Vector3 topRight = Vector3.XAxis * 0.2f + Vector3.YAxis * 0.2f;
  114. Vector3 bottomRight = Vector3.XAxis * 0.2f - Vector3.YAxis * 0.2f;
  115. Vector3 offset = Vector3.ZAxis*0.1f;
  116. Quaternion cameraRot = EditorApplication.SceneViewCamera.SceneObject.Rotation;
  117. bottomLeft = cameraRot.Rotate(bottomLeft + offset);
  118. topLeft = cameraRot.Rotate(topLeft + offset);
  119. topRight = cameraRot.Rotate(topRight + offset);
  120. bottomRight = cameraRot.Rotate(bottomRight + offset);
  121. HandleDrawing.DrawLine(bottomLeft, bottomRight, handleSize);
  122. HandleDrawing.DrawLine(bottomLeft, topLeft, handleSize);
  123. HandleDrawing.DrawLine(topLeft, topRight, handleSize);
  124. HandleDrawing.DrawLine(bottomRight, topRight, handleSize);
  125. }
  126. /// <summary>
  127. /// Returns the direction of the handle's x axis in world space.
  128. /// </summary>
  129. /// <returns>Direction of the handle's x axis in world space</returns>
  130. private Vector3 GetXDir()
  131. {
  132. return rotation.Rotate(Vector3.XAxis);
  133. }
  134. /// <summary>
  135. /// Returns the direction of the handle's y axis in world space.
  136. /// </summary>
  137. /// <returns>Direction of the handle's y axis in world space</returns>
  138. private Vector3 GetYDir()
  139. {
  140. return rotation.Rotate(Vector3.YAxis);
  141. }
  142. /// <summary>
  143. /// Returns the direction of the handle's z axis in world space.
  144. /// </summary>
  145. /// <returns>Direction of the handle's z axis in world space</returns>
  146. private Vector3 GetZDir()
  147. {
  148. return rotation.Rotate(Vector3.ZAxis);
  149. }
  150. }
  151. }