using System; using System.Collections.Generic; using System.Runtime.CompilerServices; using System.IO; using BansheeEngine; namespace BansheeEditor { /// /// Available tools in the scene view. /// public enum SceneViewTool { View, Move, Rotate, Scale } /// /// Pivot mode used by the scene view tools. /// public enum HandlePivotMode { Center, Pivot } /// /// Coordinate mode used by the scene view tools. /// public enum HandleCoordinateMode { Local, World } /// /// Manages various generic and global settings relating to the editor. /// public class EditorApplication { /// /// Determines the active tool shown in the scene view. /// public static SceneViewTool ActiveSceneTool { get { return EditorSettings.ActiveSceneTool; } set { EditorSettings.ActiveSceneTool = value; } } /// /// Determines the coordinate mode used by the tools in the scene view. /// public static HandleCoordinateMode ActiveCoordinateMode { get { return EditorSettings.ActiveCoordinateMode; } set { EditorSettings.ActiveCoordinateMode = value; } } /// /// Determines the pivot mode used by the tools in the scene view. /// public static HandlePivotMode ActivePivotMode { get { return EditorSettings.ActivePivotMode; } set { EditorSettings.ActivePivotMode = value; } } /// /// Camera used for rendering the scene view. /// public static Camera SceneViewCamera { get { return EditorWindow.GetWindow().Camera; } } /// /// Absolute path to the folder containing the currently open project. /// public static string ProjectPath { get { return Internal_GetProjectPath(); } } /// /// Name of the currently open project. /// public static string ProjectName { get { return Internal_GetProjectName(); } } /// /// Checks is any project currently loaded. /// public static bool IsProjectLoaded { get { return Internal_GetProjectLoaded(); } } /// /// Returns the path where the script compiler is located at. /// internal static string CompilerPath { get { return Internal_GetCompilerPath(); } } /// /// Returns the path to the folder where the builtin script assemblies are located at. /// internal static string BuiltinAssemblyPath { get { return Internal_GetBuiltinAssemblyPath(); } } /// /// Returns the path to the folder where the custom script assemblies are located at. /// internal static string ScriptAssemblyPath { get { return Internal_GetScriptAssemblyPath(); } } /// /// Returns the path to the folder where the .NET framework assemblies are located at. /// internal static string FrameworkAssemblyPath { get { return Internal_GetFrameworkAssemblyPath(); } } /// /// Name of the builtin assembly containing engine specific types. /// internal static string EngineAssemblyName { get { return Internal_GetEngineAssemblyName(); } } /// /// Name of the builtin assembly containing editor specific types. /// internal static string EditorAssemblyName { get { return Internal_GetEditorAssemblyName(); } } /// /// Name of the custom assembly compiled from non-editor scripts within the project. /// internal static string ScriptGameAssemblyName { get { return Internal_GetScriptGameAssemblyName(); } } /// /// Name of the custom assembly compiled from editor scripts within the project. /// internal static string ScriptEditorAssemblyName { get { return Internal_GetScriptEditorAssemblyName(); } } private static EditorApplication instance; private static FolderMonitor monitor; private static ScriptCodeManager codeManager; private static HashSet dirtyResources = new HashSet(); private static bool sceneDirty; private static bool unitTestsExecuted; /// /// Constructs a new editor application. Called at editor start-up by the runtime. /// internal EditorApplication() { instance = this; codeManager = new ScriptCodeManager(); // Register controls InputConfiguration inputConfig = VirtualInput.KeyConfig; inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.W); inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.S); inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.A); inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.D); inputConfig.RegisterButton(SceneCamera.MoveForwardBinding, ButtonCode.Up); inputConfig.RegisterButton(SceneCamera.MoveBackBinding, ButtonCode.Down); inputConfig.RegisterButton(SceneCamera.MoveLeftBinding, ButtonCode.Left); inputConfig.RegisterButton(SceneCamera.MoveRightBinding, ButtonCode.Right); inputConfig.RegisterButton(SceneCamera.FastMoveBinding, ButtonCode.LeftShift); inputConfig.RegisterButton(SceneCamera.RotateBinding, ButtonCode.MouseRight); inputConfig.RegisterAxis(SceneCamera.HorizontalAxisBinding, InputAxis.MouseX); inputConfig.RegisterAxis(SceneCamera.VerticalAxisBinding, InputAxis.MouseY); inputConfig.RegisterButton(SceneWindow.ToggleProfilerOverlayBinding, ButtonCode.P, ButtonModifier.CtrlAlt); inputConfig.RegisterButton(SceneWindow.ViewToolBinding, ButtonCode.Q); inputConfig.RegisterButton(SceneWindow.MoveToolBinding, ButtonCode.W); inputConfig.RegisterButton(SceneWindow.RotateToolBinding, ButtonCode.E); inputConfig.RegisterButton(SceneWindow.ScaleToolBinding, ButtonCode.R); inputConfig.RegisterButton(SceneWindow.DuplicateBinding, ButtonCode.D, ButtonModifier.Ctrl); if (IsProjectLoaded) { monitor = new FolderMonitor(ProjectLibrary.ResourceFolder); monitor.OnAdded += OnAssetModified; monitor.OnRemoved += OnAssetModified; monitor.OnModified += OnAssetModified; } } /// /// Triggered when the folder monitor detects an asset in the monitored folder was modified. /// /// Path to the modified file or folder. private static void OnAssetModified(string path) { ProjectLibrary.Refresh(path); } /// /// Called 60 times per second by the runtime. /// internal void OnEditorUpdate() { ProjectLibrary.Update(); codeManager.Update(); } /// /// Creates a new empty scene. /// [MenuItem("File/New Scene", 10051, true)] private static void NewScene() { LoadScene(null); } /// /// Opens a dialog that allows the user to select a new prefab to load as the current scene. If current scene /// is modified the user is offered a chance to save it. /// [MenuItem("File/Open Scene", ButtonModifier.Ctrl, ButtonCode.L, 10050)] private static void LoadScene() { string[] scenePaths; if (BrowseDialog.OpenFile(ProjectLibrary.ResourceFolder, "", false, out scenePaths)) { if (scenePaths.Length > 0) LoadScene(scenePaths[0]); } } /// /// Opens a dialog to allows the user to select a location where to save the current scene. If scene was previously /// saved it is instead automatically saved at the last location. /// [MenuItem("File/Save Scene", ButtonModifier.Ctrl, ButtonCode.S, 10049)] [ToolbarItem("Save Scene", ToolbarIcon.SaveScene, "", 1998)] private static void SaveScene() { if (!string.IsNullOrEmpty(Scene.ActiveSceneUUID)) { string scenePath = ProjectLibrary.GetPath(Scene.ActiveSceneUUID); SaveScene(scenePath); } else SaveSceneAs(); } /// /// Opens a dialog to allows the user to select a location where to save the current scene. /// [MenuItem("File/Save Scene As", 10048)] private static void SaveSceneAs() { string scenePath = ""; if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath)) { if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder)) DialogBox.Open("Error", "The location must be inside the Resources folder of the project.", DialogBox.Type.OK); else { // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise // Internal_SaveScene will silently fail. scenePath += ".prefab"; SaveScene(scenePath); } } } /// /// Loads a prefab as the current scene at the specified path. If current scene is modified the user is offered a /// chance to save it. /// /// Path to a valid prefab relative to the resource folder. If path is empty a brand new /// scene will be loaded. public static void LoadScene(string path) { Action continueLoad = (scenePath) => { if (string.IsNullOrEmpty(path)) Scene.Clear(); else Scene.Load(path); SetSceneDirty(false); ProjectSettings.LastOpenScene = scenePath; ProjectSettings.Save(); }; Action dialogCallback = (result) => { if (result == DialogBox.ResultType.Yes) { SaveScene(); continueLoad(path); } else if (result == DialogBox.ResultType.No) continueLoad(path); }; if (IsSceneModified()) { DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?", DialogBox.Type.YesNoCancel, dialogCallback); } else continueLoad(path); } /// /// Saves the currently loaded scene to the specified path. /// /// Path relative to the resource folder. This can be the path to the existing scene /// prefab if it just needs updating. public static void SaveScene(string path) { Internal_SaveScene(path); ProjectLibrary.Refresh(true); SetSceneDirty(false); } /// /// Checks does the folder at the provieded path contain a valid project. /// /// Absolute path to the root project folder. /// True if the folder contains a valid project. public static bool IsValidProject(string path) { return Internal_IsValidProject(path); } /// /// Contains a new project in the provided folder. /// /// Absolute path to the folder to create the project in. Name of this folder will be used as the /// project's name. public static void CreateProject(string path) { Internal_CreateProject(path); } /// /// Opens a Project Window allowing you to browse for or create a project. /// [MenuItem("File/Open Project", 10100)] [ToolbarItem("Open Project", ToolbarIcon.OpenProject, "", 2000)] public static void BrowseForProject() { ProjectWindow.Open(); } /// /// Saves all data in the currently open project. /// [MenuItem("File/Save Project", 10099)] [ToolbarItem("Save Project", ToolbarIcon.SaveProject, "", 1999)] public static void SaveProject() { foreach (var resourceUUID in dirtyResources) { string path = ProjectLibrary.GetPath(resourceUUID); Resource resource = ProjectLibrary.Load(path); if(resource != null) ProjectLibrary.Save(resource); } dirtyResources.Clear(); SetStatusProject(false); Internal_SaveProject(); } /// /// Loads the project at the specified path. This method executes asynchronously and will trigger /// when done. /// /// Absolute path to the project's root folder. public static void LoadProject(string path) { if (IsProjectLoaded && path == ProjectPath) return; if (!Internal_IsValidProject(path)) { Debug.LogWarning("Provided path: \"" + path + "\" is not a valid project."); return; } if (IsProjectLoaded) UnloadProject(); Internal_LoadProject(path); // Triggers OnProjectLoaded when done } /// /// Opens a file or a folder in the default external application. /// /// Absolute path to the file or folder to open. public static void OpenExternally(string path) { Internal_OpenExternally(path); } /// /// Marks a resource as dirty so that it may be saved the next time the project is saved. Optionally you may also /// call to save it immediately. /// /// Resource to mark as dirty public static void SetDirty(Resource resource) { if (resource == null) return; SetStatusProject(true); dirtyResources.Add(resource.UUID); } /// /// Marks the current scene as dirty. /// public static void SetSceneDirty() { SetSceneDirty(true); } /// /// Marks the current scene as clean or dirty. /// /// Should the scene be marked as clean or dirty. internal static void SetSceneDirty(bool dirty) { sceneDirty = dirty; SetStatusScene(Scene.ActiveSceneName, dirty); if (!dirty) dirtyResources.Remove(Scene.ActiveSceneUUID); } /// /// Checks is the specific resource dirty and needs saving. /// /// Resource to check. /// True if the resource requires saving, false otherwise. public static bool IsDirty(Resource resource) { return dirtyResources.Contains(resource.UUID); } /// /// Unloads the currently loaded project. Offers the user a chance to save the current scene if it is modified. /// Automatically saves all project data before unloading. /// private static void UnloadProject() { Action continueUnload = () => { Scene.Clear(); if (monitor != null) { monitor.Destroy(); monitor = null; } LibraryWindow window = EditorWindow.GetWindow(); if(window != null) window.Reset(); SetSceneDirty(false); Internal_UnloadProject(); SetStatusProject(false); }; Action dialogCallback = (result) => { if (result == DialogBox.ResultType.Yes) SaveScene(); continueUnload(); }; if (IsSceneModified()) { DialogBox.Open("Warning", "You current scene has modifications. Do you wish to save them first?", DialogBox.Type.YesNoCancel, dialogCallback); } else continueUnload(); } /// /// Reloads all script assemblies in case they were modified. This action is delayed and will be executed /// at the beginning of the next frame. /// public static void ReloadAssemblies() { Internal_ReloadAssemblies(); } /// /// Changes the scene displayed on the status bar. /// /// Name of the scene. /// Whether to display the scene as modified or not. private static void SetStatusScene(string name, bool modified) { Internal_SetStatusScene(name, modified); } /// /// Changes the project state displayed on the status bar. /// /// Whether to display the project as modified or not. private static void SetStatusProject(bool modified) { Internal_SetStatusProject(modified); } /// /// Displays or hides the "compilation in progress" visual on the status bar. /// /// True to display the visual, false otherwise. internal static void SetStatusCompiling(bool compiling) { Internal_SetStatusCompiling(compiling); } /// /// Checks did we make any modifications to the scene since it was last saved. /// /// True if the scene was never saved, or was modified after last save. public static bool IsSceneModified() { return sceneDirty; } /// /// Executes any editor-specific unit tests. This should be called after a project is loaded if possible. /// private static void RunUnitTests() { #if DEBUG Internal_RunUnitTests(); #endif } /// /// Triggered by the runtime when method completes. /// private static void Internal_OnProjectLoaded() { SetStatusProject(false); if (!unitTestsExecuted) { RunUnitTests(); unitTestsExecuted = true; } if (!IsProjectLoaded) { ProjectWindow.Open(); return; } string projectPath = ProjectPath; RecentProject[] recentProjects = EditorSettings.RecentProjects; bool foundPath = false; for (int i = 0; i < recentProjects.Length; i++) { if (PathEx.Compare(recentProjects[i].path, projectPath)) { recentProjects[i].accessTimestamp = (ulong)DateTime.Now.Ticks; EditorSettings.RecentProjects = recentProjects; foundPath = true; break; } } if (!foundPath) { List extendedRecentProjects = new List(); extendedRecentProjects.AddRange(recentProjects); RecentProject newProject = new RecentProject(); newProject.path = projectPath; newProject.accessTimestamp = (ulong)DateTime.Now.Ticks; extendedRecentProjects.Add(newProject); EditorSettings.RecentProjects = extendedRecentProjects.ToArray(); } EditorSettings.LastOpenProject = projectPath; EditorSettings.Save(); ProjectLibrary.Refresh(); monitor = new FolderMonitor(ProjectLibrary.ResourceFolder); monitor.OnAdded += OnAssetModified; monitor.OnRemoved += OnAssetModified; monitor.OnModified += OnAssetModified; if (!string.IsNullOrWhiteSpace(ProjectSettings.LastOpenScene)) { Scene.Load(ProjectSettings.LastOpenScene); SetSceneDirty(false); } } /// /// Triggered by the runtime when the user clicks on the status bar. /// private static void Internal_OnStatusBarClicked() { EditorWindow.OpenWindow(); } [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_SetStatusScene(string name, bool modified); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_SetStatusProject(bool modified); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_SetStatusCompiling(bool compiling); [MethodImpl(MethodImplOptions.InternalCall)] private static extern string Internal_GetProjectPath(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern string Internal_GetProjectName(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern bool Internal_GetProjectLoaded(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern string Internal_GetCompilerPath(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern string Internal_GetBuiltinAssemblyPath(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern string Internal_GetScriptAssemblyPath(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern string Internal_GetFrameworkAssemblyPath(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern string Internal_GetEngineAssemblyName(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern string Internal_GetEditorAssemblyName(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern string Internal_GetScriptGameAssemblyName(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern string Internal_GetScriptEditorAssemblyName(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern string Internal_SaveScene(string path); [MethodImpl(MethodImplOptions.InternalCall)] private static extern bool Internal_IsValidProject(string path); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_SaveProject(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_LoadProject(string path); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_UnloadProject(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_CreateProject(string path); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_ReloadAssemblies(); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_OpenExternally(string path); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_RunUnitTests(); } }