ProjectWindow.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. 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. // Add scroll area background
  73. GUIPanel scrollAreaBgPanel = GUI.AddPanel(1);
  74. GUITexture scrollAreaBgTex = new GUITexture(null, true, EditorStyles.ScrollAreaBg);
  75. scrollAreaBgPanel.AddElement(scrollAreaBgTex);
  76. Rect2I bounds = vertLayout.Bounds;
  77. Rect2I scrollAreaBounds = recentProjectsArea.Bounds;
  78. Debug.Log(scrollAreaBounds + " - " + vertLayout.Bounds);
  79. scrollAreaBounds.y += bounds.y;
  80. scrollAreaBounds.height += 2;
  81. scrollAreaBgTex.Bounds = scrollAreaBounds;
  82. }
  83. void OpenProject()
  84. {
  85. string projectPath = projectInputBox.Value;
  86. if (EditorApplication.IsValidProject(projectPath))
  87. {
  88. EditorSettings.AutoLoadLastProject = autoLoadToggle.Value;
  89. Close();
  90. EditorApplication.LoadProject(projectPath);
  91. }
  92. else
  93. {
  94. // Remove invalid project from recent projects list
  95. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  96. for (int i = 0; i < recentProjects.Length; i++)
  97. {
  98. if (PathEx.Compare(recentProjects[i].path, projectPath))
  99. {
  100. RecentProject[] newRecentProjects = new RecentProject[recentProjects.Length - 1];
  101. int idx = 0;
  102. for (int j = 0; j < recentProjects.Length; j++)
  103. {
  104. if (i == j)
  105. continue;
  106. newRecentProjects[idx] = recentProjects[j];
  107. idx++;
  108. }
  109. EditorSettings.RecentProjects = newRecentProjects;
  110. EditorSettings.Save();
  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.GetNumChildren() > 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. }