MoveHandle.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. 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 MoveHandle()
  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. if (Handles.MoveHandleSnapActive)
  41. {
  42. delta += Handles.SnapValue(xAxis.Delta, Handles.MoveSnapAmount) * GetXDir();
  43. delta += Handles.SnapValue(yAxis.Delta, Handles.MoveSnapAmount) * GetYDir();
  44. delta += Handles.SnapValue(zAxis.Delta, Handles.MoveSnapAmount) * GetZDir();
  45. }
  46. else
  47. {
  48. delta += xAxis.Delta * GetXDir();
  49. delta += yAxis.Delta * GetYDir();
  50. delta += zAxis.Delta * GetZDir();
  51. }
  52. }
  53. protected override void Draw()
  54. {
  55. Vector3 center = position;
  56. Vector3 xEnd = center + GetXDir();
  57. Vector3 yEnd = center + GetYDir();
  58. Vector3 zEnd = center + GetZDir();
  59. if (xAxis.State == HandleSlider.StateType.Active)
  60. HandleDrawing.SetColor(Color.white);
  61. else if(xAxis.State == HandleSlider.StateType.Hover)
  62. HandleDrawing.SetColor(Color.red * 0.8f);
  63. else
  64. HandleDrawing.SetColor(Color.red);
  65. float handleSize = Handles.GetHandleSize(EditorApplication.SceneViewCamera, position);
  66. HandleDrawing.DrawLine(center, xEnd - GetXDir() * CONE_HEIGHT, handleSize);
  67. HandleDrawing.DrawCone(xEnd - GetXDir() * CONE_HEIGHT, GetXDir(), CONE_HEIGHT, CONE_RADIUS, 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.green * 0.8f);
  72. else
  73. HandleDrawing.SetColor(Color.green);
  74. HandleDrawing.DrawLine(center, yEnd - GetYDir() * CONE_HEIGHT, handleSize);
  75. HandleDrawing.DrawCone(yEnd - GetYDir() * CONE_HEIGHT, GetYDir(), CONE_HEIGHT, CONE_RADIUS, handleSize);
  76. if (zAxis.State == HandleSlider.StateType.Active)
  77. HandleDrawing.SetColor(Color.white);
  78. else if (zAxis.State == HandleSlider.StateType.Hover)
  79. HandleDrawing.SetColor(Color.blue * 0.8f);
  80. else
  81. HandleDrawing.SetColor(Color.blue);
  82. HandleDrawing.DrawLine(center, zEnd - GetZDir() * CONE_HEIGHT, handleSize);
  83. HandleDrawing.DrawCone(zEnd - GetZDir() * CONE_HEIGHT, GetZDir(), CONE_HEIGHT, CONE_RADIUS, handleSize);
  84. }
  85. private Vector3 GetXDir()
  86. {
  87. return rotation.Rotate(Vector3.xAxis);
  88. }
  89. private Vector3 GetYDir()
  90. {
  91. return rotation.Rotate(Vector3.yAxis);
  92. }
  93. private Vector3 GetZDir()
  94. {
  95. return rotation.Rotate(Vector3.zAxis);
  96. }
  97. }
  98. }