SceneWindow.cs 4.5 KB

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