ProjectWindow.cs 6.7 KB

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