2
0

EditorApplication.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 Prefab", ButtonModifier.Ctrl, ButtonCode.S, 50, true)]
  103. private static void SavePrefab()
  104. {
  105. if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID))
  106. {
  107. string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
  108. Internal_SaveScene(scenePath);
  109. }
  110. else
  111. SavePrefabAs();
  112. }
  113. [MenuItem("File/Save Prefab As", 50)]
  114. private static void SavePrefabAs()
  115. {
  116. string scenePath = "";
  117. BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "", out scenePath);
  118. if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
  119. DialogBox.Open("Error", "The location must be inside the Resources folder of the project.", DialogBox.Type.OK);
  120. else
  121. {
  122. // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
  123. // Internal_SaveScene will silently fail.
  124. Scene.ActiveSceneUUID = Internal_SaveScene(scenePath);
  125. }
  126. }
  127. [MenuItem("File/Load Prefab", ButtonModifier.Ctrl, ButtonCode.L, 50)]
  128. private static void LoadPrefab()
  129. {
  130. Action doLoad =
  131. () =>
  132. {
  133. string[] scenePaths;
  134. BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths);
  135. if(scenePaths.Length > 0)
  136. Scene.Load(scenePaths[0]);
  137. };
  138. Action<DialogBox.ResultType> dialogCallback =
  139. (result) =>
  140. {
  141. if (result == DialogBox.ResultType.Yes)
  142. {
  143. SavePrefab();
  144. doLoad();
  145. }
  146. else if (result == DialogBox.ResultType.No)
  147. doLoad();
  148. };
  149. if (Scene.IsModified())
  150. {
  151. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  152. DialogBox.Type.YesNoCancel, dialogCallback);
  153. }
  154. else
  155. doLoad();
  156. }
  157. [MenuItem("Components/Camera")]
  158. private static void AddCamera()
  159. {
  160. SceneObject so = Selection.sceneObject;
  161. if (so == null)
  162. return;
  163. UndoRedo.RecordSO(so, "Added a Camera component");
  164. so.AddComponent<Camera>();
  165. }
  166. [MenuItem("Components/Renderable")]
  167. private static void AddRenderable()
  168. {
  169. SceneObject so = Selection.sceneObject;
  170. if (so == null)
  171. return;
  172. UndoRedo.RecordSO(so, "Added a Renderable component");
  173. so.AddComponent<Renderable>();
  174. }
  175. [MenuItem("Components/Point light")]
  176. private static void AddPointLight()
  177. {
  178. SceneObject so = Selection.sceneObject;
  179. if (so == null)
  180. return;
  181. UndoRedo.RecordSO(so, "Added a Light component");
  182. Light light = so.AddComponent<Light>();
  183. light.Type = LightType.Point;
  184. }
  185. [MenuItem("Components/Spot light")]
  186. private static void AddSpotLight()
  187. {
  188. SceneObject so = Selection.sceneObject;
  189. if (so == null)
  190. return;
  191. UndoRedo.RecordSO(so, "Added a Light component");
  192. Light light = so.AddComponent<Light>();
  193. light.Type = LightType.Spot;
  194. }
  195. [MenuItem("Components/Directional light")]
  196. private static void AddDirectionalLight()
  197. {
  198. SceneObject so = Selection.sceneObject;
  199. if (so == null)
  200. return;
  201. UndoRedo.RecordSO(so, "Added a Light component");
  202. Light light = so.AddComponent<Light>();
  203. light.Type = LightType.Directional;
  204. }
  205. [MenuItem("Scene Objects/Camera")]
  206. private static void AddCameraSO()
  207. {
  208. SceneObject so = UndoRedo.CreateSO("Camera", "Created a Camera");
  209. so.AddComponent<Camera>();
  210. Selection.sceneObject = so;
  211. }
  212. [MenuItem("Scene Objects/Renderable")]
  213. private static void AddRenderableSO()
  214. {
  215. SceneObject so = UndoRedo.CreateSO("Renderable", "Created a Renderable");
  216. so.AddComponent<Renderable>();
  217. Selection.sceneObject = so;
  218. }
  219. [MenuItem("Scene Objects/Point light")]
  220. private static void AddPointLightSO()
  221. {
  222. SceneObject so = UndoRedo.CreateSO("Point light", "Created a Light");
  223. Light light = so.AddComponent<Light>();
  224. light.Type = LightType.Point;
  225. Selection.sceneObject = so;
  226. }
  227. [MenuItem("Scene Objects/Spot light")]
  228. private static void AddSpotLightSO()
  229. {
  230. SceneObject so = UndoRedo.CreateSO("Spot light", "Created a Light");
  231. Light light = so.AddComponent<Light>();
  232. light.Type = LightType.Spot;
  233. Selection.sceneObject = so;
  234. }
  235. [MenuItem("Scene Objects/Directional light")]
  236. private static void AddDirectionalLightSO()
  237. {
  238. SceneObject so = UndoRedo.CreateSO("Directional light", "Created a Light");
  239. Light light = so.AddComponent<Light>();
  240. light.Type = LightType.Directional;
  241. Selection.sceneObject = so;
  242. }
  243. [MethodImpl(MethodImplOptions.InternalCall)]
  244. private static extern string Internal_GetProjectPath();
  245. [MethodImpl(MethodImplOptions.InternalCall)]
  246. private static extern string Internal_GetProjectName();
  247. [MethodImpl(MethodImplOptions.InternalCall)]
  248. private static extern string Internal_GetCompilerPath();
  249. [MethodImpl(MethodImplOptions.InternalCall)]
  250. private static extern string Internal_GetBuiltinAssemblyPath();
  251. [MethodImpl(MethodImplOptions.InternalCall)]
  252. private static extern string Internal_GetScriptAssemblyPath();
  253. [MethodImpl(MethodImplOptions.InternalCall)]
  254. private static extern string Internal_GetFrameworkAssemblyPath();
  255. [MethodImpl(MethodImplOptions.InternalCall)]
  256. private static extern string Internal_GetEngineAssemblyName();
  257. [MethodImpl(MethodImplOptions.InternalCall)]
  258. private static extern string Internal_GetEditorAssemblyName();
  259. [MethodImpl(MethodImplOptions.InternalCall)]
  260. private static extern string Internal_GetScriptGameAssemblyName();
  261. [MethodImpl(MethodImplOptions.InternalCall)]
  262. private static extern string Internal_GetScriptEditorAssemblyName();
  263. [MethodImpl(MethodImplOptions.InternalCall)]
  264. private static extern string Internal_SaveScene(string path);
  265. }
  266. }