ProjectWindow.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. bool foundPath = false;
  70. for (int i = 0; i < recentProjects.Length; i++)
  71. {
  72. if (PathEx.Compare(recentProjects[i].path, projectPath))
  73. {
  74. RecentProject[] newRecentProjects = new RecentProject[recentProjects.Length - 1];
  75. int idx = 0;
  76. for (int j = 0; j < recentProjects.Length; j++)
  77. {
  78. if (i == j)
  79. continue;
  80. newRecentProjects[idx] = recentProjects[j];
  81. idx++;
  82. }
  83. EditorSettings.RecentProjects = newRecentProjects;
  84. EditorSettings.Save();
  85. break;
  86. }
  87. }
  88. // Warn user
  89. LocString message = new LocEdString("Provided project path \"") + projectPath +
  90. new LocEdString("\" doesn't contain a valid project.");
  91. DialogBox.Open(new LocEdString("Error"), message, DialogBox.Type.OK);
  92. }
  93. }
  94. void BrowseClicked()
  95. {
  96. string projectPath = projectInputBox.Value;
  97. if (!Directory.Exists(projectPath))
  98. projectPath = Directory.GetCurrentDirectory();
  99. string selectedPath;
  100. if (BrowseDialog.OpenFolder(projectPath, "", out selectedPath))
  101. projectInputBox.Value = selectedPath;
  102. }
  103. void CreateClicked()
  104. {
  105. string projectPath = projectInputBox.Value;
  106. if (!Directory.Exists(projectPath))
  107. projectPath = Directory.GetCurrentDirectory();
  108. string selectedPath;
  109. if (BrowseDialog.OpenFolder(projectPath, "", out selectedPath))
  110. {
  111. projectInputBox.Value = selectedPath;
  112. EditorApplication.CreateProject(selectedPath);
  113. OpenProject();
  114. }
  115. }
  116. private void RefreshRecentProjects()
  117. {
  118. GUILayout scrollLayout = recentProjectsArea.Layout;
  119. while(scrollLayout.GetNumChildren() > 0)
  120. scrollLayout.GetChild(0).Destroy();
  121. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  122. Array.Sort(recentProjects, (a, b) => a.accessTimestamp.CompareTo(b.accessTimestamp));
  123. GUIToggleGroup grp = new GUIToggleGroup();
  124. for (int i = 0; i < recentProjects.Length; i++)
  125. {
  126. string projectPath = recentProjects[i].path;
  127. GUIToggle entryBtn = new GUIToggle(projectPath, grp, EditorStyles.SelectableLabel);
  128. entryBtn.OnClick += () => OnEntryClicked(projectPath);
  129. entryBtn.OnDoubleClick += () => OnEntryDoubleClicked(projectPath);
  130. scrollLayout.AddElement(entryBtn);
  131. }
  132. }
  133. void OnEntryClicked(string path)
  134. {
  135. projectInputBox.Value = path;
  136. }
  137. void OnEntryDoubleClicked(string path)
  138. {
  139. projectInputBox.Value = path;
  140. OpenProject();
  141. }
  142. }
  143. }