Exemplo n.º 1
0
        protected void DrawRules()
        {
            RuleSet       ruleSet = ((Seed)target).Rules;
            List <char>   keys    = ruleSet.Rules.KeyList;
            List <string> values  = ruleSet.Rules.ValueList;

            if (values.Count != keys.Count)
            {
                Debug.LogError("RuleSet value count (" + values.Count + ") not equal to key count(" + keys.Count + ")");
                keys.Clear();
                values.Clear();
            }

            if ((ruleSetFoldout = EditorGUILayout.Foldout(ruleSetFoldout, "Rules")))
            {
                Rect controlRect;
                for (int i = 0; i < keys.Count; i++)
                {
                    controlRect = EditorGUILayout.GetControlRect(false);
                    Rect keyRect   = new Rect(controlRect.position, new Vector2(controlRect.size.x / 2, controlRect.size.y));
                    Rect valueRect = new Rect(new Vector2(controlRect.position.x + controlRect.size.x / 2, controlRect.position.y),
                                              new Vector2(controlRect.size.x / 2 - 25, controlRect.size.y));
                    Rect removeRect = new Rect(new Vector2(controlRect.position.x + controlRect.size.x - 25, controlRect.position.y),
                                               new Vector2(25, controlRect.size.y));

                    string newKeyStr = EditorGUI.TextField(keyRect, "" + keys[i]);
                    char   newKey    = (char)33;
                    if (newKeyStr.Length > 0)
                    {
                        newKey = newKeyStr.ToCharArray()[0];
                    }

                    //Make sure key is unique.
                    if (newKey != keys[i])
                    {
                        int  max = 33;
                        bool hit = false;
                        for (int j = 0; j < keys.Count; j++)
                        {
                            if (keys[j] == newKey && i != j)
                            {
                                hit = true;
                            }

                            if (keys[j] > max)
                            {
                                max = keys[j];
                            }
                        }
                        if (hit)
                        {
                            newKey = (char)++max;
                        }
                    }

                    keys[i] = newKey;

                    values[i] = EditorGUI.TextField(valueRect, values[i]);
                    if (!EditorGUI.Toggle(removeRect, true))
                    {
                        keys.RemoveAt(i);
                        values.RemoveAt(i);
                    }
                }

                controlRect = EditorGUILayout.GetControlRect(false);
                Rect addRect = new Rect(new Vector2(controlRect.position.x + controlRect.size.x / 4 * 3, controlRect.position.y),
                                        new Vector2(controlRect.size.x / 4, controlRect.size.y));
                if (GUI.Button(addRect, "Add"))
                {
                    //to avoid duplicate keys
                    char newKey = (char)33;
                    int  max    = 33;
                    for (int i = 0; i < keys.Count; i++)
                    {
                        if (keys[i] > max)
                        {
                            max = keys[i];
                        }
                    }
                    newKey = (char)++max;
                    keys.Add(newKey);
                    values.Add("");
                } //Add Button
                EditorGUILayout.Space();
            }
        }