SceneWindow.cs 4.2 KB

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