ScaleHandle.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. public Vector3 Delta
  13. {
  14. get { return delta; }
  15. }
  16. internal override bool IsDragged()
  17. {
  18. return xAxis.State == HandleSlider.StateType.Active ||
  19. yAxis.State == HandleSlider.StateType.Active ||
  20. zAxis.State == HandleSlider.StateType.Active;
  21. }
  22. public ScaleHandle()
  23. {
  24. xAxis = new HandleSliderLine(this, Vector3.xAxis, 1.0f);
  25. yAxis = new HandleSliderLine(this, Vector3.yAxis, 1.0f);
  26. zAxis = new HandleSliderLine(this, Vector3.zAxis, 1.0f);
  27. }
  28. protected override void PreInput()
  29. {
  30. xAxis.Position = position;
  31. yAxis.Position = position;
  32. zAxis.Position = position;
  33. xAxis.Rotation = rotation;
  34. yAxis.Rotation = rotation;
  35. zAxis.Rotation = rotation;
  36. }
  37. protected override void PostInput()
  38. {
  39. delta = Vector3.zero;
  40. delta += xAxis.Delta * GetXDir();
  41. delta += yAxis.Delta * GetYDir();
  42. delta += zAxis.Delta * GetZDir();
  43. }
  44. protected override void Draw()
  45. {
  46. HandleDrawing.SetTransform(Matrix4.TRS(Position, Rotation, Vector3.one));
  47. float handleSize = Handles.GetHandleSize(EditorApplication.SceneViewCamera, position);
  48. // Draw 1D sliders
  49. Vector3 smallCubeExtents = new Vector3(SMALL_CUBE_SIZE*0.5f, SMALL_CUBE_SIZE*0.5f, SMALL_CUBE_SIZE*0.5f);
  50. Color axisHover = new Color(0.8f, 0.8f, 0.8f, 1.0f);
  51. if (xAxis.State == HandleSlider.StateType.Active)
  52. HandleDrawing.SetColor(Color.white);
  53. else if (xAxis.State == HandleSlider.StateType.Hover)
  54. HandleDrawing.SetColor(Color.red * axisHover);
  55. else
  56. HandleDrawing.SetColor(Color.red);
  57. Vector3 xCubeOffset = Vector3.xAxis * SMALL_CUBE_SIZE * 0.5f;
  58. Vector3 xCubeStart = Vector3.xAxis - xCubeOffset;
  59. HandleDrawing.DrawLine(Vector3.zero, xCubeStart, handleSize);
  60. HandleDrawing.DrawCube(xCubeStart + xCubeOffset, smallCubeExtents, handleSize);
  61. if (yAxis.State == HandleSlider.StateType.Active)
  62. HandleDrawing.SetColor(Color.white);
  63. else if (yAxis.State == HandleSlider.StateType.Hover)
  64. HandleDrawing.SetColor(Color.green * axisHover);
  65. else
  66. HandleDrawing.SetColor(Color.green);
  67. Vector3 yCubeOffset = Vector3.yAxis * SMALL_CUBE_SIZE * 0.5f;
  68. Vector3 yCubeStart = Vector3.yAxis - yCubeOffset;
  69. HandleDrawing.DrawLine(Vector3.zero, yCubeStart, handleSize);
  70. HandleDrawing.DrawCube(yCubeStart + yCubeOffset, smallCubeExtents, handleSize);
  71. if (zAxis.State == HandleSlider.StateType.Active)
  72. HandleDrawing.SetColor(Color.white);
  73. else if (zAxis.State == HandleSlider.StateType.Hover)
  74. HandleDrawing.SetColor(Color.blue * axisHover);
  75. else
  76. HandleDrawing.SetColor(Color.blue);
  77. Vector3 zCubeOffset = Vector3.zAxis * SMALL_CUBE_SIZE * 0.5f;
  78. Vector3 zCubeStart = Vector3.zAxis - zCubeOffset;
  79. HandleDrawing.DrawLine(Vector3.zero, zCubeStart, handleSize);
  80. HandleDrawing.DrawCube(zCubeStart + zCubeOffset, smallCubeExtents, handleSize);
  81. // TODO - Draw free scale handle
  82. }
  83. private Vector3 GetXDir()
  84. {
  85. return rotation.Rotate(Vector3.xAxis);
  86. }
  87. private Vector3 GetYDir()
  88. {
  89. return rotation.Rotate(Vector3.yAxis);
  90. }
  91. private Vector3 GetZDir()
  92. {
  93. return rotation.Rotate(Vector3.zAxis);
  94. }
  95. }
  96. }