ProjectWindow.cs 10 KB

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