2
0

GameWindow.cs 7.7 KB

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