RotateHandle.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. //return xAxis.State == HandleSlider.StateType.Active ||
  18. // yAxis.State == HandleSlider.StateType.Active ||
  19. // zAxis.State == HandleSlider.StateType.Active;
  20. }
  21. public RotateHandle()
  22. {
  23. xAxis = new HandleSliderDisc(this, Vector3.xAxis, 1.0f);
  24. //yAxis = new HandleSliderDisc(this, Vector3.yAxis, 1.0f);
  25. //zAxis = new HandleSliderDisc(this, Vector3.zAxis, 1.0f);
  26. }
  27. protected override void PreInput()
  28. {
  29. xAxis.Position = position;
  30. // yAxis.Position = position;
  31. //zAxis.Position = position;
  32. xAxis.Rotation = rotation;
  33. //yAxis.Rotation = rotation;
  34. //zAxis.Rotation = rotation;
  35. }
  36. protected override void PostInput()
  37. {
  38. delta = Quaternion.identity;
  39. Degree xValue = 0.0f;
  40. Degree yValue = 0.0f;
  41. Degree zValue = 0.0f;
  42. if (Handles.RotateHandleSnapActive)
  43. {
  44. xValue = Handles.SnapValue(xAxis.Delta, Handles.RotateSnapAmount);
  45. //yValue = Handles.SnapValue(yAxis.Delta, Handles.RotateSnapAmount);
  46. //zValue = Handles.SnapValue(zAxis.Delta, Handles.RotateSnapAmount);
  47. }
  48. else
  49. {
  50. xValue = xAxis.Delta;
  51. //yValue = yAxis.Delta;
  52. // zValue = zAxis.Delta;
  53. }
  54. delta = Quaternion.FromAxisAngle(GetXDir(), xValue) * delta;
  55. // delta = Quaternion.FromAxisAngle(GetYDir(), yValue) * delta;
  56. // delta = Quaternion.FromAxisAngle(GetZDir(), zValue) * delta;
  57. }
  58. protected override void Draw()
  59. {
  60. HandleDrawing.SetTransform(Matrix4.TRS(Position, Rotation, Vector3.one));
  61. float handleSize = Handles.GetHandleSize(EditorApplication.SceneViewCamera, position);
  62. // Draw arcs
  63. Color axisHover = new Color(0.8f, 0.8f, 0.8f, 1.0f);
  64. if (xAxis.State == HandleSlider.StateType.Active)
  65. HandleDrawing.SetColor(Color.white);
  66. else if(xAxis.State == HandleSlider.StateType.Hover)
  67. HandleDrawing.SetColor(Color.red * axisHover);
  68. else
  69. HandleDrawing.SetColor(Color.red);
  70. Vector3 xStartDir = Vector3.Cross(EditorApplication.SceneViewCamera.sceneObject.Forward, GetXDir());
  71. Degree xStartAngle = PointOnCircleToAngle(GetXDir(), xStartDir);
  72. Debug.Log("START ARC: " + xStartDir + " -- " + xStartAngle);
  73. HandleDrawing.DrawWireArc(Vector3.zero, GetXDir(), 1.0f, xStartAngle, 180.0f, handleSize);
  74. //if (yAxis.State == HandleSlider.StateType.Active)
  75. // HandleDrawing.SetColor(Color.white);
  76. //else if (yAxis.State == HandleSlider.StateType.Hover)
  77. // HandleDrawing.SetColor(Color.green * axisHover);
  78. //else
  79. // HandleDrawing.SetColor(Color.green);
  80. //Vector3 yStartDir = Vector3.Cross(EditorApplication.SceneViewCamera.sceneObject.Forward, GetYDir());
  81. //Degree yStartAngle = PointOnCircleToAngle(GetYDir(), yStartDir);
  82. //HandleDrawing.DrawWireArc(Vector3.zero, GetYDir(), 1.0f, yStartAngle, 180.0f, handleSize);
  83. //if (zAxis.State == HandleSlider.StateType.Active)
  84. // HandleDrawing.SetColor(Color.white);
  85. //else if (zAxis.State == HandleSlider.StateType.Hover)
  86. // HandleDrawing.SetColor(Color.blue * axisHover);
  87. //else
  88. // HandleDrawing.SetColor(Color.blue);
  89. //Vector3 zStartDir = Vector3.Cross(EditorApplication.SceneViewCamera.sceneObject.Forward, -GetZDir());
  90. //Degree zStartAngle = PointOnCircleToAngle(-GetZDir(), zStartDir);
  91. //HandleDrawing.DrawWireArc(Vector3.zero, -GetZDir(), 1.0f, zStartAngle, 180.0f, handleSize);
  92. // Draw active rotation pie
  93. Color gray = new Color(1.0f, 1.0f, 1.0f, 0.3f);
  94. HandleDrawing.SetColor(gray);
  95. if (xAxis.State == HandleSlider.StateType.Active)
  96. HandleDrawing.DrawArc(Vector3.zero, GetXDir(), 1.0f, xAxis.StartAngle, xAxis.Delta, handleSize);
  97. //else if (yAxis.State == HandleSlider.StateType.Active)
  98. // HandleDrawing.DrawArc(Vector3.zero, GetYDir(), 1.0f, yAxis.StartAngle, yAxis.Delta, handleSize);
  99. //else if (zAxis.State == HandleSlider.StateType.Active)
  100. // HandleDrawing.DrawArc(Vector3.zero, -GetZDir(), 1.0f, zAxis.StartAngle, zAxis.Delta, handleSize);
  101. // TODO - Free rotate handle
  102. }
  103. private Vector3 GetXDir()
  104. {
  105. return rotation.Rotate(Vector3.xAxis);
  106. }
  107. private Vector3 GetYDir()
  108. {
  109. return rotation.Rotate(Vector3.yAxis);
  110. }
  111. private Vector3 GetZDir()
  112. {
  113. return rotation.Rotate(Vector3.zAxis);
  114. }
  115. private Degree PointOnCircleToAngle(Vector3 up, Vector3 point)
  116. {
  117. Vector3[] arcBasis = new Vector3[3];
  118. arcBasis[1] = up;
  119. Vector3.OrthogonalComplement(arcBasis[1], out arcBasis[2], out arcBasis[0]);
  120. Matrix4 worldToPlane = Matrix4.identity;
  121. worldToPlane.SetColumn(0, (Vector4)arcBasis[0]);
  122. worldToPlane.SetColumn(1, (Vector4)arcBasis[1]);
  123. worldToPlane.SetColumn(2, (Vector4)arcBasis[2]);
  124. point = worldToPlane.Multiply(point);
  125. return (MathEx.Atan2(-point.z, -point.x) + MathEx.Pi) * MathEx.Rad2Deg;
  126. }
  127. }
  128. }