EditorApplication.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. private static bool unitTestsExecuted;
  119. /// <summary>
  120. /// Constructs a new editor application. Called at editor start-up by the runtime.
  121. /// </summary>
  122. internal EditorApplication()
  123. {
  124. instance = this;
  125. // Register controls
  126. InputConfiguration inputConfig = VirtualInput.KeyConfig;
  127. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.W);
  128. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.S);
  129. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.A);
  130. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.D);
  131. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.Up);
  132. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.Down);
  133. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.Left);
  134. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.Right);
  135. inputConfig.RegisterButton(SceneCamera.FastMoveBinding, ButtonCode.LeftShift);
  136. inputConfig.RegisterButton(SceneCamera.RotateBinding, ButtonCode.MouseRight);
  137. inputConfig.RegisterAxis(SceneCamera.HorizontalAxisBinding, InputAxis.MouseX);
  138. inputConfig.RegisterAxis(SceneCamera.VerticalAxisBinding, InputAxis.MouseY);
  139. inputConfig.RegisterButton(SceneWindow.ToggleProfilerOverlayBinding, ButtonCode.P, ButtonModifier.CtrlAlt);
  140. inputConfig.RegisterButton(SceneWindow.ViewToolBinding, ButtonCode.Q);
  141. inputConfig.RegisterButton(SceneWindow.MoveToolBinding, ButtonCode.W);
  142. inputConfig.RegisterButton(SceneWindow.RotateToolBinding, ButtonCode.E);
  143. inputConfig.RegisterButton(SceneWindow.ScaleToolBinding, ButtonCode.R);
  144. inputConfig.RegisterButton(SceneWindow.DuplicateBinding, ButtonCode.D, ButtonModifier.Ctrl);
  145. }
  146. /// <summary>
  147. /// Triggered when the folder monitor detects an asset in the monitored folder was modified.
  148. /// </summary>
  149. /// <param name="path">Path to the modified file or folder.</param>
  150. private static void OnAssetModified(string path)
  151. {
  152. ProjectLibrary.Refresh(path);
  153. }
  154. /// <summary>
  155. /// Called 60 times per second by the runtime.
  156. /// </summary>
  157. internal void OnEditorUpdate()
  158. {
  159. ProjectLibrary.Update();
  160. }
  161. /// <summary>
  162. /// Opens a dialog that allows the user to select a new prefab to load as the current scene. If current scene
  163. /// is modified the user is offered a chance to save it.
  164. /// </summary>
  165. [MenuItem("File/Open Scene", ButtonModifier.Ctrl, ButtonCode.L, 10050, true)]
  166. private static void LoadScene()
  167. {
  168. string[] scenePaths;
  169. if (BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths))
  170. {
  171. if (scenePaths.Length > 0)
  172. LoadScene(scenePaths[0]);
  173. }
  174. }
  175. /// <summary>
  176. /// Opens a dialog to allows the user to select a location where to save the current scene. If scene was previously
  177. /// saved it is instead automatically saved at the last location.
  178. /// </summary>
  179. [MenuItem("File/Save Scene", ButtonModifier.Ctrl, ButtonCode.S, 10049)]
  180. [ToolbarItem("Save Scene", ToolbarIcon.SaveScene, "", 1998)]
  181. private static void SaveScene()
  182. {
  183. if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID))
  184. {
  185. string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
  186. SaveScene(scenePath);
  187. }
  188. else
  189. SaveSceneAs();
  190. }
  191. /// <summary>
  192. /// Opens a dialog to allows the user to select a location where to save the current scene.
  193. /// </summary>
  194. [MenuItem("File/Save Scene As", 10048)]
  195. private static void SaveSceneAs()
  196. {
  197. string scenePath = "";
  198. if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath))
  199. {
  200. if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
  201. DialogBox.Open("Error", "The location must be inside the Resources folder of the project.",
  202. DialogBox.Type.OK);
  203. else
  204. {
  205. // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
  206. // Internal_SaveScene will silently fail.
  207. scenePath += ".prefab";
  208. SaveScene(scenePath);
  209. LoadScene(scenePath);
  210. }
  211. }
  212. }
  213. /// <summary>
  214. /// Loads a prefab as the current scene at the specified path. If current scene is modified the user is offered a
  215. /// chance to save it.
  216. /// </summary>
  217. /// <param name="path">Path to a valid prefab relative to the resource folder.</param>
  218. public static void LoadScene(string path)
  219. {
  220. Action<string> continueLoad =
  221. (scenePath) =>
  222. {
  223. Scene.Load(path);
  224. SetSceneDirty(false);
  225. ProjectSettings.LastOpenScene = scenePath;
  226. ProjectSettings.Save();
  227. };
  228. Action<DialogBox.ResultType> dialogCallback =
  229. (result) =>
  230. {
  231. if (result == DialogBox.ResultType.Yes)
  232. {
  233. SaveScene();
  234. continueLoad(path);
  235. }
  236. else if (result == DialogBox.ResultType.No)
  237. continueLoad(path);
  238. };
  239. if (IsSceneModified())
  240. {
  241. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  242. DialogBox.Type.YesNoCancel, dialogCallback);
  243. }
  244. else
  245. continueLoad(path);
  246. }
  247. /// <summary>
  248. /// Saves the currently loaded scene to the specified path.
  249. /// </summary>
  250. /// <param name="path">Path relative to the resource folder. This can be the path to the existing scene
  251. /// prefab if it just needs updating. </param>
  252. public static void SaveScene(string path)
  253. {
  254. Internal_SaveScene(path);
  255. ProjectLibrary.Refresh(true);
  256. SetSceneDirty(false);
  257. }
  258. /// <summary>
  259. /// Checks does the folder at the provieded path contain a valid project.
  260. /// </summary>
  261. /// <param name="path">Absolute path to the root project folder.</param>
  262. /// <returns>True if the folder contains a valid project.</returns>
  263. public static bool IsValidProject(string path)
  264. {
  265. return Internal_IsValidProject(path);
  266. }
  267. /// <summary>
  268. /// Contains a new project in the provided folder.
  269. /// </summary>
  270. /// <param name="path">Absolute path to the folder to create the project in. Name of this folder will be used as the
  271. /// project's name.</param>
  272. public static void CreateProject(string path)
  273. {
  274. Internal_CreateProject(path);
  275. }
  276. /// <summary>
  277. /// Opens a Project Window allowing you to browse for or create a project.
  278. /// </summary>
  279. [MenuItem("File/Open Project", 10100)]
  280. [ToolbarItem("Open Project", ToolbarIcon.OpenProject, "", 2000)]
  281. public static void BrowseForProject()
  282. {
  283. ProjectWindow.Open();
  284. }
  285. /// <summary>
  286. /// Saves all data in the currently open project.
  287. /// </summary>
  288. [MenuItem("File/Save Project", 10099)]
  289. [ToolbarItem("Save Project", ToolbarIcon.SaveProject, "", 1999)]
  290. public static void SaveProject()
  291. {
  292. foreach (var resourceUUID in dirtyResources)
  293. {
  294. string path = ProjectLibrary.GetPath(resourceUUID);
  295. Resource resource = ProjectLibrary.Load<Resource>(path);
  296. if(resource != null)
  297. ProjectLibrary.Save(resource);
  298. }
  299. dirtyResources.Clear();
  300. SetStatusProject(false);
  301. Internal_SaveProject();
  302. }
  303. /// <summary>
  304. /// Loads the project at the specified path. This method executes asynchronously and will trigger
  305. /// <see cref="OnProjectLoaded"/> when done.
  306. /// </summary>
  307. /// <param name="path">Absolute path to the project's root folder.</param>
  308. public static void LoadProject(string path)
  309. {
  310. if (IsProjectLoaded && path == ProjectPath)
  311. return;
  312. if (!Internal_IsValidProject(path))
  313. {
  314. Debug.LogWarning("Provided path: \"" + path + "\" is not a valid project.");
  315. return;
  316. }
  317. if (IsProjectLoaded)
  318. UnloadProject();
  319. Internal_LoadProject(path); // Triggers OnProjectLoaded when done
  320. }
  321. /// <summary>
  322. /// Opens a file or a folder in the default external application.
  323. /// </summary>
  324. /// <param name="path">Absolute path to the file or folder to open.</param>
  325. public static void OpenExternally(string path)
  326. {
  327. Internal_OpenExternally(path);
  328. }
  329. /// <summary>
  330. /// Marks a resource as dirty so that it may be saved the next time the project is saved. Optionally you may also
  331. /// call <see cref="ProjectLibrary.Save"/> to save it immediately.
  332. /// </summary>
  333. /// <param name="resource">Resource to mark as dirty</param>
  334. public static void SetDirty(Resource resource)
  335. {
  336. if (resource == null)
  337. return;
  338. SetStatusProject(true);
  339. dirtyResources.Add(resource.UUID);
  340. }
  341. /// <summary>
  342. /// Marks the current scene as dirty.
  343. /// </summary>
  344. public static void SetSceneDirty()
  345. {
  346. SetSceneDirty(true);
  347. }
  348. /// <summary>
  349. /// Marks the current scene as clean or dirty.
  350. /// </summary>
  351. /// <param name="dirty">Should the scene be marked as clean or dirty.</param>
  352. internal static void SetSceneDirty(bool dirty)
  353. {
  354. sceneDirty = dirty;
  355. SetStatusScene(Scene.ActiveSceneName, dirty);
  356. if (!dirty)
  357. dirtyResources.Remove(Scene.ActiveSceneUUID);
  358. }
  359. /// <summary>
  360. /// Checks is the specific resource dirty and needs saving.
  361. /// </summary>
  362. /// <param name="resource">Resource to check.</param>
  363. /// <returns>True if the resource requires saving, false otherwise.</returns>
  364. public static bool IsDirty(Resource resource)
  365. {
  366. return dirtyResources.Contains(resource.UUID);
  367. }
  368. /// <summary>
  369. /// Triggered when <see cref="LoadProject"/> method completes.
  370. /// </summary>
  371. private static void OnProjectLoaded()
  372. {
  373. SetStatusProject(false);
  374. if (!unitTestsExecuted)
  375. {
  376. RunUnitTests();
  377. unitTestsExecuted = true;
  378. }
  379. if (!IsProjectLoaded)
  380. {
  381. ProjectWindow.Open();
  382. return;
  383. }
  384. string projectPath = ProjectPath;
  385. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  386. bool foundPath = false;
  387. for (int i = 0; i < recentProjects.Length; i++)
  388. {
  389. if (PathEx.Compare(recentProjects[i].path, projectPath))
  390. {
  391. recentProjects[i].accessTimestamp = (ulong)DateTime.Now.Ticks;
  392. EditorSettings.RecentProjects = recentProjects;
  393. foundPath = true;
  394. break;
  395. }
  396. }
  397. if (!foundPath)
  398. {
  399. List<RecentProject> extendedRecentProjects = new List<RecentProject>();
  400. extendedRecentProjects.AddRange(recentProjects);
  401. RecentProject newProject = new RecentProject();
  402. newProject.path = projectPath;
  403. newProject.accessTimestamp = (ulong)DateTime.Now.Ticks;
  404. extendedRecentProjects.Add(newProject);
  405. EditorSettings.RecentProjects = extendedRecentProjects.ToArray();
  406. }
  407. EditorSettings.LastOpenProject = projectPath;
  408. EditorSettings.Save();
  409. ProjectLibrary.Refresh();
  410. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  411. monitor.OnAdded += OnAssetModified;
  412. monitor.OnRemoved += OnAssetModified;
  413. monitor.OnModified += OnAssetModified;
  414. if (!string.IsNullOrWhiteSpace(ProjectSettings.LastOpenScene))
  415. {
  416. Scene.Load(ProjectSettings.LastOpenScene);
  417. SetSceneDirty(false);
  418. }
  419. }
  420. /// <summary>
  421. /// Unloads the currently loaded project. Offers the user a chance to save the current scene if it is modified.
  422. /// Automatically saves all project data before unloading.
  423. /// </summary>
  424. private static void UnloadProject()
  425. {
  426. Action continueUnload =
  427. () =>
  428. {
  429. Scene.Clear();
  430. if (monitor != null)
  431. {
  432. monitor.Destroy();
  433. monitor = null;
  434. }
  435. LibraryWindow window = EditorWindow.GetWindow<LibraryWindow>();
  436. if(window != null)
  437. window.Reset();
  438. SetSceneDirty(false);
  439. Internal_UnloadProject();
  440. SetStatusProject(false);
  441. };
  442. Action<DialogBox.ResultType> dialogCallback =
  443. (result) =>
  444. {
  445. if (result == DialogBox.ResultType.Yes)
  446. SaveScene();
  447. continueUnload();
  448. };
  449. if (IsSceneModified())
  450. {
  451. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  452. DialogBox.Type.YesNoCancel, dialogCallback);
  453. }
  454. else
  455. continueUnload();
  456. }
  457. /// <summary>
  458. /// Changes the scene displayed on the status bar.
  459. /// </summary>
  460. /// <param name="name">Name of the scene.</param>
  461. /// <param name="modified">Whether to display the scene as modified or not.</param>
  462. private static void SetStatusScene(string name, bool modified)
  463. {
  464. Internal_SetStatusScene(name, modified);
  465. }
  466. /// <summary>
  467. /// Changes the project state displayed on the status bar.
  468. /// </summary>
  469. /// <param name="modified">Whether to display the project as modified or not.</param>
  470. private static void SetStatusProject(bool modified)
  471. {
  472. Internal_SetStatusProject(modified);
  473. }
  474. /// <summary>
  475. /// Checks did we make any modifications to the scene since it was last saved.
  476. /// </summary>
  477. /// <returns>True if the scene was never saved, or was modified after last save.</returns>
  478. public static bool IsSceneModified()
  479. {
  480. return sceneDirty;
  481. }
  482. /// <summary>
  483. /// Executes any editor-specific unit tests. This should be called after a project is loaded if possible.
  484. /// </summary>
  485. private static void RunUnitTests()
  486. {
  487. #if DEBUG
  488. Internal_RunUnitTests();
  489. #endif
  490. }
  491. [MethodImpl(MethodImplOptions.InternalCall)]
  492. private static extern void Internal_SetStatusScene(string name, bool modified);
  493. [MethodImpl(MethodImplOptions.InternalCall)]
  494. private static extern void Internal_SetStatusProject(bool modified);
  495. [MethodImpl(MethodImplOptions.InternalCall)]
  496. private static extern string Internal_GetProjectPath();
  497. [MethodImpl(MethodImplOptions.InternalCall)]
  498. private static extern string Internal_GetProjectName();
  499. [MethodImpl(MethodImplOptions.InternalCall)]
  500. private static extern bool Internal_GetProjectLoaded();
  501. [MethodImpl(MethodImplOptions.InternalCall)]
  502. private static extern string Internal_GetCompilerPath();
  503. [MethodImpl(MethodImplOptions.InternalCall)]
  504. private static extern string Internal_GetBuiltinAssemblyPath();
  505. [MethodImpl(MethodImplOptions.InternalCall)]
  506. private static extern string Internal_GetScriptAssemblyPath();
  507. [MethodImpl(MethodImplOptions.InternalCall)]
  508. private static extern string Internal_GetFrameworkAssemblyPath();
  509. [MethodImpl(MethodImplOptions.InternalCall)]
  510. private static extern string Internal_GetEngineAssemblyName();
  511. [MethodImpl(MethodImplOptions.InternalCall)]
  512. private static extern string Internal_GetEditorAssemblyName();
  513. [MethodImpl(MethodImplOptions.InternalCall)]
  514. private static extern string Internal_GetScriptGameAssemblyName();
  515. [MethodImpl(MethodImplOptions.InternalCall)]
  516. private static extern string Internal_GetScriptEditorAssemblyName();
  517. [MethodImpl(MethodImplOptions.InternalCall)]
  518. private static extern string Internal_SaveScene(string path);
  519. [MethodImpl(MethodImplOptions.InternalCall)]
  520. private static extern bool Internal_IsValidProject(string path);
  521. [MethodImpl(MethodImplOptions.InternalCall)]
  522. private static extern void Internal_SaveProject();
  523. [MethodImpl(MethodImplOptions.InternalCall)]
  524. private static extern void Internal_LoadProject(string path);
  525. [MethodImpl(MethodImplOptions.InternalCall)]
  526. private static extern void Internal_UnloadProject();
  527. [MethodImpl(MethodImplOptions.InternalCall)]
  528. private static extern void Internal_CreateProject(string path);
  529. [MethodImpl(MethodImplOptions.InternalCall)]
  530. private static extern void Internal_OpenExternally(string path);
  531. [MethodImpl(MethodImplOptions.InternalCall)]
  532. private static extern void Internal_RunUnitTests();
  533. }
  534. }