ProjectWindow.cs 10 KB

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