MoveHandle.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using BansheeEngine;
  2. namespace BansheeEditor
  3. {
  4. public sealed class MoveHandle : DefaultHandle
  5. {
  6. private const float CONE_HEIGHT = 0.25f;
  7. private const float CONE_RADIUS = 0.175f;
  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. public MoveHandle()
  17. {
  18. xAxis = new HandleSliderLine(this, Vector3.xAxis, 1.0f);
  19. yAxis = new HandleSliderLine(this, Vector3.yAxis, 1.0f);
  20. zAxis = new HandleSliderLine(this, Vector3.zAxis, 1.0f);
  21. }
  22. protected override void PreInput()
  23. {
  24. xAxis.Position = position;
  25. yAxis.Position = position;
  26. zAxis.Position = position;
  27. xAxis.Rotation = rotation;
  28. yAxis.Rotation = rotation;
  29. zAxis.Rotation = rotation;
  30. }
  31. protected override void PostInput()
  32. {
  33. delta = Vector3.zero;
  34. if (Handles.MoveHandleSnapActive)
  35. {
  36. delta += Handles.SnapValue(xAxis.Delta, Handles.MoveSnapAmount) * GetXDir();
  37. delta += Handles.SnapValue(yAxis.Delta, Handles.MoveSnapAmount) * GetYDir();
  38. delta += Handles.SnapValue(zAxis.Delta, Handles.MoveSnapAmount) * GetZDir();
  39. }
  40. else
  41. {
  42. delta += xAxis.Delta * GetXDir();
  43. delta += yAxis.Delta * GetYDir();
  44. delta += zAxis.Delta * GetZDir();
  45. }
  46. }
  47. protected override void Draw()
  48. {
  49. Vector3 center = position;
  50. Vector3 xEnd = center + GetXDir();
  51. Vector3 yEnd = center + GetYDir();
  52. Vector3 zEnd = center + GetZDir();
  53. if (xAxis.State == HandleSlider.StateType.Active)
  54. HandleDrawing.SetColor(Color.white);
  55. else if(xAxis.State == HandleSlider.StateType.Hover)
  56. HandleDrawing.SetColor(Color.red * 0.8f);
  57. else
  58. HandleDrawing.SetColor(Color.red);
  59. float handleSize = Handles.GetHandleSize(EditorApplication.sceneCamera, position);
  60. HandleDrawing.DrawLine(center, xEnd - GetXDir() * CONE_HEIGHT, handleSize);
  61. HandleDrawing.DrawCone(xEnd - GetXDir() * CONE_HEIGHT, GetXDir(), CONE_HEIGHT, CONE_RADIUS, handleSize);
  62. if (yAxis.State == HandleSlider.StateType.Active)
  63. HandleDrawing.SetColor(Color.white);
  64. else if (yAxis.State == HandleSlider.StateType.Hover)
  65. HandleDrawing.SetColor(Color.green * 0.8f);
  66. else
  67. HandleDrawing.SetColor(Color.green);
  68. HandleDrawing.DrawLine(center, yEnd - GetYDir() * CONE_HEIGHT, handleSize);
  69. HandleDrawing.DrawCone(yEnd - GetYDir() * CONE_HEIGHT, GetYDir(), CONE_HEIGHT, CONE_RADIUS, handleSize);
  70. if (zAxis.State == HandleSlider.StateType.Active)
  71. HandleDrawing.SetColor(Color.white);
  72. else if (zAxis.State == HandleSlider.StateType.Hover)
  73. HandleDrawing.SetColor(Color.blue * 0.8f);
  74. else
  75. HandleDrawing.SetColor(Color.blue);
  76. HandleDrawing.DrawLine(center, zEnd - GetZDir() * CONE_HEIGHT, handleSize);
  77. HandleDrawing.DrawCone(zEnd - GetZDir() * CONE_HEIGHT, GetZDir(), CONE_HEIGHT, CONE_RADIUS, handleSize);
  78. }
  79. private Vector3 GetXDir()
  80. {
  81. return rotation.Rotate(Vector3.xAxis);
  82. }
  83. private Vector3 GetYDir()
  84. {
  85. return rotation.Rotate(Vector3.yAxis);
  86. }
  87. private Vector3 GetZDir()
  88. {
  89. return rotation.Rotate(Vector3.zAxis);
  90. }
  91. }
  92. }