SceneWindow.cs 5.1 KB

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