Ejemplo n.º 1
0
        ///Replace the text of the statement found in brackets, with blackboard variables ToString and returns a Statement copy
        public IStatement BlackboardReplace(IBlackboard bb)
        {
            var copy = ParadoxNotion.Serialization.JSONSerializer.Clone <Statement>(this);

            copy.text = copy.text.ReplaceWithin('[', ']', (input) =>
            {
                object o = null;
                if (bb != null)     //referenced blackboard replace
                {
                    var v = bb.GetVariable(input, typeof(object));
                    if (v != null)
                    {
                        o = v.value;
                    }
                }

                if (input.Contains("/"))     //global blackboard replace
                {
                    var globalBB = GlobalBlackboard.Find(input.Split('/').First());
                    if (globalBB != null)
                    {
                        var v = globalBB.GetVariable(input.Split('/').Last(), typeof(object));
                        if (v != null)
                        {
                            o = v.value;
                        }
                    }
                }
                return(o != null ? o.ToString() : input);
            });

            return(copy);
        }
Ejemplo n.º 2
0
        ///Resolve the final Variable reference.
        private Variable ResolveReference(IBlackboard targetBlackboard, bool useID)
        {
            var targetName = this.name;

            if (targetName != null && targetName.Contains("/"))
            {
                var split = targetName.Split('/');
                targetBlackboard = GlobalBlackboard.Find(split[0]);
                targetName       = split[1];
            }

            Variable result = null;

            if (targetBlackboard == null)
            {
                return(null);
            }
            if (useID && targetVariableID != null)
            {
                result = targetBlackboard.GetVariableByID(targetVariableID);
            }
            if (result == null && !string.IsNullOrEmpty(targetName))
            {
                result = targetBlackboard.GetVariable(targetName, varType);
            }
            return(result);
        }
Ejemplo n.º 3
0
        ///Replace the text of the statement found in brackets, with blackboard variables ToString
        public Statement BlackboardReplace(IBlackboard bb)
        {
            var s = text;
            var i = 0;

            while ((i = s.IndexOf('[', i)) != -1)
            {
                var end     = s.Substring(i + 1).IndexOf(']');
                var varName = s.Substring(i + 1, end);
                var output  = s.Substring(i, end + 2);

                object o = null;
                if (bb != null)
                {
                    var v = bb.GetVariable(varName, typeof(object));
                    if (v != null)
                    {
                        o = v.value;
                    }
                }
                s = s.Replace(output, o != null? o.ToString() : output);

                i++;
            }

            return(new Statement(s, audio, meta));
        }
Ejemplo n.º 4
0
		///Set the blackboard reference provided for all *public* BBParameter and List<BBParameter> fields on the target object provided.
		public static void SetBBFields(object o, IBlackboard bb){
			ParseObject(o, (bbParam)=>
			{
				bbParam.bb = bb;
				if (bb != null){
					bbParam.varRef = bb.GetVariable(bbParam.name);
				}
			});
		}
Ejemplo n.º 5
0
 ///Set the blackboard reference provided for all *public* BBParameter and List<BBParameter> fields on the target object provided.
 public static void SetBBFields(object o, IBlackboard bb)
 {
     ParseObject(o, (bbParam) =>
     {
         bbParam.bb = bb;
         if (bb != null)
         {
             bbParam.varRef = bb.GetVariable(bbParam.name);
         }
     });
 }
Ejemplo n.º 6
0
        //Add variable button
        static void DoAddVariableButton(IBlackboard bb, Event e)
        {
            GUI.backgroundColor = EditorUtils.lightBlue;
            if (GUILayout.Button("Add Variable"))
            {
                System.Action <System.Type> AddNewVariable = (t) =>
                {
                    var name = "my" + t.FriendlyName();
                    while (bb.GetVariable(name) != null)
                    {
                        name += ".";
                    }
                    bb.AddVariable(name, t);
                };

                System.Action <PropertyInfo> AddBoundProp = (p) =>
                {
                    var newVar = bb.AddVariable(p.Name, p.PropertyType);
                    newVar.BindProperty(p);
                };

                System.Action <FieldInfo> AddBoundField = (f) =>
                {
                    var newVar = bb.AddVariable(f.Name, f.FieldType);
                    newVar.BindProperty(f);
                };

                var menu = new GenericMenu();
                menu = EditorUtils.GetPreferedTypesSelectionMenu(typeof(object), AddNewVariable, menu, "New", true);

                if (bb.propertiesBindTarget != null)
                {
                    foreach (var comp in bb.propertiesBindTarget.GetComponents(typeof(Component)).Where(c => c.hideFlags == 0))
                    {
                        menu = EditorUtils.GetPropertySelectionMenu(comp.GetType(), typeof(object), AddBoundProp, false, false, menu, "Bound (Self)/Property");
                        menu = EditorUtils.GetFieldSelectionMenu(comp.GetType(), typeof(object), AddBoundField, menu, "Bound (Self)/Field");
                    }
                }

                foreach (var type in UserTypePrefs.GetPreferedTypesList(typeof(object)))
                {
                    menu = EditorUtils.GetStaticPropertySelectionMenu(type, typeof(object), AddBoundProp, false, false, menu, "Bound (Static)/Property");
                    menu = EditorUtils.GetStaticFieldSelectionMenu(type, typeof(object), AddBoundField, menu, "Bound (Static)/Field");
                }


                menu.AddSeparator("/");
                menu.AddItem(new GUIContent("Add Header Separator"), false, () => { bb.AddVariable("Separator (Double Click To Rename)", new VariableSeperator()); });

                menu.ShowAsContext();
                e.Use();
            }
            GUI.backgroundColor = Color.white;
        }
Ejemplo n.º 7
0
        ///Return get add variable menu
        static GenericMenu GetAddVariableMenu(IBlackboard bb, UnityEngine.Object contextParent)
        {
            System.Action <System.Type> AddNewVariable = (t) =>
            {
                Undo.RecordObject(contextParent, "Variable Added");
                var name = "my" + t.FriendlyName();
                while (bb.GetVariable(name) != null)
                {
                    name += ".";
                }
                bb.AddVariable(name, t);
            };

            System.Action <PropertyInfo> AddBoundProp = (p) =>
            {
                Undo.RecordObject(contextParent, "Variable Added");
                var newVar = bb.AddVariable(p.Name, p.PropertyType);
                newVar.BindProperty(p);
            };

            System.Action <FieldInfo> AddBoundField = (f) =>
            {
                Undo.RecordObject(contextParent, "Variable Added");
                var newVar = bb.AddVariable(f.Name, f.FieldType);
                newVar.BindProperty(f);
            };

            var menu = new GenericMenu();

            menu = EditorUtils.GetPreferedTypesSelectionMenu(typeof(object), AddNewVariable, menu, "New", true);

            if (bb.propertiesBindTarget != null)
            {
                foreach (var comp in bb.propertiesBindTarget.GetComponents(typeof(Component)).Where(c => c.hideFlags == 0))
                {
                    menu = EditorUtils.GetInstanceFieldSelectionMenu(comp.GetType(), typeof(object), AddBoundField, menu, "Bound (Self)");
                    menu = EditorUtils.GetInstancePropertySelectionMenu(comp.GetType(), typeof(object), AddBoundProp, false, false, menu, "Bound (Self)");
                }
            }

            foreach (var type in TypePrefs.GetPreferedTypesList(typeof(object)))
            {
                menu = EditorUtils.GetStaticFieldSelectionMenu(type, typeof(object), AddBoundField, menu, "Bound (Static)");
                menu = EditorUtils.GetStaticPropertySelectionMenu(type, typeof(object), AddBoundProp, false, false, menu, "Bound (Static)");
            }


            menu.AddSeparator("/");
            menu.AddItem(new GUIContent("Add Header Separator"), false, () => { bb.AddVariable("Separator (Double Click To Rename)", new VariableSeperator()); });
            return(menu);
        }
Ejemplo n.º 8
0
        public static void UpdateVariable<T>(this IBlackboard blackboard, string nameVar, T newValue)
        {
            if (blackboard == null || nameVar == null)
            {
                return;
            }

            var v = blackboard.GetVariable<T>(nameVar);

            if (v != null)
            {
                v.SetValue(newValue);
            }
            else
            {
                blackboard.AddVariable<T>(nameVar, newValue);
            }
        }
Ejemplo n.º 9
0
        ///Replace the text of the statement found in brackets, with blackboard variables ToString and returns a Statement copy
        public Statement BlackboardReplace(IBlackboard bb)
        {
            var copy = ParadoxNotion.Serialization.JSONSerializer.Clone <Statement>(this);
            var s    = text;
            var i    = 0;

            while ((i = s.IndexOf('[', i)) != -1)
            {
                var end    = s.Substring(i + 1).IndexOf(']');
                var input  = s.Substring(i + 1, end);                //what's in the brackets
                var output = s.Substring(i, end + 2);                //what should be replaced (includes brackets)

                object o = null;
                if (bb != null)                  //referenced blackboard replace
                {
                    var v = bb.GetVariable(input, typeof(object));
                    if (v != null)
                    {
                        o = v.value;
                    }
                }

                if (input.Contains("/"))                  //global blackboard replace
                {
                    var globalBB = GlobalBlackboard.Find(input.Split('/').First());
                    if (globalBB != null)
                    {
                        var v = globalBB.GetVariable(input.Split('/').Last(), typeof(object));
                        if (v != null)
                        {
                            o = v.value;
                        }
                    }
                }

                s = s.Replace(output, o != null? o.ToString() : output);

                i++;
            }

            copy.text = s;
            return(copy);
        }
Ejemplo n.º 10
0
        private Variable ResolveReference(IBlackboard targetBlackboard, bool useID)
        {
            var targetName = this.name;
            if (targetName != null && targetName.Contains("/")){
                var split = targetName.Split('/');
                targetBlackboard = GlobalBlackboard.Find(split[0]);
                targetName = split[1];
            }

            Variable result = null;
            if (targetBlackboard == null){ return null; }
            if (useID && targetVariableID != null){ result = targetBlackboard.GetVariableByID(targetVariableID); }
            if (result == null && !string.IsNullOrEmpty(targetName)){ result = targetBlackboard.GetVariable(targetName, varType); }
            return result;
        }
Ejemplo n.º 11
0
        ///----------------------------------------------------------------------------------------------

        ///Generic version of GetVariable.
        ///Includes parent blackboards upwards the hierarchy.
        public static Variable <T> GetVariable <T>(this IBlackboard blackboard, string varName)
        {
            return((Variable <T>)blackboard.GetVariable(varName, typeof(T)));
        }
        public static void ShowVariables(IBlackboard bb, UnityEngine.Object contextParent = null)
        {
            //Assumption
            if (contextParent == null)
                contextParent = bb as UnityEngine.Object;

            var layoutOptions = new GUILayoutOption[]{GUILayout.MaxWidth(100), GUILayout.ExpandWidth(true)};

            //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<System.Reflection.PropertyInfo> SelectBound = (p) =>
                {
                    var newVar = bb.AddVariable(p.Name, p.PropertyType);
                    newVar.BindProperty(p);
                };

                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 == 0) ){
                        menu = EditorUtils.GetPropertySelectionMenu(comp.GetType(), typeof(object), SelectBound, false, false, menu, "Property Bound");
                    }
                }

                menu.ShowAsContext();
                Event.current.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();

            //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;
                }

                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 (Event.current.type == EventType.MouseDown && Event.current.button == 0 && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition) )
                        tempStates[bb].isReordering = true;

                    //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.nonEditable;
                    data.name = EditorGUILayout.TextField(data.name, layoutOptions);
                    GUI.enabled = true;
                    GUI.backgroundColor = Color.white;

                } else {

                    //Don't allow name edits in play mode. Instead show just a label
                    GUI.backgroundColor = new Color(0.7f,0.7f,0.7f);
                    GUI.color = new Color(0.8f,0.8f,1f);
                    GUILayout.Label(data.name, layoutOptions);
                }

                //reset coloring
                GUI.color = Color.white;
                GUI.backgroundColor = Color.white;

                //Show the respective data GUI
                ShowDataGUI(data, bb, contextParent, layoutOptions);

                //reset coloring
                GUI.color = Color.white;
                GUI.backgroundColor = Color.white;

                //'X' to delete data
                if (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);
                    }
                }

                GUILayout.EndHorizontal();
            }, contextParent);

            //reset coloring
            GUI.backgroundColor = Color.white;
            GUI.color = Color.white;

            if ( (GUI.changed && !tempStates[bb].isReordering) || Event.current.type == EventType.MouseUp){
                tempStates[bb].isReordering = false;
                //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);
        }
Ejemplo n.º 13
0
        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);
        }
Ejemplo n.º 14
0
        public static void ShowVariables(IBlackboard bb, UnityEngine.Object contextParent)
        {
            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 == 0))
                    {
                        menu = EditorUtils.GetPropertySelectionMenu(comp.GetType(), typeof(object), SelectBoundProp, false, false, menu, "Bound Property");
                        menu = EditorUtils.GetFieldSelectionMenu(comp.GetType(), typeof(object), SelectBoundField, menu, "Bound Field");
                    }
                }

                menu.AddSeparator("/");
                menu.AddItem(new GUIContent("Separator"), false, () => { SelectVar(typeof(VariableSeperator)); });

                menu.ShowAsContext();
                Event.current.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();
            }

            //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;
                }

                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 (Event.current.type == EventType.MouseDown && Event.current.button == 0 && GUILayoutUtility.GetLastRect().Contains(Event.current.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);
                    }
                    else
                    {
                        GUILayout.Box("------------", layoutOptions);
                    }
                    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.black;
                        GUILayout.Box("------------", layoutOptions);
                        GUI.color = Color.white;
                    }
                }

                //reset coloring
                GUI.color           = Color.white;
                GUI.backgroundColor = Color.white;

                //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);
                    }
                }

                GUILayout.EndHorizontal();
            }, contextParent);

            //reset coloring
            GUI.backgroundColor = Color.white;
            GUI.color           = Color.white;

            if ((GUI.changed && !tempStates[bb].isReordering) || Event.current.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);
        }
Ejemplo n.º 15
0
        ///Replace the text of the statement found in brackets, with blackboard variables ToString
        public Statement BlackboardReplace(IBlackboard bb)
        {
            var s = text;
            var i = 0;
            while ( (i = s.IndexOf('[', i)) != -1){

                var end = s.Substring(i + 1).IndexOf(']');
                var varName = s.Substring(i + 1, end);
                var output = s.Substring(i, end + 2);

                object o = null;
                if (bb != null){
                    var v = bb.GetVariable(varName, typeof(object));
                    if (v != null){
                        o = v.value;
                    }
                }
                s = s.Replace(output, o != null? o.ToString() : output);

                i++;
            }

            return new Statement(s, audio, meta);
        }