ProjectWindow.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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(true)
  31. {
  32. }
  33. private void OnInitialize()
  34. {
  35. Title = "Project Manager";
  36. Width = 500;
  37. Height = 300;
  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 + 5 + 75);
  68. recentProjectsArea = new GUIScrollArea(GUIOption.FixedWidth(385), GUIOption.FixedHeight(170));
  69. GUILayoutX recentProjectsLayout = recentProjectsPanel.AddLayoutX();
  70. recentProjectsLayout.AddSpace(10);
  71. GUILayoutY recentProjectsPanelY = recentProjectsLayout.AddLayoutY();
  72. recentProjectsPanelY.AddSpace(5);
  73. recentProjectsPanelY.AddElement(recentProjectsArea);
  74. recentProjectsPanelY.AddSpace(5);
  75. recentProjectsLayout.AddFlexibleSpace();
  76. GUIPanel scrollAreaBgPanel = recentProjectsPanel.AddPanel(1);
  77. GUITexture scrollAreaBgTex = new GUITexture(null, true, EditorStyles.ScrollAreaBg);
  78. scrollAreaBgPanel.AddElement(scrollAreaBgTex);
  79. autoLoadToggle = new GUIToggle("");
  80. autoLoadToggle.Value = EditorSettings.AutoLoadLastProject;
  81. GUILabel autoLoadLabel = new GUILabel(new LocEdString("Automatically load last open project"));
  82. GUIButton createBtn = new GUIButton(new LocEdString("Create new"), GUIOption.FixedWidth(75));
  83. createBtn.OnClick += CreateClicked;
  84. fourthRow.AddSpace(5);
  85. fourthRow.AddElement(autoLoadToggle);
  86. fourthRow.AddElement(autoLoadLabel);
  87. fourthRow.AddFlexibleSpace();
  88. fourthRow.AddElement(createBtn);
  89. fourthRow.AddSpace(5);
  90. RefreshRecentProjects();
  91. }
  92. /// <summary>
  93. /// Attempts to open a project at the path currently entered in the project path input box.
  94. /// </summary>
  95. private void OpenProject()
  96. {
  97. string projectPath = projectInputBox.Value;
  98. if (EditorApplication.IsValidProject(projectPath))
  99. {
  100. EditorSettings.AutoLoadLastProject = autoLoadToggle.Value;
  101. Close();
  102. EditorApplication.LoadProject(projectPath);
  103. }
  104. else
  105. {
  106. // Remove invalid project from recent projects list
  107. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  108. for (int i = 0; i < recentProjects.Length; i++)
  109. {
  110. if (PathEx.Compare(recentProjects[i].path, projectPath))
  111. {
  112. RecentProject[] newRecentProjects = new RecentProject[recentProjects.Length - 1];
  113. int idx = 0;
  114. for (int j = 0; j < recentProjects.Length; j++)
  115. {
  116. if (i == j)
  117. continue;
  118. newRecentProjects[idx] = recentProjects[j];
  119. idx++;
  120. }
  121. EditorSettings.RecentProjects = newRecentProjects;
  122. EditorSettings.Save();
  123. RefreshRecentProjects();
  124. break;
  125. }
  126. }
  127. // Warn user
  128. LocString message = new LocEdString("Provided project path \"{0}\" doesn't contain a valid project.");
  129. message.SetParameter(0, projectPath);
  130. DialogBox.Open(new LocEdString("Error"), message, DialogBox.Type.OK);
  131. }
  132. }
  133. /// <summary>
  134. /// Triggered when the user clicks on the browse button. Opens a browse dialog that allows the user to select
  135. /// a project folder anywhere on the file system.
  136. /// </summary>
  137. void BrowseClicked()
  138. {
  139. string projectPath = projectInputBox.Value;
  140. if (!Directory.Exists(projectPath))
  141. projectPath = Directory.GetCurrentDirectory();
  142. string selectedPath;
  143. if (BrowseDialog.OpenFolder(projectPath, out selectedPath))
  144. projectInputBox.Value = selectedPath;
  145. }
  146. /// <summary>
  147. /// Triggered when the user clicks on the create buttons. Opens a browse dialog that allows the user to select
  148. /// a folde to a new project to create. Project data will be initialized in the chosen folder and new project
  149. /// will be opened.
  150. /// </summary>
  151. void CreateClicked()
  152. {
  153. string projectPath = projectInputBox.Value;
  154. if (!Directory.Exists(projectPath))
  155. projectPath = Directory.GetCurrentDirectory();
  156. string selectedPath;
  157. if (BrowseDialog.OpenFolder(projectPath, out selectedPath))
  158. {
  159. projectInputBox.Value = selectedPath;
  160. EditorApplication.CreateProject(selectedPath);
  161. OpenProject();
  162. }
  163. }
  164. /// <summary>
  165. /// Updates GUI for the recent projects list.
  166. /// </summary>
  167. private void RefreshRecentProjects()
  168. {
  169. GUILayout scrollLayout = recentProjectsArea.Layout;
  170. while(scrollLayout.ChildCount > 0)
  171. scrollLayout.GetChild(0).Destroy();
  172. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  173. Array.Sort(recentProjects, (a, b) => a.accessTimestamp.CompareTo(b.accessTimestamp));
  174. GUIToggleGroup grp = new GUIToggleGroup();
  175. for (int i = 0; i < recentProjects.Length; i++)
  176. {
  177. string projectPath = recentProjects[i].path;
  178. GUIToggle entryBtn = new GUIToggle(projectPath, grp, EditorStyles.SelectableLabel);
  179. entryBtn.OnClick += () => OnEntryClicked(projectPath);
  180. entryBtn.OnDoubleClick += () => OnEntryDoubleClicked(projectPath);
  181. scrollLayout.AddElement(entryBtn);
  182. }
  183. }
  184. /// <summary>
  185. /// Triggered when an entry in the recent projects list was clicked.
  186. /// </summary>
  187. /// <param name="path">Absolute path to the project folder that was selected.</param>
  188. void OnEntryClicked(string path)
  189. {
  190. projectInputBox.Value = path;
  191. }
  192. /// <summary>
  193. /// Triggered when an entry in the recent projects list was double clicked. Opens the project at the selected path.
  194. /// </summary>
  195. /// <param name="path">Absolute path to the project folder that was selected.</param>
  196. void OnEntryDoubleClicked(string path)
  197. {
  198. projectInputBox.Value = path;
  199. OpenProject();
  200. }
  201. }
  202. }