Exemplo n.º 1
0
    void OnGUI()
    {
        //Name

        GUILayout.Label("Name", GUILayout.Width(100));

        List <string> names = new List <string> ();

        try {
            names = AtomsContainer.Load(AtomListWindow.path).list;
        }

        catch (Exception e) {
            Debug.Log(e.Message);
        }



        if (names.Count > 0)
        {
            selectedName = names.IndexOf(atom.name);
            selectedName = EditorGUILayout.Popup(selectedName, names.ToArray(), GUILayout.Width(100));

            if (selectedName == -1)
            {
                selectedName = 0;
            }

            atom.name = names [selectedName];
        }

        else
        {
            EditorUtility.DisplayDialog("", "No names in atom names list. Please, fill the list before adding a condition", "OK");
        }


        //Sign
        switch (atom.sign)
        {
        case true:
            selectedSign = 0;
            break;

        case false:
            selectedSign = 1;
            break;
        }

        GUILayout.Space(20.0f);


        GUILayout.Label("Sign", GUILayout.Width(100));

        string[] options2 = { "true", "false" };

        selectedSign = EditorGUILayout.Popup(selectedSign, options2, GUILayout.Width(100));

        if (selectedSign == 0)
        {
            atom.sign = true;
        }
        else if (selectedSign == 1)
        {
            atom.sign = false;
        }


        GUILayout.Space(20.0f);

        GUILayout.Label("Variables", GUILayout.Width(100));

        if (atom.terms.Count > 0)
        {
            for (int i = 0; i < atom.terms.Count; i++)
            {
                Term variable = (Term)atom.terms[i];


                GUILayout.BeginHorizontal(GUILayout.Width(200));
                GUILayout.Label("Name");


                List <string> domVarNamesList = new List <string> ();


                foreach (Term v in agent.domainVariables)
                {
                    domVarNamesList.Add(v.key);
                }


                foreach (Term v in agent.domainVariables)
                {
                    if (v.key == variable.key)
                    {
                        varIndex = agent.domainVariables.IndexOf(v);
                        break;
                    }
                }


                string[] DomvarNames = domVarNamesList.ToArray();

                varIndex = EditorGUILayout.Popup(varIndex, DomvarNames);

                string newVar = agent.domainVariables [varIndex].key;


                GUILayout.Space(10.0f);

                if (agent.domainVariables [varIndex].key != variable.key)
                {
                    atom.removeTerm(variable);
                    atom.addTerm(new Term(newVar));
                }

                if (GUILayout.Button("Delete", GUILayout.Width(100)))
                {
                    atom.removeTerm(variable);
                }

                GUILayout.EndHorizontal();
            }
        }

        if (GUILayout.Button("Add Variable", GUILayout.Width(200)))
        {
            if (agent.domainVariables.Count > 0)
            {
                for (int i = 0; i < agent.domainVariables.Count; i++)
                {
                    if (!atom.terms.Contains(agent.domainVariables [i]))
                    {
                        atom.addTerm(new Term(agent.domainVariables [i].key, null));
                        break;
                    }
                }
            }
            else
            {
                EditorUtility.DisplayDialog("", "No variables in planning variables list", "OK");
            }
        }
    }
Exemplo n.º 2
0
    void OnGUI()
    {
        //VIEW
        for (int i = 0; i < definitions.list.Count; i++)
        {
            FactDefinition fd = definitions.list[i];

            GUILayout.BeginHorizontal(GUILayout.Width(200));

            //Name

            GUILayout.Label("Name", GUILayout.Width(40));

            List <string> names = new List <string> ();

            try {
                names = AtomsContainer.Load(AtomListWindow.path).list;
            }

            catch (Exception e) {
                Debug.Log(e.Message);
            }



            selectedName = names.IndexOf(fd.name);

            selectedName = EditorGUILayout.Popup(selectedName, names.ToArray(), GUILayout.Width(50));

            if (selectedName == -1)
            {
                selectedName = 0;
            }

            fd.name = names [selectedName];



            //Type
            GUILayout.Label("Type");

            if (fd.factType == "Property")
            {
                selectedFactType = 0;
            }

            else if (fd.factType == "Relation")
            {
                selectedFactType = 1;
            }

            selectedFactType = EditorGUILayout.Popup(selectedFactType, factTypes, GUILayout.Width(100));
            fd.factType      = factTypes[selectedFactType];

            GUILayout.Space(20);


            //Arguments of expression
            GUILayout.Label("Arguments");

            int j;

            for (j = 0; j < argsTypes.list.Count; j++)
            {
                if (fd.argsType == argsTypes.list[j])
                {
                    selectedArgType = j;
                    break;
                }
            }



            selectedArgType = EditorGUILayout.Popup(selectedArgType, argsTypes.list.ToArray(), GUILayout.Width(100));
            fd.argsType     = argsTypes.list[selectedArgType];

            //MODEL/CONTROLLER
            if (GUILayout.Button("Edit", GUILayout.Width(50)))
            {
                AddArgumentWindow.ShowWindow();
            }


            GUILayout.Space(20);


            //Expression
            GUILayout.Label("Expression");
            fd.expression = EditorGUILayout.TextField(fd.expression, GUILayout.Width(300));


            definitions.list[i] = fd;



            GUILayout.Space(20);

            if (GUILayout.Button("Delete", GUILayout.Width(100)))
            {
                definitions.Remove(definitions.list[i]);
            }

            GUILayout.EndHorizontal();
        }


        //MODEL/CONTROLLER
        if (GUILayout.Button("Add Definition", GUILayout.Width(200)))
        {
            if (AtomListWindow.atomNames.list.Count > 0)
            {
                definitions.Add(new FactDefinition("New Fact"));
            }
            else
            {
                EditorUtility.DisplayDialog("", "No names in atom names list. Please, fill the list before adding a definition", "OK");
            }
        }
    }
Exemplo n.º 3
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();


        //Sensory
        GUILayout.BeginHorizontal(GUILayout.Width(300));
        GUILayout.Label("Sensor", GUILayout.Width(110));

        if (agent.sensor == 0)
        {
            sensIndex = 0;
        }
        else if (agent.sensor == 1)
        {
            sensIndex = 1;
        }

        sensIndex = EditorGUILayout.Popup(sensIndex, sensors, GUILayout.Width(100));

        if (sensIndex == 1)
        {
            GUILayout.Label("Length");
            agent.radiusLength = EditorGUILayout.FloatField(agent.radiusLength, GUILayout.Width(25));
        }

        GUILayout.EndHorizontal();


        //Goal
        if (GUILayout.Button("Edit Goal"))
        {
            HTNEditorWindow.ShowWindow(agent, agent.goal);
        }


        agent.sensor = sensIndex;


        //Personality

        if (agent.knownPersonalities == null)
        {
            agent.knownPersonalities = PersonalitiesContainer.Load("Assets/personalities.xml").list;
        }

        if (PersonalityEditorWindow.personalities.list.Count > 0)
        {
            foreach (string personality in PersonalityEditorWindow.personalities.list)
            {
                if (!agent.knownPersonalities.Contains(personality))
                {
                    agent.knownPersonalities.Add(personality);
                }
            }


            GUILayout.BeginHorizontal(GUILayout.Width(200));

            GUILayout.Label("Personality");


            persIndex = PersonalityEditorWindow.personalities.list.Concat(new List <string>(new string[] { "none" })).ToList().IndexOf(agent.personality);


            string[] personalities = PersonalityEditorWindow.personalities.list.Concat(new List <string> (new string[] { "none" })).ToList().ToArray();

            GUILayout.Space(40.0f);

            persIndex = EditorGUILayout.Popup(persIndex, personalities, GUILayout.Width(100));

            if (persIndex == -1)
            {
                persIndex = 0;
            }
            agent.personality = personalities [persIndex];

            GUILayout.EndHorizontal();
        }


        //Algorithm

        GUILayout.BeginHorizontal(GUILayout.Width(200));
        GUILayout.Label("Backtracking");

        string[] options = { "Yes", "No" };

        backTracking = EditorGUILayout.Popup(backTracking, options, GUILayout.Width(100));

        if (backTracking == 0)
        {
            agent.backTracking = true;
        }
        else if (backTracking == 1)
        {
            agent.backTracking = false;
        }
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal(GUILayout.Width(200));
        GUILayout.Label("Methods ordering");

        GUILayout.Space(10.0f);
        string[] options2 = { "None", "Preferences", "Preferences and costs" };

        chosenOrdering = EditorGUILayout.Popup(chosenOrdering, options2, GUILayout.Width(100));

        switch (chosenOrdering)
        {
        case 0:
            agent.methodsOrdering = MethodsOrdering.NONE;
            break;

        case 1:
            agent.methodsOrdering = MethodsOrdering.PREF;
            break;

        case 2:
            agent.methodsOrdering = MethodsOrdering.COSTANDPREF;
            break;

        default:
            break;
        }
        GUILayout.EndHorizontal();


        //Domain variables
        foldout = EditorGUILayout.Foldout(foldout, "Internal knowledge");

        if (foldout)
        {
            //MODEL

            GUIStyle style = new GUIStyle();

            style.alignment = (TextAnchor)TextAlignment.Left;


            EditorGUILayout.BeginHorizontal(style, GUILayout.Width(400));

            EditorGUILayout.LabelField("Size", GUILayout.Width(130));

            int oldSize = agent.serializedKnowledge.Count;

            string size = EditorGUILayout.TextField(oldSize.ToString(), GUILayout.Width(300));


            int newSize = 0;

            System.Int32.TryParse(size, out newSize);

            if (newSize > oldSize)
            {
                for (int i = 0; i < newSize - oldSize; i++)
                {
                    SerializedFact fact = new SerializedFact("New Fact");
                    fact.values.Add(GameObject.FindObjectOfType <GameObject>().name);

                    agent.serializedKnowledge.Add(fact);
                }
            }

            else if (newSize < oldSize)
            {
                for (int i = oldSize - 1; i > (oldSize - (oldSize - newSize) - 1); i--)
                {
                    agent.serializedKnowledge.Remove(agent.serializedKnowledge[i]);
                }
            }

            EditorGUILayout.EndHorizontal();

            if (agent.serializedKnowledge.Count > 0)
            {
                for (int i = 0; i < newSize; i++)
                {
                    SerializedFact fact = agent.serializedKnowledge[i];

                    EditorGUILayout.BeginHorizontal(style, GUILayout.Width(400));

                    //Fact name

                    EditorGUILayout.LabelField("Name", GUILayout.Width(60));

                    List <string> names = new List <string> ();

                    try {
                        names = AtomsContainer.Load(AtomListWindow.path).list;
                    }

                    catch (Exception e) {
                        Debug.Log(e.Message);
                    }


                    if (names.Count > 0)
                    {
                        selectedName = names.IndexOf(fact.name);

                        selectedName = EditorGUILayout.Popup(selectedName, names.ToArray(), GUILayout.Width(50));


                        if (selectedName == -1)
                        {
                            selectedName = 0;
                        }

                        fact.name = names [selectedName];
                    }

                    else
                    {
                        EditorUtility.DisplayDialog("", "No names in atom names list. Please, fill the list before adding a definition", "OK");
                    }

                    //Fact sign
                    fact.sign = System.Convert.ToBoolean(EditorGUILayout.Popup(System.Convert.ToInt32(fact.sign), factSigns, GUILayout.Width(60)));

                    //Fact type

                    if (fact.values.Count == 1)
                    {
                        selectedFactType = 0;
                    }

                    else if (fact.values.Count == 2)
                    {
                        selectedFactType = 1;
                    }

                    selectedFactType = EditorGUILayout.Popup(selectedFactType, factTypes, GUILayout.Width(40));

                    if (selectedFactType == 1 && fact.values.Count == 1)
                    {
                        fact.values.Add(GameObject.FindObjectOfType <GameObject>().name);
                    }

                    if (selectedFactType == 0 && fact.values.Count == 2)
                    {
                        fact.values.Remove(fact.values [1]);
                    }

                    //Fact arguments

                    GameObject found = GameObject.Find(fact.values [0]);

                    fact.values[0] = ((GameObject)EditorGUILayout.ObjectField(found, typeof(GameObject), true, GUILayout.Width(80))).name;

                    if (fact.values.Count == 2)
                    {
                        found = GameObject.Find(fact.values [1]);

                        fact.values [1] = ((GameObject)EditorGUILayout.ObjectField(found, typeof(GameObject), true, GUILayout.Width(80))).name;
                    }

                    EditorGUILayout.EndHorizontal();

                    agent.serializedKnowledge[i] = fact;
                }
            }
        }

        //Joint plan
        if (agent.agentType == AgentTypes.LEADER)
        {
            foldout3 = EditorGUILayout.Foldout(foldout3, "Joint plan");

            if (foldout3)
            {
                GUIStyle style = new GUIStyle();

                style.alignment = (TextAnchor)TextAlignment.Left;


                EditorGUILayout.BeginHorizontal(style, GUILayout.Width(400));

                EditorGUILayout.LabelField("Size", GUILayout.Width(130));

                int oldSize = agent.groupMembers.Count;

                string size = EditorGUILayout.TextField(oldSize.ToString(), GUILayout.Width(300));


                int newSize = 0;

                System.Int32.TryParse(size, out newSize);


                if (newSize > oldSize)
                {
                    for (int i = 0; i < newSize - oldSize; i++)
                    {
                        HTNAgent[] agents = GameObject.FindObjectsOfType <HTNAgent>();

                        foreach (HTNAgent a in agents)
                        {
                            if (a.agentType == AgentTypes.MEMBER)
                            {
                                agent.groupMembers.Add(a);
                            }
                        }
                    }
                }
                else if (newSize < oldSize)
                {
                    for (int i = oldSize - 1; i > (oldSize - (oldSize - newSize) - 1); i--)
                    {
                        agent.groupMembers.Remove(agent.groupMembers[i]);
                    }
                }

                EditorGUILayout.EndHorizontal();


                //VIEW


                if (agent.groupMembers.Count > 0)
                {
                    for (int i = 0; i < newSize; i++)
                    {
                        HTNAgent member = agent.groupMembers[i];


                        EditorGUILayout.BeginHorizontal(style, GUILayout.Width(400));

                        //Agent
                        EditorGUILayout.LabelField("Agent", GUILayout.Width(130));


                        member = (HTNAgent)EditorGUILayout.ObjectField(member, typeof(HTNAgent), GUILayout.Width(100));


                        //Goal
                        if (GUILayout.Button("Edit Goal"))
                        {
                            HTNEditorWindow.ShowWindow(member, member.goal);
                        }

                        member.domainVariables           = agent.domainVariables;
                        member.serializedDomainVariables = agent.serializedDomainVariables;


                        EditorGUILayout.EndHorizontal();

                        if (member.agentType == AgentTypes.MEMBER)
                        {
                            agent.groupMembers[i] = member;
                        }
                        else
                        {
                            EditorUtility.DisplayDialog("INVALID ACTION", "Can only add MEMBER type agents", "OK");
                        }
                    }
                }
            }
        }



        //Domain variables
        foldout2 = EditorGUILayout.Foldout(foldout2, "Planning variables");

        if (foldout2)
        {
            //MODEL


            GUIStyle style = new GUIStyle();

            style.alignment = (TextAnchor)TextAlignment.Left;


            EditorGUILayout.BeginHorizontal(style, GUILayout.Width(400));

            EditorGUILayout.LabelField("Size", GUILayout.Width(130));

            int oldSize = agent.domainVariables.Count;

            string size = EditorGUILayout.TextField(oldSize.ToString(), GUILayout.Width(300));


            int newSize = 0;

            System.Int32.TryParse(size, out newSize);


            if (newSize > oldSize)
            {
                for (int i = 0; i < newSize - oldSize; i++)
                {
                    agent.domainVariables.Add(new Term(""));
                    agent.serializedDomainVariables.Add(new SerializedVariable(""));
                }
            }
            else if (newSize < oldSize)
            {
                for (int i = oldSize - 1; i > (oldSize - (oldSize - newSize) - 1); i--)
                {
                    agent.domainVariables.Remove(agent.domainVariables[i]);
                    agent.serializedDomainVariables.Remove(agent.serializedDomainVariables[i]);
                }
            }

            EditorGUILayout.EndHorizontal();


            //VIEW


            if (agent.domainVariables.Count > 0)
            {
                for (int i = 0; i < newSize; i++)
                {
                    SerializedVariable variable = agent.serializedDomainVariables[i];


                    EditorGUILayout.BeginHorizontal(style, GUILayout.Width(400));

                    //Name
                    EditorGUILayout.LabelField("Name", GUILayout.Width(130));

                    variable.key = EditorGUILayout.TextField(variable.key, GUILayout.Width(100));


                    //Value
                    GameObject found = GameObject.Find(variable.value);


                    GameObject obj = ((GameObject)EditorGUILayout.ObjectField(found, typeof(GameObject), true, GUILayout.Width(200)));

                    if (obj == null)
                    {
                        variable.value = "";
                    }
                    else
                    {
                        variable.value = obj.name;
                    }

                    EditorGUILayout.EndHorizontal();

                    agent.serializedDomainVariables[i] = variable;
                    agent.domainVariables[i].key       = variable.key;
                }
            }
        }
    }