SceneWindow.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. if (!HasFocus)
  40. return;
  41. Vector2I scenePos;
  42. if (ScreenToScenePos(Input.PointerPosition, out scenePos))
  43. {
  44. Debug.Log(scenePos);
  45. if (Input.IsButtonDown(ButtonCode.MouseLeft))
  46. {
  47. sceneViewHandler.TrySelectHandle(scenePos);
  48. }
  49. else if (Input.IsButtonUp(ButtonCode.MouseLeft))
  50. {
  51. bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
  52. Input.IsButtonHeld(ButtonCode.RightControl);
  53. if (sceneViewHandler.IsHandleActive())
  54. sceneViewHandler.ClearHandleSelection();
  55. else
  56. sceneViewHandler.PickObject(scenePos, ctrlHeld);
  57. }
  58. sceneViewHandler.Update(scenePos, Input.PointerDelta);
  59. }
  60. }
  61. protected override void WindowResized(int width, int height)
  62. {
  63. UpdateRenderTexture(width, height);
  64. base.WindowResized(width, height);
  65. }
  66. protected override void FocusChanged(bool inFocus)
  67. {
  68. if (!inFocus)
  69. {
  70. sceneViewHandler.ClearHandleSelection();
  71. }
  72. }
  73. private void UpdateRenderTexture(int width, int height)
  74. {
  75. width = MathEx.Max(20, width);
  76. height = MathEx.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. }