BuildWindow.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. /// <summary>
  6. /// Provides options for customizing and activating the build process which will output an executable of the game for a
  7. /// specific platform, as well as any required resources.
  8. /// </summary>
  9. [DefaultSize(300, 200)]
  10. internal sealed class BuildWindow : EditorWindow
  11. {
  12. private PlatformType selectedPlatform;
  13. private GUIScrollArea optionsScrollArea;
  14. private bool buildScheduled;
  15. /// <summary>
  16. /// Opens the build window if its not open already.
  17. /// </summary>
  18. [MenuItem("Tools/Build", 9296)]
  19. private static void OpenSettingsWindow()
  20. {
  21. OpenWindow<SettingsWindow>();
  22. }
  23. /// <inheritdoc/>
  24. protected override LocString GetDisplayName()
  25. {
  26. return new LocEdString("Build");
  27. }
  28. private void OnInitialize()
  29. {
  30. GUILayoutX splitLayout = GUI.AddLayoutX();
  31. GUILayoutY platformLayout = splitLayout.AddLayoutY();
  32. GUILayoutY optionsLayout = splitLayout.AddLayoutY();
  33. GUIToggleGroup platformToggleGroup = new GUIToggleGroup();
  34. PlatformType[] availablePlatforms = BuildManager.AvailablePlatforms;
  35. for (int i = 0; i < availablePlatforms.Length; i++)
  36. {
  37. PlatformType currentPlatform = availablePlatforms[i];
  38. string platformName = Enum.GetName(typeof(PlatformType), currentPlatform);
  39. GUIToggle platformToggle = new GUIToggle(new LocEdString(platformName), platformToggleGroup, EditorStyles.Button);
  40. platformToggle.OnToggled += x => OnSelectedPlatformChanged(currentPlatform, x);
  41. platformLayout.AddElement(platformToggle);
  42. if (currentPlatform == BuildManager.ActivePlatform)
  43. {
  44. platformToggle.Value = true;
  45. selectedPlatform = currentPlatform;
  46. }
  47. }
  48. platformLayout.AddFlexibleSpace();
  49. GUIButton changePlatformBtn = new GUIButton(new LocEdString("Set active"));
  50. platformLayout.AddElement(changePlatformBtn);
  51. changePlatformBtn.OnClick += ChangeActivePlatform;
  52. optionsScrollArea = new GUIScrollArea();
  53. optionsLayout.AddElement(optionsScrollArea);
  54. BuildPlatformOptionsGUI();
  55. }
  56. private void OnEditorUpdate()
  57. {
  58. if (buildScheduled)
  59. {
  60. BuildManager.Build();
  61. ProgressBar.Hide();
  62. buildScheduled = false;
  63. }
  64. }
  65. /// <summary>
  66. /// Changes the currently selected platform. Be aware that while platform is selected and you may build for it,
  67. /// it will not be made the active platform.
  68. /// </summary>
  69. /// <param name="type">Platform that was selected or deselected.</param>
  70. /// <param name="selected">True if the platform was selected, false otherwise.</param>
  71. private void OnSelectedPlatformChanged(PlatformType type, bool selected)
  72. {
  73. if (selected)
  74. {
  75. selectedPlatform = type;
  76. BuildPlatformOptionsGUI();
  77. }
  78. }
  79. /// <summary>
  80. /// Changes the currently active build platform.
  81. /// </summary>
  82. private void ChangeActivePlatform()
  83. {
  84. BuildManager.ActivePlatform = selectedPlatform;
  85. }
  86. /// <summary>
  87. /// (Re)creates GUI with platform-specific options.
  88. /// </summary>
  89. private void BuildPlatformOptionsGUI()
  90. {
  91. optionsScrollArea.Layout.Clear();
  92. GUILayout layout = optionsScrollArea.Layout;
  93. PlatformInfo platformInfo = BuildManager.GetPlatformInfo(selectedPlatform);
  94. GUIResourceField sceneField = new GUIResourceField(typeof(Prefab), new LocEdString("Startup scene"));
  95. GUIToggleField debugToggle = new GUIToggleField(new LocEdString("Debug"));
  96. GUIToggleField fullscreenField = new GUIToggleField(new LocEdString("Fullscreen"));
  97. GUIIntField widthField = new GUIIntField(new LocEdString("Window width"));
  98. GUIIntField heightField = new GUIIntField(new LocEdString("Window height"));
  99. GUITextField definesField = new GUITextField(new LocEdString("Defines"));
  100. layout.AddSpace(5);
  101. layout.AddElement(sceneField);
  102. layout.AddElement(debugToggle);
  103. layout.AddElement(fullscreenField);
  104. layout.AddElement(widthField);
  105. layout.AddElement(heightField);
  106. layout.AddSpace(5);
  107. layout.AddElement(definesField);
  108. layout.AddSpace(5);
  109. sceneField.Value = platformInfo.MainScene;
  110. debugToggle.Value = platformInfo.Debug;
  111. definesField.Value = platformInfo.Defines;
  112. fullscreenField.Value = platformInfo.Fullscreen;
  113. widthField.Value = platformInfo.WindowedWidth;
  114. heightField.Value = platformInfo.WindowedHeight;
  115. if (platformInfo.Fullscreen)
  116. {
  117. widthField.Active = false;
  118. heightField.Active = false;
  119. }
  120. sceneField.OnChanged += x => platformInfo.MainScene = (Prefab)x;
  121. debugToggle.OnChanged += x => platformInfo.Debug = x;
  122. definesField.OnChanged += x => platformInfo.Defines = x;
  123. fullscreenField.OnChanged += x =>
  124. {
  125. widthField.Active = !x;
  126. heightField.Active = !x;
  127. platformInfo.Fullscreen = x;
  128. };
  129. widthField.OnChanged += x => platformInfo.WindowedWidth = x;
  130. heightField.OnChanged += x => platformInfo.WindowedHeight = x;
  131. switch (platformInfo.Type)
  132. {
  133. case PlatformType.Windows:
  134. {
  135. WinPlatformInfo winPlatformInfo = (WinPlatformInfo) platformInfo;
  136. GUITextField titleField = new GUITextField(new LocEdString("Title"));
  137. GUIToggle iconsFoldout = new GUIToggle(new LocEdString("Icons"), EditorStyles.Foldout);
  138. layout.AddElement(titleField);
  139. layout.AddElement(iconsFoldout);
  140. GUILayoutY iconsLayout = layout.AddLayoutY();
  141. int[] iconSizes = (int[]) Enum.GetValues(typeof (WinIconSizes));
  142. GUITextureField[] iconFields = new GUITextureField[iconSizes.Length];
  143. for (int i = 0; i < iconSizes.Length; i++)
  144. {
  145. int size = iconSizes[i];
  146. iconFields[i] = new GUITextureField(new LocEdString("Icon (" + size + "x" + size + ")"));
  147. iconFields[i].Value = winPlatformInfo.GetIcon((WinIconSizes) size);
  148. iconFields[i].OnChanged += x => winPlatformInfo.SetIcon((WinIconSizes) size, x as Texture2D);
  149. iconsLayout.AddElement(iconFields[i]);
  150. }
  151. GUITextureField taskbarIconField = new GUITextureField(new LocEdString("Taskbar icon (32x32)"));
  152. iconsLayout.AddElement(taskbarIconField);
  153. titleField.Value = winPlatformInfo.TitleText;
  154. taskbarIconField.Value = winPlatformInfo.TaskbarIcon;
  155. titleField.OnChanged += x => winPlatformInfo.TitleText = x;
  156. iconsFoldout.OnToggled += x => iconsLayout.Active = x;
  157. taskbarIconField.OnChanged += x => winPlatformInfo.TaskbarIcon = x as Texture2D;
  158. iconsLayout.Active = false;
  159. }
  160. break;
  161. }
  162. GUIButton buildButton = new GUIButton(new LocEdString("Build"));
  163. layout.AddFlexibleSpace();
  164. layout.AddElement(buildButton);
  165. buildButton.OnClick += TryStartBuild;
  166. // TODO - Different background for platform selection bit
  167. }
  168. /// <summary>
  169. /// Starts the build process for the currently selected platform.
  170. /// </summary>
  171. private void Build()
  172. {
  173. ProgressBar.Show(new LocEdString("Building..."), 0.0f);
  174. EditorApplication.SaveProject();
  175. // HACK - Delay build one frame so that progress bar has a chance to show. Use coroutines here once implemented.
  176. buildScheduled = true;
  177. }
  178. /// <summary>
  179. /// Attempts to save the current scene, and keeps retrying if failed or until user cancels.
  180. /// </summary>
  181. private void TrySaveScene()
  182. {
  183. EditorApplication.SaveScene(Build, TrySaveScene);
  184. }
  185. /// <summary>
  186. /// Attempts to start the build process if user confirms.
  187. /// </summary>
  188. private void TryStartBuild()
  189. {
  190. Action<DialogBox.ResultType> dialogCallback =
  191. (result) =>
  192. {
  193. if (result == DialogBox.ResultType.Yes)
  194. TrySaveScene();
  195. else if (result == DialogBox.ResultType.No)
  196. {
  197. EditorApplication.SaveProject();
  198. EditorApplication.Quit();
  199. }
  200. };
  201. if (EditorApplication.IsSceneModified())
  202. {
  203. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  204. DialogBox.Type.YesNoCancel, dialogCallback);
  205. }
  206. else
  207. {
  208. Build();
  209. }
  210. }
  211. }
  212. }