ScaleHandle.cs 7.5 KB

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