GameWindow.cs 8.4 KB

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