SceneAxesGUI.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using BansheeEngine;
  2. namespace BansheeEditor
  3. {
  4. /// <summary>
  5. /// Handles rendering of scene axis handles into a GUI element.
  6. /// </summary>
  7. internal class SceneAxesGUI
  8. {
  9. private RenderTexture2D renderTexture;
  10. private Camera camera;
  11. private SceneHandles sceneHandles;
  12. private GUIRenderTexture renderTextureGUI;
  13. private Rect2I bounds;
  14. /// <summary>
  15. /// Creates a new scene axes GUI.
  16. /// </summary>
  17. /// <param name="window">Window in which the GUI is located in.</param>
  18. /// <param name="panel">Panel onto which to place the GUI element.</param>
  19. /// <param name="width">Width of the GUI element.</param>
  20. /// <param name="height">Height of the GUI element.</param>
  21. public SceneAxesGUI(EditorWindow window, GUIPanel panel, int width, int height)
  22. {
  23. renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height);
  24. renderTexture.Priority = 1;
  25. SceneObject cameraSO = new SceneObject("SceneAxesCamera", true);
  26. camera = cameraSO.AddComponent<Camera>();
  27. camera.Target = renderTexture;
  28. camera.ViewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  29. cameraSO.Position = new Vector3(0, 0, 5);
  30. cameraSO.LookAt(new Vector3(0, 0, 0));
  31. camera.Priority = 2;
  32. camera.NearClipPlane = 0.05f;
  33. camera.FarClipPlane = 1000.0f;
  34. camera.ClearColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
  35. camera.ProjectionType = ProjectionType.Orthographic;
  36. camera.Layers = SceneAxesHandle.LAYER;
  37. camera.AspectRatio = 1.0f;
  38. camera.OrthoHeight = 2.0f;
  39. renderTextureGUI = new GUIRenderTexture(renderTexture, true);
  40. panel.AddElement(renderTextureGUI);
  41. Rect2I bounds = new Rect2I(0, 0, width, height);
  42. sceneHandles = new SceneHandles(window, camera);
  43. renderTextureGUI.Bounds = bounds;
  44. this.bounds = bounds;
  45. }
  46. /// <summary>
  47. /// Selects a handle under the pointer position.
  48. /// </summary>
  49. /// <param name="pointerPos">Position of the pointer relative to the parent GUI panel.</param>
  50. public void TrySelect(Vector2I pointerPos)
  51. {
  52. if (!bounds.Contains(pointerPos))
  53. return;
  54. pointerPos.x -= bounds.x;
  55. pointerPos.y -= bounds.y;
  56. sceneHandles.TrySelect(pointerPos);
  57. }
  58. /// <summary>
  59. /// Checks is any handle currently active.
  60. /// </summary>
  61. /// <returns>True if a handle is active.</returns>
  62. internal bool IsActive()
  63. {
  64. return sceneHandles.IsActive();
  65. }
  66. /// <summary>
  67. /// Deselects any currently active handles.
  68. /// </summary>
  69. public void ClearSelection()
  70. {
  71. sceneHandles.ClearSelection();
  72. }
  73. /// <summary>
  74. /// Updates active handles by moving them as a result of any input.
  75. /// </summary>
  76. /// <param name="pointerPos">Position of the pointer relative to the parent GUI panel</param>
  77. public void UpdateInput(Vector2I pointerPos)
  78. {
  79. pointerPos.x -= bounds.x;
  80. pointerPos.y -= bounds.y;
  81. sceneHandles.UpdateInput(pointerPos, Input.PointerDelta);
  82. }
  83. /// <summary>
  84. /// Draws the scene axes onto the underlying camera.
  85. /// </summary>
  86. public void Draw()
  87. {
  88. sceneHandles.Draw();
  89. }
  90. /// <summary>
  91. /// Moves the GUI element to the specified position.
  92. /// </summary>
  93. /// <param name="x">Horizontal position of the GUI element relative to the parent panel.</param>
  94. /// <param name="y">Vertical position of the GUI element relative to the parent panel.</param>
  95. public void SetPosition(int x, int y)
  96. {
  97. bounds.x = x;
  98. bounds.y = y;
  99. renderTextureGUI.Bounds = bounds;
  100. }
  101. }
  102. }