GameWindow.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using bs;
  4. namespace bs.Editor
  5. {
  6. /** @addtogroup Windows
  7. * @{
  8. */
  9. /// <summary>
  10. /// Displays in-game viewport in the editor.
  11. /// </summary>
  12. public class GameWindow : EditorWindow
  13. {
  14. private const int HeaderHeight = 20;
  15. private static readonly Color BG_COLOR = Color.VeryDarkGray;
  16. private readonly AspectRatio[] aspectRatios =
  17. {
  18. new AspectRatio(16, 9),
  19. new AspectRatio(16, 10),
  20. new AspectRatio(5, 4),
  21. new AspectRatio(4, 3),
  22. new AspectRatio(3, 2)
  23. };
  24. private int selectedAspectRatio = 0;
  25. private GUIRenderTexture renderTextureGUI;
  26. private GUITexture renderTextureBg;
  27. private GUILabel noCameraLabel;
  28. /// <summary>
  29. /// Opens the game window.
  30. /// </summary>
  31. [MenuItem("Windows/Game", ButtonModifier.CtrlAlt, ButtonCode.G, 6000)]
  32. private static void OpenGameWindow()
  33. {
  34. OpenWindow<GameWindow>();
  35. }
  36. /// <summary>
  37. /// Starts execution of the game in the game window.
  38. /// </summary>
  39. [MenuItem("Tools/Play", 9300)]
  40. [ToolbarItem("Play", ToolbarIcon.Play, "Play", 1800, true)]
  41. private static void Play()
  42. {
  43. GameWindow gameWindow = GetWindow<GameWindow>();
  44. SceneWindow sceneWindow = GetWindow<SceneWindow>();
  45. PlayInEditorState state = PlayInEditor.State;
  46. if (state != PlayInEditorState.Playing)
  47. {
  48. gameWindow.Active = true;
  49. gameWindow.HasFocus = true;
  50. }
  51. else
  52. {
  53. sceneWindow.Active = true;
  54. sceneWindow.HasFocus = true;
  55. }
  56. if (state == PlayInEditorState.Paused)
  57. PlayInEditor.State = PlayInEditorState.Playing;
  58. else
  59. {
  60. if(state == PlayInEditorState.Playing)
  61. PlayInEditor.State = PlayInEditorState.Stopped;
  62. else
  63. PlayInEditor.State = PlayInEditorState.Playing;
  64. }
  65. }
  66. /// <summary>
  67. /// Pauses the execution of the game on the current frame.
  68. /// </summary>
  69. [MenuItem("Tools/Pause", 9299)]
  70. [ToolbarItem("Pause", ToolbarIcon.Pause, "Pause", 1799)]
  71. private static void Pause()
  72. {
  73. if (PlayInEditor.State == PlayInEditorState.Paused)
  74. PlayInEditor.State = PlayInEditorState.Playing;
  75. else
  76. PlayInEditor.State = PlayInEditorState.Paused;
  77. }
  78. /// <summary>
  79. /// Moves the execution of the game by one frame forward.
  80. /// </summary>
  81. [MenuItem("Tools/Step", 9298)]
  82. [ToolbarItem("Step", ToolbarIcon.Step, "Frame step", 1798)]
  83. private static void Step()
  84. {
  85. PlayInEditor.FrameStep();
  86. }
  87. /// <inheritdoc/>
  88. protected override LocString GetDisplayName()
  89. {
  90. return new LocEdString("Game");
  91. }
  92. private void OnInitialize()
  93. {
  94. GUILayoutY mainLayout = GUI.AddLayoutY();
  95. string[] aspectRatioTitles = new string[aspectRatios.Length + 1];
  96. aspectRatioTitles[0] = "Free";
  97. for (int i = 0; i < aspectRatios.Length; i++)
  98. aspectRatioTitles[i + 1] = aspectRatios[i].width + ":" + aspectRatios[i].height;
  99. GUIListBoxField aspectField = new GUIListBoxField(aspectRatioTitles, new LocEdString("Aspect ratio"));
  100. aspectField.OnSelectionChanged += OnAspectRatioChanged;
  101. GUILayoutY buttonLayoutVert = mainLayout.AddLayoutY();
  102. GUILayoutX buttonLayout = buttonLayoutVert.AddLayoutX();
  103. buttonLayout.AddElement(aspectField);
  104. buttonLayout.AddFlexibleSpace();
  105. buttonLayoutVert.AddFlexibleSpace();
  106. renderTextureGUI = new GUIRenderTexture(null);
  107. renderTextureBg = new GUITexture(Builtin.WhiteTexture);
  108. renderTextureBg.SetTint(BG_COLOR);
  109. noCameraLabel = new GUILabel(new LocEdString("(No main camera in scene)"));
  110. GUIPanel rtPanel = mainLayout.AddPanel();
  111. rtPanel.AddElement(renderTextureGUI);
  112. GUIPanel bgPanel = rtPanel.AddPanel(1);
  113. bgPanel.AddElement(renderTextureBg);
  114. GUILayoutY alignLayoutY = rtPanel.AddLayoutY();
  115. alignLayoutY.AddFlexibleSpace();
  116. GUILayoutX alignLayoutX = alignLayoutY.AddLayoutX();
  117. alignLayoutX.AddFlexibleSpace();
  118. alignLayoutX.AddElement(noCameraLabel);
  119. alignLayoutX.AddFlexibleSpace();
  120. alignLayoutY.AddFlexibleSpace();
  121. UpdateRenderTexture(Width, Height);
  122. bool hasMainCamera = Scene.Camera != null;
  123. renderTextureGUI.Active = hasMainCamera;
  124. noCameraLabel.Active = !hasMainCamera;
  125. }
  126. private void OnEditorUpdate()
  127. {
  128. bool hasMainCamera = Scene.Camera != null;
  129. renderTextureGUI.Active = hasMainCamera;
  130. noCameraLabel.Active = !hasMainCamera;
  131. }
  132. private void OnDestroy()
  133. {
  134. EditorApplication.MainRenderTarget = null;
  135. }
  136. /// <summary>
  137. /// Notifies the system that the 3D viewport should be redrawn.
  138. /// </summary>
  139. internal void NotifyNeedsRedraw()
  140. {
  141. Camera camera = Scene.Camera;
  142. camera?.NotifyNeedsRedraw();
  143. }
  144. /// <summary>
  145. /// Enables or disables on-demand drawing. When enabled the 3D viewport will only be redrawn when
  146. /// <see cref="NotifyNeedsRedraw"/> is called. If disabled the viewport will be redrawn every frame.
  147. /// Normally you always want to keep this disabled unless you know the viewport will require updates
  148. /// every frame (e.g. when a game is running, or when previewing animations).
  149. /// </summary>
  150. /// <param name="enabled">True to enable on-demand drawing, false otherwise.</param>
  151. internal void ToggleOnDemandDrawing(bool enabled)
  152. {
  153. Camera camera = Scene.Camera;
  154. if (camera == null)
  155. return;
  156. if (enabled)
  157. camera.Flags = CameraFlag.OnDemand;
  158. else
  159. camera.Flags = new CameraFlag();
  160. }
  161. /// <summary>
  162. /// Creates or rebuilds the main render texture. Should be called at least once before using the
  163. /// game window. Should be called whenever the window is resized.
  164. /// </summary>
  165. /// <param name="width">Width of the scene render target, in pixels.</param>
  166. /// <param name="height">Height of the scene render target, in pixels.</param>
  167. private void UpdateRenderTexture(int width, int height)
  168. {
  169. height = height - HeaderHeight;
  170. int rtWidth = MathEx.Max(20, width);
  171. int rtHeight = MathEx.Max(20, height);
  172. if (selectedAspectRatio != 0) // 0 is free aspect
  173. {
  174. AspectRatio aspectRatio = aspectRatios[selectedAspectRatio - 1];
  175. int visibleAreaHeight = rtHeight;
  176. float aspectInv = aspectRatio.height/(float)aspectRatio.width;
  177. rtHeight = MathEx.RoundToInt(rtWidth*aspectInv);
  178. if (rtHeight > visibleAreaHeight)
  179. {
  180. rtHeight = visibleAreaHeight;
  181. float aspect = aspectRatio.width / (float)aspectRatio.height;
  182. rtWidth = MathEx.RoundToInt(rtHeight * aspect);
  183. }
  184. }
  185. RenderTexture renderTexture = new RenderTexture(PixelFormat.RGBA8, rtWidth, rtHeight) { Priority = 1};
  186. EditorApplication.MainRenderTarget = renderTexture;
  187. renderTextureGUI.RenderTexture = renderTexture;
  188. int offsetX = (width - rtWidth)/2;
  189. int offsetY = (height - rtHeight)/2;
  190. Rect2I rtBounds = new Rect2I(offsetX, offsetY, rtWidth, rtHeight);
  191. renderTextureGUI.Bounds = rtBounds;
  192. Rect2I bgBounds = new Rect2I(0, 0, width, height);
  193. renderTextureBg.Bounds = bgBounds;
  194. }
  195. /// <summary>
  196. /// Triggered when the user selects a new aspect ratio from the drop down box.
  197. /// </summary>
  198. /// <param name="idx">Index of the aspect ratio the user selected.</param>
  199. private void OnAspectRatioChanged(int idx)
  200. {
  201. selectedAspectRatio = idx;
  202. UpdateRenderTexture(Width, Height);
  203. }
  204. /// <inheritdoc/>
  205. protected override void WindowResized(int width, int height)
  206. {
  207. UpdateRenderTexture(width, height);
  208. base.WindowResized(width, height);
  209. }
  210. /// <summary>
  211. /// Camera aspect ratio as numerator and denominator.
  212. /// </summary>
  213. struct AspectRatio
  214. {
  215. /// <summary>
  216. /// Creates a new object that holds the aspect ratio.
  217. /// </summary>
  218. /// <param name="width">Numerator of the aspect ratio.</param>
  219. /// <param name="height">Denominator of the aspect ratio.</param>
  220. public AspectRatio(int width, int height)
  221. {
  222. this.width = width;
  223. this.height = height;
  224. }
  225. public int width;
  226. public int height;
  227. }
  228. }
  229. /** @} */
  230. }