EditorApplication.cs 18 KB

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