///Get a generic menu per variable static GenericMenu GetVariableMenu(Variable data, IBlackboard bb) { var menu = new GenericMenu(); if (data.varType == typeof(VariableSeperator)) { menu.AddItem(new GUIContent("Rename"), false, () => { (data.value as VariableSeperator).isEditingName = true; }); menu.AddItem(new GUIContent("Remove"), false, () => { bb.RemoveVariable(data.name); }); return(menu); } System.Action <PropertyInfo> SelectProp = (p) => { data.BindProperty(p); }; System.Action <FieldInfo> SelectField = (f) => { data.BindProperty(f); }; menu.AddDisabledItem(new GUIContent(string.Format("Type: {0}", data.varType.FriendlyName()))); if (bb.propertiesBindTarget != null) { foreach (var comp in bb.propertiesBindTarget.GetComponents(typeof(Component)).Where(c => c.hideFlags != HideFlags.HideInInspector)) { menu = EditorUtils.GetInstanceFieldSelectionMenu(comp.GetType(), data.varType, SelectField, menu, "Bind (Self)"); menu = EditorUtils.GetInstancePropertySelectionMenu(comp.GetType(), data.varType, SelectProp, false, false, menu, "Bind (Self)"); } } foreach (var type in TypePrefs.GetPreferedTypesList()) { menu = EditorUtils.GetStaticFieldSelectionMenu(type, data.varType, SelectField, menu, "Bind (Static)"); menu = EditorUtils.GetStaticPropertySelectionMenu(type, data.varType, SelectProp, false, false, menu, "Bind (Static)"); } menu.AddItem(new GUIContent("Duplicate"), false, () => { var dup = bb.AddVariable(data.name + '.', data.varType); dup.value = data.value; }); menu.AddItem(new GUIContent("Protected"), data.isProtected, () => { data.isProtected = !data.isProtected; }); menu.AddSeparator("/"); if (data.hasBinding) { menu.AddItem(new GUIContent("UnBind"), false, () => { data.UnBindProperty(); }); } else { menu.AddDisabledItem(new GUIContent("UnBind")); } menu.AddItem(new GUIContent("Delete Variable"), false, () => { if (EditorUtility.DisplayDialog("Delete Variable '" + data.name + "'", "Are you sure?", "Yes", "No")) { bb.RemoveVariable(data.name); GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; } }); return(menu); }
public void ClearBlackboardAtChild() { var childCount = transform.childCount; for (int i = 0; i < childCount; ++i) { var child = transform.GetChild(i); IBlackboard bb = child.GetComponent <Blackboard>(); if (bb != null) { List <string> variableNameList = new List <string>(); foreach (var pair in bb.variables) { variableNameList.Add(pair.Key); } foreach (var name in variableNameList) { bb.RemoveVariable(name); } } } }
public static void ShowVariables(IBlackboard bb, UnityEngine.Object contextParent) { GUI.skin.label.richText = true; var e = Event.current; var layoutOptions = new GUILayoutOption[] { GUILayout.MaxWidth(100), GUILayout.ExpandWidth(true), GUILayout.Height(16) }; //Begin undo check UndoManager.CheckUndo(contextParent, "Blackboard Inspector"); //Add variable button GUI.backgroundColor = new Color(0.8f, 0.8f, 1); if (GUILayout.Button("Add Variable")) { System.Action <System.Type> SelectVar = (t) => { var name = "my" + t.FriendlyName(); while (bb.GetVariable(name) != null) { name += "."; } bb.AddVariable(name, t); }; System.Action <PropertyInfo> SelectBoundProp = (p) => { var newVar = bb.AddVariable(p.Name, p.PropertyType); newVar.BindProperty(p); }; System.Action <FieldInfo> SelectBoundField = (f) => { var newVar = bb.AddVariable(f.Name, f.FieldType); newVar.BindProperty(f); }; var menu = new GenericMenu(); menu = EditorUtils.GetPreferedTypesSelectionMenu(typeof(object), SelectVar, true, menu, "New"); if (bb.propertiesBindTarget != null) { foreach (var comp in bb.propertiesBindTarget.GetComponents(typeof(Component)).Where(c => c.hideFlags != HideFlags.HideInInspector)) { menu = EditorUtils.GetPropertySelectionMenu(comp.GetType(), typeof(object), SelectBoundProp, false, false, menu, "Bound (Self)/Property"); menu = EditorUtils.GetFieldSelectionMenu(comp.GetType(), typeof(object), SelectBoundField, menu, "Bound (Self)/Field"); } } foreach (var type in UserTypePrefs.GetPreferedTypesList(typeof(object), true)) { menu = EditorUtils.GetStaticPropertySelectionMenu(type, typeof(object), SelectBoundProp, false, false, menu, "Bound (Static)/Property"); menu = EditorUtils.GetStaticFieldSelectionMenu(type, typeof(object), SelectBoundField, menu, "Bound (Static)/Field"); } menu.AddSeparator("/"); menu.AddItem(new GUIContent("Add Header Separator"), false, () => { SelectVar(typeof(VariableSeperator)); }); menu.ShowAsContext(); e.Use(); } GUI.backgroundColor = Color.white; //Simple column header info if (bb.variables.Keys.Count != 0) { GUILayout.BeginHorizontal(); GUI.color = Color.yellow; GUILayout.Label("Name", layoutOptions); GUILayout.Label("Value", layoutOptions); GUI.color = Color.white; GUILayout.EndHorizontal(); } else { EditorGUILayout.HelpBox("Blackboard has no variables", MessageType.Info); } if (!tempStates.ContainsKey(bb)) { tempStates.Add(bb, new ReorderingState(bb.variables.Values.ToList())); } //Make temporary list for editing variables if (!tempStates[bb].isReordering) { tempStates[bb].list = bb.variables.Values.ToList(); } //store the names of the variables being used by current graph selection. var usedVariables = GetUsedVariablesBySelectionParameters(); //The actual variables reorderable list EditorUtils.ReorderableList(tempStates[bb].list, delegate(int i){ var data = tempStates[bb].list[i]; if (data == null) { GUILayout.Label("NULL Variable!"); return; } var isUsed = usedVariables.Contains(data.name); GUILayout.Space(data.varType == typeof(VariableSeperator)? 5 : 0); GUILayout.BeginHorizontal(); //Name of the variable GUI control if (!Application.isPlaying) { //The small box on the left to re-order variables GUI.backgroundColor = new Color(1, 1, 1, 0.8f); GUILayout.Box("", GUILayout.Width(6)); GUI.backgroundColor = new Color(0.7f, 0.7f, 0.7f, 0.3f); if (e.type == EventType.MouseDown && e.button == 0 && GUILayoutUtility.GetLastRect().Contains(e.mousePosition)) { tempStates[bb].isReordering = true; if (data.varType != typeof(VariableSeperator)) { pickedVariable = data; pickedVariableBlackboard = bb; } } //Make name field red if same name exists if (tempStates[bb].list.Where(v => v != data).Select(v => v.name).Contains(data.name)) { GUI.backgroundColor = Color.red; } GUI.enabled = !data.isProtected; if (data.varType != typeof(VariableSeperator)) { data.name = EditorGUILayout.TextField(data.name, layoutOptions); EditorGUI.indentLevel = 0; } else { var separator = (VariableSeperator)data.value; GUI.color = Color.yellow; if (separator.isEditingName) { data.name = EditorGUILayout.TextField(data.name, layoutOptions); } else { GUILayout.Label(string.Format("<b>{0}</b>", data.name).ToUpper(), layoutOptions); } GUI.color = Color.white; if (!separator.isEditingName) { if (e.type == EventType.MouseDown && e.button == 0 && e.clickCount == 2 && GUILayoutUtility.GetLastRect().Contains(e.mousePosition)) { separator.isEditingName = true; GUIUtility.keyboardControl = 0; } } if (separator.isEditingName) { if ((e.isKey && e.keyCode == KeyCode.Return) || (e.rawType == EventType.MouseUp && !GUILayoutUtility.GetLastRect().Contains(e.mousePosition))) { separator.isEditingName = false; GUIUtility.keyboardControl = 0; } } data.value = separator; } GUI.enabled = true; GUI.backgroundColor = Color.white; } else { //Don't allow name edits in play mode. Instead show just a label if (data.varType != typeof(VariableSeperator)) { GUI.backgroundColor = new Color(0.7f, 0.7f, 0.7f); GUI.color = new Color(0.8f, 0.8f, 1f); GUILayout.Label(data.name, layoutOptions); } else { GUI.color = Color.yellow; GUILayout.Label(string.Format("<b>{0}</b>", data.name.ToUpper()), layoutOptions); GUI.color = Color.white; } } //reset coloring GUI.color = Color.white; GUI.backgroundColor = Color.white; //Highlight used variable by selection? if (isUsed) { var r = GUILayoutUtility.GetLastRect(); r.xMin += 2; r.xMax -= 2; r.yMax -= 4; GUI.Box(r, "", (GUIStyle)"LightmapEditorSelectedHighlight"); } //Show the respective data GUI if (data.varType != typeof(VariableSeperator)) { ShowDataGUI(data, bb, contextParent, layoutOptions); } else { GUILayout.Space(0); GUILayout.Space(0); } //reset coloring GUI.color = Color.white; GUI.backgroundColor = Color.white; //'X' to delete data if (!Application.isPlaying && GUILayout.Button("X", GUILayout.Width(20), GUILayout.Height(16))) { if (EditorUtility.DisplayDialog("Delete Variable '" + data.name + "'", "Are you sure?", "Yes", "No!")) { tempStates[bb].list.Remove(data); bb.RemoveVariable(data.name); GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; } } GUILayout.EndHorizontal(); }, contextParent); //reset coloring GUI.backgroundColor = Color.white; GUI.color = Color.white; if ((GUI.changed && !tempStates[bb].isReordering) || e.rawType == EventType.MouseUp) { tempStates[bb].isReordering = false; EditorApplication.delayCall += () => { pickedVariable = null; pickedVariableBlackboard = null; }; //reconstruct the dictionary try { bb.variables = tempStates[bb].list.ToDictionary(d => d.name, d => d); } catch { Debug.LogError("Blackboard has duplicate names!"); } } //Check dirty UndoManager.CheckDirty(contextParent); }
static void ShowDataGUI(Variable data, IBlackboard bb, UnityEngine.Object contextParent, GUILayoutOption[] layoutOptions) { //Bind info or value GUI control if (data.hasBinding) { var idx = data.propertyPath.LastIndexOf('.'); var typeName = data.propertyPath.Substring(0, idx); var memberName = data.propertyPath.Substring(idx + 1); GUI.color = new Color(0.8f, 0.8f, 1); GUILayout.Label(string.Format(".{0} ({1})", memberName, typeName.Split('.').Last()), layoutOptions); GUI.color = Color.white; } else { GUI.enabled = !data.isProtected; data.value = VariableField(data, contextParent, layoutOptions); GUI.enabled = true; GUI.backgroundColor = Color.white; } //Variable options menu if (!Application.isPlaying && GUILayout.Button("●", GUILayout.Width(17), GUILayout.Height(16))) { System.Action <PropertyInfo> SelectProp = (p) => { data.BindProperty(p); }; System.Action <FieldInfo> SelectField = (f) => { data.BindProperty(f); }; var menu = new GenericMenu(); menu.AddDisabledItem(new GUIContent(string.Format("Type: {0}", data.varType.FriendlyName()))); if (bb.propertiesBindTarget != null) { foreach (var comp in bb.propertiesBindTarget.GetComponents(typeof(Component)).Where(c => c.hideFlags != HideFlags.HideInInspector)) { menu = EditorUtils.GetPropertySelectionMenu(comp.GetType(), data.varType, SelectProp, false, false, menu, "Bind (Self)/Property"); menu = EditorUtils.GetFieldSelectionMenu(comp.GetType(), data.varType, SelectField, menu, "Bind (Self)/Field"); } } foreach (var type in UserTypePrefs.GetPreferedTypesList(typeof(object))) { menu = EditorUtils.GetStaticPropertySelectionMenu(type, data.varType, SelectProp, false, false, menu, "Bind (Static)/Property"); menu = EditorUtils.GetStaticFieldSelectionMenu(type, data.varType, SelectField, menu, "Bind (Static)/Field"); } menu.AddItem(new GUIContent("Protected"), data.isProtected, () => { data.isProtected = !data.isProtected; }); menu.AddSeparator("/"); if (data.hasBinding) { menu.AddItem(new GUIContent("UnBind"), false, () => { data.UnBindProperty(); }); } else { menu.AddDisabledItem(new GUIContent("UnBind")); } menu.AddItem(new GUIContent("Delete Variable"), false, () => { if (EditorUtility.DisplayDialog("Delete Variable '" + data.name + "'", "Are you sure?", "Yes", "No")) { bb.RemoveVariable(data.name); GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; } }); menu.ShowAsContext(); Event.current.Use(); } }