2
0

SceneAxesGUI.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using bs;
  4. namespace bs.Editor
  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. camera.RenderSettings.EnableSkybox = false;
  56. renderTextureGUI = new GUIRenderTexture(renderTexture, true);
  57. GUILayoutY layout = panel.AddLayoutY();
  58. GUILayoutX textureLayout = layout.AddLayoutX();
  59. textureLayout.AddElement(renderTextureGUI);
  60. textureLayout.AddFlexibleSpace();
  61. Rect2I bounds = new Rect2I(0, 0, width, height);
  62. sceneHandles = new SceneHandles(window, camera);
  63. renderTextureGUI.Bounds = bounds;
  64. labelGUI = new GUILabel(projType.ToString(), EditorStyles.LabelCentered);
  65. layout.AddElement(labelGUI);
  66. layout.AddFlexibleSpace();
  67. this.panel = panel;
  68. this.bounds = bounds;
  69. }
  70. /// <summary>
  71. /// Selects a handle under the pointer position.
  72. /// </summary>
  73. /// <param name="pointerPos">Position of the pointer relative to the parent GUI panel.</param>
  74. public void TrySelect(Vector2I pointerPos)
  75. {
  76. if (!bounds.Contains(pointerPos))
  77. return;
  78. pointerPos.x -= bounds.x;
  79. pointerPos.y -= bounds.y;
  80. sceneHandles.TrySelect(pointerPos);
  81. }
  82. /// <summary>
  83. /// Checks is any handle currently active.
  84. /// </summary>
  85. /// <returns>True if a handle is active.</returns>
  86. internal bool IsActive()
  87. {
  88. return sceneHandles.IsActive();
  89. }
  90. /// <summary>
  91. /// Deselects any currently active handles.
  92. /// </summary>
  93. public void ClearSelection()
  94. {
  95. sceneHandles.ClearSelection();
  96. }
  97. /// <summary>
  98. /// Updates active handles by moving them as a result of any input.
  99. /// </summary>
  100. /// <param name="pointerPos">Position of the pointer relative to the parent GUI panel</param>
  101. public void UpdateInput(Vector2I pointerPos)
  102. {
  103. pointerPos.x -= bounds.x;
  104. pointerPos.y -= bounds.y;
  105. sceneHandles.UpdateInput(pointerPos, Input.PointerDelta);
  106. }
  107. /// <summary>
  108. /// Draws the scene axes onto the underlying camera.
  109. /// </summary>
  110. public void Draw()
  111. {
  112. sceneHandles.Draw();
  113. }
  114. /// <summary>
  115. /// Moves the GUI element to the specified position.
  116. /// </summary>
  117. /// <param name="x">Horizontal position of the GUI element relative to the parent panel.</param>
  118. /// <param name="y">Vertical position of the GUI element relative to the parent panel.</param>
  119. public void SetPosition(int x, int y)
  120. {
  121. bounds.x = x;
  122. bounds.y = y;
  123. renderTextureGUI.Bounds = bounds;
  124. panel.SetPosition(x, y);
  125. }
  126. /// <summary>
  127. /// Call this when done with the object so internal resources can be cleaned up.
  128. /// </summary>
  129. public void Destroy()
  130. {
  131. camera.SceneObject.Destroy();
  132. }
  133. }
  134. /** @} */
  135. }