SceneAxesGUI.cs 5.3 KB

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