SceneWindow.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 (Input.IsButtonDown(ButtonCode.MouseLeft))
  41. {
  42. sceneViewHandler.PointerPressed(Input.PointerPosition);
  43. Vector2I scenePos;
  44. if (ScreenToScenePos(Input.PointerPosition, out scenePos))
  45. dragActive = true;
  46. }
  47. else if (Input.IsButtonUp(ButtonCode.MouseLeft))
  48. {
  49. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) || Input.IsButtonHeld(ButtonCode.RightControl);
  50. sceneViewHandler.PointerReleased(Input.PointerPosition, ctrlHeld);
  51. dragActive = false;
  52. }
  53. if (dragActive)
  54. {
  55. Vector2I scenePos;
  56. ScreenToScenePos(Input.PointerPosition, out scenePos);
  57. sceneViewHandler.Update(scenePos, Input.PointerDelta);
  58. }
  59. else
  60. {
  61. Vector2I scenePos;
  62. if (ScreenToScenePos(Input.PointerPosition, out scenePos))
  63. {
  64. sceneViewHandler.Update(scenePos, Input.PointerDelta);
  65. }
  66. }
  67. }
  68. protected override void WindowResized(int width, int height)
  69. {
  70. UpdateRenderTexture(width, height);
  71. base.WindowResized(width, height);
  72. }
  73. private void UpdateRenderTexture(int width, int height)
  74. {
  75. width = Math.Max(20, width);
  76. height = Math.Max(20, height);
  77. renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height);
  78. renderTexture.Priority = 1;
  79. if (camera == null)
  80. {
  81. SceneObject sceneCameraSO = new SceneObject("SceneCamera");
  82. camera = sceneCameraSO.AddComponent<Camera>();
  83. camera.target = renderTexture;
  84. camera.viewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);
  85. sceneCameraSO.Position = new Vector3(0, 0.5f, 1);
  86. sceneCameraSO.LookAt(new Vector3(0, 0, 0));
  87. camera.priority = 1;
  88. camera.nearClipPlane = 0.005f;
  89. camera.farClipPlane = 1000.0f;
  90. cameraController = sceneCameraSO.AddComponent<SceneCamera>();
  91. renderTextureGUI = new GUIRenderTexture(renderTexture);
  92. GUI.layout.AddElement(renderTextureGUI);
  93. sceneViewHandler = new SceneViewHandler(this, camera);
  94. }
  95. else
  96. {
  97. camera.target = renderTexture;
  98. renderTextureGUI.RenderTexture = renderTexture;
  99. }
  100. // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
  101. // render target destroy/create cycle for every little pixel.
  102. camera.aspectRatio = width / (float)height;
  103. }
  104. }
  105. }