EditorApplication.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. public enum SceneViewTool
  9. {
  10. View,
  11. Move,
  12. Rotate,
  13. Scale
  14. }
  15. public enum HandlePivotMode
  16. {
  17. Center,
  18. Pivot
  19. }
  20. public enum HandleCoordinateMode
  21. {
  22. Local,
  23. World
  24. }
  25. public enum EditorPlatformType
  26. {
  27. Windows
  28. }
  29. public class EditorApplication
  30. {
  31. public static SceneViewTool ActiveSceneTool
  32. {
  33. get { return EditorSettings.ActiveSceneTool; }
  34. set { EditorSettings.ActiveSceneTool = value; }
  35. }
  36. public static HandleCoordinateMode ActiveCoordinateMode
  37. {
  38. get { return EditorSettings.ActiveCoordinateMode; }
  39. set { EditorSettings.ActiveCoordinateMode = value; }
  40. }
  41. public static HandlePivotMode ActivePivotMode
  42. {
  43. get { return EditorSettings.ActivePivotMode; }
  44. set { EditorSettings.ActivePivotMode = value; }
  45. }
  46. public static Camera SceneViewCamera
  47. {
  48. get { return EditorWindow.GetWindow<SceneWindow>().GetCamera(); }
  49. }
  50. public static EditorPlatformType EditorPlatform
  51. {
  52. get { return EditorPlatformType.Windows; } // TODO - Set this properly once we have support for more platforms
  53. }
  54. public static string ProjectPath { get { return Internal_GetProjectPath(); } }
  55. public static string ProjectName { get { return Internal_GetProjectName(); } }
  56. public static bool IsProjectLoaded { get { return Internal_GetProjectLoaded(); } }
  57. internal static string CompilerPath { get { return Internal_GetCompilerPath(); } }
  58. internal static string BuiltinAssemblyPath { get { return Internal_GetBuiltinAssemblyPath(); } }
  59. internal static string ScriptAssemblyPath { get { return Internal_GetScriptAssemblyPath(); } }
  60. internal static string FrameworkAssemblyPath { get { return Internal_GetFrameworkAssemblyPath(); } }
  61. internal static string EngineAssembly { get { return Internal_GetEngineAssemblyName(); } }
  62. internal static string EditorAssembly { get { return Internal_GetEditorAssemblyName(); } }
  63. internal static string ScriptGameAssembly { get { return Internal_GetScriptGameAssemblyName(); } }
  64. internal static string ScriptEditorAssembly { get { return Internal_GetScriptEditorAssemblyName(); } }
  65. private static EditorApplication instance;
  66. private static FolderMonitor monitor;
  67. internal EditorApplication()
  68. {
  69. instance = this;
  70. // Register controls
  71. InputConfiguration inputConfig = VirtualInput.KeyConfig;
  72. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.W);
  73. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.S);
  74. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.A);
  75. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.D);
  76. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.Up);
  77. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.Down);
  78. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.Left);
  79. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.Right);
  80. inputConfig.RegisterButton(SceneCamera.FastMoveBinding, ButtonCode.LeftShift);
  81. inputConfig.RegisterButton(SceneCamera.RotateBinding, ButtonCode.MouseRight);
  82. inputConfig.RegisterAxis(SceneCamera.HorizontalAxisBinding, InputAxis.MouseX);
  83. inputConfig.RegisterAxis(SceneCamera.VerticalAxisBinding, InputAxis.MouseY);
  84. inputConfig.RegisterButton(SceneWindow.ToggleProfilerOverlayBinding, ButtonCode.P, ButtonModifier.CtrlAlt);
  85. inputConfig.RegisterButton(SceneWindow.ViewToolBinding, ButtonCode.Q);
  86. inputConfig.RegisterButton(SceneWindow.MoveToolBinding, ButtonCode.W);
  87. inputConfig.RegisterButton(SceneWindow.RotateToolBinding, ButtonCode.E);
  88. inputConfig.RegisterButton(SceneWindow.ScaleToolBinding, ButtonCode.R);
  89. inputConfig.RegisterButton(SceneWindow.DuplicateBinding, ButtonCode.D, ButtonModifier.Ctrl);
  90. if (EditorSettings.AutoLoadLastProject)
  91. {
  92. string projectPath = EditorSettings.LastOpenProject;
  93. if (Internal_IsValidProject(projectPath))
  94. LoadProject(projectPath);
  95. else
  96. ProjectWindow.Open();
  97. }
  98. else
  99. ProjectWindow.Open();
  100. }
  101. private static void OnAssetModified(string path)
  102. {
  103. ProjectLibrary.Refresh(path);
  104. }
  105. internal void OnEditorUpdate()
  106. {
  107. ProjectLibrary.Update();
  108. }
  109. [MenuItem("File/Save Scene", ButtonModifier.Ctrl, ButtonCode.S, 50, true)]
  110. private static void SaveScene()
  111. {
  112. if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID))
  113. {
  114. string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
  115. Internal_SaveScene(scenePath);
  116. }
  117. else
  118. SaveSceneAs();
  119. }
  120. [MenuItem("File/Save Scene As", 50)]
  121. private static void SaveSceneAs()
  122. {
  123. string scenePath = "";
  124. if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath))
  125. {
  126. if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
  127. DialogBox.Open("Error", "The location must be inside the Resources folder of the project.",
  128. DialogBox.Type.OK);
  129. else
  130. {
  131. // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
  132. // Internal_SaveScene will silently fail.
  133. Scene.ActiveSceneUUID = Internal_SaveScene(scenePath + ".prefab");
  134. }
  135. }
  136. }
  137. [MenuItem("File/Load Scene", ButtonModifier.Ctrl, ButtonCode.L, 50)]
  138. private static void LoadScene()
  139. {
  140. string[] scenePaths;
  141. if (BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths))
  142. {
  143. if (scenePaths.Length > 0)
  144. LoadScene(scenePaths[0]);
  145. }
  146. }
  147. public static void LoadScene(string path)
  148. {
  149. Action<string> continueLoad =
  150. (scenePath) =>
  151. {
  152. Scene.Load(path);
  153. ProjectSettings.LastOpenScene = scenePath;
  154. ProjectSettings.Save();
  155. };
  156. Action<DialogBox.ResultType> dialogCallback =
  157. (result) =>
  158. {
  159. if (result == DialogBox.ResultType.Yes)
  160. {
  161. SaveScene();
  162. continueLoad(path);
  163. }
  164. else if (result == DialogBox.ResultType.No)
  165. continueLoad(path);
  166. };
  167. if (Scene.IsModified())
  168. {
  169. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  170. DialogBox.Type.YesNoCancel, dialogCallback);
  171. }
  172. else
  173. continueLoad(path);
  174. }
  175. public static bool IsValidProject(string path)
  176. {
  177. return Internal_IsValidProject(path);
  178. }
  179. [MenuItem("File/Create Project", 0)]
  180. public static void CreateProject()
  181. {
  182. string projectPath = EditorSettings.LastOpenProject;
  183. if (!Directory.Exists(projectPath))
  184. projectPath = Directory.GetCurrentDirectory();
  185. string selectedPath;
  186. if (BrowseDialog.OpenFolder(projectPath, "", out selectedPath))
  187. {
  188. CreateProject(selectedPath);
  189. LoadProject(selectedPath);
  190. }
  191. }
  192. public static void CreateProject(string path)
  193. {
  194. Internal_CreateProject(path);
  195. }
  196. [MenuItem("File/Load Project", 0)]
  197. public static void BrowseForProject()
  198. {
  199. string projectPath = EditorSettings.LastOpenProject;
  200. if (!Directory.Exists(projectPath))
  201. projectPath = Directory.GetCurrentDirectory();
  202. string selectedPath;
  203. if (BrowseDialog.OpenFolder(projectPath, "", out selectedPath))
  204. LoadProject(selectedPath);
  205. }
  206. [MenuItem("File/Save Project", 0)]
  207. public static void SaveProject()
  208. {
  209. // TODO - Save dirty resources
  210. Internal_SaveProject();
  211. }
  212. // Note: Async, runs next frame
  213. public static void LoadProject(string path)
  214. {
  215. if (IsProjectLoaded && path == ProjectPath)
  216. return;
  217. if (!Internal_IsValidProject(path))
  218. {
  219. Debug.LogWarning("Provided path: \"" + path + "\" is not a valid project.");
  220. return;
  221. }
  222. if (IsProjectLoaded)
  223. {
  224. SaveProject();
  225. UnloadProject();
  226. }
  227. Internal_LoadProject(path); // Triggers OnProjectLoaded when done
  228. }
  229. private static void OnProjectLoaded()
  230. {
  231. if (!IsProjectLoaded)
  232. {
  233. ProjectWindow.Open();
  234. return;
  235. }
  236. Debug.Log("PRE COLLECT 0");
  237. GC.Collect();
  238. Debug.Log("POST COLLECT 0");
  239. string projectPath = ProjectPath;
  240. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  241. bool foundPath = false;
  242. for (int i = 0; i < recentProjects.Length; i++)
  243. {
  244. if (PathEx.Compare(recentProjects[i].path, projectPath))
  245. {
  246. recentProjects[i].accessTimestamp = (ulong)DateTime.Now.Ticks;
  247. EditorSettings.RecentProjects = recentProjects;
  248. foundPath = true;
  249. break;
  250. }
  251. }
  252. if (!foundPath)
  253. {
  254. List<RecentProject> extendedRecentProjects = new List<RecentProject>();
  255. extendedRecentProjects.AddRange(recentProjects);
  256. RecentProject newProject = new RecentProject();
  257. newProject.path = projectPath;
  258. newProject.accessTimestamp = (ulong)DateTime.Now.Ticks;
  259. extendedRecentProjects.Add(newProject);
  260. EditorSettings.RecentProjects = extendedRecentProjects.ToArray();
  261. }
  262. EditorSettings.LastOpenProject = projectPath;
  263. EditorSettings.Save();
  264. ProjectLibrary.Refresh();
  265. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  266. monitor.OnAdded += OnAssetModified;
  267. monitor.OnRemoved += OnAssetModified;
  268. monitor.OnModified += OnAssetModified;
  269. if (!string.IsNullOrWhiteSpace(ProjectSettings.LastOpenScene))
  270. Scene.Load(ProjectSettings.LastOpenScene);
  271. }
  272. private static void UnloadProject()
  273. {
  274. Action continueUnload =
  275. () =>
  276. {
  277. Scene.Clear();
  278. if (monitor != null)
  279. {
  280. monitor.Destroy();
  281. monitor = null;
  282. }
  283. LibraryWindow window = EditorWindow.GetWindow<LibraryWindow>();
  284. if(window != null)
  285. window.Reset();
  286. Internal_UnloadProject();
  287. };
  288. Action<DialogBox.ResultType> dialogCallback =
  289. (result) =>
  290. {
  291. if (result == DialogBox.ResultType.Yes)
  292. SaveScene();
  293. continueUnload();
  294. };
  295. if (Scene.IsModified())
  296. {
  297. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  298. DialogBox.Type.YesNoCancel, dialogCallback);
  299. }
  300. else
  301. continueUnload();
  302. }
  303. [MenuItem("Components/Camera")]
  304. private static void AddCamera()
  305. {
  306. SceneObject so = Selection.sceneObject;
  307. if (so == null)
  308. return;
  309. UndoRedo.RecordSO(so, "Added a Camera component");
  310. so.AddComponent<Camera>();
  311. }
  312. [MenuItem("Components/Renderable")]
  313. private static void AddRenderable()
  314. {
  315. SceneObject so = Selection.sceneObject;
  316. if (so == null)
  317. return;
  318. UndoRedo.RecordSO(so, "Added a Renderable component");
  319. so.AddComponent<Renderable>();
  320. }
  321. [MenuItem("Components/Point light")]
  322. private static void AddPointLight()
  323. {
  324. SceneObject so = Selection.sceneObject;
  325. if (so == null)
  326. return;
  327. UndoRedo.RecordSO(so, "Added a Light component");
  328. Light light = so.AddComponent<Light>();
  329. light.Type = LightType.Point;
  330. }
  331. [MenuItem("Components/Spot light")]
  332. private static void AddSpotLight()
  333. {
  334. SceneObject so = Selection.sceneObject;
  335. if (so == null)
  336. return;
  337. UndoRedo.RecordSO(so, "Added a Light component");
  338. Light light = so.AddComponent<Light>();
  339. light.Type = LightType.Spot;
  340. }
  341. [MenuItem("Components/Directional light")]
  342. private static void AddDirectionalLight()
  343. {
  344. SceneObject so = Selection.sceneObject;
  345. if (so == null)
  346. return;
  347. UndoRedo.RecordSO(so, "Added a Light component");
  348. Light light = so.AddComponent<Light>();
  349. light.Type = LightType.Directional;
  350. }
  351. [MenuItem("Scene Objects/Camera")]
  352. private static void AddCameraSO()
  353. {
  354. SceneObject so = UndoRedo.CreateSO("Camera", "Created a Camera");
  355. so.AddComponent<Camera>();
  356. Selection.sceneObject = so;
  357. }
  358. [MenuItem("Scene Objects/Renderable")]
  359. private static void AddRenderableSO()
  360. {
  361. SceneObject so = UndoRedo.CreateSO("Renderable", "Created a Renderable");
  362. so.AddComponent<Renderable>();
  363. Selection.sceneObject = so;
  364. }
  365. [MenuItem("Scene Objects/Point light")]
  366. private static void AddPointLightSO()
  367. {
  368. SceneObject so = UndoRedo.CreateSO("Point light", "Created a Light");
  369. Light light = so.AddComponent<Light>();
  370. light.Type = LightType.Point;
  371. Selection.sceneObject = so;
  372. }
  373. [MenuItem("Scene Objects/Spot light")]
  374. private static void AddSpotLightSO()
  375. {
  376. SceneObject so = UndoRedo.CreateSO("Spot light", "Created a Light");
  377. Light light = so.AddComponent<Light>();
  378. light.Type = LightType.Spot;
  379. Selection.sceneObject = so;
  380. }
  381. [MenuItem("Scene Objects/Directional light")]
  382. private static void AddDirectionalLightSO()
  383. {
  384. SceneObject so = UndoRedo.CreateSO("Directional light", "Created a Light");
  385. Light light = so.AddComponent<Light>();
  386. light.Type = LightType.Directional;
  387. Selection.sceneObject = so;
  388. }
  389. [MethodImpl(MethodImplOptions.InternalCall)]
  390. private static extern string Internal_GetProjectPath();
  391. [MethodImpl(MethodImplOptions.InternalCall)]
  392. private static extern string Internal_GetProjectName();
  393. [MethodImpl(MethodImplOptions.InternalCall)]
  394. private static extern bool Internal_GetProjectLoaded();
  395. [MethodImpl(MethodImplOptions.InternalCall)]
  396. private static extern string Internal_GetCompilerPath();
  397. [MethodImpl(MethodImplOptions.InternalCall)]
  398. private static extern string Internal_GetBuiltinAssemblyPath();
  399. [MethodImpl(MethodImplOptions.InternalCall)]
  400. private static extern string Internal_GetScriptAssemblyPath();
  401. [MethodImpl(MethodImplOptions.InternalCall)]
  402. private static extern string Internal_GetFrameworkAssemblyPath();
  403. [MethodImpl(MethodImplOptions.InternalCall)]
  404. private static extern string Internal_GetEngineAssemblyName();
  405. [MethodImpl(MethodImplOptions.InternalCall)]
  406. private static extern string Internal_GetEditorAssemblyName();
  407. [MethodImpl(MethodImplOptions.InternalCall)]
  408. private static extern string Internal_GetScriptGameAssemblyName();
  409. [MethodImpl(MethodImplOptions.InternalCall)]
  410. private static extern string Internal_GetScriptEditorAssemblyName();
  411. [MethodImpl(MethodImplOptions.InternalCall)]
  412. private static extern string Internal_SaveScene(string path);
  413. [MethodImpl(MethodImplOptions.InternalCall)]
  414. private static extern bool Internal_IsValidProject(string path);
  415. [MethodImpl(MethodImplOptions.InternalCall)]
  416. private static extern void Internal_SaveProject();
  417. [MethodImpl(MethodImplOptions.InternalCall)]
  418. private static extern void Internal_LoadProject(string path);
  419. [MethodImpl(MethodImplOptions.InternalCall)]
  420. private static extern void Internal_UnloadProject();
  421. [MethodImpl(MethodImplOptions.InternalCall)]
  422. private static extern void Internal_CreateProject(string path);
  423. }
  424. }