SceneWindow.cs 3.3 KB

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