EditorApplication.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. ProjectLibrary.Refresh();
  84. monitor = new FolderMonitor(ProjectLibrary.ResourceFolder);
  85. monitor.OnAdded += OnAssetModified;
  86. monitor.OnRemoved += OnAssetModified;
  87. monitor.OnModified += OnAssetModified;
  88. }
  89. private void OnAssetModified(string path)
  90. {
  91. ProjectLibrary.Refresh(path);
  92. }
  93. internal void OnEditorUpdate()
  94. {
  95. ProjectLibrary.Update();
  96. }
  97. [MenuItem("File/Save Prefab", ButtonModifier.Ctrl, ButtonCode.S)]
  98. private static void SavePrefab()
  99. {
  100. if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID))
  101. {
  102. string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID);
  103. Internal_SaveScene(scenePath);
  104. }
  105. else
  106. {
  107. string scenePath = "";
  108. BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "", out scenePath);
  109. if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
  110. DialogBox.Open("Error", "The location must be inside the Resources folder of the project.", DialogBox.Type.OK);
  111. else
  112. {
  113. // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
  114. // Internal_SaveScene will silently fail.
  115. Scene.ActiveSceneUUID = Internal_SaveScene(scenePath);
  116. }
  117. }
  118. }
  119. [MenuItem("File/Load Prefab", ButtonModifier.Ctrl, ButtonCode.L)]
  120. private static void LoadPrefab()
  121. {
  122. Action doLoad =
  123. () =>
  124. {
  125. string[] scenePaths;
  126. BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths);
  127. if(scenePaths.Length > 0)
  128. Scene.Load(scenePaths[0]);
  129. };
  130. Action<DialogBox.ResultType> dialogCallback =
  131. (result) =>
  132. {
  133. if (result == DialogBox.ResultType.Yes)
  134. {
  135. SavePrefab();
  136. doLoad();
  137. }
  138. else if (result == DialogBox.ResultType.No)
  139. doLoad();
  140. };
  141. if (Scene.IsModified())
  142. {
  143. DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?",
  144. DialogBox.Type.YesNoCancel, dialogCallback);
  145. }
  146. else
  147. doLoad();
  148. }
  149. [MethodImpl(MethodImplOptions.InternalCall)]
  150. private static extern string Internal_GetProjectPath();
  151. [MethodImpl(MethodImplOptions.InternalCall)]
  152. private static extern string Internal_GetProjectName();
  153. [MethodImpl(MethodImplOptions.InternalCall)]
  154. private static extern string Internal_GetCompilerPath();
  155. [MethodImpl(MethodImplOptions.InternalCall)]
  156. private static extern string Internal_GetBuiltinAssemblyPath();
  157. [MethodImpl(MethodImplOptions.InternalCall)]
  158. private static extern string Internal_GetScriptAssemblyPath();
  159. [MethodImpl(MethodImplOptions.InternalCall)]
  160. private static extern string Internal_GetFrameworkAssemblyPath();
  161. [MethodImpl(MethodImplOptions.InternalCall)]
  162. private static extern string Internal_GetEngineAssemblyName();
  163. [MethodImpl(MethodImplOptions.InternalCall)]
  164. private static extern string Internal_GetEditorAssemblyName();
  165. [MethodImpl(MethodImplOptions.InternalCall)]
  166. private static extern string Internal_GetScriptGameAssemblyName();
  167. [MethodImpl(MethodImplOptions.InternalCall)]
  168. private static extern string Internal_GetScriptEditorAssemblyName();
  169. [MethodImpl(MethodImplOptions.InternalCall)]
  170. private static extern string Internal_SaveScene(string path);
  171. }
  172. }