EditorApplication.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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>().Camera; }
  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. }
  91. private static void OnEditorLoad()
  92. {
  93. if (EditorSettings.AutoLoadLastProject)
  94. {
  95. string projectPath = EditorSettings.LastOpenProject;
  96. if (Internal_IsValidProject(projectPath))
  97. LoadProject(projectPath);
  98. else
  99. ProjectWindow.Open();
  100. }
  101. else
  102. ProjectWindow.Open();
  103. }
  104. private static void OnAssetModified(string path)
  105. {
  106. ProjectLibrary.Refresh(path);
  107. }
  108. internal void OnEditorUpdate()
  109. {
  110. ProjectLibrary.Update();
  111. }
  112. [MenuItem("File/Open Scene", ButtonModifier.Ctrl, ButtonCode.L, 50, true)]
  113. private static void LoadScene()
  114. {
  115. string[] scenePaths;
  116. if (BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths))
  117. {
  118. if (scenePaths.Length > 0)
  119. LoadScene(scenePaths[0]);
  120. }
  121. }
  122. [MenuItem("File/Save Scene", ButtonModifier.Ctrl, ButtonCode.S, 49)]
  123. private static void SaveScene()
  124. {
  125. if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID))
  126. {
  127. string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
  128. Internal_SaveScene(scenePath);
  129. }
  130. else
  131. SaveSceneAs();
  132. }
  133. [MenuItem("File/Save Scene As", 48)]
  134. private static void SaveSceneAs()
  135. {
  136. string scenePath = "";
  137. if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath))
  138. {
  139. if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
  140. DialogBox.Open("Error", "The location must be inside the Resources folder of the project.",
  141. DialogBox.Type.OK);
  142. else
  143. {
  144. // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
  145. // Internal_SaveScene will silently fail.
  146. Scene.ActiveSceneUUID = Internal_SaveScene(scenePath + ".prefab");
  147. }
  148. }
  149. }
  150. public static void LoadScene(string path)
  151. {
  152. Action<string> continueLoad =
  153. (scenePath) =>
  154. {
  155. Scene.Load(path);
  156. ProjectSettings.LastOpenScene = scenePath;
  157. ProjectSettings.Save();
  158. };
  159. Action<DialogBox.ResultType> dialogCallback =
  160. (result) =>
  161. {
  162. if (result == DialogBox.ResultType.Yes)
  163. {
  164. SaveScene();
  165. continueLoad(path);
  166. }
  167. else if (result == DialogBox.ResultType.No)
  168. continueLoad(path);
  169. };
  170. if (Scene.IsModified())
  171. {
  172. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  173. DialogBox.Type.YesNoCancel, dialogCallback);
  174. }
  175. else
  176. continueLoad(path);
  177. }
  178. public static bool IsValidProject(string path)
  179. {
  180. return Internal_IsValidProject(path);
  181. }
  182. public static void CreateProject(string path)
  183. {
  184. Internal_CreateProject(path);
  185. }
  186. [MenuItem("File/Open Project", 100)]
  187. public static void BrowseForProject()
  188. {
  189. ProjectWindow.Open();
  190. }
  191. [MenuItem("File/Save Project", 99)]
  192. public static void SaveProject()
  193. {
  194. // TODO - Save dirty resources
  195. Internal_SaveProject();
  196. }
  197. // Note: Async, runs next frame
  198. public static void LoadProject(string path)
  199. {
  200. if (IsProjectLoaded && path == ProjectPath)
  201. return;
  202. if (!Internal_IsValidProject(path))
  203. {
  204. Debug.LogWarning("Provided path: \"" + path + "\" is not a valid project.");
  205. return;
  206. }
  207. if (IsProjectLoaded)
  208. UnloadProject();
  209. Internal_LoadProject(path); // Triggers OnProjectLoaded when done
  210. }
  211. /// <summary>
  212. /// Opens a file or a folder in the default external application.
  213. /// </summary>
  214. /// <param name="path">Absolute path to the file or folder to open.</param>
  215. public static void OpenExternally(string path)
  216. {
  217. Internal_OpenExternally(path);
  218. }
  219. private static void OnProjectLoaded()
  220. {
  221. if (!IsProjectLoaded)
  222. {
  223. ProjectWindow.Open();
  224. return;
  225. }
  226. string projectPath = ProjectPath;
  227. RecentProject[] recentProjects = EditorSettings.RecentProjects;
  228. bool foundPath = false;
  229. for (int i = 0; i < recentProjects.Length; i++)
  230. {
  231. if (PathEx.Compare(recentProjects[i].path, projectPath))
  232. {
  233. recentProjects[i].accessTimestamp = (ulong)DateTime.Now.Ticks;
  234. EditorSettings.RecentProjects = recentProjects;
  235. foundPath = true;
  236. break;
  237. }
  238. }
  239. if (!foundPath)
  240. {
  241. List<RecentProject> extendedRecentProjects = new List<RecentProject>();
  242. extendedRecentProjects.AddRange(recentProjects);
  243. RecentProject newProject = new RecentProject();
  244. newProject.path = projectPath;
  245. newProject.accessTimestamp = (ulong)DateTime.Now.Ticks;
  246. extendedRecentProjects.Add(newProject);
  247. EditorSettings.RecentProjects = extendedRecentProjects.ToArray();
  248. }
  249. EditorSettings.LastOpenProject = projectPath;
  250. EditorSettings.Save();
  251. ProjectLibrary.Refresh();
  252. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  253. monitor.OnAdded += OnAssetModified;
  254. monitor.OnRemoved += OnAssetModified;
  255. monitor.OnModified += OnAssetModified;
  256. if (!string.IsNullOrWhiteSpace(ProjectSettings.LastOpenScene))
  257. Scene.Load(ProjectSettings.LastOpenScene);
  258. }
  259. private static void UnloadProject()
  260. {
  261. Action continueUnload =
  262. () =>
  263. {
  264. Scene.Clear();
  265. if (monitor != null)
  266. {
  267. monitor.Destroy();
  268. monitor = null;
  269. }
  270. LibraryWindow window = EditorWindow.GetWindow<LibraryWindow>();
  271. if(window != null)
  272. window.Reset();
  273. Internal_UnloadProject();
  274. };
  275. Action<DialogBox.ResultType> dialogCallback =
  276. (result) =>
  277. {
  278. if (result == DialogBox.ResultType.Yes)
  279. SaveScene();
  280. continueUnload();
  281. };
  282. if (Scene.IsModified())
  283. {
  284. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  285. DialogBox.Type.YesNoCancel, dialogCallback);
  286. }
  287. else
  288. continueUnload();
  289. }
  290. [MethodImpl(MethodImplOptions.InternalCall)]
  291. private static extern string Internal_GetProjectPath();
  292. [MethodImpl(MethodImplOptions.InternalCall)]
  293. private static extern string Internal_GetProjectName();
  294. [MethodImpl(MethodImplOptions.InternalCall)]
  295. private static extern bool Internal_GetProjectLoaded();
  296. [MethodImpl(MethodImplOptions.InternalCall)]
  297. private static extern string Internal_GetCompilerPath();
  298. [MethodImpl(MethodImplOptions.InternalCall)]
  299. private static extern string Internal_GetBuiltinAssemblyPath();
  300. [MethodImpl(MethodImplOptions.InternalCall)]
  301. private static extern string Internal_GetScriptAssemblyPath();
  302. [MethodImpl(MethodImplOptions.InternalCall)]
  303. private static extern string Internal_GetFrameworkAssemblyPath();
  304. [MethodImpl(MethodImplOptions.InternalCall)]
  305. private static extern string Internal_GetEngineAssemblyName();
  306. [MethodImpl(MethodImplOptions.InternalCall)]
  307. private static extern string Internal_GetEditorAssemblyName();
  308. [MethodImpl(MethodImplOptions.InternalCall)]
  309. private static extern string Internal_GetScriptGameAssemblyName();
  310. [MethodImpl(MethodImplOptions.InternalCall)]
  311. private static extern string Internal_GetScriptEditorAssemblyName();
  312. [MethodImpl(MethodImplOptions.InternalCall)]
  313. private static extern string Internal_SaveScene(string path);
  314. [MethodImpl(MethodImplOptions.InternalCall)]
  315. private static extern bool Internal_IsValidProject(string path);
  316. [MethodImpl(MethodImplOptions.InternalCall)]
  317. private static extern void Internal_SaveProject();
  318. [MethodImpl(MethodImplOptions.InternalCall)]
  319. private static extern void Internal_LoadProject(string path);
  320. [MethodImpl(MethodImplOptions.InternalCall)]
  321. private static extern void Internal_UnloadProject();
  322. [MethodImpl(MethodImplOptions.InternalCall)]
  323. private static extern void Internal_CreateProject(string path);
  324. [MethodImpl(MethodImplOptions.InternalCall)]
  325. private static extern void Internal_OpenExternally(string path);
  326. }
  327. }