ProjectWindow.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. {
  153. projectInputBox.Value = selectedPath;
  154. OpenProject();
  155. }
  156. }
  157. /// <summary>
  158. /// Triggered when the user clicks on the create buttons. Opens a browse dialog that allows the user to select
  159. /// a folder to a new project to create. Project data will be initialized in the chosen folder and new project
  160. /// will be opened.
  161. /// </summary>
  162. void CreateClicked()
  163. {
  164. string projectPath = projectInputBox.Value;
  165. if (!Directory.Exists(projectPath))
  166. projectPath = Directory.GetCurrentDirectory();
  167. string selectedPath;
  168. if (BrowseDialog.OpenFolder(projectPath, out selectedPath))
  169. {
  170. projectInputBox.Value = selectedPath;
  171. EditorApplication.CreateProject(selectedPath);
  172. OpenProject();
  173. }
  174. }
  175. /// <summary>
  176. /// Triggered when the user clicks the cancel button.
  177. /// </summary>
  178. void CancelClicked()
  179. {
  180. if (EditorApplication.IsProjectLoaded)
  181. Close(); // Just close the window
  182. else
  183. EditorApplication.Quit(); // Close the application, we cannot do anything without a project
  184. }
  185. /// <summary>
  186. /// Updates GUI for the recent projects list.
  187. /// </summary>
  188. private void RefreshRecentProjects()
  189. {
  190. GUILayout scrollLayout = recentProjectsArea.Layout;
  191. while (scrollLayout.ChildCount > 0)
  192. scrollLayout.GetChild(0).Destroy();
  193. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  194. Array.Sort(recentProjects, (a, b) => a.accessTimestamp.CompareTo(b.accessTimestamp));
  195. GUIToggleGroup grp = new GUIToggleGroup();
  196. for (int i = 0; i < recentProjects.Length; i++)
  197. {
  198. string projectPath = recentProjects[i].path;
  199. GUIToggle entryBtn = new GUIToggle(projectPath, grp, EditorStyles.SelectableLabel);
  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. }