SceneAxesGUI.cs 5.4 KB

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