GameWindow.cs 8.2 KB

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