SceneAxesHandle.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. /// <summary>
  6. /// Draws axes that display the orientation of the scene camera. Also allows you to change camera orientation along
  7. /// one of the axes, or change projection modes.
  8. /// </summary>
  9. [CustomHandle(null)]
  10. internal class SceneAxesHandle : Handle
  11. {
  12. public const UInt64 LAYER = 0x7000000000000000;
  13. private const float CONE_HEIGHT = 0.25f;
  14. private const float CONE_RADIUS = 0.175f;
  15. private HandleSliderLine xAxis;
  16. private HandleSliderLine yAxis;
  17. private HandleSliderLine zAxis;
  18. private HandleSliderPlane projTypePlane;
  19. private bool[] clickStates = new bool[4];
  20. private Vector3 position = Vector3.Zero;
  21. private Quaternion rotation = Quaternion.Identity;
  22. /// <summary>
  23. /// Creates a new scene axes handle.
  24. /// </summary>
  25. public SceneAxesHandle()
  26. {
  27. xAxis = new HandleSliderLine(this, Vector3.XAxis, 1.0f, false, LAYER);
  28. yAxis = new HandleSliderLine(this, Vector3.YAxis, 1.0f, false, LAYER);
  29. zAxis = new HandleSliderLine(this, Vector3.ZAxis, 1.0f, false, LAYER);
  30. projTypePlane = new HandleSliderPlane(this, Vector3.XAxis, Vector3.YAxis, 0.4f, false, LAYER);
  31. }
  32. /// <inheritdoc/>
  33. protected internal override void PreInput()
  34. {
  35. Camera cam = EditorApplication.SceneViewCamera;
  36. if (cam == null)
  37. return;
  38. position = new Vector3(0, 0, -5.0f);
  39. rotation = cam.SceneObject.Rotation.Inverse;
  40. xAxis.Position = position;
  41. yAxis.Position = position;
  42. zAxis.Position = position;
  43. xAxis.Rotation = rotation;
  44. yAxis.Rotation = rotation;
  45. zAxis.Rotation = rotation;
  46. Vector3 freeAxisOffset = new Vector3(-0.2f, -0.2f, 0.2f);
  47. projTypePlane.Rotation = Quaternion.Identity;
  48. projTypePlane.Position = position + freeAxisOffset;
  49. }
  50. /// <inheritdoc/>
  51. protected internal override void PostInput()
  52. {
  53. var axes = new []
  54. {
  55. new Tuple<HandleSlider, Action>(xAxis, () => RotateCameraTo(Vector3.XAxis)),
  56. new Tuple<HandleSlider, Action>(yAxis, () => RotateCameraTo(Vector3.YAxis)),
  57. new Tuple<HandleSlider, Action>(zAxis, () => RotateCameraTo(Vector3.ZAxis)),
  58. new Tuple<HandleSlider, Action>(projTypePlane, ToggleProjectionType)
  59. };
  60. for (int i = 0; i < axes.Length; i++)
  61. {
  62. if (axes[i].Item1.State == HandleSlider.StateType.Active)
  63. {
  64. if (!clickStates[i])
  65. {
  66. axes[i].Item2();
  67. clickStates[i] = true;
  68. }
  69. }
  70. else
  71. {
  72. clickStates[i] = false;
  73. }
  74. }
  75. }
  76. /// <inheritdoc/>
  77. protected internal override void Draw()
  78. {
  79. HandleDrawing.Layer = LAYER;
  80. HandleDrawing.Transform = Matrix4.TRS(position, rotation, Vector3.One);
  81. Vector3 cameraForward = EditorApplication.SceneViewCamera.SceneObject.Forward;
  82. // Draw 1D arrows
  83. Color xColor = Color.Red;
  84. if (xAxis.State == HandleSlider.StateType.Active)
  85. xColor = Color.White;
  86. else if (xAxis.State == HandleSlider.StateType.Hover)
  87. xColor = Color.BansheeOrange;
  88. xColor.a = MathEx.Lerp(1.0f, 0.0f, MathEx.Abs(Vector3.Dot(cameraForward, Vector3.XAxis)), 0.8f, 1.0f);
  89. HandleDrawing.Color = xColor;
  90. Vector3 xConeStart = Vector3.XAxis * (1.0f - CONE_HEIGHT);
  91. HandleDrawing.DrawLine(Vector3.Zero, xConeStart);
  92. HandleDrawing.DrawCone(xConeStart, Vector3.XAxis, CONE_HEIGHT, CONE_RADIUS);
  93. Color yColor = Color.Green;
  94. if (yAxis.State == HandleSlider.StateType.Active)
  95. yColor = Color.White;
  96. else if (yAxis.State == HandleSlider.StateType.Hover)
  97. yColor = Color.BansheeOrange;
  98. yColor.a = MathEx.Lerp(1.0f, 0.0f, MathEx.Abs(Vector3.Dot(cameraForward, Vector3.YAxis)), 0.8f, 1.0f);
  99. HandleDrawing.Color = yColor;
  100. Vector3 yConeStart = Vector3.YAxis * (1.0f - CONE_HEIGHT);
  101. HandleDrawing.DrawLine(Vector3.Zero, yConeStart);
  102. HandleDrawing.DrawCone(yConeStart, Vector3.YAxis, CONE_HEIGHT, CONE_RADIUS);
  103. Color zColor = Color.Blue;
  104. if (zAxis.State == HandleSlider.StateType.Active)
  105. zColor = Color.White;
  106. else if (zAxis.State == HandleSlider.StateType.Hover)
  107. zColor = Color.BansheeOrange;
  108. zColor.a = MathEx.Lerp(1.0f, 0.0f, MathEx.Abs(Vector3.Dot(cameraForward, Vector3.ZAxis)), 0.8f, 1.0f);
  109. HandleDrawing.Color = zColor;
  110. Vector3 zConeStart = Vector3.ZAxis * (1.0f - CONE_HEIGHT);
  111. HandleDrawing.DrawLine(Vector3.Zero, zConeStart);
  112. HandleDrawing.DrawCone(zConeStart, Vector3.ZAxis, CONE_HEIGHT, CONE_RADIUS);
  113. // Draw projection type handle
  114. if (projTypePlane.State == HandleSlider.StateType.Active)
  115. HandleDrawing.Color = Color.White;
  116. else if (projTypePlane.State == HandleSlider.StateType.Hover)
  117. HandleDrawing.Color = Color.BansheeOrange;
  118. else
  119. HandleDrawing.Color = Color.White;
  120. HandleDrawing.DrawCube(Vector3.Zero, new Vector3(0.2f, 0.2f, 0.2f));
  121. // TODO - Add a text notifying the user whether ortho/proj is active
  122. }
  123. private void RotateCameraTo(Vector3 axis)
  124. {
  125. // TODO - Rotate to the provided axis. If already looking at that axis, rotate in the opposite direction (-axis)
  126. }
  127. private void ToggleProjectionType()
  128. {
  129. // TODO - Switch between ortographic and perspective
  130. }
  131. }
  132. }