EditorApplication.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. /// Determines is the game currently running in the editor, or is it stopped or paused. Setting this value to false
  84. /// will stop the game, but if you just want to pause it use <see cref="IsPaused"/> property.
  85. /// </summary>
  86. public static bool IsPlaying
  87. {
  88. get { return Internal_GetIsPlaying(); }
  89. set
  90. {
  91. ToggleToolbarItem("Play", value);
  92. ToggleToolbarItem("Pause", false);
  93. if (!value)
  94. Selection.SceneObject = null;
  95. else
  96. {
  97. if (EditorSettings.GetBool(ConsoleWindow.CLEAR_ON_PLAY_KEY, true))
  98. {
  99. Debug.Clear();
  100. ConsoleWindow console = EditorWindow.GetWindow<ConsoleWindow>();
  101. if (console != null)
  102. console.Refresh();
  103. }
  104. }
  105. Internal_SetIsPlaying(value);
  106. }
  107. }
  108. /// <summary>
  109. /// Determines if the game is currently running in the editor, but paused. If the game is stopped and not running
  110. /// this will return false. If the game is not running and this is enabled, the game will start running but be
  111. /// immediately paused.
  112. /// </summary>
  113. public static bool IsPaused
  114. {
  115. get { return Internal_GetIsPaused(); }
  116. set
  117. {
  118. ToggleToolbarItem("Play", !value);
  119. ToggleToolbarItem("Pause", value);
  120. Internal_SetIsPaused(value);
  121. }
  122. }
  123. /// <summary>
  124. /// Returns true if the game is currently neither running nor paused. Use <see cref="IsPlaying"/> or
  125. /// <see cref="IsPaused"/> to actually change these states.
  126. /// </summary>
  127. public static bool IsStopped
  128. {
  129. get { return !IsPlaying && !IsPaused; }
  130. }
  131. /// <summary>
  132. /// Checks whether the editor currently has focus.
  133. /// </summary>
  134. public static bool HasFocus
  135. {
  136. get { return Internal_HasFocus(); }
  137. }
  138. /// <summary>
  139. /// Render target that the main camera in the scene (if any) will render its view to. This generally means the main
  140. /// game window when running standalone, or the Game viewport when running in editor.
  141. /// </summary>
  142. internal static RenderTarget MainRenderTarget
  143. {
  144. set
  145. {
  146. IntPtr rtPtr = IntPtr.Zero;
  147. if (value != null)
  148. rtPtr = value.GetCachedPtr();
  149. Internal_SetMainRenderTarget(rtPtr);
  150. }
  151. }
  152. /// <summary>
  153. /// Returns the path where the script compiler is located at.
  154. /// </summary>
  155. internal static string CompilerPath { get { return Internal_GetCompilerPath(); } }
  156. /// <summary>
  157. /// Returns the path to the folder where the custom script assemblies are located at.
  158. /// </summary>
  159. internal static string ScriptAssemblyPath { get { return Internal_GetScriptAssemblyPath(); } }
  160. /// <summary>
  161. /// Returns the path to the folder where the .NET framework assemblies are located at.
  162. /// </summary>
  163. internal static string FrameworkAssemblyPath { get { return Internal_GetFrameworkAssemblyPath(); } }
  164. /// <summary>
  165. /// Name of the builtin assembly containing engine specific types.
  166. /// </summary>
  167. internal static string EngineAssemblyName { get { return Internal_GetEngineAssemblyName(); } }
  168. /// <summary>
  169. /// Name of the builtin assembly containing editor specific types.
  170. /// </summary>
  171. internal static string EditorAssemblyName { get { return Internal_GetEditorAssemblyName(); } }
  172. /// <summary>
  173. /// Name of the custom assembly compiled from non-editor scripts within the project.
  174. /// </summary>
  175. internal static string ScriptGameAssemblyName { get { return Internal_GetScriptGameAssemblyName(); } }
  176. /// <summary>
  177. /// Name of the custom assembly compiled from editor scripts within the project.
  178. /// </summary>
  179. internal static string ScriptEditorAssemblyName { get { return Internal_GetScriptEditorAssemblyName(); } }
  180. /// <summary>
  181. /// Returns the path to the folder where the builtin release script assemblies are located at.
  182. /// </summary>
  183. internal static string BuiltinReleaseAssemblyPath { get { return Internal_GetBuiltinReleaseAssemblyPath(); } }
  184. /// <summary>
  185. /// Returns the path to the folder where the builtin debug script assemblies are located at.
  186. /// </summary>
  187. internal static string BuiltinDebugAssemblyPath { get { return Internal_GetBuiltinDebugAssemblyPath(); } }
  188. private static EditorApplication instance;
  189. private static FolderMonitor monitor;
  190. private static ScriptCodeManager codeManager;
  191. private static HashSet<string> dirtyResources = new HashSet<string>();
  192. private static bool sceneDirty;
  193. private static bool unitTestsExecuted;
  194. /// <summary>
  195. /// Constructs a new editor application. Called at editor start-up by the runtime.
  196. /// </summary>
  197. internal EditorApplication()
  198. {
  199. instance = this;
  200. codeManager = new ScriptCodeManager();
  201. // Register controls
  202. InputConfiguration inputConfig = VirtualInput.KeyConfig;
  203. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.W);
  204. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.S);
  205. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.A);
  206. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.D);
  207. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.Up);
  208. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.Down);
  209. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.Left);
  210. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.Right);
  211. inputConfig.RegisterButton(SceneCamera.FastMoveBinding, ButtonCode.LeftShift);
  212. inputConfig.RegisterButton(SceneCamera.RotateBinding, ButtonCode.MouseRight);
  213. inputConfig.RegisterAxis(SceneCamera.HorizontalAxisBinding, InputAxis.MouseX);
  214. inputConfig.RegisterAxis(SceneCamera.VerticalAxisBinding, InputAxis.MouseY);
  215. inputConfig.RegisterButton(SceneWindow.ToggleProfilerOverlayBinding, ButtonCode.P, ButtonModifier.CtrlAlt);
  216. inputConfig.RegisterButton(SceneWindow.ViewToolBinding, ButtonCode.Q);
  217. inputConfig.RegisterButton(SceneWindow.FrameBinding, ButtonCode.F);
  218. inputConfig.RegisterButton(SceneWindow.MoveToolBinding, ButtonCode.W);
  219. inputConfig.RegisterButton(SceneWindow.RotateToolBinding, ButtonCode.E);
  220. inputConfig.RegisterButton(SceneWindow.ScaleToolBinding, ButtonCode.R);
  221. inputConfig.RegisterButton(SceneWindow.DuplicateBinding, ButtonCode.D, ButtonModifier.Ctrl);
  222. if (IsProjectLoaded)
  223. {
  224. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  225. monitor.OnAdded += OnAssetModified;
  226. monitor.OnRemoved += OnAssetModified;
  227. monitor.OnModified += OnAssetModified;
  228. }
  229. }
  230. /// <summary>
  231. /// Triggered when the folder monitor detects an asset in the monitored folder was modified.
  232. /// </summary>
  233. /// <param name="path">Path to the modified file or folder.</param>
  234. private static void OnAssetModified(string path)
  235. {
  236. ProjectLibrary.Refresh(path);
  237. }
  238. /// <summary>
  239. /// Called 60 times per second by the runtime.
  240. /// </summary>
  241. internal void OnEditorUpdate()
  242. {
  243. ProjectLibrary.Update();
  244. codeManager.Update();
  245. }
  246. /// <summary>
  247. /// Creates a new empty scene.
  248. /// </summary>
  249. [MenuItem("File/New Scene", 10051, true)]
  250. private static void NewScene()
  251. {
  252. LoadScene(null);
  253. }
  254. /// <summary>
  255. /// Opens a dialog that allows the user to select a new prefab to load as the current scene. If current scene
  256. /// is modified the user is offered a chance to save it.
  257. /// </summary>
  258. [MenuItem("File/Open Scene", ButtonModifier.Ctrl, ButtonCode.L, 10050)]
  259. private static void LoadScene()
  260. {
  261. string[] scenePaths;
  262. if (BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths))
  263. {
  264. if (scenePaths.Length > 0)
  265. LoadScene(scenePaths[0]);
  266. }
  267. }
  268. /// <summary>
  269. /// Opens a dialog to allows the user to select a location where to save the current scene. If scene was previously
  270. /// saved it is instead automatically saved at the last location.
  271. /// </summary>
  272. public static void SaveScene(Action onSuccess = null, Action onFailure = null)
  273. {
  274. if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID))
  275. {
  276. string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
  277. SaveScene(scenePath);
  278. if (onSuccess != null)
  279. onSuccess();
  280. }
  281. else
  282. SaveSceneAs(onSuccess, onFailure);
  283. }
  284. /// <summary>
  285. /// Opens a dialog to allows the user to select a location where to save the current scene.
  286. /// </summary>
  287. public static void SaveSceneAs(Action onSuccess = null, Action onFailure = null)
  288. {
  289. string scenePath = "";
  290. if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath))
  291. {
  292. if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
  293. {
  294. DialogBox.Open("Error", "The location must be inside the Resources folder of the project.",
  295. DialogBox.Type.OK,
  296. x =>
  297. {
  298. if (onFailure != null)
  299. onFailure();
  300. });
  301. }
  302. else
  303. {
  304. // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
  305. // Internal_SaveScene will silently fail.
  306. scenePath = Path.ChangeExtension(scenePath, ".prefab");
  307. SaveScene(scenePath);
  308. }
  309. }
  310. else
  311. {
  312. // User canceled, so technically a success
  313. if (onSuccess != null)
  314. onSuccess();
  315. }
  316. }
  317. /// <summary>
  318. /// Loads a prefab as the current scene at the specified path. If current scene is modified the user is offered a
  319. /// chance to save it.
  320. /// </summary>
  321. /// <param name="path">Path to a valid prefab relative to the resource folder. If path is empty a brand new
  322. /// scene will be loaded.</param>
  323. public static void LoadScene(string path)
  324. {
  325. Action<string> continueLoad =
  326. (scenePath) =>
  327. {
  328. if (string.IsNullOrEmpty(path))
  329. Scene.Clear();
  330. else
  331. Scene.Load(path);
  332. SetSceneDirty(false);
  333. ProjectSettings.LastOpenScene = scenePath;
  334. ProjectSettings.Save();
  335. };
  336. Action<DialogBox.ResultType> dialogCallback =
  337. (result) =>
  338. {
  339. if (result == DialogBox.ResultType.Yes)
  340. {
  341. SaveScene();
  342. continueLoad(path);
  343. }
  344. else if (result == DialogBox.ResultType.No)
  345. continueLoad(path);
  346. };
  347. if (IsSceneModified())
  348. {
  349. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  350. DialogBox.Type.YesNoCancel, dialogCallback);
  351. }
  352. else
  353. continueLoad(path);
  354. }
  355. /// <summary>
  356. /// Saves the currently loaded scene to the specified path.
  357. /// </summary>
  358. /// <param name="path">Path relative to the resource folder. This can be the path to the existing scene
  359. /// prefab if it just needs updating. </param>
  360. public static void SaveScene(string path)
  361. {
  362. Prefab scene = Internal_SaveScene(path);
  363. Scene.SetActive(scene);
  364. ProjectLibrary.Refresh(true);
  365. SetSceneDirty(false);
  366. }
  367. /// <summary>
  368. /// Checks does the folder at the provieded path contain a valid project.
  369. /// </summary>
  370. /// <param name="path">Absolute path to the root project folder.</param>
  371. /// <returns>True if the folder contains a valid project.</returns>
  372. public static bool IsValidProject(string path)
  373. {
  374. return Internal_IsValidProject(path);
  375. }
  376. /// <summary>
  377. /// Contains a new project in the provided folder.
  378. /// </summary>
  379. /// <param name="path">Absolute path to the folder to create the project in. Name of this folder will be used as the
  380. /// project's name.</param>
  381. public static void CreateProject(string path)
  382. {
  383. Internal_CreateProject(path);
  384. }
  385. /// <summary>
  386. /// Wrapper for menu items for <see cref="SaveScene(Action, Action)"/> method
  387. /// </summary>
  388. [MenuItem("File/Save Scene", ButtonModifier.Ctrl, ButtonCode.S, 10049)]
  389. [ToolbarItem("Save Scene", ToolbarIcon.SaveScene, "Save scene (Ctrl + S)", 1998)]
  390. private static void SaveSceneMenu()
  391. {
  392. SaveScene();
  393. }
  394. /// <summary>
  395. /// Wrapper for menu items for <see cref="SaveSceneAs(Action, Action)"/> method
  396. /// </summary>
  397. [MenuItem("File/Save Scene As", 10048)]
  398. private static void SaveSceneAsMenu()
  399. {
  400. SaveSceneAs();
  401. }
  402. /// <summary>
  403. /// Opens a Project Window allowing you to browse for or create a project.
  404. /// </summary>
  405. [MenuItem("File/Open Project", 10100)]
  406. [ToolbarItem("Open Project", ToolbarIcon.OpenProject, "Project manager", 2000)]
  407. public static void BrowseForProject()
  408. {
  409. ProjectWindow.Open();
  410. }
  411. /// <summary>
  412. /// Saves all data in the currently open project.
  413. /// </summary>
  414. [MenuItem("File/Save Project", 10099)]
  415. [ToolbarItem("Save Project", ToolbarIcon.SaveProject, "Save project", 1999)]
  416. public static void SaveProject()
  417. {
  418. foreach (var resourceUUID in dirtyResources)
  419. {
  420. string path = ProjectLibrary.GetPath(resourceUUID);
  421. Resource resource = ProjectLibrary.Load<Resource>(path);
  422. if(resource != null)
  423. ProjectLibrary.Save(resource);
  424. }
  425. dirtyResources.Clear();
  426. SetStatusProject(false);
  427. Internal_SaveProject();
  428. }
  429. /// <summary>
  430. /// Loads the project at the specified path. This method executes asynchronously.
  431. /// </summary>
  432. /// <param name="path">Absolute path to the project's root folder.</param>
  433. public static void LoadProject(string path)
  434. {
  435. if (IsProjectLoaded && path == ProjectPath)
  436. return;
  437. if (!Internal_IsValidProject(path))
  438. {
  439. Debug.LogWarning("Provided path: \"" + path + "\" is not a valid project.");
  440. return;
  441. }
  442. if (IsProjectLoaded)
  443. UnloadProject();
  444. Internal_LoadProject(path); // Triggers Internal_OnProjectLoaded when done
  445. }
  446. /// <summary>
  447. /// Closes the editor.
  448. /// </summary>
  449. public static void Quit()
  450. {
  451. Internal_Quit();
  452. }
  453. /// <summary>
  454. /// Toggles an existing toolbar button into an on or off state which changes the visuals of the button.
  455. /// </summary>
  456. /// <param name="name">Name of the existing button to toggle</param>
  457. /// <param name="on">True to toggle on, false to toggle off (default)</param>
  458. public static void ToggleToolbarItem(string name, bool on)
  459. {
  460. Internal_ToggleToolbarItem(name, on);
  461. }
  462. /// <summary>
  463. /// Opens a file or a folder in the default external application.
  464. /// </summary>
  465. /// <param name="path">Absolute path to the file or folder to open.</param>
  466. public static void OpenExternally(string path)
  467. {
  468. Internal_OpenExternally(path);
  469. }
  470. /// <summary>
  471. /// Marks a resource as dirty so that it may be saved the next time the project is saved. Optionally you may also
  472. /// call <see cref="ProjectLibrary.Save"/> to save it immediately.
  473. /// </summary>
  474. /// <param name="resource">Resource to mark as dirty</param>
  475. public static void SetDirty(Resource resource)
  476. {
  477. if (resource == null)
  478. return;
  479. SetStatusProject(true);
  480. dirtyResources.Add(resource.UUID);
  481. }
  482. /// <summary>
  483. /// Marks the current scene as dirty.
  484. /// </summary>
  485. public static void SetSceneDirty()
  486. {
  487. SetSceneDirty(true);
  488. }
  489. /// <summary>
  490. /// Marks the current scene as clean or dirty.
  491. /// </summary>
  492. /// <param name="dirty">Should the scene be marked as clean or dirty.</param>
  493. internal static void SetSceneDirty(bool dirty)
  494. {
  495. sceneDirty = dirty;
  496. SetStatusScene(Scene.ActiveSceneName, dirty);
  497. if (!dirty)
  498. dirtyResources.Remove(Scene.ActiveSceneUUID);
  499. }
  500. /// <summary>
  501. /// Checks is the specific resource dirty and needs saving.
  502. /// </summary>
  503. /// <param name="resource">Resource to check.</param>
  504. /// <returns>True if the resource requires saving, false otherwise.</returns>
  505. public static bool IsDirty(Resource resource)
  506. {
  507. return dirtyResources.Contains(resource.UUID);
  508. }
  509. /// <summary>
  510. /// Unloads the currently loaded project. Offers the user a chance to save the current scene if it is modified.
  511. /// Automatically saves all project data before unloading.
  512. /// </summary>
  513. private static void UnloadProject()
  514. {
  515. Action continueUnload =
  516. () =>
  517. {
  518. Scene.Clear();
  519. if (monitor != null)
  520. {
  521. monitor.Destroy();
  522. monitor = null;
  523. }
  524. LibraryWindow window = EditorWindow.GetWindow<LibraryWindow>();
  525. if(window != null)
  526. window.Reset();
  527. SetSceneDirty(false);
  528. Internal_UnloadProject();
  529. SetStatusProject(false);
  530. };
  531. Action<DialogBox.ResultType> dialogCallback =
  532. (result) =>
  533. {
  534. if (result == DialogBox.ResultType.Yes)
  535. SaveScene();
  536. continueUnload();
  537. };
  538. if (IsSceneModified())
  539. {
  540. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  541. DialogBox.Type.YesNoCancel, dialogCallback);
  542. }
  543. else
  544. continueUnload();
  545. }
  546. /// <summary>
  547. /// Reloads all script assemblies in case they were modified. This action is delayed and will be executed
  548. /// at the beginning of the next frame.
  549. /// </summary>
  550. public static void ReloadAssemblies()
  551. {
  552. Internal_ReloadAssemblies();
  553. }
  554. /// <summary>
  555. /// Changes the scene displayed on the status bar.
  556. /// </summary>
  557. /// <param name="name">Name of the scene.</param>
  558. /// <param name="modified">Whether to display the scene as modified or not.</param>
  559. private static void SetStatusScene(string name, bool modified)
  560. {
  561. Internal_SetStatusScene(name, modified);
  562. }
  563. /// <summary>
  564. /// Changes the project state displayed on the status bar.
  565. /// </summary>
  566. /// <param name="modified">Whether to display the project as modified or not.</param>
  567. private static void SetStatusProject(bool modified)
  568. {
  569. Internal_SetStatusProject(modified);
  570. }
  571. /// <summary>
  572. /// Displays or hides the "compilation in progress" visual on the status bar.
  573. /// </summary>
  574. /// <param name="compiling">True to display the visual, false otherwise.</param>
  575. internal static void SetStatusCompiling(bool compiling)
  576. {
  577. Internal_SetStatusCompiling(compiling);
  578. }
  579. /// <summary>
  580. /// Checks did we make any modifications to the scene since it was last saved.
  581. /// </summary>
  582. /// <returns>True if the scene was never saved, or was modified after last save.</returns>
  583. public static bool IsSceneModified()
  584. {
  585. return sceneDirty;
  586. }
  587. /// <summary>
  588. /// Runs a single frame of the game and pauses it. If the game is not currently running it will be started.
  589. /// </summary>
  590. public static void FrameStep()
  591. {
  592. if (IsStopped)
  593. {
  594. if (EditorSettings.GetBool(ConsoleWindow.CLEAR_ON_PLAY_KEY, true))
  595. {
  596. Debug.Clear();
  597. ConsoleWindow console = EditorWindow.GetWindow<ConsoleWindow>();
  598. if (console != null)
  599. console.Refresh();
  600. }
  601. }
  602. ToggleToolbarItem("Play", false);
  603. ToggleToolbarItem("Pause", true);
  604. Internal_FrameStep();
  605. }
  606. /// <summary>
  607. /// Executes any editor-specific unit tests. This should be called after a project is loaded if possible.
  608. /// </summary>
  609. private static void RunUnitTests()
  610. {
  611. #if DEBUG
  612. Internal_RunUnitTests();
  613. #endif
  614. }
  615. /// <summary>
  616. /// Triggered by the runtime when <see cref="LoadProject"/> method completes.
  617. /// </summary>
  618. private static void Internal_OnProjectLoaded()
  619. {
  620. SetStatusProject(false);
  621. if (!unitTestsExecuted)
  622. {
  623. RunUnitTests();
  624. unitTestsExecuted = true;
  625. }
  626. if (!IsProjectLoaded)
  627. {
  628. ProjectWindow.Open();
  629. return;
  630. }
  631. string projectPath = ProjectPath;
  632. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  633. bool foundPath = false;
  634. for (int i = 0; i < recentProjects.Length; i++)
  635. {
  636. if (PathEx.Compare(recentProjects[i].path, projectPath))
  637. {
  638. recentProjects[i].accessTimestamp = (ulong)DateTime.Now.Ticks;
  639. EditorSettings.RecentProjects = recentProjects;
  640. foundPath = true;
  641. break;
  642. }
  643. }
  644. if (!foundPath)
  645. {
  646. List<RecentProject> extendedRecentProjects = new List<RecentProject>();
  647. extendedRecentProjects.AddRange(recentProjects);
  648. RecentProject newProject = new RecentProject();
  649. newProject.path = projectPath;
  650. newProject.accessTimestamp = (ulong)DateTime.Now.Ticks;
  651. extendedRecentProjects.Add(newProject);
  652. EditorSettings.RecentProjects = extendedRecentProjects.ToArray();
  653. }
  654. EditorSettings.LastOpenProject = projectPath;
  655. EditorSettings.Save();
  656. ProjectLibrary.Refresh();
  657. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  658. monitor.OnAdded += OnAssetModified;
  659. monitor.OnRemoved += OnAssetModified;
  660. monitor.OnModified += OnAssetModified;
  661. if (!string.IsNullOrWhiteSpace(ProjectSettings.LastOpenScene))
  662. {
  663. Scene.Load(ProjectSettings.LastOpenScene);
  664. SetSceneDirty(false);
  665. }
  666. }
  667. /// <summary>
  668. /// Triggered by the runtime when the user clicks on the status bar.
  669. /// </summary>
  670. private static void Internal_OnStatusBarClicked()
  671. {
  672. EditorWindow.OpenWindow<ConsoleWindow>();
  673. }
  674. [MethodImpl(MethodImplOptions.InternalCall)]
  675. private static extern void Internal_SetStatusScene(string name, bool modified);
  676. [MethodImpl(MethodImplOptions.InternalCall)]
  677. private static extern void Internal_SetStatusProject(bool modified);
  678. [MethodImpl(MethodImplOptions.InternalCall)]
  679. private static extern void Internal_SetStatusCompiling(bool compiling);
  680. [MethodImpl(MethodImplOptions.InternalCall)]
  681. private static extern string Internal_GetProjectPath();
  682. [MethodImpl(MethodImplOptions.InternalCall)]
  683. private static extern string Internal_GetProjectName();
  684. [MethodImpl(MethodImplOptions.InternalCall)]
  685. private static extern bool Internal_GetProjectLoaded();
  686. [MethodImpl(MethodImplOptions.InternalCall)]
  687. private static extern string Internal_GetCompilerPath();
  688. [MethodImpl(MethodImplOptions.InternalCall)]
  689. private static extern string Internal_GetBuiltinReleaseAssemblyPath();
  690. [MethodImpl(MethodImplOptions.InternalCall)]
  691. private static extern string Internal_GetBuiltinDebugAssemblyPath();
  692. [MethodImpl(MethodImplOptions.InternalCall)]
  693. private static extern string Internal_GetScriptAssemblyPath();
  694. [MethodImpl(MethodImplOptions.InternalCall)]
  695. private static extern string Internal_GetFrameworkAssemblyPath();
  696. [MethodImpl(MethodImplOptions.InternalCall)]
  697. private static extern string Internal_GetEngineAssemblyName();
  698. [MethodImpl(MethodImplOptions.InternalCall)]
  699. private static extern string Internal_GetEditorAssemblyName();
  700. [MethodImpl(MethodImplOptions.InternalCall)]
  701. private static extern string Internal_GetScriptGameAssemblyName();
  702. [MethodImpl(MethodImplOptions.InternalCall)]
  703. private static extern string Internal_GetScriptEditorAssemblyName();
  704. [MethodImpl(MethodImplOptions.InternalCall)]
  705. private static extern Prefab Internal_SaveScene(string path);
  706. [MethodImpl(MethodImplOptions.InternalCall)]
  707. private static extern bool Internal_IsValidProject(string path);
  708. [MethodImpl(MethodImplOptions.InternalCall)]
  709. private static extern void Internal_SaveProject();
  710. [MethodImpl(MethodImplOptions.InternalCall)]
  711. private static extern void Internal_LoadProject(string path);
  712. [MethodImpl(MethodImplOptions.InternalCall)]
  713. private static extern void Internal_UnloadProject();
  714. [MethodImpl(MethodImplOptions.InternalCall)]
  715. private static extern void Internal_CreateProject(string path);
  716. [MethodImpl(MethodImplOptions.InternalCall)]
  717. private static extern void Internal_ReloadAssemblies();
  718. [MethodImpl(MethodImplOptions.InternalCall)]
  719. private static extern void Internal_OpenExternally(string path);
  720. [MethodImpl(MethodImplOptions.InternalCall)]
  721. private static extern void Internal_RunUnitTests();
  722. [MethodImpl(MethodImplOptions.InternalCall)]
  723. private static extern void Internal_Quit();
  724. [MethodImpl(MethodImplOptions.InternalCall)]
  725. private static extern void Internal_ToggleToolbarItem(string name, bool on);
  726. [MethodImpl(MethodImplOptions.InternalCall)]
  727. private static extern bool Internal_GetIsPlaying();
  728. [MethodImpl(MethodImplOptions.InternalCall)]
  729. private static extern void Internal_SetIsPlaying(bool value);
  730. [MethodImpl(MethodImplOptions.InternalCall)]
  731. private static extern bool Internal_GetIsPaused();
  732. [MethodImpl(MethodImplOptions.InternalCall)]
  733. private static extern void Internal_SetIsPaused(bool value);
  734. [MethodImpl(MethodImplOptions.InternalCall)]
  735. private static extern void Internal_FrameStep();
  736. [MethodImpl(MethodImplOptions.InternalCall)]
  737. private static extern void Internal_SetMainRenderTarget(IntPtr rendertarget);
  738. [MethodImpl(MethodImplOptions.InternalCall)]
  739. private static extern bool Internal_HasFocus();
  740. }
  741. }