SceneAxesGUI.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. GUILayoutX textureLayout = layout.AddLayoutX();
  52. textureLayout.AddElement(renderTextureGUI);
  53. textureLayout.AddFlexibleSpace();
  54. Rect2I bounds = new Rect2I(0, 0, width, height);
  55. sceneHandles = new SceneHandles(window, camera);
  56. renderTextureGUI.Bounds = bounds;
  57. labelGUI = new GUILabel(projType.ToString(), EditorStyles.LabelCentered);
  58. layout.AddElement(labelGUI);
  59. layout.AddFlexibleSpace();
  60. this.panel = panel;
  61. this.bounds = bounds;
  62. }
  63. /// <summary>
  64. /// Selects a handle under the pointer position.
  65. /// </summary>
  66. /// <param name="pointerPos">Position of the pointer relative to the parent GUI panel.</param>
  67. public void TrySelect(Vector2I pointerPos)
  68. {
  69. if (!bounds.Contains(pointerPos))
  70. return;
  71. pointerPos.x -= bounds.x;
  72. pointerPos.y -= bounds.y;
  73. sceneHandles.TrySelect(pointerPos);
  74. }
  75. /// <summary>
  76. /// Checks is any handle currently active.
  77. /// </summary>
  78. /// <returns>True if a handle is active.</returns>
  79. internal bool IsActive()
  80. {
  81. return sceneHandles.IsActive();
  82. }
  83. /// <summary>
  84. /// Deselects any currently active handles.
  85. /// </summary>
  86. public void ClearSelection()
  87. {
  88. sceneHandles.ClearSelection();
  89. }
  90. /// <summary>
  91. /// Updates active handles by moving them as a result of any input.
  92. /// </summary>
  93. /// <param name="pointerPos">Position of the pointer relative to the parent GUI panel</param>
  94. public void UpdateInput(Vector2I pointerPos)
  95. {
  96. pointerPos.x -= bounds.x;
  97. pointerPos.y -= bounds.y;
  98. sceneHandles.UpdateInput(pointerPos, Input.PointerDelta);
  99. }
  100. /// <summary>
  101. /// Draws the scene axes onto the underlying camera.
  102. /// </summary>
  103. public void Draw()
  104. {
  105. sceneHandles.Draw();
  106. }
  107. /// <summary>
  108. /// Moves the GUI element to the specified position.
  109. /// </summary>
  110. /// <param name="x">Horizontal position of the GUI element relative to the parent panel.</param>
  111. /// <param name="y">Vertical position of the GUI element relative to the parent panel.</param>
  112. public void SetPosition(int x, int y)
  113. {
  114. bounds.x = x;
  115. bounds.y = y;
  116. renderTextureGUI.Bounds = bounds;
  117. panel.SetPosition(x, y);
  118. }
  119. /// <summary>
  120. /// Call this when done with the object so internal resources can be cleaned up.
  121. /// </summary>
  122. public void Destroy()
  123. {
  124. camera.SceneObject.Destroy();
  125. }
  126. }
  127. }