SceneWindow.cs 5.8 KB

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