EditorApplication.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using System.IO;
  5. using BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /// <summary>
  9. /// Available tools in the scene view.
  10. /// </summary>
  11. public enum SceneViewTool
  12. {
  13. View,
  14. Move,
  15. Rotate,
  16. Scale
  17. }
  18. /// <summary>
  19. /// Pivot mode used by the scene view tools.
  20. /// </summary>
  21. public enum HandlePivotMode
  22. {
  23. Center,
  24. Pivot
  25. }
  26. /// <summary>
  27. /// Coordinate mode used by the scene view tools.
  28. /// </summary>
  29. public enum HandleCoordinateMode
  30. {
  31. Local,
  32. World
  33. }
  34. /// <summary>
  35. /// Manages various generic and global settings relating to the editor.
  36. /// </summary>
  37. public class EditorApplication
  38. {
  39. /// <summary>
  40. /// Determines the active tool shown in the scene view.
  41. /// </summary>
  42. public static SceneViewTool ActiveSceneTool
  43. {
  44. get { return EditorSettings.ActiveSceneTool; }
  45. set { EditorSettings.ActiveSceneTool = value; }
  46. }
  47. /// <summary>
  48. /// Determines the coordinate mode used by the tools in the scene view.
  49. /// </summary>
  50. public static HandleCoordinateMode ActiveCoordinateMode
  51. {
  52. get { return EditorSettings.ActiveCoordinateMode; }
  53. set { EditorSettings.ActiveCoordinateMode = value; }
  54. }
  55. /// <summary>
  56. /// Determines the pivot mode used by the tools in the scene view.
  57. /// </summary>
  58. public static HandlePivotMode ActivePivotMode
  59. {
  60. get { return EditorSettings.ActivePivotMode; }
  61. set { EditorSettings.ActivePivotMode = value; }
  62. }
  63. /// <summary>
  64. /// Camera used for rendering the scene view.
  65. /// </summary>
  66. public static Camera SceneViewCamera
  67. {
  68. get { return EditorWindow.GetWindow<SceneWindow>().Camera; }
  69. }
  70. /// <summary>
  71. /// Absolute path to the folder containing the currently open project.
  72. /// </summary>
  73. public static string ProjectPath { get { return Internal_GetProjectPath(); } }
  74. /// <summary>
  75. /// Name of the currently open project.
  76. /// </summary>
  77. public static string ProjectName { get { return Internal_GetProjectName(); } }
  78. /// <summary>
  79. /// Checks is any project currently loaded.
  80. /// </summary>
  81. public static bool IsProjectLoaded { get { return Internal_GetProjectLoaded(); } }
  82. /// <summary>
  83. /// Returns the path where the script compiler is located at.
  84. /// </summary>
  85. internal static string CompilerPath { get { return Internal_GetCompilerPath(); } }
  86. /// <summary>
  87. /// Returns the path to the folder where the builtin script assemblies are located at.
  88. /// </summary>
  89. internal static string BuiltinAssemblyPath { get { return Internal_GetBuiltinAssemblyPath(); } }
  90. /// <summary>
  91. /// Returns the path to the folder where the custom script assemblies are located at.
  92. /// </summary>
  93. internal static string ScriptAssemblyPath { get { return Internal_GetScriptAssemblyPath(); } }
  94. /// <summary>
  95. /// Returns the path to the folder where the .NET framework assemblies are located at.
  96. /// </summary>
  97. internal static string FrameworkAssemblyPath { get { return Internal_GetFrameworkAssemblyPath(); } }
  98. /// <summary>
  99. /// Name of the builtin assembly containing engine specific types.
  100. /// </summary>
  101. internal static string EngineAssembly { get { return Internal_GetEngineAssemblyName(); } }
  102. /// <summary>
  103. /// Name of the builtin assembly containing editor specific types.
  104. /// </summary>
  105. internal static string EditorAssembly { get { return Internal_GetEditorAssemblyName(); } }
  106. /// <summary>
  107. /// Name of the custom assembly compiled from non-editor scripts within the project.
  108. /// </summary>
  109. internal static string ScriptGameAssembly { get { return Internal_GetScriptGameAssemblyName(); } }
  110. /// <summary>
  111. /// Name of the custom assembly compiled from editor scripts within the project.
  112. /// </summary>
  113. internal static string ScriptEditorAssembly { get { return Internal_GetScriptEditorAssemblyName(); } }
  114. private static EditorApplication instance;
  115. private static FolderMonitor monitor;
  116. private static HashSet<string> dirtyResources = new HashSet<string>();
  117. private static bool sceneDirty;
  118. /// <summary>
  119. /// Constructs a new editor application. Called at editor start-up by the runtime.
  120. /// </summary>
  121. internal EditorApplication()
  122. {
  123. instance = this;
  124. // Register controls
  125. InputConfiguration inputConfig = VirtualInput.KeyConfig;
  126. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.W);
  127. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.S);
  128. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.A);
  129. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.D);
  130. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.Up);
  131. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.Down);
  132. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.Left);
  133. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.Right);
  134. inputConfig.RegisterButton(SceneCamera.FastMoveBinding, ButtonCode.LeftShift);
  135. inputConfig.RegisterButton(SceneCamera.RotateBinding, ButtonCode.MouseRight);
  136. inputConfig.RegisterAxis(SceneCamera.HorizontalAxisBinding, InputAxis.MouseX);
  137. inputConfig.RegisterAxis(SceneCamera.VerticalAxisBinding, InputAxis.MouseY);
  138. inputConfig.RegisterButton(SceneWindow.ToggleProfilerOverlayBinding, ButtonCode.P, ButtonModifier.CtrlAlt);
  139. inputConfig.RegisterButton(SceneWindow.ViewToolBinding, ButtonCode.Q);
  140. inputConfig.RegisterButton(SceneWindow.MoveToolBinding, ButtonCode.W);
  141. inputConfig.RegisterButton(SceneWindow.RotateToolBinding, ButtonCode.E);
  142. inputConfig.RegisterButton(SceneWindow.ScaleToolBinding, ButtonCode.R);
  143. inputConfig.RegisterButton(SceneWindow.DuplicateBinding, ButtonCode.D, ButtonModifier.Ctrl);
  144. }
  145. /// <summary>
  146. /// Triggered when the folder monitor detects an asset in the monitored folder was modified.
  147. /// </summary>
  148. /// <param name="path">Path to the modified file or folder.</param>
  149. private static void OnAssetModified(string path)
  150. {
  151. ProjectLibrary.Refresh(path);
  152. }
  153. /// <summary>
  154. /// Called 60 times per second by the runtime.
  155. /// </summary>
  156. internal void OnEditorUpdate()
  157. {
  158. ProjectLibrary.Update();
  159. }
  160. /// <summary>
  161. /// Opens a dialog that allows the user to select a new prefab to load as the current scene. If current scene
  162. /// is modified the user is offered a chance to save it.
  163. /// </summary>
  164. [MenuItem("File/Open Scene", ButtonModifier.Ctrl, ButtonCode.L, 10050, true)]
  165. private static void LoadScene()
  166. {
  167. string[] scenePaths;
  168. if (BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths))
  169. {
  170. if (scenePaths.Length > 0)
  171. LoadScene(scenePaths[0]);
  172. }
  173. }
  174. /// <summary>
  175. /// Opens a dialog to allows the user to select a location where to save the current scene. If scene was previously
  176. /// saved it is instead automatically saved at the last location.
  177. /// </summary>
  178. [MenuItem("File/Save Scene", ButtonModifier.Ctrl, ButtonCode.S, 10049)]
  179. [ToolbarItem("Save Scene", ToolbarIcon.SaveScene, "", 1998)]
  180. private static void SaveScene()
  181. {
  182. if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID))
  183. {
  184. string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
  185. SaveScene(scenePath);
  186. }
  187. else
  188. SaveSceneAs();
  189. }
  190. /// <summary>
  191. /// Opens a dialog to allows the user to select a location where to save the current scene.
  192. /// </summary>
  193. [MenuItem("File/Save Scene As", 10048)]
  194. private static void SaveSceneAs()
  195. {
  196. string scenePath = "";
  197. if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath))
  198. {
  199. if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
  200. DialogBox.Open("Error", "The location must be inside the Resources folder of the project.",
  201. DialogBox.Type.OK);
  202. else
  203. {
  204. // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
  205. // Internal_SaveScene will silently fail.
  206. scenePath += ".prefab";
  207. SaveScene(scenePath);
  208. LoadScene(scenePath);
  209. }
  210. }
  211. }
  212. /// <summary>
  213. /// Loads a prefab as the current scene at the specified path. If current scene is modified the user is offered a
  214. /// chance to save it.
  215. /// </summary>
  216. /// <param name="path">Path to a valid prefab relative to the resource folder.</param>
  217. public static void LoadScene(string path)
  218. {
  219. Action<string> continueLoad =
  220. (scenePath) =>
  221. {
  222. Scene.Load(path);
  223. SetSceneDirty(false);
  224. ProjectSettings.LastOpenScene = scenePath;
  225. ProjectSettings.Save();
  226. };
  227. Action<DialogBox.ResultType> dialogCallback =
  228. (result) =>
  229. {
  230. if (result == DialogBox.ResultType.Yes)
  231. {
  232. SaveScene();
  233. continueLoad(path);
  234. }
  235. else if (result == DialogBox.ResultType.No)
  236. continueLoad(path);
  237. };
  238. if (IsSceneModified())
  239. {
  240. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  241. DialogBox.Type.YesNoCancel, dialogCallback);
  242. }
  243. else
  244. continueLoad(path);
  245. }
  246. /// <summary>
  247. /// Saves the currently loaded scene to the specified path.
  248. /// </summary>
  249. /// <param name="path">Path relative to the resource folder. This can be the path to the existing scene
  250. /// prefab it just needs updating. </param>
  251. public static void SaveScene(string path)
  252. {
  253. Internal_SaveScene(path);
  254. ProjectLibrary.Refresh(true);
  255. SetSceneDirty(false);
  256. }
  257. /// <summary>
  258. /// Checks does the folder at the provieded path contain a valid project.
  259. /// </summary>
  260. /// <param name="path">Absolute path to the root project folder.</param>
  261. /// <returns>True if the folder contains a valid project.</returns>
  262. public static bool IsValidProject(string path)
  263. {
  264. return Internal_IsValidProject(path);
  265. }
  266. /// <summary>
  267. /// Contains a new project in the provided folder.
  268. /// </summary>
  269. /// <param name="path">Absolute path to the folder to create the project in. Name of this folder will be used as the
  270. /// project's name.</param>
  271. public static void CreateProject(string path)
  272. {
  273. Internal_CreateProject(path);
  274. }
  275. /// <summary>
  276. /// Opens a Project Window allowing you to browse for or create a project.
  277. /// </summary>
  278. [MenuItem("File/Open Project", 10100)]
  279. [ToolbarItem("Open Project", ToolbarIcon.OpenProject, "", 2000)]
  280. public static void BrowseForProject()
  281. {
  282. ProjectWindow.Open();
  283. }
  284. /// <summary>
  285. /// Saves all data in the currently open project.
  286. /// </summary>
  287. [MenuItem("File/Save Project", 10099)]
  288. [ToolbarItem("Save Project", ToolbarIcon.SaveProject, "", 1999)]
  289. public static void SaveProject()
  290. {
  291. foreach (var resourceUUID in dirtyResources)
  292. {
  293. string path = ProjectLibrary.GetPath(resourceUUID);
  294. Resource resource = ProjectLibrary.Load<Resource>(path);
  295. if(resource != null)
  296. ProjectLibrary.Save(resource);
  297. }
  298. dirtyResources.Clear();
  299. SetStatusProject(false);
  300. Internal_SaveProject();
  301. }
  302. /// <summary>
  303. /// Loads the project at the specified path. This method executes asynchronously and will trigger
  304. /// <see cref="OnProjectLoaded"/> when done.
  305. /// </summary>
  306. /// <param name="path">Absolute path to the project's root folder.</param>
  307. public static void LoadProject(string path)
  308. {
  309. if (IsProjectLoaded && path == ProjectPath)
  310. return;
  311. if (!Internal_IsValidProject(path))
  312. {
  313. Debug.LogWarning("Provided path: \"" + path + "\" is not a valid project.");
  314. return;
  315. }
  316. if (IsProjectLoaded)
  317. UnloadProject();
  318. Internal_LoadProject(path); // Triggers OnProjectLoaded when done
  319. }
  320. /// <summary>
  321. /// Opens a file or a folder in the default external application.
  322. /// </summary>
  323. /// <param name="path">Absolute path to the file or folder to open.</param>
  324. public static void OpenExternally(string path)
  325. {
  326. Internal_OpenExternally(path);
  327. }
  328. /// <summary>
  329. /// Marks a resource as dirty so that it may be saved the next time the project is saved. Optionally you may also
  330. /// call <see cref="ProjectLibrary.Save"/> to save it immediately.
  331. /// </summary>
  332. /// <param name="resource">Resource to mark as dirty</param>
  333. public static void SetDirty(Resource resource)
  334. {
  335. if (resource == null)
  336. return;
  337. SetStatusProject(true);
  338. dirtyResources.Add(resource.UUID);
  339. }
  340. /// <summary>
  341. /// Marks the current scene as dirty.
  342. /// </summary>
  343. public static void SetSceneDirty()
  344. {
  345. SetSceneDirty(true);
  346. }
  347. /// <summary>
  348. /// Marks the current scene as clean or dirty.
  349. /// </summary>
  350. /// <param name="dirty">Should the scene be marked as clean or dirty.</param>
  351. internal static void SetSceneDirty(bool dirty)
  352. {
  353. sceneDirty = dirty;
  354. SetStatusScene(Scene.ActiveSceneName, dirty);
  355. if (!dirty)
  356. dirtyResources.Remove(Scene.ActiveSceneUUID);
  357. }
  358. /// <summary>
  359. /// Checks is the specific resource dirty and needs saving.
  360. /// </summary>
  361. /// <param name="resource">Resource to check.</param>
  362. /// <returns>True if the resource requires saving, false otherwise.</returns>
  363. public static bool IsDirty(Resource resource)
  364. {
  365. return dirtyResources.Contains(resource.UUID);
  366. }
  367. /// <summary>
  368. /// Triggered when <see cref="LoadProject"/> method completes.
  369. /// </summary>
  370. private static void OnProjectLoaded()
  371. {
  372. SetStatusProject(false);
  373. if (!IsProjectLoaded)
  374. {
  375. ProjectWindow.Open();
  376. return;
  377. }
  378. string projectPath = ProjectPath;
  379. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  380. bool foundPath = false;
  381. for (int i = 0; i < recentProjects.Length; i++)
  382. {
  383. if (PathEx.Compare(recentProjects[i].path, projectPath))
  384. {
  385. recentProjects[i].accessTimestamp = (ulong)DateTime.Now.Ticks;
  386. EditorSettings.RecentProjects = recentProjects;
  387. foundPath = true;
  388. break;
  389. }
  390. }
  391. if (!foundPath)
  392. {
  393. List<RecentProject> extendedRecentProjects = new List<RecentProject>();
  394. extendedRecentProjects.AddRange(recentProjects);
  395. RecentProject newProject = new RecentProject();
  396. newProject.path = projectPath;
  397. newProject.accessTimestamp = (ulong)DateTime.Now.Ticks;
  398. extendedRecentProjects.Add(newProject);
  399. EditorSettings.RecentProjects = extendedRecentProjects.ToArray();
  400. }
  401. EditorSettings.LastOpenProject = projectPath;
  402. EditorSettings.Save();
  403. ProjectLibrary.Refresh();
  404. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  405. monitor.OnAdded += OnAssetModified;
  406. monitor.OnRemoved += OnAssetModified;
  407. monitor.OnModified += OnAssetModified;
  408. if (!string.IsNullOrWhiteSpace(ProjectSettings.LastOpenScene))
  409. {
  410. Scene.Load(ProjectSettings.LastOpenScene);
  411. SetSceneDirty(false);
  412. }
  413. }
  414. /// <summary>
  415. /// Unloads the currently loaded project. Offers the user a chance to save the current scene if it is modified.
  416. /// Automatically saves all project data before unloading.
  417. /// </summary>
  418. private static void UnloadProject()
  419. {
  420. Action continueUnload =
  421. () =>
  422. {
  423. Scene.Clear();
  424. if (monitor != null)
  425. {
  426. monitor.Destroy();
  427. monitor = null;
  428. }
  429. LibraryWindow window = EditorWindow.GetWindow<LibraryWindow>();
  430. if(window != null)
  431. window.Reset();
  432. SetSceneDirty(false);
  433. Internal_UnloadProject();
  434. SetStatusProject(false);
  435. };
  436. Action<DialogBox.ResultType> dialogCallback =
  437. (result) =>
  438. {
  439. if (result == DialogBox.ResultType.Yes)
  440. SaveScene();
  441. continueUnload();
  442. };
  443. if (IsSceneModified())
  444. {
  445. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  446. DialogBox.Type.YesNoCancel, dialogCallback);
  447. }
  448. else
  449. continueUnload();
  450. }
  451. /// <summary>
  452. /// Changes the scene displayed on the status bar.
  453. /// </summary>
  454. /// <param name="name">Name of the scene.</param>
  455. /// <param name="modified">Whether to display the scene as modified or not.</param>
  456. private static void SetStatusScene(string name, bool modified)
  457. {
  458. Internal_SetStatusScene(name, modified);
  459. }
  460. /// <summary>
  461. /// Changes the project state displayed on the status bar.
  462. /// </summary>
  463. /// <param name="modified">Whether to display the project as modified or not.</param>
  464. private static void SetStatusProject(bool modified)
  465. {
  466. Internal_SetStatusProject(modified);
  467. }
  468. /// <summary>
  469. /// Checks did we make any modifications to the scene since it was last saved.
  470. /// </summary>
  471. /// <returns>True if the scene was never saved, or was modified after last save.</returns>
  472. public static bool IsSceneModified()
  473. {
  474. return sceneDirty;
  475. }
  476. [MethodImpl(MethodImplOptions.InternalCall)]
  477. private static extern void Internal_SetStatusScene(string name, bool modified);
  478. [MethodImpl(MethodImplOptions.InternalCall)]
  479. private static extern void Internal_SetStatusProject(bool modified);
  480. [MethodImpl(MethodImplOptions.InternalCall)]
  481. private static extern string Internal_GetProjectPath();
  482. [MethodImpl(MethodImplOptions.InternalCall)]
  483. private static extern string Internal_GetProjectName();
  484. [MethodImpl(MethodImplOptions.InternalCall)]
  485. private static extern bool Internal_GetProjectLoaded();
  486. [MethodImpl(MethodImplOptions.InternalCall)]
  487. private static extern string Internal_GetCompilerPath();
  488. [MethodImpl(MethodImplOptions.InternalCall)]
  489. private static extern string Internal_GetBuiltinAssemblyPath();
  490. [MethodImpl(MethodImplOptions.InternalCall)]
  491. private static extern string Internal_GetScriptAssemblyPath();
  492. [MethodImpl(MethodImplOptions.InternalCall)]
  493. private static extern string Internal_GetFrameworkAssemblyPath();
  494. [MethodImpl(MethodImplOptions.InternalCall)]
  495. private static extern string Internal_GetEngineAssemblyName();
  496. [MethodImpl(MethodImplOptions.InternalCall)]
  497. private static extern string Internal_GetEditorAssemblyName();
  498. [MethodImpl(MethodImplOptions.InternalCall)]
  499. private static extern string Internal_GetScriptGameAssemblyName();
  500. [MethodImpl(MethodImplOptions.InternalCall)]
  501. private static extern string Internal_GetScriptEditorAssemblyName();
  502. [MethodImpl(MethodImplOptions.InternalCall)]
  503. private static extern string Internal_SaveScene(string path);
  504. [MethodImpl(MethodImplOptions.InternalCall)]
  505. private static extern bool Internal_IsValidProject(string path);
  506. [MethodImpl(MethodImplOptions.InternalCall)]
  507. private static extern void Internal_SaveProject();
  508. [MethodImpl(MethodImplOptions.InternalCall)]
  509. private static extern void Internal_LoadProject(string path);
  510. [MethodImpl(MethodImplOptions.InternalCall)]
  511. private static extern void Internal_UnloadProject();
  512. [MethodImpl(MethodImplOptions.InternalCall)]
  513. private static extern void Internal_CreateProject(string path);
  514. [MethodImpl(MethodImplOptions.InternalCall)]
  515. private static extern void Internal_OpenExternally(string path);
  516. }
  517. }