ProjectWindow.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. }
  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(15);
  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(425));
  39. projectInputBox.Value = EditorSettings.LastOpenProject;
  40. GUIButton openBtn = new GUIButton(new LocEdString("Open"), GUIOption.FixedWidth(50));
  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. recentProjectsArea = new GUIScrollArea(GUIOption.FixedWidth(405), GUIOption.FixedHeight(140));
  52. thirdRow.AddSpace(5 + 10);
  53. thirdRow.AddElement(recentProjectsArea);
  54. thirdRow.AddSpace(15 + 10);
  55. GUILayout browseBtnLayout = thirdRow.AddLayoutY();
  56. GUIButton browseBtn = new GUIButton(new LocEdString("Browse"), GUIOption.FixedWidth(50));
  57. browseBtn.OnClick += BrowseClicked;
  58. browseBtnLayout.AddElement(browseBtn);
  59. browseBtnLayout.AddFlexibleSpace();
  60. thirdRow.AddSpace(5);
  61. autoLoadToggle = new GUIToggle("");
  62. autoLoadToggle.Value = EditorSettings.AutoLoadLastProject;
  63. GUILabel autoLoadLabel = new GUILabel(new LocEdString("Automatically load last open project"));
  64. GUIButton createBtn = new GUIButton(new LocEdString("Create"), GUIOption.FixedWidth(50));
  65. createBtn.OnClick += CreateClicked;
  66. fourthRow.AddSpace(5);
  67. fourthRow.AddElement(autoLoadToggle);
  68. fourthRow.AddElement(autoLoadLabel);
  69. fourthRow.AddFlexibleSpace();
  70. fourthRow.AddElement(createBtn);
  71. fourthRow.AddSpace(5);
  72. RefreshRecentProjects();
  73. // Add scroll area background
  74. GUIPanel scrollAreaBgPanel = GUI.AddPanel(1);
  75. GUITexture scrollAreaBgTex = new GUITexture(null, true, EditorStyles.ScrollAreaBg);
  76. scrollAreaBgPanel.AddElement(scrollAreaBgTex);
  77. Rect2I bounds = vertLayout.Bounds;
  78. Rect2I scrollAreaBounds = recentProjectsArea.Bounds;
  79. scrollAreaBounds.x -= 10;
  80. scrollAreaBounds.y += bounds.y - 10;
  81. scrollAreaBounds.height += 20;
  82. scrollAreaBounds.width += 20;
  83. scrollAreaBgTex.Bounds = scrollAreaBounds;
  84. }
  85. void OpenProject()
  86. {
  87. string projectPath = projectInputBox.Value;
  88. if (EditorApplication.IsValidProject(projectPath))
  89. {
  90. EditorSettings.AutoLoadLastProject = autoLoadToggle.Value;
  91. Close();
  92. EditorApplication.LoadProject(projectPath);
  93. }
  94. else
  95. {
  96. // Remove invalid project from recent projects list
  97. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  98. for (int i = 0; i < recentProjects.Length; i++)
  99. {
  100. if (PathEx.Compare(recentProjects[i].path, projectPath))
  101. {
  102. RecentProject[] newRecentProjects = new RecentProject[recentProjects.Length - 1];
  103. int idx = 0;
  104. for (int j = 0; j < recentProjects.Length; j++)
  105. {
  106. if (i == j)
  107. continue;
  108. newRecentProjects[idx] = recentProjects[j];
  109. idx++;
  110. }
  111. EditorSettings.RecentProjects = newRecentProjects;
  112. EditorSettings.Save();
  113. RefreshRecentProjects();
  114. break;
  115. }
  116. }
  117. // Warn user
  118. LocString message = new LocEdString("Provided project path \"{0}\" doesn't contain a valid project.");
  119. message.SetParameter(0, projectPath);
  120. DialogBox.Open(new LocEdString("Error"), message, DialogBox.Type.OK);
  121. }
  122. }
  123. void BrowseClicked()
  124. {
  125. string projectPath = projectInputBox.Value;
  126. if (!Directory.Exists(projectPath))
  127. projectPath = Directory.GetCurrentDirectory();
  128. string selectedPath;
  129. if (BrowseDialog.OpenFolder(projectPath, "", out selectedPath))
  130. projectInputBox.Value = selectedPath;
  131. }
  132. void CreateClicked()
  133. {
  134. string projectPath = projectInputBox.Value;
  135. if (!Directory.Exists(projectPath))
  136. projectPath = Directory.GetCurrentDirectory();
  137. string selectedPath;
  138. if (BrowseDialog.OpenFolder(projectPath, "", out selectedPath))
  139. {
  140. projectInputBox.Value = selectedPath;
  141. EditorApplication.CreateProject(selectedPath);
  142. OpenProject();
  143. }
  144. }
  145. private void RefreshRecentProjects()
  146. {
  147. GUILayout scrollLayout = recentProjectsArea.Layout;
  148. while(scrollLayout.GetNumChildren() > 0)
  149. scrollLayout.GetChild(0).Destroy();
  150. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  151. Array.Sort(recentProjects, (a, b) => a.accessTimestamp.CompareTo(b.accessTimestamp));
  152. GUIToggleGroup grp = new GUIToggleGroup();
  153. for (int i = 0; i < recentProjects.Length; i++)
  154. {
  155. string projectPath = recentProjects[i].path;
  156. GUIToggle entryBtn = new GUIToggle(projectPath, grp, EditorStyles.SelectableLabel);
  157. entryBtn.OnClick += () => OnEntryClicked(projectPath);
  158. entryBtn.OnDoubleClick += () => OnEntryDoubleClicked(projectPath);
  159. scrollLayout.AddElement(entryBtn);
  160. }
  161. }
  162. void OnEntryClicked(string path)
  163. {
  164. projectInputBox.Value = path;
  165. }
  166. void OnEntryDoubleClicked(string path)
  167. {
  168. projectInputBox.Value = path;
  169. OpenProject();
  170. }
  171. }
  172. }