SceneWindow.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using BansheeEngine;
  7. namespace BansheeEditor
  8. {
  9. internal sealed class SceneWindow : EditorWindow
  10. {
  11. private Camera camera;
  12. private SceneCamera cameraController;
  13. private RenderTexture2D renderTexture;
  14. private GUIRenderTexture renderTextureGUI;
  15. private SceneViewHandler sceneViewHandler;
  16. public Camera GetCamera()
  17. {
  18. return camera;
  19. }
  20. internal SceneWindow()
  21. { }
  22. private void OnInitialize()
  23. {
  24. UpdateRenderTexture(Width, Height);
  25. }
  26. private void OnDestroy()
  27. {
  28. if (camera != null)
  29. {
  30. camera.sceneObject.Destroy();
  31. }
  32. }
  33. private bool ScreenToScenePos(Vector2I screenPos, out Vector2I scenePos)
  34. {
  35. scenePos = screenPos;
  36. Vector2I windowPos = ScreenToWindowPos(screenPos);
  37. Rect2I bounds = GUILayoutUtility.CalculateBounds(renderTextureGUI);
  38. if (bounds.Contains(windowPos))
  39. {
  40. scenePos.x = windowPos.x - bounds.x;
  41. scenePos.y = windowPos.y - bounds.y;
  42. return true;
  43. }
  44. return false;
  45. }
  46. private void EditorUpdate()
  47. {
  48. sceneViewHandler.Update();
  49. bool handleActive = false;
  50. if (Input.IsButtonUp(ButtonCode.MouseLeft))
  51. {
  52. if (sceneViewHandler.IsHandleActive())
  53. {
  54. sceneViewHandler.ClearHandleSelection();
  55. handleActive = true;
  56. }
  57. }
  58. if (!HasFocus)
  59. return;
  60. Vector2I scenePos;
  61. if (ScreenToScenePos(Input.PointerPosition, out scenePos))
  62. {
  63. if (Input.IsButtonDown(ButtonCode.MouseLeft))
  64. {
  65. sceneViewHandler.TrySelectHandle(scenePos);
  66. }
  67. else if (Input.IsButtonUp(ButtonCode.MouseLeft))
  68. {
  69. if (!handleActive)
  70. {
  71. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
  72. Input.IsButtonHeld(ButtonCode.RightControl);
  73. sceneViewHandler.PickObject(scenePos, ctrlHeld);
  74. }
  75. }
  76. sceneViewHandler.UpdateHandle(scenePos, Input.PointerDelta);
  77. }
  78. }
  79. protected override void WindowResized(int width, int height)
  80. {
  81. UpdateRenderTexture(width, height);
  82. base.WindowResized(width, height);
  83. }
  84. protected override void FocusChanged(bool inFocus)
  85. {
  86. if (!inFocus)
  87. {
  88. sceneViewHandler.ClearHandleSelection();
  89. }
  90. }
  91. private void UpdateRenderTexture(int width, int height)
  92. {
  93. width = MathEx.Max(20, width);
  94. height = MathEx.Max(20, height);
  95. renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height);
  96. renderTexture.Priority = 1;
  97. if (camera == null)
  98. {
  99. SceneObject sceneCameraSO = new SceneObject("SceneCamera");
  100. camera = sceneCameraSO.AddComponent<Camera>();
  101. camera.target = renderTexture;
  102. camera.viewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  103. sceneCameraSO.position = new Vector3(0, 0.5f, 1);
  104. sceneCameraSO.LookAt(new Vector3(0, 0, 0));
  105. camera.priority = 1;
  106. camera.nearClipPlane = 0.005f;
  107. camera.farClipPlane = 1000.0f;
  108. cameraController = sceneCameraSO.AddComponent<SceneCamera>();
  109. renderTextureGUI = new GUIRenderTexture(renderTexture);
  110. GUI.layout.AddElement(renderTextureGUI);
  111. sceneViewHandler = new SceneViewHandler(this, camera);
  112. }
  113. else
  114. {
  115. camera.target = renderTexture;
  116. renderTextureGUI.RenderTexture = renderTexture;
  117. }
  118. // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
  119. // render target destroy/create cycle for every little pixel.
  120. camera.aspectRatio = width / (float)height;
  121. }
  122. }
  123. }