EditorApplication.cs 17 KB

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