Exemplo n.º 1
0
        public object DrawAndGetNewValue(Type type, string fieldName, object value, Entity entity, int index, IComponent component)
        {
            var dictionary = (IDictionary)value;
            var keyType    = type.GetGenericArguments()[0];
            var valueType  = type.GetGenericArguments()[1];

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField(fieldName);
            if (GUILayout.Button("+", GUILayout.Width(19), GUILayout.Height(14)))
            {
                object defaultKey;
                if (EntityInspector.CreateDefault(keyType, out defaultKey))
                {
                    object defaultValue;
                    if (EntityInspector.CreateDefault(valueType, out defaultValue))
                    {
                        dictionary[defaultKey] = defaultValue;
                    }
                }
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();

            if (dictionary.Count > 0)
            {
                var indent = EditorGUI.indentLevel;
                EditorGUI.indentLevel = indent + 1;

                var keys = new ArrayList(dictionary.Keys);
                for (int i = 0; i < keys.Count; i++)
                {
                    var key = keys[i];
                    EntityInspector.DrawAndSetElement(keyType, "key", key,
                                                      entity, index, component, newValue => {
                        var tmpValue = dictionary[key];
                        dictionary.Remove(key);
                        if (newValue != null)
                        {
                            dictionary[newValue] = tmpValue;
                        }
                    });

                    EntityInspector.DrawAndSetElement(valueType, "value", dictionary[key],
                                                      entity, index, component, newValue => dictionary[key] = newValue);

                    EditorGUILayout.Space();
                }

                EditorGUI.indentLevel = indent;
            }

            return(dictionary);
        }
Exemplo n.º 2
0
        public object DrawAndGetNewValue(Type type, string fieldName, object value, Entity entity, int index, IComponent component)
        {
            var array       = (Array)value;
            var elementType = type.GetElementType();
            var indent      = EditorGUI.indentLevel;

            if (array.Rank == 1)
            {
                if (array.GetLength(0) == 0)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField(fieldName);
                    if (GUILayout.Button("Add element", GUILayout.Height(14)))
                    {
                        object defaultValue;
                        if (EntityInspector.CreateDefault(elementType, out defaultValue))
                        {
                            var newArray = Array.CreateInstance(elementType, 1);
                            newArray.SetValue(defaultValue, 0);
                            array = newArray;
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }
                else
                {
                    EditorGUILayout.LabelField(fieldName);
                }
                EditorGUI.indentLevel = indent + 1;

                Func <Array> editAction = null;
                for (int i = 0; i < array.GetLength(0); i++)
                {
                    EditorGUILayout.BeginHorizontal();
                    EntityInspector.DrawAndSetElement(elementType, fieldName + "[" + i + "]", array.GetValue(i),
                                                      entity, index, component, newValue => array.SetValue(newValue, i));

                    var localIndex = i;
                    var action     = drawEditActions(array, elementType, localIndex);
                    if (action != null)
                    {
                        editAction = action;
                    }
                    EditorGUILayout.EndHorizontal();
                }

                if (editAction != null)
                {
                    array = editAction();
                }
            }
            else if (array.Rank == 2)
            {
                EditorGUILayout.LabelField(fieldName);
                for (int i = 0; i < array.GetLength(0); i++)
                {
                    for (int j = 0; j < array.GetLength(1); j++)
                    {
                        EntityInspector.DrawAndSetElement(elementType, fieldName + "[" + i + ", " + j + "]", array.GetValue(i, j),
                                                          entity, index, component, newValue => array.SetValue(newValue, i, j));
                    }
                    EditorGUILayout.Space();
                }
            }
            else if (array.Rank == 3)
            {
                EditorGUILayout.LabelField(fieldName);
                for (int i = 0; i < array.GetLength(0); i++)
                {
                    for (int j = 0; j < array.GetLength(1); j++)
                    {
                        for (int k = 0; k < array.GetLength(2); k++)
                        {
                            EntityInspector.DrawAndSetElement(elementType, fieldName + "[" + i + ", " + j + " ," + k + "]", array.GetValue(i, j, k),
                                                              entity, index, component, newValue => array.SetValue(newValue, i, j, k));
                        }
                        EditorGUILayout.Space();
                    }
                    EditorGUILayout.Space();
                }
            }

            EditorGUI.indentLevel = indent;

            return(array);
        }
Exemplo n.º 3
0
        public object DrawAndGetNewValue(Type type, string fieldName, object value, Entity entity, int index, IComponent component)
        {
            var list        = (IList)value;
            var elementType = type.GetGenericArguments()[0];

            if (list.Count == 0)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField(fieldName);
                if (GUILayout.Button("Add element", GUILayout.Height(14)))
                {
                    object defaultValue;
                    if (EntityInspector.CreateDefault(elementType, out defaultValue))
                    {
                        list.Add(defaultValue);
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
            else
            {
                EditorGUILayout.LabelField(fieldName);
            }

            var indent = EditorGUI.indentLevel;

            EditorGUI.indentLevel = indent + 1;
            Action editAction = null;

            for (int i = 0; i < list.Count; i++)
            {
                EditorGUILayout.BeginHorizontal();
                EntityInspector.DrawAndSetElement(elementType, fieldName + "[" + i + "]", list[i],
                                                  entity, index, component, newValue => list[i] = newValue);

                if (GUILayout.Button("-", GUILayout.Width(19), GUILayout.Height(14)))
                {
                    var removeAt = i;
                    editAction = () => list.RemoveAt(removeAt);
                }
                if (GUILayout.Button("▴", GUILayout.Width(19), GUILayout.Height(14)))
                {
                    object defaultValue;
                    if (EntityInspector.CreateDefault(elementType, out defaultValue))
                    {
                        var insertAt = i;
                        editAction = () => list.Insert(insertAt, defaultValue);
                    }
                }
                if (GUILayout.Button("▾", GUILayout.Width(19), GUILayout.Height(14)))
                {
                    object defaultValue;
                    if (EntityInspector.CreateDefault(elementType, out defaultValue))
                    {
                        var insertAt = i + 1;
                        editAction = () => list.Insert(insertAt, defaultValue);
                    }
                }
                EditorGUILayout.EndHorizontal();
            }

            if (editAction != null)
            {
                editAction();
            }
            EditorGUI.indentLevel = indent;

            return(list);
        }