ScaleHandle.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using BansheeEngine;
  2. namespace BansheeEditor
  3. {
  4. public sealed class ScaleHandle : DefaultHandle
  5. {
  6. private const float SMALL_CUBE_SIZE = 0.175f;
  7. private const float CENTER_CUBE_SIZE = 0.33f;
  8. private Vector3 delta;
  9. private HandleSliderLine xAxis;
  10. private HandleSliderLine yAxis;
  11. private HandleSliderLine zAxis;
  12. private HandleSliderPlane freeAxis;
  13. public Vector3 Delta
  14. {
  15. get { return delta; }
  16. }
  17. internal override bool IsDragged()
  18. {
  19. return xAxis.State == HandleSlider.StateType.Active ||
  20. yAxis.State == HandleSlider.StateType.Active ||
  21. zAxis.State == HandleSlider.StateType.Active ||
  22. freeAxis.State == HandleSlider.StateType.Active;
  23. }
  24. public ScaleHandle()
  25. {
  26. xAxis = new HandleSliderLine(this, Vector3.xAxis, 1.0f);
  27. yAxis = new HandleSliderLine(this, Vector3.yAxis, 1.0f);
  28. zAxis = new HandleSliderLine(this, Vector3.zAxis, 1.0f);
  29. freeAxis = new HandleSliderPlane(this, Vector3.xAxis, Vector3.yAxis, 0.6f);
  30. }
  31. protected override void PreInput()
  32. {
  33. xAxis.Position = position;
  34. yAxis.Position = position;
  35. zAxis.Position = position;
  36. xAxis.Rotation = rotation;
  37. yAxis.Rotation = rotation;
  38. zAxis.Rotation = rotation;
  39. float handleSize = Handles.GetHandleSize(EditorApplication.SceneViewCamera, position);
  40. Vector3 freeAxisOffset = (Vector3.xAxis * -0.3f + Vector3.yAxis * -0.3f) * handleSize;
  41. freeAxis.Rotation = EditorApplication.SceneViewCamera.SceneObject.Rotation;
  42. freeAxis.Position = position + freeAxis.Rotation.Rotate(freeAxisOffset);
  43. }
  44. protected override void PostInput()
  45. {
  46. delta = Vector3.zero;
  47. delta += xAxis.Delta * GetXDir() * 0.01f;
  48. delta += yAxis.Delta * GetYDir() * 0.01f;
  49. delta += zAxis.Delta * GetZDir() * 0.01f;
  50. delta += (freeAxis.Delta.x + freeAxis.Delta.y) * Vector3.one * 0.01f;
  51. }
  52. protected override void Draw()
  53. {
  54. HandleDrawing.SetTransform(Matrix4.TRS(Position, Rotation, Vector3.one));
  55. float handleSize = Handles.GetHandleSize(EditorApplication.SceneViewCamera, position);
  56. // Draw 1D sliders
  57. Vector3 smallCubeExtents = new Vector3(SMALL_CUBE_SIZE*0.5f, SMALL_CUBE_SIZE*0.5f, SMALL_CUBE_SIZE*0.5f);
  58. if (xAxis.State == HandleSlider.StateType.Active)
  59. HandleDrawing.SetColor(Color.White);
  60. else if (xAxis.State == HandleSlider.StateType.Hover)
  61. HandleDrawing.SetColor(Color.BansheeOrange);
  62. else
  63. HandleDrawing.SetColor(Color.Red);
  64. Vector3 xCubeOffset = Vector3.xAxis * SMALL_CUBE_SIZE * 0.5f;
  65. Vector3 xCubeStart = Vector3.xAxis - xCubeOffset;
  66. HandleDrawing.DrawLine(Vector3.zero, xCubeStart, handleSize);
  67. HandleDrawing.DrawCube(xCubeStart + xCubeOffset, smallCubeExtents, handleSize);
  68. if (yAxis.State == HandleSlider.StateType.Active)
  69. HandleDrawing.SetColor(Color.White);
  70. else if (yAxis.State == HandleSlider.StateType.Hover)
  71. HandleDrawing.SetColor(Color.BansheeOrange);
  72. else
  73. HandleDrawing.SetColor(Color.Green);
  74. Vector3 yCubeOffset = Vector3.yAxis * SMALL_CUBE_SIZE * 0.5f;
  75. Vector3 yCubeStart = Vector3.yAxis - yCubeOffset;
  76. HandleDrawing.DrawLine(Vector3.zero, yCubeStart, handleSize);
  77. HandleDrawing.DrawCube(yCubeStart + yCubeOffset, smallCubeExtents, handleSize);
  78. if (zAxis.State == HandleSlider.StateType.Active)
  79. HandleDrawing.SetColor(Color.White);
  80. else if (zAxis.State == HandleSlider.StateType.Hover)
  81. HandleDrawing.SetColor(Color.BansheeOrange);
  82. else
  83. HandleDrawing.SetColor(Color.Blue);
  84. Vector3 zCubeOffset = Vector3.zAxis * SMALL_CUBE_SIZE * 0.5f;
  85. Vector3 zCubeStart = Vector3.zAxis - zCubeOffset;
  86. HandleDrawing.DrawLine(Vector3.zero, zCubeStart, handleSize);
  87. HandleDrawing.DrawCube(zCubeStart + zCubeOffset, smallCubeExtents, handleSize);
  88. // Draw free scale handle
  89. if (freeAxis.State == HandleSlider.StateType.Active)
  90. HandleDrawing.SetColor(Color.White);
  91. else if (freeAxis.State == HandleSlider.StateType.Hover)
  92. HandleDrawing.SetColor(Color.BansheeOrange);
  93. else
  94. HandleDrawing.SetColor(Color.White);
  95. //// Rotate it so it always faces the camera, and move it forward a bit to always render in front
  96. Vector3 bottomLeft = -Vector3.xAxis * 0.3f - Vector3.yAxis * 0.3f;
  97. Vector3 topLeft = -Vector3.xAxis * 0.3f + Vector3.yAxis * 0.3f;
  98. Vector3 topRight = Vector3.xAxis * 0.3f + Vector3.yAxis * 0.3f;
  99. Vector3 bottomRight = Vector3.xAxis * 0.3f - Vector3.yAxis * 0.3f;
  100. Vector3 offset = Vector3.zAxis*0.1f;
  101. Quaternion cameraRot = EditorApplication.SceneViewCamera.SceneObject.Rotation;
  102. bottomLeft = cameraRot.Rotate(bottomLeft + offset);
  103. topLeft = cameraRot.Rotate(topLeft + offset);
  104. topRight = cameraRot.Rotate(topRight + offset);
  105. bottomRight = cameraRot.Rotate(bottomRight + offset);
  106. HandleDrawing.DrawLine(bottomLeft, bottomRight, handleSize);
  107. HandleDrawing.DrawLine(bottomLeft, topLeft, handleSize);
  108. HandleDrawing.DrawLine(topLeft, topRight, handleSize);
  109. HandleDrawing.DrawLine(bottomRight, topRight, handleSize);
  110. }
  111. private Vector3 GetXDir()
  112. {
  113. return rotation.Rotate(Vector3.xAxis);
  114. }
  115. private Vector3 GetYDir()
  116. {
  117. return rotation.Rotate(Vector3.yAxis);
  118. }
  119. private Vector3 GetZDir()
  120. {
  121. return rotation.Rotate(Vector3.zAxis);
  122. }
  123. }
  124. }