MoveHandle.cs 4.1 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. HandleDrawing.SetTransform(Matrix4.TRS(Position, Rotation, Vector3.one));
  56. if (xAxis.State == HandleSlider.StateType.Active)
  57. HandleDrawing.SetColor(Color.white);
  58. else if(xAxis.State == HandleSlider.StateType.Hover)
  59. HandleDrawing.SetColor(Color.red * 0.8f);
  60. else
  61. HandleDrawing.SetColor(Color.red);
  62. float handleSize = Handles.GetHandleSize(EditorApplication.SceneViewCamera, position);
  63. Vector3 xConeStart = Vector3.xAxis*(1.0f - CONE_HEIGHT);
  64. HandleDrawing.DrawLine(Vector3.zero, xConeStart, handleSize);
  65. HandleDrawing.DrawCone(xConeStart, Vector3.xAxis, CONE_HEIGHT, CONE_RADIUS, handleSize);
  66. if (yAxis.State == HandleSlider.StateType.Active)
  67. HandleDrawing.SetColor(Color.white);
  68. else if (yAxis.State == HandleSlider.StateType.Hover)
  69. HandleDrawing.SetColor(Color.green * 0.8f);
  70. else
  71. HandleDrawing.SetColor(Color.green);
  72. Vector3 yConeStart = Vector3.yAxis * (1.0f - CONE_HEIGHT);
  73. HandleDrawing.DrawLine(Vector3.zero, yConeStart, handleSize);
  74. HandleDrawing.DrawCone(yConeStart, Vector3.yAxis, CONE_HEIGHT, CONE_RADIUS, handleSize);
  75. if (zAxis.State == HandleSlider.StateType.Active)
  76. HandleDrawing.SetColor(Color.white);
  77. else if (zAxis.State == HandleSlider.StateType.Hover)
  78. HandleDrawing.SetColor(Color.blue * 0.8f);
  79. else
  80. HandleDrawing.SetColor(Color.blue);
  81. Vector3 zConeStart = Vector3.zAxis * (1.0f - CONE_HEIGHT);
  82. HandleDrawing.DrawLine(Vector3.zero, zConeStart, handleSize);
  83. HandleDrawing.DrawCone(zConeStart, Vector3.zAxis, 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. }