SceneAxesGUI.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. camera.Flags = CameraFlag.OnDemand;
  57. renderTextureGUI = new GUIRenderTexture(renderTexture, true);
  58. GUILayoutY layout = panel.AddLayoutY();
  59. GUILayoutX textureLayout = layout.AddLayoutX();
  60. textureLayout.AddElement(renderTextureGUI);
  61. textureLayout.AddFlexibleSpace();
  62. Rect2I bounds = new Rect2I(0, 0, width, height);
  63. sceneHandles = new SceneHandles(window, camera);
  64. renderTextureGUI.Bounds = bounds;
  65. labelGUI = new GUILabel(projType.ToString(), EditorStyles.LabelCentered);
  66. layout.AddElement(labelGUI);
  67. layout.AddFlexibleSpace();
  68. this.panel = panel;
  69. this.bounds = bounds;
  70. NotifyNeedsRedraw();
  71. }
  72. /// <summary>
  73. /// Selects a handle under the pointer position.
  74. /// </summary>
  75. /// <param name="pointerPos">Position of the pointer relative to the parent GUI panel.</param>
  76. public void TrySelect(Vector2I pointerPos)
  77. {
  78. if (!bounds.Contains(pointerPos))
  79. return;
  80. pointerPos.x -= bounds.x;
  81. pointerPos.y -= bounds.y;
  82. if(sceneHandles.TrySelect(pointerPos))
  83. NotifyNeedsRedraw();
  84. }
  85. /// <summary>
  86. /// Checks is any handle currently active.
  87. /// </summary>
  88. /// <returns>True if a handle is active.</returns>
  89. internal bool IsActive()
  90. {
  91. return sceneHandles.IsActive();
  92. }
  93. /// <summary>
  94. /// Deselects any currently active handles.
  95. /// </summary>
  96. public void ClearSelection()
  97. {
  98. sceneHandles.ClearSelection();
  99. NotifyNeedsRedraw();
  100. }
  101. /// <summary>
  102. /// Updates active handles by moving them as a result of any input.
  103. /// </summary>
  104. /// <param name="pointerPos">Position of the pointer relative to the parent GUI panel</param>
  105. public void UpdateInput(Vector2I pointerPos)
  106. {
  107. pointerPos.x -= bounds.x;
  108. pointerPos.y -= bounds.y;
  109. if(sceneHandles.UpdateInput(pointerPos, Input.PointerDelta))
  110. NotifyNeedsRedraw();
  111. }
  112. /// <summary>
  113. /// Draws the scene axes onto the underlying camera.
  114. /// </summary>
  115. public void Draw()
  116. {
  117. sceneHandles.Draw();
  118. }
  119. /// <summary>
  120. /// Notifies the system that the 3D viewport should be redrawn.
  121. /// </summary>
  122. internal void NotifyNeedsRedraw()
  123. {
  124. camera.NotifyNeedsRedraw();
  125. }
  126. /// <summary>
  127. /// Enables or disables on-demand drawing. When enabled the 3D viewport will only be redrawn when
  128. /// <see cref="NotifyNeedsRedraw"/> is called. If disabled the viewport will be redrawn every frame.
  129. /// </summary>
  130. /// <param name="enabled">True to enable on-demand drawing, false otherwise.</param>
  131. internal void ToggleOnDemandDrawing(bool enabled)
  132. {
  133. camera.Flags = enabled ? CameraFlag.OnDemand : new CameraFlag();
  134. }
  135. /// <summary>
  136. /// Moves the GUI element to the specified position.
  137. /// </summary>
  138. /// <param name="x">Horizontal position of the GUI element relative to the parent panel.</param>
  139. /// <param name="y">Vertical position of the GUI element relative to the parent panel.</param>
  140. public void SetPosition(int x, int y)
  141. {
  142. bounds.x = x;
  143. bounds.y = y;
  144. renderTextureGUI.Bounds = bounds;
  145. panel.SetPosition(x, y);
  146. }
  147. /// <summary>
  148. /// Call this when done with the object so internal resources can be cleaned up.
  149. /// </summary>
  150. public void Destroy()
  151. {
  152. camera.SceneObject.Destroy();
  153. }
  154. }
  155. /** @} */
  156. }