ProjectWindow.cs 5.9 KB

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