//public virtual void InitActionEvents() //{ // actions += a=> { if (a.type == EventType.mouseDown) { } }; //} public virtual void UpdateView(Event e, vFSMBehaviour curGraph) { if (viewSkin == null) { GetEditorSkin(); return; } // Set the current View Graph this.currentFSM = curGraph; }
void AddComponentContext(vFSMBehaviour graph, Rect rect) { try { GenericMenu menu = new GenericMenu(); List <GenericMenuItem> menuItems = new List <GenericMenuItem>(); var possibleActions = typeof(vStateAction).FindSubClasses(); menu.AddItem(new GUIContent("Action/New Action Script"), false, () => { vNodeMenus.CreateNewAction(); }); menu.AddSeparator("Action/"); foreach (var type in possibleActions) { var instance = (vStateAction)ScriptableObject.CreateInstance(type.FullName); if (instance) { menuItems.Add(new GenericMenuItem(new GUIContent("Action/" + (instance.categoryName) + (instance.defaultName)), () => { AddAction(graph, type); if (instance) { GameObject.DestroyImmediate(instance); } })); } } menuItems.Sort((x, y) => string.Compare(x.content.text, y.content.text)); foreach (var item in menuItems) { menu.AddItem(item.content, false, item.func); } menuItems.Clear(); menu.AddItem(new GUIContent("Decision/New Decision Script"), false, () => { vNodeMenus.CreateNewDecision(); }); menu.AddSeparator("Decision/"); var possibleDecisions = typeof(vStateDecision).FindSubClasses(); foreach (var type in possibleDecisions) { var instance = (vStateDecision)ScriptableObject.CreateInstance(type.Name); if (instance) { menuItems.Add(new GenericMenuItem(new GUIContent("Decision/" + (instance.categoryName) + (instance.defaultName)), () => { AddTransition(graph, type); if (instance) { GameObject.DestroyImmediate(instance); } })); } } menuItems.Sort((x, y) => string.Compare(x.content.text, y.content.text)); foreach (var item in menuItems) { menu.AddItem(item.content, false, item.func); } menu.ShowAsContext(); } catch (UnityException e) { Debug.LogWarning("Add FSM Compornent Error :\n" + e.Message, currentFSM); } }
public void AddAction(vFSMBehaviour curGraph, Type type) { if (curGraph != null && type != null) { var action = ScriptableObject.CreateInstance(type) as vStateAction; action.name = action.defaultName; if (action != null) { action.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(action, curGraph); curGraph.OnChangeChilds(); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } }
public static T CreateNode <T>(string name, vFSMBehaviour parentGraph) where T : ScriptableObject { T curNode = null; curNode = (T)ScriptableObject.CreateInstance <T>(); curNode.name = name; if (curNode != null) { AssetDatabase.AddObjectToAsset(curNode, parentGraph); curNode.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } return(curNode); }
public static void DeleteNode(int id, vFSMBehaviour curGraph) { if (curGraph) { if (id >= 0 && id < curGraph.states.Count) { vFSMState deleteNode = curGraph.states[id]; if (deleteNode) { curGraph.selectedNode = null; curGraph.overNode = false; curGraph.states.RemoveAt(id); for (int i = 0; i < deleteNode.actions.Count; i++) { if (deleteNode.actions[i]) { var o = new SerializedObject(deleteNode.actions[i]); o.ApplyModifiedProperties(); } } for (int i = 0; i < deleteNode.transitions.Count; i++) { if (deleteNode.transitions[i].decisions != null) { for (int a = 0; a < deleteNode.transitions[i].decisions.Count; a++) { if (deleteNode.transitions[i].decisions[i].decision) { var o = new SerializedObject(deleteNode.transitions[i].decisions[i].decision); o.ApplyModifiedProperties(); } } } } if (curGraph.states.Count > 2 && curGraph.states[0].defaultTransition == deleteNode) { curGraph.states[0].defaultTransition = curGraph.states[2] as vFSMState; } Undo.DestroyObjectImmediate(deleteNode); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } } }
public static void InitEditorWindow(vFSMBehaviour graph) { curWindow = (vFSMNodeEditorWindow)EditorWindow.GetWindow <vFSMNodeEditorWindow>("AI FSM", false, typeof(SceneView)); curWindow.titleContent.image = Resources.Load("Textures/Editor/FSMIconWindow") as Texture2D; curWindow.curGraph = graph; CreateViews(); if (curWindow.workView != null && graph.states.Count > 0) { var states = graph.states; for (int i = 0; i < states.Count; i++) { states[i].inDrag = false; } curWindow.workView.CenterView(graph); } }
public static void CreateNewGraph(string path) { vFSMBehaviour curGraph = ScriptableObject.CreateInstance <vFSMBehaviour>(); if (curGraph != null) { AssetDatabase.CreateAsset(curGraph, path); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); Selection.activeObject = curGraph; var entryNode = CreateNode <vFSMState>("Entry", curGraph); entryNode.resetCurrentDestination = false; entryNode.parentGraph = curGraph; entryNode.useDecisions = false; entryNode.useActions = false; entryNode.canEditName = false; entryNode.canRemove = false; entryNode.canTranstTo = false; entryNode.canEditColor = false; entryNode.canSetAsDefault = false; entryNode.description = "This State Run Just in Start\n to init first state"; entryNode.nodeColor = Color.green; var anyState = CreateNode <vFSMState>("AnyState", curGraph); anyState.resetCurrentDestination = false; anyState.useDecisions = true; anyState.useActions = false; anyState.canEditName = false; anyState.canRemove = false; anyState.canTranstTo = false; anyState.canEditColor = false; anyState.canSetAsDefault = false; anyState.description = "This State Run after current state"; anyState.nodeColor = Color.cyan; anyState.nodeRect.y += 100; anyState.parentGraph = curGraph; curGraph.states.Add(entryNode); curGraph.states.Add(anyState); curGraph.InitGraph(); vFSMNodeEditorWindow.InitEditorWindow(curGraph); } else { EditorUtility.DisplayDialog("Node Message", "Unable to create new graph, please see your friendly programmer!", "OK"); } }
public static void LoadGraph() { vFSMBehaviour curGraph = null; string graphPath = EditorUtility.OpenFilePanel("Load FSM Behaviour", Application.dataPath + "/Invector-AIController/", "asset"); if (!string.IsNullOrEmpty(graphPath)) { int dataPathLength = Application.dataPath.Length - 6; string finalPah = graphPath.Substring(dataPathLength); curGraph = (vFSMBehaviour)AssetDatabase.LoadAssetAtPath(finalPah, typeof(vFSMBehaviour)); vFSMNodeEditorWindow curwindow = EditorWindow.GetWindow <vFSMNodeEditorWindow>(); if (curwindow != null) { curwindow.curGraph = curGraph; Selection.activeObject = curGraph; } } }
public virtual void ChangeBehaviour(vFSMBehaviour behaviour) { if (_fsmBehaviour != behaviour) { inChangeState = true; _fsmBehaviour = behaviour; currentState = null; if (!isStopped) { Entry(); } if (debugMode) { SendDebug("CHANGE BEHAVIOUR TO " + behaviour.name); } inChangeState = false; } }
public void CenterView(vFSMBehaviour behaviour) { behaviour.panOffset.x = behaviour.states[0].nodeRect.x; behaviour.panOffset.y = behaviour.states[0].nodeRect.y; var x = behaviour.states[0].nodeRect.x; var y = behaviour.states[0].nodeRect.y; Vector2 position = new Vector2(x, y); behaviour.states[0].nodeRect.x = this.viewRect.position.x + this.viewRect.width / 2; behaviour.states[0].nodeRect.y = 100; if (behaviour.states.Count > 1) { for (int i = 1; i < behaviour.states.Count; i++) { var diferencePosition = behaviour.states[i].nodeRect.position - position; var newPosition = behaviour.states[0].nodeRect.position + diferencePosition; behaviour.states[i].nodeRect.position = newPosition; } } }
public static void CreateNode(vFSMBehaviour curGraph, Vector2 mousePos) { if (curGraph != null) { vFSMState curState = null; Undo.RecordObject(curGraph, "New Node"); curState = ScriptableObject.CreateInstance <vFSMState>(); curState.Name = "State " + (curGraph.states.Count > 1 ? (curGraph.states.Count - 2).ToString() : ""); if (curGraph.states.Count > 0) { if (curGraph.states[0].defaultTransition == null) { curGraph.states[0].defaultTransition = curState; } } curState.nodeColor = Color.white; if (curState != null) { curState.InitNode(); curState.nodeRect.x = mousePos.x; curState.nodeRect.y = mousePos.y; curState.parentGraph = curGraph; curState.hideFlags = HideFlags.HideInHierarchy; curGraph.states.Add(curState); AssetDatabase.AddObjectToAsset(curState, curGraph); var count = curState.GetSameComponentNameCount <vFSMState>(); if (count > 0) { curState.Name += " " + (count - 1).ToString(); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } }
public static void CreateNode(Type type, vFSMBehaviour curGraph, Vector2 mousePos) { if (curGraph != null && type.IsSubclassOf(typeof(vFSMState))) { var curState = ScriptableObject.CreateInstance(type.FullName); Undo.RegisterCreatedObjectUndo(curState, "Create object"); curState.name = "State " + (curGraph.states.Count > 1 ? (curGraph.states.Count - 2).ToString() : ""); if (curGraph.states.Count > 0) { if (curGraph.states[0].defaultTransition == null) { curGraph.states[0].defaultTransition = curState as vFSMState; } } (curState as vFSMState).nodeColor = Color.white; if (curState != null) { (curState as vFSMState).InitNode(); (curState as vFSMState).nodeRect.x = mousePos.x; (curState as vFSMState).nodeRect.y = mousePos.y; (curState as vFSMState).parentGraph = curGraph; curState.hideFlags = HideFlags.HideInHierarchy; curGraph.states.Add((curState as vFSMState)); AssetDatabase.AddObjectToAsset(curState, curGraph); var count = curState.GetSameComponentNameCount <vFSMState>(); if (count > 0) { (curState as vFSMState).Name += " " + (count - 1).ToString(); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } }
public override void UpdateView(Event e, vFSMBehaviour curGraph) { if ((!Application.isPlaying || Selection.activeGameObject == null) && curGraph) { if (curGraphSerialized == null || curGraphSerialized.targetObject != curGraph) { curGraphSerialized = new SerializedObject(curGraph); } } else if (Application.isPlaying && Selection.activeGameObject != null) { var fsmBehaviour = Selection.activeGameObject.GetComponent <vIFSMBehaviourController>(); if (fsmBehaviour != null) { List <MonoBehaviour> monoBehaviours = new List <MonoBehaviour>(); Selection.activeGameObject.GetComponents <MonoBehaviour>(monoBehaviours); if (monoBehaviours.Count > 0) { var monoFSM = monoBehaviours.Find(m => m is vIFSMBehaviourController); if (monoFSM) { curGraphSerialized = new SerializedObject(monoFSM); } else if (curGraphSerialized == null || curGraphSerialized.targetObject != curGraph) { curGraphSerialized = new SerializedObject(curGraph); } } } else if (curGraph) { if (curGraphSerialized == null || curGraphSerialized.targetObject != curGraph) { curGraphSerialized = new SerializedObject(curGraph); } } } else if (curGraph) { if (curGraphSerialized == null || curGraphSerialized.targetObject != curGraph) { curGraphSerialized = new SerializedObject(curGraph); } } base.UpdateView(e, curGraph); GUI.Box(viewRect, "", viewSkin.GetStyle("ToolBar")); var toolbarRect = viewRect; toolbarRect.height = 20; // selected = GUI.Toolbar(toolbarRect,selected, toolBar,viewSkin.GetStyle("ToolBar")); var rectoffset = viewRect; // rectoffset.y += 20; rectoffset.x += 5; rectoffset.width -= 10; rectoffset.height -= 20; GUILayout.BeginArea(rectoffset); { if (curGraph) { GUILayout.Space(5); GUILayout.BeginHorizontal(viewSkin.box, GUILayout.Height(20)); GUILayout.Label("FSM COMPONENTS", viewSkin.GetStyle("LabelHeader")); if (GUILayout.Button("+", viewSkin.box, GUILayout.Width(20), GUILayout.ExpandHeight(true))) { var rect = GUILayoutUtility.GetLastRect(); AddComponentContext(curGraph, rect); } GUILayout.EndHorizontal(); GUILayout.Space(10); componentsScrollView = GUILayout.BeginScrollView(componentsScrollView); GUILayout.Box("", viewSkin.GetStyle("Separator"), GUILayout.Height(2), GUILayout.ExpandWidth(true)); GUILayout.Space(10); GUILayout.BeginVertical(viewSkin.box); GUILayout.Label("Actions", viewSkin.GetStyle("LabelHeader"), GUILayout.ExpandWidth(true)); GUILayout.Space(5); if (curGraph.actions.Count > 0) { for (int a = 0; a < curGraph.actions.Count; a++) { if (curGraph.actions[a]) { curGraph.actions[a].OnInspectorGUI(); var rect = GUILayoutUtility.GetLastRect(); rect.height = 15; if (rect.Contains(e.mousePosition) && e.type != EventType.ContextClick) { if (e.button == 1) { GenericMenu menu = new GenericMenu(); int index = a; var refList = curGraph.actions; var action = curGraph.actions[a].target as vStateAction; menu.AddItem(new GUIContent("Delete"), false, () => { DeleteObjet(ref refList, index, GetStatesUsingAction(action)); e.Use(); }); menu.ShowAsContext(); } } var tooltip = GetStatesUsingAction(curGraph.actions[a].target as vStateAction); bool isInUse = !String.IsNullOrEmpty(tooltip); var _color = GUI.color; GUI.color = isInUse ? new Color(1, 1, 1, 0.5f) : new Color(1, 1, 1, 0.1f); if (!isInUse) { tooltip = "Not being used"; } var _checkerRect = rect; _checkerRect.x = viewRect.width - 25 + componentsScrollView.x; _checkerRect.width = 10; _checkerRect.height = 10; GUI.Toggle(_checkerRect, false, new GUIContent("", tooltip), EditorStyles.radioButton); GUI.color = _color; } else { curGraph.actions.RemoveAt(a); } } } else { GUILayout.Box("NONE", viewSkin.box, GUILayout.ExpandWidth(true)); } GUILayout.EndVertical(); GUILayout.Space(10); GUILayout.Box("", viewSkin.GetStyle("Separator"), GUILayout.Height(2), GUILayout.ExpandWidth(true)); GUILayout.Space(10); GUILayout.BeginVertical(viewSkin.box); GUILayout.Box("Decisions", viewSkin.GetStyle("LabelHeader"), GUILayout.ExpandWidth(true)); GUILayout.Space(5); if (curGraph.decisions.Count > 0) { for (int a = 0; a < curGraph.decisions.Count; a++) { if (curGraph.decisions[a]) { curGraph.decisions[a].OnInspectorGUI(); var rect = GUILayoutUtility.GetLastRect(); rect.height = 15; if (rect.Contains(e.mousePosition) && e.type != EventType.ContextClick) { if (e.button == 1) { GenericMenu menu = new GenericMenu(); int index = a; var refList = curGraph.decisions; var decision = curGraph.decisions[a].target as vStateDecision; menu.AddItem(new GUIContent("Delete"), false, () => { DeleteObjet(ref refList, index, GetStatesUsingDecision(decision)); e.Use(); }); menu.ShowAsContext(); } } var tooltip = GetStatesUsingDecision(curGraph.decisions[a].target as vStateDecision); bool isInUse = !String.IsNullOrEmpty(tooltip); var _color = GUI.color; GUI.color = isInUse ? new Color(1, 1, 1, 0.5f) : new Color(1, 1, 1, 0.1f); if (!isInUse) { tooltip = "Not being used"; } var _checkerRect = rect; _checkerRect.x = viewRect.width - 25 + componentsScrollView.x; _checkerRect.width = 10; _checkerRect.height = 10; GUI.Toggle(_checkerRect, false, new GUIContent("", tooltip), EditorStyles.radioButton); GUI.color = _color; } else { curGraph.decisions.RemoveAt(a); break; } } } else { GUILayout.Box("NONE", viewSkin.box, GUILayout.ExpandWidth(true)); } GUILayout.EndVertical(); GUILayout.EndScrollView(); } } GUILayout.EndArea(); }
public override void UpdateView(Event e, vFSMBehaviour curGraph) { base.UpdateView(e, curGraph); if (!backgroundTexture) { this.backgroundTexture = Resources.Load("grid") as Texture; } var color = GUI.color; GUI.color = vFSMBehaviourPreferences.gridBackgroundColor; if (!indrag) { if (curGraph) { curGraph.panOffset.x = curGraph.panOffset.x.NearestRound(vFSMHelper.dragSnap); curGraph.panOffset.y = curGraph.panOffset.y.NearestRound(vFSMHelper.dragSnap); } } GUI.Box(viewRect, GUIContent.none); if (Event.current.type == EventType.Repaint) { // Draw Background when Repainting // Offset from origin in tile units Vector2 tileOffset = new Vector2(-(1 + (curGraph ? curGraph.panOffset.x : 0)) / backgroundTexture.width, ((1 + (curGraph ? curGraph.panOffset.y : 0))) / backgroundTexture.height); // Amount of tiles Vector2 tileAmount = new Vector2(Mathf.Round(viewRect.width * 1) / backgroundTexture.width, Mathf.Round(viewRect.height * 1) / backgroundTexture.height); // Draw tiled background GUI.color = vFSMBehaviourPreferences.gridLinesColor; GUI.DrawTextureWithTexCoords(viewRect, backgroundTexture, new Rect(tileOffset, tileAmount)); if (curGraph == null) { GUI.Label(viewRect, "NO BEHAVIOUR", viewSkin.GetStyle("ViewMessage")); } } GUI.color = color; if (inDrawRect) { GUI.Box(selectorRect, "", viewSkin.GetStyle("SelectorArea")); } if (curGraph) { if (curGraph.onSelectState == null) { curGraph.onSelectState = OnSelectState; } curGraph.UpdateGraphGUI(e, viewRect, viewSkin); } #region Draw Work View Icons GUI.BeginGroup(viewRect); GUI.color = new Color(1, 1, 1, 0.2f); GUI.DrawTexture(new Rect(viewRect.width - 105, viewRect.height - 105, 100, 100), Resources.Load("Textures/Editor/logo") as Texture2D); if (currentFSM != null && currentFSM.icon != null) { GUI.DrawTexture(new Rect(viewRect.width - 105, viewRect.y + 5, 100, 100), currentFSM ? currentFSM.icon : null); } GUI.color = color; #endregion GUI.EndGroup(); GUI.Box(viewRect, "", viewSkin.GetStyle("BoxShadown")); ProcessEvents(e); }