ProjectWindow.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. public class ProjectWindow : ModalWindow
  11. {
  12. private GUITextField projectInputBox;
  13. private GUIScrollArea recentProjectsArea;
  14. private GUIToggle autoLoadToggle;
  15. public static ProjectWindow Open()
  16. {
  17. return new ProjectWindow();
  18. }
  19. protected ProjectWindow()
  20. : base(true)
  21. {
  22. }
  23. private void OnInitialize()
  24. {
  25. Title = "Project Manager";
  26. Width = 500;
  27. Height = 250;
  28. GUILayout vertLayout = GUI.AddLayoutY();
  29. vertLayout.AddSpace(5);
  30. GUILayout firstRow = vertLayout.AddLayoutX();
  31. vertLayout.AddFlexibleSpace();
  32. GUILayout secondRow = vertLayout.AddLayoutX();
  33. vertLayout.AddSpace(5);
  34. GUILayout thirdRow = vertLayout.AddLayoutX();
  35. vertLayout.AddFlexibleSpace();
  36. GUILayout fourthRow = vertLayout.AddLayoutX();
  37. vertLayout.AddSpace(5);
  38. projectInputBox = new GUITextField(new LocEdString("Project path"), 70, false, "", GUIOption.FixedWidth(398));
  39. projectInputBox.Value = EditorSettings.LastOpenProject;
  40. GUIButton openBtn = new GUIButton(new LocEdString("Open"), GUIOption.FixedWidth(75));
  41. openBtn.OnClick += OpenProject;
  42. firstRow.AddSpace(5);
  43. firstRow.AddElement(projectInputBox);
  44. firstRow.AddSpace(15);
  45. firstRow.AddElement(openBtn);
  46. firstRow.AddSpace(5);
  47. GUILabel recentProjectsLabel = new GUILabel(new LocEdString("Recent projects:"));
  48. secondRow.AddSpace(5);
  49. secondRow.AddElement(recentProjectsLabel);
  50. secondRow.AddFlexibleSpace();
  51. GUIButton browseBtn = new GUIButton(new LocEdString("Browse"), GUIOption.FixedWidth(75));
  52. browseBtn.OnClick += BrowseClicked;
  53. secondRow.AddElement(browseBtn);
  54. secondRow.AddSpace(5);
  55. thirdRow.AddSpace(5);
  56. GUIPanel recentProjectsPanel = thirdRow.AddPanel();
  57. thirdRow.AddSpace(15 + 5 + 75);
  58. recentProjectsArea = new GUIScrollArea(GUIOption.FixedWidth(385), GUIOption.FixedHeight(170));
  59. GUILayoutX recentProjectsLayout = recentProjectsPanel.AddLayoutX();
  60. recentProjectsLayout.AddSpace(10);
  61. GUILayoutY recentProjectsPanelY = recentProjectsLayout.AddLayoutY();
  62. recentProjectsPanelY.AddSpace(5);
  63. recentProjectsPanelY.AddElement(recentProjectsArea);
  64. recentProjectsPanelY.AddSpace(5);
  65. recentProjectsLayout.AddFlexibleSpace();
  66. GUIPanel scrollAreaBgPanel = recentProjectsPanel.AddPanel(1);
  67. GUITexture scrollAreaBgTex = new GUITexture(null, true, EditorStyles.ScrollAreaBg);
  68. scrollAreaBgPanel.AddElement(scrollAreaBgTex);
  69. autoLoadToggle = new GUIToggle("");
  70. autoLoadToggle.Value = EditorSettings.AutoLoadLastProject;
  71. GUILabel autoLoadLabel = new GUILabel(new LocEdString("Automatically load last open project"));
  72. GUIButton createBtn = new GUIButton(new LocEdString("Create new"), GUIOption.FixedWidth(75));
  73. createBtn.OnClick += CreateClicked;
  74. fourthRow.AddSpace(5);
  75. fourthRow.AddElement(autoLoadToggle);
  76. fourthRow.AddElement(autoLoadLabel);
  77. fourthRow.AddFlexibleSpace();
  78. fourthRow.AddElement(createBtn);
  79. fourthRow.AddSpace(5);
  80. RefreshRecentProjects();
  81. }
  82. void OpenProject()
  83. {
  84. string projectPath = projectInputBox.Value;
  85. if (EditorApplication.IsValidProject(projectPath))
  86. {
  87. EditorSettings.AutoLoadLastProject = autoLoadToggle.Value;
  88. Close();
  89. EditorApplication.LoadProject(projectPath);
  90. }
  91. else
  92. {
  93. // Remove invalid project from recent projects list
  94. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  95. for (int i = 0; i < recentProjects.Length; i++)
  96. {
  97. if (PathEx.Compare(recentProjects[i].path, projectPath))
  98. {
  99. RecentProject[] newRecentProjects = new RecentProject[recentProjects.Length - 1];
  100. int idx = 0;
  101. for (int j = 0; j < recentProjects.Length; j++)
  102. {
  103. if (i == j)
  104. continue;
  105. newRecentProjects[idx] = recentProjects[j];
  106. idx++;
  107. }
  108. EditorSettings.RecentProjects = newRecentProjects;
  109. EditorSettings.Save();
  110. RefreshRecentProjects();
  111. break;
  112. }
  113. }
  114. // Warn user
  115. LocString message = new LocEdString("Provided project path \"{0}\" doesn't contain a valid project.");
  116. message.SetParameter(0, projectPath);
  117. DialogBox.Open(new LocEdString("Error"), message, DialogBox.Type.OK);
  118. }
  119. }
  120. void BrowseClicked()
  121. {
  122. string projectPath = projectInputBox.Value;
  123. if (!Directory.Exists(projectPath))
  124. projectPath = Directory.GetCurrentDirectory();
  125. string selectedPath;
  126. if (BrowseDialog.OpenFolder(projectPath, "", out selectedPath))
  127. projectInputBox.Value = selectedPath;
  128. }
  129. void CreateClicked()
  130. {
  131. string projectPath = projectInputBox.Value;
  132. if (!Directory.Exists(projectPath))
  133. projectPath = Directory.GetCurrentDirectory();
  134. string selectedPath;
  135. if (BrowseDialog.OpenFolder(projectPath, "", out selectedPath))
  136. {
  137. projectInputBox.Value = selectedPath;
  138. EditorApplication.CreateProject(selectedPath);
  139. OpenProject();
  140. }
  141. }
  142. private void RefreshRecentProjects()
  143. {
  144. GUILayout scrollLayout = recentProjectsArea.Layout;
  145. while(scrollLayout.ChildCount > 0)
  146. scrollLayout.GetChild(0).Destroy();
  147. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  148. Array.Sort(recentProjects, (a, b) => a.accessTimestamp.CompareTo(b.accessTimestamp));
  149. GUIToggleGroup grp = new GUIToggleGroup();
  150. for (int i = 0; i < recentProjects.Length; i++)
  151. {
  152. string projectPath = recentProjects[i].path;
  153. GUIToggle entryBtn = new GUIToggle(projectPath, grp, EditorStyles.SelectableLabel);
  154. entryBtn.OnClick += () => OnEntryClicked(projectPath);
  155. entryBtn.OnDoubleClick += () => OnEntryDoubleClicked(projectPath);
  156. scrollLayout.AddElement(entryBtn);
  157. }
  158. }
  159. void OnEntryClicked(string path)
  160. {
  161. projectInputBox.Value = path;
  162. }
  163. void OnEntryDoubleClicked(string path)
  164. {
  165. projectInputBox.Value = path;
  166. OpenProject();
  167. }
  168. }
  169. }