Func <Array> drawEditActions(Array array, Type elementType, int index)
        {
            if (GUILayout.Button("-", GUILayout.Width(19), GUILayout.Height(14)))
            {
                return(() => arrayRemoveAt(array, elementType, index));
            }
            if (GUILayout.Button("▴", GUILayout.Width(19), GUILayout.Height(14)))
            {
                object defaultValue;
                if (EntityDebugEditor.CreateDefault(elementType, out defaultValue))
                {
                    return(() => arrayInsertAt(array, elementType, defaultValue, index));
                }
            }
            if (GUILayout.Button("▾", GUILayout.Width(19), GUILayout.Height(14)))
            {
                object defaultValue;
                if (EntityDebugEditor.CreateDefault(elementType, out defaultValue))
                {
                    return(() => arrayInsertAt(array, elementType, defaultValue, index + 1));
                }
            }

            return(null);
        }
Beispiel #2
0
        public object DrawAndGetNewValue(Type type, string fieldName, object value, Entity entity, int index, IComponent component)
        {
            var elementType   = type.GetGenericArguments()[0];
            var itemsToRemove = new ArrayList();
            var itemsToAdd    = new ArrayList();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField(fieldName);
            if (GUILayout.Button("+", GUILayout.Width(19), GUILayout.Height(14)))
            {
                object defaultValue;
                if (EntityDebugEditor.CreateDefault(elementType, out defaultValue))
                {
                    itemsToAdd.Add(defaultValue);
                }
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();

            var indent = EditorGUI.indentLevel;

            EditorGUI.indentLevel = indent + 1;
            foreach (var item in (IEnumerable)value)
            {
                EditorGUILayout.BeginHorizontal();
                var newItem = EntityDebugEditor.DrawAndGetNewValue(elementType, string.Empty, item, entity, index, component);
                if (EntityDebugEditor.DidValueChange(item, newItem))
                {
                    itemsToRemove.Add(item);
                    itemsToAdd.Add(newItem);
                }

                if (GUILayout.Button("-", GUILayout.Width(19), GUILayout.Height(14)))
                {
                    itemsToRemove.Add(item);
                }
                EditorGUILayout.EndHorizontal();
            }

            EditorGUI.indentLevel = indent;

            foreach (var item in itemsToRemove)
            {
                type.GetMethod("Remove").Invoke(value, new [] { item });
            }

            foreach (var item in itemsToAdd)
            {
                type.GetMethod("Add").Invoke(value, new [] { item });
            }

            return(value);
        }
        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 (EntityDebugEditor.CreateDefault(keyType, out defaultKey))
                {
                    object defaultValue;
                    if (EntityDebugEditor.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];
                    EntityDebugEditor.DrawAndSetElement(keyType, "key", key,
                                                        entity, index, component, newValue => {
                        var tmpValue = dictionary[key];
                        dictionary.Remove(key);
                        if (newValue != null)
                        {
                            dictionary[newValue] = tmpValue;
                        }
                    });

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

                    EditorGUILayout.Space();
                }

                EditorGUI.indentLevel = indent;
            }

            return(dictionary);
        }
        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 (EntityDebugEditor.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();
                    EntityDebugEditor.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++)
                    {
                        EntityDebugEditor.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++)
                        {
                            EntityDebugEditor.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);
        }
        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 (EntityDebugEditor.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();
                EntityDebugEditor.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 (EntityDebugEditor.CreateDefault(elementType, out defaultValue))
                    {
                        var insertAt = i;
                        editAction = () => list.Insert(insertAt, defaultValue);
                    }
                }
                if (GUILayout.Button("▾", GUILayout.Width(19), GUILayout.Height(14)))
                {
                    object defaultValue;
                    if (EntityDebugEditor.CreateDefault(elementType, out defaultValue))
                    {
                        var insertAt = i + 1;
                        editAction = () => list.Insert(insertAt, defaultValue);
                    }
                }
                EditorGUILayout.EndHorizontal();
            }

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

            return(list);
        }