EditorApplication.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. internal static string CompilerPath { get { return Internal_GetCompilerPath(); } }
  56. internal static string BuiltinAssemblyPath { get { return Internal_GetBuiltinAssemblyPath(); } }
  57. internal static string ScriptAssemblyPath { get { return Internal_GetScriptAssemblyPath(); } }
  58. internal static string FrameworkAssemblyPath { get { return Internal_GetFrameworkAssemblyPath(); } }
  59. internal static string EngineAssembly { get { return Internal_GetEngineAssemblyName(); } }
  60. internal static string EditorAssembly { get { return Internal_GetEditorAssemblyName(); } }
  61. internal static string ScriptGameAssembly { get { return Internal_GetScriptGameAssemblyName(); } }
  62. internal static string ScriptEditorAssembly { get { return Internal_GetScriptEditorAssemblyName(); } }
  63. private static EditorApplication instance;
  64. private FolderMonitor monitor;
  65. internal EditorApplication()
  66. {
  67. instance = this;
  68. // Register controls
  69. InputConfiguration inputConfig = VirtualInput.KeyConfig;
  70. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.W);
  71. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.S);
  72. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.A);
  73. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.D);
  74. inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.Up);
  75. inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.Down);
  76. inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.Left);
  77. inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.Right);
  78. inputConfig.RegisterButton(SceneCamera.FastMoveBinding, ButtonCode.LeftShift);
  79. inputConfig.RegisterButton(SceneCamera.RotateBinding, ButtonCode.MouseRight);
  80. inputConfig.RegisterAxis(SceneCamera.HorizontalAxisBinding, InputAxis.MouseX);
  81. inputConfig.RegisterAxis(SceneCamera.VerticalAxisBinding, InputAxis.MouseY);
  82. inputConfig.RegisterButton(SceneWindow.ToggleProfilerOverlayBinding, ButtonCode.P, ButtonModifier.CtrlAlt);
  83. inputConfig.RegisterButton(SceneWindow.ViewToolBinding, ButtonCode.Q);
  84. inputConfig.RegisterButton(SceneWindow.MoveToolBinding, ButtonCode.W);
  85. inputConfig.RegisterButton(SceneWindow.RotateToolBinding, ButtonCode.E);
  86. inputConfig.RegisterButton(SceneWindow.ScaleToolBinding, ButtonCode.R);
  87. inputConfig.RegisterButton(SceneWindow.DuplicateBinding, ButtonCode.D, ButtonModifier.Ctrl);
  88. ProjectLibrary.Refresh();
  89. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  90. monitor.OnAdded += OnAssetModified;
  91. monitor.OnRemoved += OnAssetModified;
  92. monitor.OnModified += OnAssetModified;
  93. }
  94. private void OnAssetModified(string path)
  95. {
  96. ProjectLibrary.Refresh(path);
  97. }
  98. internal void OnEditorUpdate()
  99. {
  100. ProjectLibrary.Update();
  101. }
  102. [MenuItem("File/Save Scene", ButtonModifier.Ctrl, ButtonCode.S, 50, true)]
  103. private static void SaveScene()
  104. {
  105. if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID))
  106. {
  107. string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
  108. Internal_SaveScene(scenePath);
  109. }
  110. else
  111. SaveSceneAs();
  112. }
  113. [MenuItem("File/Save Scene As", 50)]
  114. private static void SaveSceneAs()
  115. {
  116. string scenePath = "";
  117. if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath))
  118. {
  119. if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
  120. DialogBox.Open("Error", "The location must be inside the Resources folder of the project.",
  121. DialogBox.Type.OK);
  122. else
  123. {
  124. // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
  125. // Internal_SaveScene will silently fail.
  126. Scene.ActiveSceneUUID = Internal_SaveScene(scenePath + ".prefab");
  127. }
  128. }
  129. }
  130. [MenuItem("File/Load Scene", ButtonModifier.Ctrl, ButtonCode.L, 50)]
  131. private static void LoadScene()
  132. {
  133. string[] scenePaths;
  134. if (BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths))
  135. {
  136. if (scenePaths.Length > 0)
  137. LoadScene(scenePaths[0]);
  138. }
  139. }
  140. public static void LoadScene(string path)
  141. {
  142. Action<DialogBox.ResultType> dialogCallback =
  143. (result) =>
  144. {
  145. if (result == DialogBox.ResultType.Yes)
  146. {
  147. SaveScene();
  148. Scene.Load(path);
  149. }
  150. else if (result == DialogBox.ResultType.No)
  151. Scene.Load(path);
  152. };
  153. if (Scene.IsModified())
  154. {
  155. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  156. DialogBox.Type.YesNoCancel, dialogCallback);
  157. }
  158. else
  159. Scene.Load(path);
  160. }
  161. [MenuItem("Components/Camera")]
  162. private static void AddCamera()
  163. {
  164. SceneObject so = Selection.sceneObject;
  165. if (so == null)
  166. return;
  167. UndoRedo.RecordSO(so, "Added a Camera component");
  168. so.AddComponent<Camera>();
  169. }
  170. [MenuItem("Components/Renderable")]
  171. private static void AddRenderable()
  172. {
  173. SceneObject so = Selection.sceneObject;
  174. if (so == null)
  175. return;
  176. UndoRedo.RecordSO(so, "Added a Renderable component");
  177. so.AddComponent<Renderable>();
  178. }
  179. [MenuItem("Components/Point light")]
  180. private static void AddPointLight()
  181. {
  182. SceneObject so = Selection.sceneObject;
  183. if (so == null)
  184. return;
  185. UndoRedo.RecordSO(so, "Added a Light component");
  186. Light light = so.AddComponent<Light>();
  187. light.Type = LightType.Point;
  188. }
  189. [MenuItem("Components/Spot light")]
  190. private static void AddSpotLight()
  191. {
  192. SceneObject so = Selection.sceneObject;
  193. if (so == null)
  194. return;
  195. UndoRedo.RecordSO(so, "Added a Light component");
  196. Light light = so.AddComponent<Light>();
  197. light.Type = LightType.Spot;
  198. }
  199. [MenuItem("Components/Directional light")]
  200. private static void AddDirectionalLight()
  201. {
  202. SceneObject so = Selection.sceneObject;
  203. if (so == null)
  204. return;
  205. UndoRedo.RecordSO(so, "Added a Light component");
  206. Light light = so.AddComponent<Light>();
  207. light.Type = LightType.Directional;
  208. }
  209. [MenuItem("Scene Objects/Camera")]
  210. private static void AddCameraSO()
  211. {
  212. SceneObject so = UndoRedo.CreateSO("Camera", "Created a Camera");
  213. so.AddComponent<Camera>();
  214. Selection.sceneObject = so;
  215. }
  216. [MenuItem("Scene Objects/Renderable")]
  217. private static void AddRenderableSO()
  218. {
  219. SceneObject so = UndoRedo.CreateSO("Renderable", "Created a Renderable");
  220. so.AddComponent<Renderable>();
  221. Selection.sceneObject = so;
  222. }
  223. [MenuItem("Scene Objects/Point light")]
  224. private static void AddPointLightSO()
  225. {
  226. SceneObject so = UndoRedo.CreateSO("Point light", "Created a Light");
  227. Light light = so.AddComponent<Light>();
  228. light.Type = LightType.Point;
  229. Selection.sceneObject = so;
  230. }
  231. [MenuItem("Scene Objects/Spot light")]
  232. private static void AddSpotLightSO()
  233. {
  234. SceneObject so = UndoRedo.CreateSO("Spot light", "Created a Light");
  235. Light light = so.AddComponent<Light>();
  236. light.Type = LightType.Spot;
  237. Selection.sceneObject = so;
  238. }
  239. [MenuItem("Scene Objects/Directional light")]
  240. private static void AddDirectionalLightSO()
  241. {
  242. SceneObject so = UndoRedo.CreateSO("Directional light", "Created a Light");
  243. Light light = so.AddComponent<Light>();
  244. light.Type = LightType.Directional;
  245. Selection.sceneObject = so;
  246. }
  247. [MethodImpl(MethodImplOptions.InternalCall)]
  248. private static extern string Internal_GetProjectPath();
  249. [MethodImpl(MethodImplOptions.InternalCall)]
  250. private static extern string Internal_GetProjectName();
  251. [MethodImpl(MethodImplOptions.InternalCall)]
  252. private static extern string Internal_GetCompilerPath();
  253. [MethodImpl(MethodImplOptions.InternalCall)]
  254. private static extern string Internal_GetBuiltinAssemblyPath();
  255. [MethodImpl(MethodImplOptions.InternalCall)]
  256. private static extern string Internal_GetScriptAssemblyPath();
  257. [MethodImpl(MethodImplOptions.InternalCall)]
  258. private static extern string Internal_GetFrameworkAssemblyPath();
  259. [MethodImpl(MethodImplOptions.InternalCall)]
  260. private static extern string Internal_GetEngineAssemblyName();
  261. [MethodImpl(MethodImplOptions.InternalCall)]
  262. private static extern string Internal_GetEditorAssemblyName();
  263. [MethodImpl(MethodImplOptions.InternalCall)]
  264. private static extern string Internal_GetScriptGameAssemblyName();
  265. [MethodImpl(MethodImplOptions.InternalCall)]
  266. private static extern string Internal_GetScriptEditorAssemblyName();
  267. [MethodImpl(MethodImplOptions.InternalCall)]
  268. private static extern string Internal_SaveScene(string path);
  269. }
  270. }