SceneAxesGUI.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 GUIPanel panel;
  13. private GUIRenderTexture renderTextureGUI;
  14. private GUILabel labelGUI;
  15. private Rect2I bounds;
  16. /// <summary>
  17. /// Projection type to display on the GUI.
  18. /// </summary>
  19. public ProjectionType ProjectionType
  20. {
  21. set { labelGUI.SetContent(value.ToString()); }
  22. }
  23. /// <summary>
  24. /// Creates a new scene axes GUI.
  25. /// </summary>
  26. /// <param name="window">Window in which the GUI is located in.</param>
  27. /// <param name="panel">Panel onto which to place the GUI element.</param>
  28. /// <param name="width">Width of the GUI element.</param>
  29. /// <param name="height">Height of the GUI element.</param>
  30. /// <param name="projType">Projection type to display on the GUI.</param>
  31. public SceneAxesGUI(SceneWindow window, GUIPanel panel, int width, int height, ProjectionType projType)
  32. {
  33. renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height);
  34. renderTexture.Priority = 1;
  35. SceneObject cameraSO = new SceneObject("SceneAxesCamera", true);
  36. camera = cameraSO.AddComponent<Camera>();
  37. camera.Target = renderTexture;
  38. camera.ViewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  39. cameraSO.Position = new Vector3(0, 0, 5);
  40. cameraSO.LookAt(new Vector3(0, 0, 0));
  41. camera.Priority = 2;
  42. camera.NearClipPlane = 0.05f;
  43. camera.FarClipPlane = 1000.0f;
  44. camera.ClearColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
  45. camera.ProjectionType = ProjectionType.Orthographic;
  46. camera.Layers = SceneAxesHandle.LAYER;
  47. camera.AspectRatio = 1.0f;
  48. camera.OrthoHeight = 2.0f;
  49. renderTextureGUI = new GUIRenderTexture(renderTexture, true);
  50. GUILayoutY layout = panel.AddLayoutY();
  51. layout.AddElement(renderTextureGUI);
  52. Rect2I bounds = new Rect2I(0, 0, width, height);
  53. sceneHandles = new SceneHandles(window, camera);
  54. renderTextureGUI.Bounds = bounds;
  55. labelGUI = new GUILabel(projType.ToString(), EditorStyles.LabelCentered);
  56. layout.AddElement(labelGUI);
  57. this.panel = panel;
  58. this.bounds = bounds;
  59. }
  60. /// <summary>
  61. /// Selects a handle under the pointer position.
  62. /// </summary>
  63. /// <param name="pointerPos">Position of the pointer relative to the parent GUI panel.</param>
  64. public void TrySelect(Vector2I pointerPos)
  65. {
  66. if (!bounds.Contains(pointerPos))
  67. return;
  68. pointerPos.x -= bounds.x;
  69. pointerPos.y -= bounds.y;
  70. sceneHandles.TrySelect(pointerPos);
  71. }
  72. /// <summary>
  73. /// Checks is any handle currently active.
  74. /// </summary>
  75. /// <returns>True if a handle is active.</returns>
  76. internal bool IsActive()
  77. {
  78. return sceneHandles.IsActive();
  79. }
  80. /// <summary>
  81. /// Deselects any currently active handles.
  82. /// </summary>
  83. public void ClearSelection()
  84. {
  85. sceneHandles.ClearSelection();
  86. }
  87. /// <summary>
  88. /// Updates active handles by moving them as a result of any input.
  89. /// </summary>
  90. /// <param name="pointerPos">Position of the pointer relative to the parent GUI panel</param>
  91. public void UpdateInput(Vector2I pointerPos)
  92. {
  93. pointerPos.x -= bounds.x;
  94. pointerPos.y -= bounds.y;
  95. sceneHandles.UpdateInput(pointerPos, Input.PointerDelta);
  96. }
  97. /// <summary>
  98. /// Draws the scene axes onto the underlying camera.
  99. /// </summary>
  100. public void Draw()
  101. {
  102. sceneHandles.Draw();
  103. }
  104. /// <summary>
  105. /// Moves the GUI element to the specified position.
  106. /// </summary>
  107. /// <param name="x">Horizontal position of the GUI element relative to the parent panel.</param>
  108. /// <param name="y">Vertical position of the GUI element relative to the parent panel.</param>
  109. public void SetPosition(int x, int y)
  110. {
  111. bounds.x = x;
  112. bounds.y = y;
  113. renderTextureGUI.Bounds = bounds;
  114. panel.SetPosition(x, y);
  115. }
  116. /// <summary>
  117. /// Call this when done with the object so internal resources can be cleaned up.
  118. /// </summary>
  119. public void Destroy()
  120. {
  121. camera.SceneObject.Destroy();
  122. }
  123. }
  124. }