ScaleHandle.cs 7.5 KB

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