ProjectWindow.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.IO;
  5. using bs;
  6. namespace bs.Editor
  7. {
  8. /** @addtogroup Windows
  9. * @{
  10. */
  11. /// <summary>
  12. /// Project management window that allows the user to open and create new projects.
  13. /// </summary>
  14. internal sealed class ProjectWindow : ModalWindow
  15. {
  16. private GUITextField projectInputBox;
  17. private GUIScrollArea recentProjectsArea;
  18. private GUIToggle autoLoadToggle;
  19. /// <summary>
  20. /// Opens the project management window.
  21. /// </summary>
  22. /// <returns>Instance of the project window</returns>
  23. public static ProjectWindow Open()
  24. {
  25. return new ProjectWindow();
  26. }
  27. /// <summary>
  28. /// Constructs a new project window.
  29. /// </summary>
  30. private ProjectWindow()
  31. : base(false)
  32. {
  33. }
  34. private void OnInitialize()
  35. {
  36. Title = "Project Manager";
  37. Width = 500;
  38. Height = 290;
  39. GUILayout vertLayout = GUI.AddLayoutY();
  40. vertLayout.AddSpace(5);
  41. GUILayout firstRow = vertLayout.AddLayoutX();
  42. vertLayout.AddFlexibleSpace();
  43. GUILayout secondRow = vertLayout.AddLayoutX();
  44. vertLayout.AddSpace(5);
  45. GUILayout thirdRow = vertLayout.AddLayoutX();
  46. vertLayout.AddFlexibleSpace();
  47. GUILayout fourthRow = vertLayout.AddLayoutX();
  48. vertLayout.AddSpace(5);
  49. projectInputBox = new GUITextField(new LocEdString("Project path"), 70, false, "", GUIOption.FixedWidth(398));
  50. projectInputBox.Value = EditorSettings.LastOpenProject;
  51. GUIButton openBtn = new GUIButton(new LocEdString("Open"), GUIOption.FixedWidth(75));
  52. openBtn.OnClick += OpenProject;
  53. firstRow.AddSpace(5);
  54. firstRow.AddElement(projectInputBox);
  55. firstRow.AddSpace(15);
  56. firstRow.AddElement(openBtn);
  57. firstRow.AddSpace(5);
  58. GUILabel recentProjectsLabel = new GUILabel(new LocEdString("Recent projects:"));
  59. secondRow.AddSpace(5);
  60. secondRow.AddElement(recentProjectsLabel);
  61. secondRow.AddFlexibleSpace();
  62. GUIButton browseBtn = new GUIButton(new LocEdString("Browse"), GUIOption.FixedWidth(75));
  63. browseBtn.OnClick += BrowseClicked;
  64. secondRow.AddElement(browseBtn);
  65. secondRow.AddSpace(5);
  66. thirdRow.AddSpace(5);
  67. GUIPanel recentProjectsPanel = thirdRow.AddPanel();
  68. thirdRow.AddSpace(15);
  69. GUILayoutY thirdRowVertical = thirdRow.AddLayoutY();
  70. GUIButton createBtn = new GUIButton(new LocEdString("Create new"), GUIOption.FixedWidth(75));
  71. createBtn.OnClick += CreateClicked;
  72. thirdRowVertical.AddElement(createBtn);
  73. thirdRowVertical.AddFlexibleSpace();
  74. thirdRow.AddSpace(5);
  75. recentProjectsArea = new GUIScrollArea(GUIOption.FixedWidth(385), GUIOption.FixedHeight(170));
  76. GUILayoutX recentProjectsLayout = recentProjectsPanel.AddLayoutX();
  77. recentProjectsLayout.AddSpace(10);
  78. GUILayoutY recentProjectsPanelY = recentProjectsLayout.AddLayoutY();
  79. recentProjectsPanelY.AddSpace(5);
  80. recentProjectsPanelY.AddElement(recentProjectsArea);
  81. recentProjectsPanelY.AddSpace(5);
  82. recentProjectsLayout.AddFlexibleSpace();
  83. GUIPanel scrollAreaBgPanel = recentProjectsPanel.AddPanel(1);
  84. GUITexture scrollAreaBgTex = new GUITexture(null, true, EditorStylesInternal.ScrollAreaBg);
  85. scrollAreaBgPanel.AddElement(scrollAreaBgTex);
  86. autoLoadToggle = new GUIToggle("");
  87. autoLoadToggle.Value = EditorSettings.AutoLoadLastProject;
  88. GUILabel autoLoadLabel = new GUILabel(new LocEdString("Automatically load last open project"));
  89. GUIButton cancelBtn = new GUIButton(new LocEdString("Cancel"), GUIOption.FixedWidth(75));
  90. cancelBtn.OnClick += CancelClicked;
  91. fourthRow.AddSpace(5);
  92. fourthRow.AddElement(autoLoadToggle);
  93. fourthRow.AddElement(autoLoadLabel);
  94. fourthRow.AddFlexibleSpace();
  95. fourthRow.AddElement(cancelBtn);
  96. fourthRow.AddSpace(5);
  97. RefreshRecentProjects();
  98. }
  99. /// <summary>
  100. /// Attempts to open a project at the path currently entered in the project path input box.
  101. /// </summary>
  102. private void OpenProject()
  103. {
  104. string projectPath = projectInputBox.Value;
  105. if (EditorApplication.IsValidProject(projectPath))
  106. {
  107. EditorSettings.AutoLoadLastProject = autoLoadToggle.Value;
  108. Close();
  109. EditorApplication.LoadProject(projectPath);
  110. }
  111. else
  112. {
  113. // Remove invalid project from recent projects list
  114. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  115. for (int i = 0; i < recentProjects.Length; i++)
  116. {
  117. if (PathEx.Compare(recentProjects[i].path, projectPath))
  118. {
  119. RecentProject[] newRecentProjects = new RecentProject[recentProjects.Length - 1];
  120. int idx = 0;
  121. for (int j = 0; j < recentProjects.Length; j++)
  122. {
  123. if (i == j)
  124. continue;
  125. newRecentProjects[idx] = recentProjects[j];
  126. idx++;
  127. }
  128. EditorSettings.RecentProjects = newRecentProjects;
  129. EditorSettings.Save();
  130. RefreshRecentProjects();
  131. break;
  132. }
  133. }
  134. // Warn user
  135. LocString message = new LocEdString("Provided project path \"{0}\" doesn't contain a valid project.");
  136. message.SetParameter(0, projectPath);
  137. DialogBox.Open(new LocEdString("Error"), message, DialogBox.Type.OK);
  138. }
  139. }
  140. /// <summary>
  141. /// Triggered when the user clicks on the browse button. Opens a browse dialog that allows the user to select
  142. /// a project folder anywhere on the file system.
  143. /// </summary>
  144. void BrowseClicked()
  145. {
  146. string projectPath = projectInputBox.Value;
  147. if (!Directory.Exists(projectPath))
  148. projectPath = Directory.GetCurrentDirectory();
  149. string selectedPath;
  150. if (BrowseDialog.OpenFolder(projectPath, out selectedPath))
  151. {
  152. projectInputBox.Value = selectedPath;
  153. OpenProject();
  154. }
  155. }
  156. /// <summary>
  157. /// Triggered when the user clicks on the create buttons. Opens a browse dialog that allows the user to select
  158. /// a folder to a new project to create. Project data will be initialized in the chosen folder and new project
  159. /// will be opened.
  160. /// </summary>
  161. void CreateClicked()
  162. {
  163. string projectPath = projectInputBox.Value;
  164. if (!Directory.Exists(projectPath))
  165. projectPath = Directory.GetCurrentDirectory();
  166. string selectedPath;
  167. if (BrowseDialog.OpenFolder(projectPath, out selectedPath))
  168. {
  169. projectInputBox.Value = selectedPath;
  170. EditorApplication.CreateProject(selectedPath);
  171. OpenProject();
  172. }
  173. }
  174. /// <summary>
  175. /// Triggered when the user clicks the cancel button.
  176. /// </summary>
  177. void CancelClicked()
  178. {
  179. if (EditorApplication.IsProjectLoaded)
  180. Close(); // Just close the window
  181. else
  182. EditorApplication.Quit(); // Close the application, we cannot do anything without a project
  183. }
  184. /// <summary>
  185. /// Updates GUI for the recent projects list.
  186. /// </summary>
  187. private void RefreshRecentProjects()
  188. {
  189. GUILayout scrollLayout = recentProjectsArea.Layout;
  190. while (scrollLayout.ChildCount > 0)
  191. scrollLayout.GetChild(0).Destroy();
  192. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  193. Array.Sort(recentProjects, (a, b) => a.accessTimestamp.CompareTo(b.accessTimestamp));
  194. GUIToggleGroup grp = new GUIToggleGroup();
  195. for (int i = 0; i < recentProjects.Length; i++)
  196. {
  197. string projectPath = recentProjects[i].path;
  198. GUIToggle entryBtn = new GUIToggle(projectPath, grp, EditorStylesInternal.SelectableLabel);
  199. entryBtn.AcceptsKeyFocus = false;
  200. entryBtn.OnClick += () => OnEntryClicked(projectPath);
  201. entryBtn.OnDoubleClick += () => OnEntryDoubleClicked(projectPath);
  202. if (PathEx.Compare(projectPath, projectInputBox.Value))
  203. entryBtn.Value = true;
  204. scrollLayout.AddElement(entryBtn);
  205. }
  206. }
  207. /// <summary>
  208. /// Triggered when an entry in the recent projects list was clicked.
  209. /// </summary>
  210. /// <param name="path">Absolute path to the project folder that was selected.</param>
  211. void OnEntryClicked(string path)
  212. {
  213. projectInputBox.Value = path;
  214. }
  215. /// <summary>
  216. /// Triggered when an entry in the recent projects list was double clicked. Opens the project at the selected path.
  217. /// </summary>
  218. /// <param name="path">Absolute path to the project folder that was selected.</param>
  219. void OnEntryDoubleClicked(string path)
  220. {
  221. projectInputBox.Value = path;
  222. OpenProject();
  223. }
  224. }
  225. /** @} */
  226. }