GameWindow.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. if (!EditorApplication.IsPlaying)
  46. {
  47. gameWindow.Active = true;
  48. gameWindow.HasFocus = true;
  49. }
  50. else
  51. {
  52. sceneWindow.Active = true;
  53. sceneWindow.HasFocus = true;
  54. }
  55. if (EditorApplication.IsPaused)
  56. EditorApplication.IsPaused = false;
  57. else
  58. EditorApplication.IsPlaying = !EditorApplication.IsPlaying;
  59. }
  60. /// <summary>
  61. /// Pauses the execution of the game on the current frame.
  62. /// </summary>
  63. [MenuItem("Tools/Pause", 9299)]
  64. [ToolbarItem("Pause", ToolbarIcon.Pause, "Pause", 1799)]
  65. private static void Pause()
  66. {
  67. EditorApplication.IsPaused = !EditorApplication.IsPaused;
  68. }
  69. /// <summary>
  70. /// Moves the execution of the game by one frame forward.
  71. /// </summary>
  72. [MenuItem("Tools/Step", 9298)]
  73. [ToolbarItem("Step", ToolbarIcon.Step, "Frame step", 1798)]
  74. private static void Step()
  75. {
  76. EditorApplication.FrameStep();
  77. }
  78. /// <inheritdoc/>
  79. protected override LocString GetDisplayName()
  80. {
  81. return new LocEdString("Game");
  82. }
  83. private void OnInitialize()
  84. {
  85. GUILayoutY mainLayout = GUI.AddLayoutY();
  86. string[] aspectRatioTitles = new string[aspectRatios.Length + 1];
  87. aspectRatioTitles[0] = "Free";
  88. for (int i = 0; i < aspectRatios.Length; i++)
  89. aspectRatioTitles[i + 1] = aspectRatios[i].width + ":" + aspectRatios[i].height;
  90. GUIListBoxField aspectField = new GUIListBoxField(aspectRatioTitles, new LocEdString("Aspect ratio"));
  91. aspectField.OnSelectionChanged += OnAspectRatioChanged;
  92. GUILayoutY buttonLayoutVert = mainLayout.AddLayoutY();
  93. GUILayoutX buttonLayout = buttonLayoutVert.AddLayoutX();
  94. buttonLayout.AddElement(aspectField);
  95. buttonLayout.AddFlexibleSpace();
  96. buttonLayoutVert.AddFlexibleSpace();
  97. renderTextureGUI = new GUIRenderTexture(null);
  98. renderTextureBg = new GUITexture(Builtin.WhiteTexture);
  99. renderTextureBg.SetTint(BG_COLOR);
  100. noCameraLabel = new GUILabel(new LocEdString("(No main camera in scene)"));
  101. GUIPanel rtPanel = mainLayout.AddPanel();
  102. rtPanel.AddElement(renderTextureGUI);
  103. GUIPanel bgPanel = rtPanel.AddPanel(1);
  104. bgPanel.AddElement(renderTextureBg);
  105. GUILayoutY alignLayoutY = rtPanel.AddLayoutY();
  106. alignLayoutY.AddFlexibleSpace();
  107. GUILayoutX alignLayoutX = alignLayoutY.AddLayoutX();
  108. alignLayoutX.AddFlexibleSpace();
  109. alignLayoutX.AddElement(noCameraLabel);
  110. alignLayoutX.AddFlexibleSpace();
  111. alignLayoutY.AddFlexibleSpace();
  112. UpdateRenderTexture(Width, Height);
  113. bool hasMainCamera = Scene.Camera != null;
  114. renderTextureGUI.Active = hasMainCamera;
  115. noCameraLabel.Active = !hasMainCamera;
  116. }
  117. private void OnEditorUpdate()
  118. {
  119. bool hasMainCamera = Scene.Camera != null;
  120. renderTextureGUI.Active = hasMainCamera;
  121. noCameraLabel.Active = !hasMainCamera;
  122. }
  123. private void OnDestroy()
  124. {
  125. EditorApplication.MainRenderTarget = null;
  126. }
  127. /// <summary>
  128. /// Creates or rebuilds the main render texture. Should be called at least once before using the
  129. /// game window. Should be called whenever the window is resized.
  130. /// </summary>
  131. /// <param name="width">Width of the scene render target, in pixels.</param>
  132. /// <param name="height">Height of the scene render target, in pixels.</param>
  133. private void UpdateRenderTexture(int width, int height)
  134. {
  135. height = height - HeaderHeight;
  136. int rtWidth = MathEx.Max(20, width);
  137. int rtHeight = MathEx.Max(20, height);
  138. if (selectedAspectRatio != 0) // 0 is free aspect
  139. {
  140. AspectRatio aspectRatio = aspectRatios[selectedAspectRatio - 1];
  141. int visibleAreaHeight = rtHeight;
  142. float aspectInv = aspectRatio.height/(float)aspectRatio.width;
  143. rtHeight = MathEx.RoundToInt(rtWidth*aspectInv);
  144. if (rtHeight > visibleAreaHeight)
  145. {
  146. rtHeight = visibleAreaHeight;
  147. float aspect = aspectRatio.width / (float)aspectRatio.height;
  148. rtWidth = MathEx.RoundToInt(rtHeight * aspect);
  149. }
  150. }
  151. RenderTexture renderTexture = new RenderTexture(PixelFormat.RGBA8, rtWidth, rtHeight) { Priority = 1};
  152. EditorApplication.MainRenderTarget = renderTexture;
  153. renderTextureGUI.RenderTexture = renderTexture;
  154. int offsetX = (width - rtWidth)/2;
  155. int offsetY = (height - rtHeight)/2;
  156. Rect2I rtBounds = new Rect2I(offsetX, offsetY, rtWidth, rtHeight);
  157. renderTextureGUI.Bounds = rtBounds;
  158. Rect2I bgBounds = new Rect2I(0, 0, width, height);
  159. renderTextureBg.Bounds = bgBounds;
  160. }
  161. /// <summary>
  162. /// Triggered when the user selects a new aspect ratio from the drop down box.
  163. /// </summary>
  164. /// <param name="idx">Index of the aspect ratio the user selected.</param>
  165. private void OnAspectRatioChanged(int idx)
  166. {
  167. selectedAspectRatio = idx;
  168. UpdateRenderTexture(Width, Height);
  169. }
  170. /// <inheritdoc/>
  171. protected override void WindowResized(int width, int height)
  172. {
  173. UpdateRenderTexture(width, height);
  174. base.WindowResized(width, height);
  175. }
  176. /// <summary>
  177. /// Camera aspect ratio as numerator and denominator.
  178. /// </summary>
  179. struct AspectRatio
  180. {
  181. /// <summary>
  182. /// Creates a new object that holds the aspect ratio.
  183. /// </summary>
  184. /// <param name="width">Numerator of the aspect ratio.</param>
  185. /// <param name="height">Denominator of the aspect ratio.</param>
  186. public AspectRatio(int width, int height)
  187. {
  188. this.width = width;
  189. this.height = height;
  190. }
  191. public int width;
  192. public int height;
  193. }
  194. }
  195. /** @} */
  196. }