EditorApplication.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. public enum SceneViewTool
  8. {
  9. View,
  10. Move,
  11. Rotate,
  12. Scale
  13. }
  14. public enum HandlePivotMode
  15. {
  16. Center,
  17. Pivot
  18. }
  19. public enum HandleCoordinateMode
  20. {
  21. Local,
  22. World
  23. }
  24. public enum EditorPlatformType
  25. {
  26. Windows
  27. }
  28. public class EditorApplication
  29. {
  30. public static SceneViewTool ActiveSceneTool
  31. {
  32. get { return EditorSettings.ActiveSceneTool; }
  33. set { EditorSettings.ActiveSceneTool = value; }
  34. }
  35. public static HandleCoordinateMode ActiveCoordinateMode
  36. {
  37. get { return EditorSettings.ActiveCoordinateMode; }
  38. set { EditorSettings.ActiveCoordinateMode = value; }
  39. }
  40. public static HandlePivotMode ActivePivotMode
  41. {
  42. get { return EditorSettings.ActivePivotMode; }
  43. set { EditorSettings.ActivePivotMode = value; }
  44. }
  45. public static Camera SceneViewCamera
  46. {
  47. get { return EditorWindow.GetWindow<SceneWindow>().GetCamera(); }
  48. }
  49. public static EditorPlatformType EditorPlatform
  50. {
  51. get { return EditorPlatformType.Windows; } // TODO - Set this properly once we have support for more platforms
  52. }
  53. public static string ProjectPath { get { return Internal_GetProjectPath(); } }
  54. public static string ProjectName { get { return Internal_GetProjectName(); } }
  55. public static bool IsProjectLoaded { get { return Internal_GetProjectLoaded(); } }
  56. internal static string CompilerPath { get { return Internal_GetCompilerPath(); } }
  57. internal static string BuiltinAssemblyPath { get { return Internal_GetBuiltinAssemblyPath(); } }
  58. internal static string ScriptAssemblyPath { get { return Internal_GetScriptAssemblyPath(); } }
  59. internal static string FrameworkAssemblyPath { get { return Internal_GetFrameworkAssemblyPath(); } }
  60. internal static string EngineAssembly { get { return Internal_GetEngineAssemblyName(); } }
  61. internal static string EditorAssembly { get { return Internal_GetEditorAssemblyName(); } }
  62. internal static string ScriptGameAssembly { get { return Internal_GetScriptGameAssemblyName(); } }
  63. internal static string ScriptEditorAssembly { get { return Internal_GetScriptEditorAssemblyName(); } }
  64. private static EditorApplication instance;
  65. private static FolderMonitor monitor;
  66. internal EditorApplication()
  67. {
  68. instance = this;
  69. // Register controls
  70. InputConfiguration inputConfig = VirtualInput.KeyConfig;
  71. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.W);
  72. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.S);
  73. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.A);
  74. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.D);
  75. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.Up);
  76. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.Down);
  77. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.Left);
  78. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.Right);
  79. inputConfig.RegisterButton(SceneCamera.FastMoveBinding, ButtonCode.LeftShift);
  80. inputConfig.RegisterButton(SceneCamera.RotateBinding, ButtonCode.MouseRight);
  81. inputConfig.RegisterAxis(SceneCamera.HorizontalAxisBinding, InputAxis.MouseX);
  82. inputConfig.RegisterAxis(SceneCamera.VerticalAxisBinding, InputAxis.MouseY);
  83. inputConfig.RegisterButton(SceneWindow.ToggleProfilerOverlayBinding, ButtonCode.P, ButtonModifier.CtrlAlt);
  84. inputConfig.RegisterButton(SceneWindow.ViewToolBinding, ButtonCode.Q);
  85. inputConfig.RegisterButton(SceneWindow.MoveToolBinding, ButtonCode.W);
  86. inputConfig.RegisterButton(SceneWindow.RotateToolBinding, ButtonCode.E);
  87. inputConfig.RegisterButton(SceneWindow.ScaleToolBinding, ButtonCode.R);
  88. inputConfig.RegisterButton(SceneWindow.DuplicateBinding, ButtonCode.D, ButtonModifier.Ctrl);
  89. if (EditorSettings.AutoLoadLastProject)
  90. {
  91. string projectPath = EditorSettings.LastOpenProject;
  92. if (Internal_IsValidProject(projectPath))
  93. LoadProject(projectPath);
  94. }
  95. if (!IsProjectLoaded)
  96. ProjectWindow.Show();
  97. }
  98. private static void OnAssetModified(string path)
  99. {
  100. ProjectLibrary.Refresh(path);
  101. }
  102. internal void OnEditorUpdate()
  103. {
  104. ProjectLibrary.Update();
  105. }
  106. [MenuItem("File/Save Scene", ButtonModifier.Ctrl, ButtonCode.S, 50, true)]
  107. private static void SaveScene()
  108. {
  109. if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID))
  110. {
  111. string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
  112. Internal_SaveScene(scenePath);
  113. }
  114. else
  115. SaveSceneAs();
  116. }
  117. [MenuItem("File/Save Scene As", 50)]
  118. private static void SaveSceneAs()
  119. {
  120. string scenePath = "";
  121. if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath))
  122. {
  123. if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
  124. DialogBox.Open("Error", "The location must be inside the Resources folder of the project.",
  125. DialogBox.Type.OK);
  126. else
  127. {
  128. // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
  129. // Internal_SaveScene will silently fail.
  130. Scene.ActiveSceneUUID = Internal_SaveScene(scenePath + ".prefab");
  131. }
  132. }
  133. }
  134. [MenuItem("File/Load Scene", ButtonModifier.Ctrl, ButtonCode.L, 50)]
  135. private static void LoadScene()
  136. {
  137. string[] scenePaths;
  138. if (BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths))
  139. {
  140. if (scenePaths.Length > 0)
  141. LoadScene(scenePaths[0]);
  142. }
  143. }
  144. public static void LoadScene(string path)
  145. {
  146. Action<string> continueLoad =
  147. (scenePath) =>
  148. {
  149. Scene.Load(path);
  150. ProjectSettings.LastOpenScene = scenePath;
  151. ProjectSettings.Save();
  152. };
  153. Action<DialogBox.ResultType> dialogCallback =
  154. (result) =>
  155. {
  156. if (result == DialogBox.ResultType.Yes)
  157. {
  158. SaveScene();
  159. continueLoad(path);
  160. }
  161. else if (result == DialogBox.ResultType.No)
  162. continueLoad(path);
  163. };
  164. if (Scene.IsModified())
  165. {
  166. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  167. DialogBox.Type.YesNoCancel, dialogCallback);
  168. }
  169. else
  170. continueLoad(path);
  171. }
  172. public static void LoadProject(string path)
  173. {
  174. if (Internal_IsValidProject(path))
  175. {
  176. Debug.LogWarning("Provided path: \"" + path + "\" is not a valid project.");
  177. return;
  178. }
  179. Internal_LoadProject(path);
  180. ProjectLibrary.Refresh();
  181. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  182. monitor.OnAdded += OnAssetModified;
  183. monitor.OnRemoved += OnAssetModified;
  184. monitor.OnModified += OnAssetModified;
  185. if(!string.IsNullOrWhiteSpace(ProjectSettings.LastOpenScene))
  186. Scene.Load(ProjectSettings.LastOpenScene);
  187. }
  188. public static void UnloadProject()
  189. {
  190. // TODO - Save dirty assets
  191. Action continueUnload =
  192. () =>
  193. {
  194. Scene.Clear();
  195. if (monitor != null)
  196. {
  197. monitor.Destroy();
  198. monitor = null;
  199. }
  200. LibraryWindow window = EditorWindow.GetWindow<LibraryWindow>();
  201. if(window != null)
  202. window.Reset();
  203. Internal_UnloadProject();
  204. };
  205. Action<DialogBox.ResultType> dialogCallback =
  206. (result) =>
  207. {
  208. if (result == DialogBox.ResultType.Yes)
  209. SaveScene();
  210. continueUnload();
  211. };
  212. if (Scene.IsModified())
  213. {
  214. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  215. DialogBox.Type.YesNoCancel, dialogCallback);
  216. }
  217. else
  218. continueUnload();
  219. }
  220. [MenuItem("Components/Camera")]
  221. private static void AddCamera()
  222. {
  223. SceneObject so = Selection.sceneObject;
  224. if (so == null)
  225. return;
  226. UndoRedo.RecordSO(so, "Added a Camera component");
  227. so.AddComponent<Camera>();
  228. }
  229. [MenuItem("Components/Renderable")]
  230. private static void AddRenderable()
  231. {
  232. SceneObject so = Selection.sceneObject;
  233. if (so == null)
  234. return;
  235. UndoRedo.RecordSO(so, "Added a Renderable component");
  236. so.AddComponent<Renderable>();
  237. }
  238. [MenuItem("Components/Point light")]
  239. private static void AddPointLight()
  240. {
  241. SceneObject so = Selection.sceneObject;
  242. if (so == null)
  243. return;
  244. UndoRedo.RecordSO(so, "Added a Light component");
  245. Light light = so.AddComponent<Light>();
  246. light.Type = LightType.Point;
  247. }
  248. [MenuItem("Components/Spot light")]
  249. private static void AddSpotLight()
  250. {
  251. SceneObject so = Selection.sceneObject;
  252. if (so == null)
  253. return;
  254. UndoRedo.RecordSO(so, "Added a Light component");
  255. Light light = so.AddComponent<Light>();
  256. light.Type = LightType.Spot;
  257. }
  258. [MenuItem("Components/Directional light")]
  259. private static void AddDirectionalLight()
  260. {
  261. SceneObject so = Selection.sceneObject;
  262. if (so == null)
  263. return;
  264. UndoRedo.RecordSO(so, "Added a Light component");
  265. Light light = so.AddComponent<Light>();
  266. light.Type = LightType.Directional;
  267. }
  268. [MenuItem("Scene Objects/Camera")]
  269. private static void AddCameraSO()
  270. {
  271. SceneObject so = UndoRedo.CreateSO("Camera", "Created a Camera");
  272. so.AddComponent<Camera>();
  273. Selection.sceneObject = so;
  274. }
  275. [MenuItem("Scene Objects/Renderable")]
  276. private static void AddRenderableSO()
  277. {
  278. SceneObject so = UndoRedo.CreateSO("Renderable", "Created a Renderable");
  279. so.AddComponent<Renderable>();
  280. Selection.sceneObject = so;
  281. }
  282. [MenuItem("Scene Objects/Point light")]
  283. private static void AddPointLightSO()
  284. {
  285. SceneObject so = UndoRedo.CreateSO("Point light", "Created a Light");
  286. Light light = so.AddComponent<Light>();
  287. light.Type = LightType.Point;
  288. Selection.sceneObject = so;
  289. }
  290. [MenuItem("Scene Objects/Spot light")]
  291. private static void AddSpotLightSO()
  292. {
  293. SceneObject so = UndoRedo.CreateSO("Spot light", "Created a Light");
  294. Light light = so.AddComponent<Light>();
  295. light.Type = LightType.Spot;
  296. Selection.sceneObject = so;
  297. }
  298. [MenuItem("Scene Objects/Directional light")]
  299. private static void AddDirectionalLightSO()
  300. {
  301. SceneObject so = UndoRedo.CreateSO("Directional light", "Created a Light");
  302. Light light = so.AddComponent<Light>();
  303. light.Type = LightType.Directional;
  304. Selection.sceneObject = so;
  305. }
  306. [MethodImpl(MethodImplOptions.InternalCall)]
  307. private static extern string Internal_GetProjectPath();
  308. [MethodImpl(MethodImplOptions.InternalCall)]
  309. private static extern string Internal_GetProjectName();
  310. [MethodImpl(MethodImplOptions.InternalCall)]
  311. private static extern bool Internal_GetProjectLoaded();
  312. [MethodImpl(MethodImplOptions.InternalCall)]
  313. private static extern string Internal_GetCompilerPath();
  314. [MethodImpl(MethodImplOptions.InternalCall)]
  315. private static extern string Internal_GetBuiltinAssemblyPath();
  316. [MethodImpl(MethodImplOptions.InternalCall)]
  317. private static extern string Internal_GetScriptAssemblyPath();
  318. [MethodImpl(MethodImplOptions.InternalCall)]
  319. private static extern string Internal_GetFrameworkAssemblyPath();
  320. [MethodImpl(MethodImplOptions.InternalCall)]
  321. private static extern string Internal_GetEngineAssemblyName();
  322. [MethodImpl(MethodImplOptions.InternalCall)]
  323. private static extern string Internal_GetEditorAssemblyName();
  324. [MethodImpl(MethodImplOptions.InternalCall)]
  325. private static extern string Internal_GetScriptGameAssemblyName();
  326. [MethodImpl(MethodImplOptions.InternalCall)]
  327. private static extern string Internal_GetScriptEditorAssemblyName();
  328. [MethodImpl(MethodImplOptions.InternalCall)]
  329. private static extern string Internal_SaveScene(string path);
  330. [MethodImpl(MethodImplOptions.InternalCall)]
  331. private static extern bool Internal_IsValidProject(string path);
  332. [MethodImpl(MethodImplOptions.InternalCall)]
  333. private static extern void Internal_LoadProject(string path);
  334. [MethodImpl(MethodImplOptions.InternalCall)]
  335. private static extern void Internal_UnloadProject();
  336. }
  337. }