SceneWindow.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 bool ScreenToScenePos(Vector2I screenPos, out Vector2I scenePos)
  27. {
  28. scenePos = screenPos;
  29. Vector2I windowPos = ScreenToWindowPos(screenPos);
  30. Rect2I bounds = GUILayoutUtility.CalculateBounds(renderTextureGUI);
  31. if (bounds.Contains(windowPos))
  32. {
  33. scenePos.x = windowPos.x - bounds.x;
  34. scenePos.y = windowPos.y - bounds.y;
  35. return true;
  36. }
  37. return false;
  38. }
  39. private void EditorUpdate()
  40. {
  41. sceneViewHandler.Update();
  42. bool handleActive = false;
  43. if (Input.IsButtonUp(ButtonCode.MouseLeft))
  44. {
  45. if (sceneViewHandler.IsHandleActive())
  46. {
  47. sceneViewHandler.ClearHandleSelection();
  48. handleActive = true;
  49. }
  50. }
  51. if (!HasFocus)
  52. return;
  53. Vector2I scenePos;
  54. if (ScreenToScenePos(Input.PointerPosition, out scenePos))
  55. {
  56. if (Input.IsButtonDown(ButtonCode.MouseLeft))
  57. {
  58. sceneViewHandler.TrySelectHandle(scenePos);
  59. }
  60. else if (Input.IsButtonUp(ButtonCode.MouseLeft))
  61. {
  62. if (!handleActive)
  63. {
  64. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
  65. Input.IsButtonHeld(ButtonCode.RightControl);
  66. sceneViewHandler.PickObject(scenePos, ctrlHeld);
  67. }
  68. }
  69. sceneViewHandler.UpdateHandle(scenePos, Input.PointerDelta);
  70. }
  71. }
  72. protected override void WindowResized(int width, int height)
  73. {
  74. UpdateRenderTexture(width, height);
  75. base.WindowResized(width, height);
  76. }
  77. protected override void FocusChanged(bool inFocus)
  78. {
  79. if (!inFocus)
  80. {
  81. sceneViewHandler.ClearHandleSelection();
  82. }
  83. }
  84. private void UpdateRenderTexture(int width, int height)
  85. {
  86. width = MathEx.Max(20, width);
  87. height = MathEx.Max(20, height);
  88. renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height);
  89. renderTexture.Priority = 1;
  90. if (camera == null)
  91. {
  92. SceneObject sceneCameraSO = new SceneObject("SceneCamera");
  93. camera = sceneCameraSO.AddComponent<Camera>();
  94. camera.target = renderTexture;
  95. camera.viewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  96. sceneCameraSO.position = new Vector3(0, 0.5f, 1);
  97. sceneCameraSO.LookAt(new Vector3(0, 0, 0));
  98. camera.priority = 1;
  99. camera.nearClipPlane = 0.005f;
  100. camera.farClipPlane = 1000.0f;
  101. cameraController = sceneCameraSO.AddComponent<SceneCamera>();
  102. renderTextureGUI = new GUIRenderTexture(renderTexture);
  103. GUI.layout.AddElement(renderTextureGUI);
  104. sceneViewHandler = new SceneViewHandler(this, camera);
  105. }
  106. else
  107. {
  108. camera.target = renderTexture;
  109. renderTextureGUI.RenderTexture = renderTexture;
  110. }
  111. // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
  112. // render target destroy/create cycle for every little pixel.
  113. camera.aspectRatio = width / (float)height;
  114. }
  115. }
  116. }