EditorApplication.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. }
  96. }
  97. private static void OnAssetModified(string path)
  98. {
  99. ProjectLibrary.Refresh(path);
  100. }
  101. internal void OnEditorUpdate()
  102. {
  103. ProjectLibrary.Update();
  104. }
  105. [MenuItem("File/Save Scene", ButtonModifier.Ctrl, ButtonCode.S, 50, true)]
  106. private static void SaveScene()
  107. {
  108. if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID))
  109. {
  110. string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
  111. Internal_SaveScene(scenePath);
  112. }
  113. else
  114. SaveSceneAs();
  115. }
  116. [MenuItem("File/Save Scene As", 50)]
  117. private static void SaveSceneAs()
  118. {
  119. string scenePath = "";
  120. if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath))
  121. {
  122. if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
  123. DialogBox.Open("Error", "The location must be inside the Resources folder of the project.",
  124. DialogBox.Type.OK);
  125. else
  126. {
  127. // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
  128. // Internal_SaveScene will silently fail.
  129. Scene.ActiveSceneUUID = Internal_SaveScene(scenePath + ".prefab");
  130. }
  131. }
  132. }
  133. [MenuItem("File/Load Scene", ButtonModifier.Ctrl, ButtonCode.L, 50)]
  134. private static void LoadScene()
  135. {
  136. string[] scenePaths;
  137. if (BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths))
  138. {
  139. if (scenePaths.Length > 0)
  140. LoadScene(scenePaths[0]);
  141. }
  142. }
  143. public static void LoadScene(string path)
  144. {
  145. Action<string> continueLoad =
  146. (scenePath) =>
  147. {
  148. Scene.Load(path);
  149. ProjectSettings.LastOpenScene = scenePath;
  150. ProjectSettings.Save();
  151. };
  152. Action<DialogBox.ResultType> dialogCallback =
  153. (result) =>
  154. {
  155. if (result == DialogBox.ResultType.Yes)
  156. {
  157. SaveScene();
  158. continueLoad(path);
  159. }
  160. else if (result == DialogBox.ResultType.No)
  161. continueLoad(path);
  162. };
  163. if (Scene.IsModified())
  164. {
  165. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  166. DialogBox.Type.YesNoCancel, dialogCallback);
  167. }
  168. else
  169. continueLoad(path);
  170. }
  171. public static bool IsValidProject(string path)
  172. {
  173. return Internal_IsValidProject(path);
  174. }
  175. [MenuItem("File/Create Project", 0)]
  176. public static void CreateProject()
  177. {
  178. string projectPath = EditorSettings.LastOpenProject;
  179. if (!Directory.Exists(projectPath))
  180. projectPath = Directory.GetCurrentDirectory();
  181. string selectedPath;
  182. if (BrowseDialog.OpenFolder(projectPath, "", out selectedPath))
  183. {
  184. CreateProject(selectedPath);
  185. LoadProject(selectedPath);
  186. }
  187. }
  188. public static void CreateProject(string path)
  189. {
  190. Internal_CreateProject(path);
  191. }
  192. [MenuItem("File/Load Project", 0)]
  193. public static void BrowseForProject()
  194. {
  195. string projectPath = EditorSettings.LastOpenProject;
  196. if (!Directory.Exists(projectPath))
  197. projectPath = Directory.GetCurrentDirectory();
  198. string selectedPath;
  199. if (BrowseDialog.OpenFolder(projectPath, "", out selectedPath))
  200. LoadProject(selectedPath);
  201. }
  202. [MenuItem("File/Save Project", 0)]
  203. public static void SaveProject()
  204. {
  205. // TODO - Save dirty resources
  206. Internal_SaveProject();
  207. }
  208. // Note: Async, runs next frame
  209. public static void LoadProject(string path)
  210. {
  211. if (IsProjectLoaded && path == ProjectPath)
  212. return;
  213. if (!Internal_IsValidProject(path))
  214. {
  215. Debug.LogWarning("Provided path: \"" + path + "\" is not a valid project.");
  216. return;
  217. }
  218. if (IsProjectLoaded)
  219. {
  220. SaveProject();
  221. UnloadProject();
  222. }
  223. Internal_LoadProject(path); // Triggers OnProjectLoaded when done
  224. }
  225. private static void OnProjectLoaded()
  226. {
  227. if (!IsProjectLoaded)
  228. {
  229. ProjectWindow.Open();
  230. return;
  231. }
  232. string projectPath = ProjectPath;
  233. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  234. bool foundPath = false;
  235. for (int i = 0; i < recentProjects.Length; i++)
  236. {
  237. if (PathEx.Compare(recentProjects[i].path, projectPath))
  238. {
  239. recentProjects[i].accessTimestamp = (ulong)DateTime.Now.Ticks;
  240. EditorSettings.RecentProjects = recentProjects;
  241. foundPath = true;
  242. break;
  243. }
  244. }
  245. if (!foundPath)
  246. {
  247. List<RecentProject> extendedRecentProjects = new List<RecentProject>();
  248. extendedRecentProjects.AddRange(recentProjects);
  249. RecentProject newProject = new RecentProject();
  250. newProject.path = projectPath;
  251. newProject.accessTimestamp = (ulong)DateTime.Now.Ticks;
  252. extendedRecentProjects.Add(newProject);
  253. EditorSettings.RecentProjects = extendedRecentProjects.ToArray();
  254. }
  255. EditorSettings.LastOpenProject = projectPath;
  256. EditorSettings.Save();
  257. ProjectLibrary.Refresh();
  258. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  259. monitor.OnAdded += OnAssetModified;
  260. monitor.OnRemoved += OnAssetModified;
  261. monitor.OnModified += OnAssetModified;
  262. if (!string.IsNullOrWhiteSpace(ProjectSettings.LastOpenScene))
  263. Scene.Load(ProjectSettings.LastOpenScene);
  264. }
  265. private static void UnloadProject()
  266. {
  267. Action continueUnload =
  268. () =>
  269. {
  270. Scene.Clear();
  271. if (monitor != null)
  272. {
  273. monitor.Destroy();
  274. monitor = null;
  275. }
  276. LibraryWindow window = EditorWindow.GetWindow<LibraryWindow>();
  277. if(window != null)
  278. window.Reset();
  279. Internal_UnloadProject();
  280. };
  281. Action<DialogBox.ResultType> dialogCallback =
  282. (result) =>
  283. {
  284. if (result == DialogBox.ResultType.Yes)
  285. SaveScene();
  286. continueUnload();
  287. };
  288. if (Scene.IsModified())
  289. {
  290. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  291. DialogBox.Type.YesNoCancel, dialogCallback);
  292. }
  293. else
  294. continueUnload();
  295. }
  296. [MenuItem("Components/Camera")]
  297. private static void AddCamera()
  298. {
  299. SceneObject so = Selection.sceneObject;
  300. if (so == null)
  301. return;
  302. UndoRedo.RecordSO(so, "Added a Camera component");
  303. so.AddComponent<Camera>();
  304. }
  305. [MenuItem("Components/Renderable")]
  306. private static void AddRenderable()
  307. {
  308. SceneObject so = Selection.sceneObject;
  309. if (so == null)
  310. return;
  311. UndoRedo.RecordSO(so, "Added a Renderable component");
  312. so.AddComponent<Renderable>();
  313. }
  314. [MenuItem("Components/Point light")]
  315. private static void AddPointLight()
  316. {
  317. SceneObject so = Selection.sceneObject;
  318. if (so == null)
  319. return;
  320. UndoRedo.RecordSO(so, "Added a Light component");
  321. Light light = so.AddComponent<Light>();
  322. light.Type = LightType.Point;
  323. }
  324. [MenuItem("Components/Spot light")]
  325. private static void AddSpotLight()
  326. {
  327. SceneObject so = Selection.sceneObject;
  328. if (so == null)
  329. return;
  330. UndoRedo.RecordSO(so, "Added a Light component");
  331. Light light = so.AddComponent<Light>();
  332. light.Type = LightType.Spot;
  333. }
  334. [MenuItem("Components/Directional light")]
  335. private static void AddDirectionalLight()
  336. {
  337. SceneObject so = Selection.sceneObject;
  338. if (so == null)
  339. return;
  340. UndoRedo.RecordSO(so, "Added a Light component");
  341. Light light = so.AddComponent<Light>();
  342. light.Type = LightType.Directional;
  343. }
  344. [MenuItem("Scene Objects/Camera")]
  345. private static void AddCameraSO()
  346. {
  347. SceneObject so = UndoRedo.CreateSO("Camera", "Created a Camera");
  348. so.AddComponent<Camera>();
  349. Selection.sceneObject = so;
  350. }
  351. [MenuItem("Scene Objects/Renderable")]
  352. private static void AddRenderableSO()
  353. {
  354. SceneObject so = UndoRedo.CreateSO("Renderable", "Created a Renderable");
  355. so.AddComponent<Renderable>();
  356. Selection.sceneObject = so;
  357. }
  358. [MenuItem("Scene Objects/Point light")]
  359. private static void AddPointLightSO()
  360. {
  361. SceneObject so = UndoRedo.CreateSO("Point light", "Created a Light");
  362. Light light = so.AddComponent<Light>();
  363. light.Type = LightType.Point;
  364. Selection.sceneObject = so;
  365. }
  366. [MenuItem("Scene Objects/Spot light")]
  367. private static void AddSpotLightSO()
  368. {
  369. SceneObject so = UndoRedo.CreateSO("Spot light", "Created a Light");
  370. Light light = so.AddComponent<Light>();
  371. light.Type = LightType.Spot;
  372. Selection.sceneObject = so;
  373. }
  374. [MenuItem("Scene Objects/Directional light")]
  375. private static void AddDirectionalLightSO()
  376. {
  377. SceneObject so = UndoRedo.CreateSO("Directional light", "Created a Light");
  378. Light light = so.AddComponent<Light>();
  379. light.Type = LightType.Directional;
  380. Selection.sceneObject = so;
  381. }
  382. [MethodImpl(MethodImplOptions.InternalCall)]
  383. private static extern string Internal_GetProjectPath();
  384. [MethodImpl(MethodImplOptions.InternalCall)]
  385. private static extern string Internal_GetProjectName();
  386. [MethodImpl(MethodImplOptions.InternalCall)]
  387. private static extern bool Internal_GetProjectLoaded();
  388. [MethodImpl(MethodImplOptions.InternalCall)]
  389. private static extern string Internal_GetCompilerPath();
  390. [MethodImpl(MethodImplOptions.InternalCall)]
  391. private static extern string Internal_GetBuiltinAssemblyPath();
  392. [MethodImpl(MethodImplOptions.InternalCall)]
  393. private static extern string Internal_GetScriptAssemblyPath();
  394. [MethodImpl(MethodImplOptions.InternalCall)]
  395. private static extern string Internal_GetFrameworkAssemblyPath();
  396. [MethodImpl(MethodImplOptions.InternalCall)]
  397. private static extern string Internal_GetEngineAssemblyName();
  398. [MethodImpl(MethodImplOptions.InternalCall)]
  399. private static extern string Internal_GetEditorAssemblyName();
  400. [MethodImpl(MethodImplOptions.InternalCall)]
  401. private static extern string Internal_GetScriptGameAssemblyName();
  402. [MethodImpl(MethodImplOptions.InternalCall)]
  403. private static extern string Internal_GetScriptEditorAssemblyName();
  404. [MethodImpl(MethodImplOptions.InternalCall)]
  405. private static extern string Internal_SaveScene(string path);
  406. [MethodImpl(MethodImplOptions.InternalCall)]
  407. private static extern bool Internal_IsValidProject(string path);
  408. [MethodImpl(MethodImplOptions.InternalCall)]
  409. private static extern void Internal_SaveProject();
  410. [MethodImpl(MethodImplOptions.InternalCall)]
  411. private static extern void Internal_LoadProject(string path);
  412. [MethodImpl(MethodImplOptions.InternalCall)]
  413. private static extern void Internal_UnloadProject();
  414. [MethodImpl(MethodImplOptions.InternalCall)]
  415. private static extern void Internal_CreateProject(string path);
  416. }
  417. }