RotateHandle.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using BansheeEngine;
  2. namespace BansheeEditor
  3. {
  4. public sealed class RotateHandle : DefaultHandle
  5. {
  6. private Quaternion delta;
  7. private HandleSliderDisc xAxis;
  8. private HandleSliderDisc yAxis;
  9. private HandleSliderDisc zAxis;
  10. public Quaternion Delta
  11. {
  12. get { return delta; }
  13. }
  14. internal override bool IsDragged()
  15. {
  16. return xAxis.State == HandleSlider.StateType.Active ||
  17. yAxis.State == HandleSlider.StateType.Active ||
  18. zAxis.State == HandleSlider.StateType.Active;
  19. }
  20. public RotateHandle()
  21. {
  22. xAxis = new HandleSliderDisc(this, Vector3.xAxis, 1.0f);
  23. yAxis = new HandleSliderDisc(this, Vector3.yAxis, 1.0f);
  24. zAxis = new HandleSliderDisc(this, Vector3.zAxis, 1.0f);
  25. }
  26. protected override void PreInput()
  27. {
  28. xAxis.Position = position;
  29. yAxis.Position = position;
  30. zAxis.Position = position;
  31. }
  32. protected override void PostInput()
  33. {
  34. delta = Quaternion.identity;
  35. Degree xValue = 0.0f;
  36. Degree yValue = 0.0f;
  37. Degree zValue = 0.0f;
  38. if (Handles.RotateHandleSnapActive)
  39. {
  40. xValue = Handles.SnapValue(xAxis.Delta, Handles.RotateSnapAmount);
  41. yValue = Handles.SnapValue(yAxis.Delta, Handles.RotateSnapAmount);
  42. zValue = Handles.SnapValue(zAxis.Delta, Handles.RotateSnapAmount);
  43. }
  44. else
  45. {
  46. xValue = xAxis.Delta;
  47. yValue = yAxis.Delta;
  48. zValue = zAxis.Delta;
  49. }
  50. delta = Quaternion.FromAxisAngle(GetXDir(), xValue) * delta;
  51. delta = Quaternion.FromAxisAngle(GetYDir(), yValue) * delta;
  52. delta = Quaternion.FromAxisAngle(GetZDir(), zValue) * delta;
  53. }
  54. protected override void Draw()
  55. {
  56. HandleDrawing.SetTransform(Matrix4.TRS(Position, Quaternion.identity, Vector3.one));
  57. float handleSize = Handles.GetHandleSize(EditorApplication.SceneViewCamera, position);
  58. // Draw arcs
  59. if (xAxis.State == HandleSlider.StateType.Active)
  60. HandleDrawing.SetColor(Color.White);
  61. else if(xAxis.State == HandleSlider.StateType.Hover)
  62. HandleDrawing.SetColor(Color.BansheeOrange);
  63. else
  64. HandleDrawing.SetColor(Color.Red);
  65. Vector3 xStartDir = Vector3.Cross(EditorApplication.SceneViewCamera.sceneObject.Forward, GetXDir());
  66. Degree xStartAngle = PointOnCircleToAngle(GetXDir(), xStartDir);
  67. HandleDrawing.DrawWireArc(Vector3.zero, GetXDir(), 1.0f, xStartAngle, 180.0f, 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 yStartDir = Vector3.Cross(EditorApplication.SceneViewCamera.sceneObject.Forward, GetYDir());
  75. Degree yStartAngle = PointOnCircleToAngle(GetYDir(), yStartDir);
  76. HandleDrawing.DrawWireArc(Vector3.zero, GetYDir(), 1.0f, yStartAngle, 180.0f, handleSize);
  77. if (zAxis.State == HandleSlider.StateType.Active)
  78. HandleDrawing.SetColor(Color.White);
  79. else if (zAxis.State == HandleSlider.StateType.Hover)
  80. HandleDrawing.SetColor(Color.BansheeOrange);
  81. else
  82. HandleDrawing.SetColor(Color.Blue);
  83. Vector3 zStartDir = Vector3.Cross(EditorApplication.SceneViewCamera.sceneObject.Forward, GetZDir());
  84. Degree zStartAngle = PointOnCircleToAngle(GetZDir(), zStartDir);
  85. HandleDrawing.DrawWireArc(Vector3.zero, GetZDir(), 1.0f, zStartAngle, 180.0f, handleSize);
  86. // Draw active rotation pie
  87. Color gray = new Color(1.0f, 1.0f, 1.0f, 0.3f);
  88. HandleDrawing.SetColor(gray);
  89. if (xAxis.State == HandleSlider.StateType.Active)
  90. HandleDrawing.DrawArc(Vector3.zero, GetXDir(), 1.0f, xAxis.StartAngle, xAxis.Delta, handleSize);
  91. else if (yAxis.State == HandleSlider.StateType.Active)
  92. HandleDrawing.DrawArc(Vector3.zero, GetYDir(), 1.0f, yAxis.StartAngle, yAxis.Delta, handleSize);
  93. else if (zAxis.State == HandleSlider.StateType.Active)
  94. HandleDrawing.DrawArc(Vector3.zero, GetZDir(), 1.0f, zAxis.StartAngle, zAxis.Delta, handleSize);
  95. // TODO - Free rotate handle
  96. }
  97. private Vector3 GetXDir()
  98. {
  99. return Vector3.xAxis;
  100. }
  101. private Vector3 GetYDir()
  102. {
  103. return Vector3.yAxis;
  104. }
  105. private Vector3 GetZDir()
  106. {
  107. return Vector3.zAxis;
  108. }
  109. private Degree PointOnCircleToAngle(Vector3 up, Vector3 point)
  110. {
  111. Quaternion rot = Quaternion.FromToRotation(up, Vector3.yAxis);
  112. Matrix4 worldToPlane = Matrix4.TRS(Vector3.zero, rot, Vector3.one);
  113. point = worldToPlane.MultiplyAffine(point);
  114. return (MathEx.Atan2(-point.z, -point.x) + MathEx.Pi) * MathEx.Rad2Deg;
  115. }
  116. }
  117. }