ProjectWindow.cs 9.7 KB

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