/// <summary> /// Create a new C# BehaviorTree Action Script Based on template /// </summary> /// <param name="savefolder">Script save folder</param> /// <param name="isaction">Should make an Action or a Decision (false) ?</param> private static void CreateBTScript(string savefolder, bool isaction, string scriptName) { MoonIO.ValidateFolder(savefolder); string savep_rel = savefolder + "/" + scriptName + ".cs"; string savep = savep_rel.Replace("Assets", Application.dataPath).Replace(@"\", @"/"); string templatePath = "ScriptTemplates" + (isaction ? "/BTActionTemplate" : "/BTDecisionTemplate"); TextAsset ScriptTemplateAsset = MoonResources.LoadCustomResource <TextAsset>(templatePath); if (ScriptTemplateAsset != null) { string scriptTemplate = ScriptTemplateAsset.text.Replace("#NAME#", scriptName); System.IO.File.WriteAllText(savep, scriptTemplate); AssetDatabase.Refresh(); Object assetsaved = AssetDatabase.LoadAssetAtPath(savep_rel, typeof(TextAsset)); AssetDatabase.OpenAsset(assetsaved); } }
/// <summary> /// BT Canvas extra GUI States /// </summary> /// <param name="controller">MoonEditor Controller</param> /// <returns>Defined gui states</returns> public override Dictionary <int, System.Action> CanvasGUIStates(MoonEditorController controller) { Dictionary <int, System.Action> dic = new Dictionary <int, System.Action>(); dic.Add(4, () => { MoonGUI.DrawGrid(new Rect(0, 17, Screen.width, Screen.height), Vector2.zero); NewScriptName = MoonModal.InputPanel(new Vector2(200, 100), "Create " + ((NewScriptMode == 0) ? " Action" : "Decision"), "Ok", "Cancel", "Class Name", NewScriptName, () => { if (MoonIO.ValidFileName(NewScriptName)) { if (MoonIO.ValidClassName(NewScriptName)) { string Savefolder = MoonSettings.ActionsSaveDirectory + "/BehaviorTrees"; if (NewScriptMode >= 1) { // create Decition Savefolder += "/_Decisions"; } CreateBTScript(Savefolder, NewScriptMode == 0, NewScriptName); NewScriptName = string.Empty; controller.CurrentGUIState = 0; } } }, () => { NewScriptName = string.Empty; controller.CurrentGUIState = 0; }); }); return(dic); }
/// <summary> /// Displays the editor depending on it's state /// </summary> /// <param name="e">User event</param> public void DoEditor(Event e) { GUIState st = (GUIState)CurrentGUIState; switch (st) { case GUIState.Normal: if (WorkingGraph != null) { MoonGUI.DrawGrid(new Rect(0, 17, Screen.width, Screen.height), WorkingGraph.Offset, WorkingGraph.Zoom); canvas.DoNodeEditor(e); WorkingGraph.OnCanvasGUI(); } else { MoonGUI.DrawGrid(new Rect(0, 17, Screen.width, Screen.height), Vector2.zero); MoonGUI.DrawCanvasLabel(new Rect(10, 15, 500, 300), new GUIContent("No Graph"), 26, true); } break; case GUIState.New: MoonGUI.DrawGrid(new Rect(0, 17, Screen.width, Screen.height), Vector2.zero); if (ModuleData != null) { NewGraphName = MoonModal.InputPanel(new Vector2(200, 100), "New " + ModuleData.Name, "Ok", "Cancel", "Name", NewGraphName, () => { if (MoonIO.ValidFileName(NewGraphName)) { string foldername = (!string.IsNullOrEmpty(ModuleData.ExportFolderName)) ? ModuleData.ExportFolderName : ModuleData.Name; NodeGraph newg = MoonIO.CreateGraphAsset(NewGraphName, foldername, ModuleData.GraphType, ModuleData.ModuleType); if (newg != null) { WorkingGraph = newg; } NewGraphName = string.Empty; CurrentGUIState = (int)GUIState.Normal; } }, () => { NewGraphName = string.Empty; CurrentGUIState = (int)GUIState.Normal; }); } else { CurrentGUIState = (int)GUIState.Normal; } break; case GUIState.Open: MoonGUI.DrawGrid(new Rect(0, 17, Screen.width, Screen.height), Vector2.zero); MoonModal.OpenGraphPanel(ref OpenGraphOffset, ref OpenGraphSearch, FoundGraphs, (NodeGraph ng) => { WorkingGraph = ng; CurrentGUIState = (int)GUIState.Normal; }, () => CurrentGUIState = (int)GUIState.Normal, () => FoundGraphs = MoonIO.GetAllGraphs()); if (!string.Equals(OpenGraphSearch, LastOpenGraphSearch)) { FoundGraphs = MoonIO.GetAllGraphs(OpenGraphSearch); LastOpenGraphSearch = OpenGraphSearch; } break; case GUIState.SaveAs: MoonGUI.DrawGrid(new Rect(0, 17, Screen.width, Screen.height), Vector2.zero); NewGraphName = MoonModal.InputPanel(new Vector2(200, 100), "Save as ", "Ok", "Cancel", "Name", NewGraphName, () => { if (MoonIO.ValidFileName(NewGraphName)) { NodeGraph copy = MoonIO.CopyGraph(WorkingGraph, NewGraphName); if (copy != null) { WorkingGraph = copy; } CurrentGUIState = (int)GUIState.Normal; } }, () => CurrentGUIState = (int)GUIState.Normal); break; default: for (int i = 0; i < UserKeyStates.Length; i++) { if (CurrentGUIState == UserKeyStates[i]) { System.Action act; if (UserStates.TryGetValue(UserKeyStates[i], out act)) { act.Invoke(); } } } break; } }
/// <summary> /// Displays the toolbar menu /// </summary> public void DoMenu() { GUILayout.BeginHorizontal(EditorStyles.toolbar); if (GUILayout.Button("New", EditorStyles.toolbarDropDown, GUILayout.Width(MenuBtnWidth))) { System.Type[] foundmodules = MoonReflection.GetAllDerivedTypes(typeof(NodeGraph)).ToArray(); GenericMenu menu = new GenericMenu(); for (int i = 0; i < foundmodules.Length; i++) { System.Type curr = foundmodules[i]; var atr = (GraphModuleAttribute)MoonReflection.GetAttribute(curr, typeof(GraphModuleAttribute), true); if (atr != null) { atr.Init(curr); menu.AddItem(new GUIContent(atr.Name), false, OnNewGraph, atr); } } menu.DropDown(new Rect(5, 15, 0, 0)); } if (GUILayout.Button("Open", EditorStyles.toolbarButton, GUILayout.Width(MenuBtnWidth))) { canvas.HideAddNode(); CurrentGUIState = (int)GUIState.Open; FoundGraphs = MoonIO.GetAllGraphs(); } if (WorkingGraph != null) { if (GUILayout.Button("Save", EditorStyles.toolbarDropDown, GUILayout.Width(MenuBtnWidth))) { GenericMenu men = new GenericMenu(); men.AddItem(new GUIContent("Save"), false, new GenericMenu.MenuFunction(() => { AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); })); men.AddItem(new GUIContent("Save as..."), false, new GenericMenu.MenuFunction(() => { CurrentGUIState = (int)GUIState.SaveAs; })); float x = (MenuBtnWidth * 2) + 5; men.DropDown(new Rect(x, 15, 0, 0)); } DoZoomArea(); GUILayout.Space(5); WorkingGraph.OnMenuGUI(); } GUILayout.FlexibleSpace(); if (WorkingGraph != null) { WorkingGraph.OnToolBarGUI(); } else { GUILayout.FlexibleSpace(); } GUILayout.EndHorizontal(); }