ScaleHandle.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.Transform = Matrix4.TRS(Position, Rotation, Vector3.One);
  69. float handleSize = Handles.GetHandleSize(EditorApplication.SceneViewCamera, position);
  70. // Draw 1D sliders
  71. Vector3 smallCubeExtents = new Vector3(SMALL_CUBE_SIZE*0.5f, SMALL_CUBE_SIZE*0.5f, SMALL_CUBE_SIZE*0.5f);
  72. if (xAxis.State == HandleSlider.StateType.Active)
  73. HandleDrawing.Color = Color.White;
  74. else if (xAxis.State == HandleSlider.StateType.Hover)
  75. HandleDrawing.Color = Color.BansheeOrange;
  76. else
  77. HandleDrawing.Color = Color.Red;
  78. Vector3 xCubeOffset = Vector3.XAxis * SMALL_CUBE_SIZE * 0.5f;
  79. Vector3 xCubeStart = Vector3.XAxis - xCubeOffset;
  80. HandleDrawing.DrawLine(Vector3.Zero, xCubeStart, handleSize);
  81. HandleDrawing.DrawCube(xCubeStart + xCubeOffset, smallCubeExtents, handleSize);
  82. if (yAxis.State == HandleSlider.StateType.Active)
  83. HandleDrawing.Color = Color.White;
  84. else if (yAxis.State == HandleSlider.StateType.Hover)
  85. HandleDrawing.Color = Color.BansheeOrange;
  86. else
  87. HandleDrawing.Color = Color.Green;
  88. Vector3 yCubeOffset = Vector3.YAxis * SMALL_CUBE_SIZE * 0.5f;
  89. Vector3 yCubeStart = Vector3.YAxis - yCubeOffset;
  90. HandleDrawing.DrawLine(Vector3.Zero, yCubeStart, handleSize);
  91. HandleDrawing.DrawCube(yCubeStart + yCubeOffset, smallCubeExtents, handleSize);
  92. if (zAxis.State == HandleSlider.StateType.Active)
  93. HandleDrawing.Color = Color.White;
  94. else if (zAxis.State == HandleSlider.StateType.Hover)
  95. HandleDrawing.Color = Color.BansheeOrange;
  96. else
  97. HandleDrawing.Color = Color.Blue;
  98. Vector3 zCubeOffset = Vector3.ZAxis * SMALL_CUBE_SIZE * 0.5f;
  99. Vector3 zCubeStart = Vector3.ZAxis - zCubeOffset;
  100. HandleDrawing.DrawLine(Vector3.Zero, zCubeStart, handleSize);
  101. HandleDrawing.DrawCube(zCubeStart + zCubeOffset, smallCubeExtents, handleSize);
  102. // Draw free scale handle
  103. if (freeAxis.State == HandleSlider.StateType.Active)
  104. HandleDrawing.Color = Color.White;
  105. else if (freeAxis.State == HandleSlider.StateType.Hover)
  106. HandleDrawing.Color = Color.BansheeOrange;
  107. else
  108. HandleDrawing.Color = Color.White;
  109. //// Rotate it so it always faces the camera, and move it forward a bit to always render in front
  110. Vector3 bottomLeft = -Vector3.XAxis * 0.2f - Vector3.YAxis * 0.2f;
  111. Vector3 topLeft = -Vector3.XAxis * 0.2f + Vector3.YAxis * 0.2f;
  112. Vector3 topRight = Vector3.XAxis * 0.2f + Vector3.YAxis * 0.2f;
  113. Vector3 bottomRight = Vector3.XAxis * 0.2f - Vector3.YAxis * 0.2f;
  114. Vector3 offset = Vector3.ZAxis*0.1f;
  115. Quaternion cameraRot = EditorApplication.SceneViewCamera.SceneObject.Rotation;
  116. bottomLeft = cameraRot.Rotate(bottomLeft + offset);
  117. topLeft = cameraRot.Rotate(topLeft + offset);
  118. topRight = cameraRot.Rotate(topRight + offset);
  119. bottomRight = cameraRot.Rotate(bottomRight + offset);
  120. HandleDrawing.DrawLine(bottomLeft, bottomRight, handleSize);
  121. HandleDrawing.DrawLine(bottomLeft, topLeft, handleSize);
  122. HandleDrawing.DrawLine(topLeft, topRight, handleSize);
  123. HandleDrawing.DrawLine(bottomRight, topRight, handleSize);
  124. }
  125. /// <summary>
  126. /// Returns the direction of the handle's x axis in world space.
  127. /// </summary>
  128. /// <returns>Direction of the handle's x axis in world space</returns>
  129. private Vector3 GetXDir()
  130. {
  131. return rotation.Rotate(Vector3.XAxis);
  132. }
  133. /// <summary>
  134. /// Returns the direction of the handle's y axis in world space.
  135. /// </summary>
  136. /// <returns>Direction of the handle's y axis in world space</returns>
  137. private Vector3 GetYDir()
  138. {
  139. return rotation.Rotate(Vector3.YAxis);
  140. }
  141. /// <summary>
  142. /// Returns the direction of the handle's z axis in world space.
  143. /// </summary>
  144. /// <returns>Direction of the handle's z axis in world space</returns>
  145. private Vector3 GetZDir()
  146. {
  147. return rotation.Rotate(Vector3.ZAxis);
  148. }
  149. }
  150. }