Exemplo n.º 1
0
        public static void DrawComponents(IEntity entity)
        {
            var unfoldedComponents    = GetUnfoldedComponents(entity);
            var componentMemberSearch = GetComponentMemberSearch(entity);

            EditorGUILayoutTools.BeginVerticalBox();

            EditorGUILayout.BeginHorizontal();
            {
                EditorGUILayout.LabelField("Components (" + entity.GetComponents().Length + ")", EditorStyles.boldLabel);
                if (EditorGUILayoutTools.MiniButtonLeft("▸"))
                {
                    for (var i = 0; i < unfoldedComponents.Length; i++)
                    {
                        unfoldedComponents[i] = false;
                    }
                }

                if (EditorGUILayoutTools.MiniButtonRight("▾"))
                {
                    for (var i = 0; i < unfoldedComponents.Length; i++)
                    {
                        unfoldedComponents[i] = true;
                    }
                }
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Space();

            var index = DrawAddComponentMenu(entity);

            if (index >= 0)
            {
                var componentType = entity.ContextInfo.componentTypes[index];
                var component     = entity.CreateComponent(index, componentType);
                entity.AddComponent(index, component);
            }

            EditorGUILayout.Space();

            ComponentNameSearchString = EditorGUILayoutTools.SearchTextField(ComponentNameSearchString);

            EditorGUILayout.Space();

            var indices    = entity.GetComponentIndices();
            var components = entity.GetComponents();

            for (var i = 0; i < components.Length; i++)
            {
                DrawComponent(
                    unfoldedComponents,
                    componentMemberSearch,
                    entity,
                    indices[i],
                    components[i]);
            }

            EditorGUILayoutTools.EndVerticalBox();
        }
        private static Func <IList> DrawEditActions(IList list, Type elementType, int index)
        {
            if (EditorGUILayoutTools.MiniButtonLeft("↑"))
            {
                if (index > 0)
                {
                    return(() =>
                    {
                        var otherIndex = index - 1;
                        var other = list[otherIndex];
                        list[otherIndex] = list[index];
                        list[index] = other;
                        return list;
                    });
                }
            }

            if (EditorGUILayoutTools.MiniButtonMid("↓"))
            {
                if (index < list.Count - 1)
                {
                    return(() =>
                    {
                        var otherIndex = index + 1;
                        var other = list[otherIndex];
                        list[otherIndex] = list[index];
                        list[index] = other;
                        return list;
                    });
                }
            }

            if (EditorGUILayoutTools.MiniButtonMid("+"))
            {
                if (EntityDrawer.CreateDefault(elementType, out var defaultValue))
                {
                    var insertAt = index + 1;
                    return(() =>
                    {
                        list.Insert(insertAt, defaultValue);
                        return list;
                    });
                }
            }

            if (EditorGUILayoutTools.MiniButtonRight("-"))
            {
                var removeAt = index;
                return(() =>
                {
                    list.RemoveAt(removeAt);
                    return list;
                });
            }

            return(null);
        }
Exemplo n.º 3
0
        public static void DrawMultipleEntities(IEntity[] entities)
        {
            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal();
            {
                var entity = entities[0];
                var index  = DrawAddComponentMenu(entity);
                if (index >= 0)
                {
                    var componentType = entity.ContextInfo.componentTypes[index];
                    foreach (var e in entities)
                    {
                        var component = e.CreateComponent(index, componentType);
                        e.AddComponent(index, component);
                    }
                }
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Space();

            var bgColor = GUI.backgroundColor;

            GUI.backgroundColor = Color.red;

            if (GUILayout.Button("Destroy selected entities"))
            {
                foreach (var e in entities)
                {
                    e.Destroy();
                }
            }

            GUI.backgroundColor = bgColor;

            EditorGUILayout.Space();

            foreach (var e in entities)
            {
                EditorGUILayout.BeginHorizontal();
                {
                    EditorGUILayout.LabelField(e.ToString());

                    bgColor             = GUI.backgroundColor;
                    GUI.backgroundColor = Color.red;

                    if (EditorGUILayoutTools.MiniButton("Destroy Entity"))
                    {
                        e.Destroy();
                    }

                    GUI.backgroundColor = bgColor;
                }
                EditorGUILayout.EndHorizontal();
            }
        }
Exemplo n.º 4
0
        private static Func <Array> DrawEditActions(Array array, Type elementType, int index)
        {
            if (EditorGUILayoutTools.MiniButtonLeft("↑"))
            {
                if (index > 0)
                {
                    return(() =>
                    {
                        var otherIndex = index - 1;
                        var other = array.GetValue(otherIndex);
                        array.SetValue(array.GetValue(index), otherIndex);
                        array.SetValue(other, index);
                        return array;
                    });
                }
            }

            if (EditorGUILayoutTools.MiniButtonMid("↓"))
            {
                if (index < array.Length - 1)
                {
                    return(() =>
                    {
                        var otherIndex = index + 1;
                        var other = array.GetValue(otherIndex);
                        array.SetValue(array.GetValue(index), otherIndex);
                        array.SetValue(other, index);
                        return array;
                    });
                }
            }

            if (EditorGUILayoutTools.MiniButtonMid("+"))
            {
                if (EntityDrawer.CreateDefault(elementType, out var defaultValue))
                {
                    return(() => ArrayInsertAt(
                               array,
                               elementType,
                               defaultValue,
                               index + 1));
                }
            }

            if (EditorGUILayoutTools.MiniButtonRight("-"))
            {
                return(() => ArrayRemoveAt(array, elementType, index));
            }

            return(null);
        }
        private IList DrawAddElement(IList list, string memberName, Type elementType)
        {
            EditorGUILayout.BeginHorizontal();
            {
                EditorGUILayout.LabelField(memberName, "empty");
                if (EditorGUILayoutTools.MiniButton("add " + elementType.ToCompilableString().ShortTypeName()))
                {
                    if (EntityDrawer.CreateDefault(elementType, out var defaultValue))
                    {
                        list.Add(defaultValue);
                    }
                }
            }
            EditorGUILayout.EndHorizontal();

            return(list);
        }
Exemplo n.º 6
0
        private Array DrawAddElement(Array array, string memberName, Type elementType)
        {
            EditorGUILayout.BeginHorizontal();
            {
                EditorGUILayout.LabelField(memberName, "empty");
                if (EditorGUILayoutTools.MiniButton("add " + elementType.ToCompilableString().ShortTypeName()))
                {
                    if (EntityDrawer.CreateDefault(elementType, out var defaultValue))
                    {
                        var newArray = Array.CreateInstance(elementType, 1);
                        newArray.SetValue(defaultValue, 0);
                        array = newArray;
                    }
                }
            }
            EditorGUILayout.EndHorizontal();

            return(array);
        }
Exemplo n.º 7
0
        public static void DrawEntity(IEntity entity)
        {
            var bgColor = GUI.backgroundColor;

            GUI.backgroundColor = Color.red;
            if (GUILayout.Button("Destroy Entity"))
            {
                entity.Destroy();
            }

            GUI.backgroundColor = bgColor;

            DrawComponents(entity);

            EditorGUILayout.Space();

            EditorGUILayout.LabelField("Retained by (" + entity.RetainCount + ")", EditorStyles.boldLabel);

            if (entity.AERC is SafeAERC safeAerc)
            {
                EditorGUILayoutTools.BeginVerticalBox();
                {
                    foreach (var owner in safeAerc.Owners.OrderBy(o => o.GetType().Name))
                    {
                        EditorGUILayout.BeginHorizontal();
                        {
                            EditorGUILayout.LabelField(owner.ToString());
                            if (EditorGUILayoutTools.MiniButton("Release"))
                            {
                                entity.Release(owner);
                            }

                            EditorGUILayout.EndHorizontal();
                        }
                    }
                }
                EditorGUILayoutTools.EndVerticalBox();
            }
        }
Exemplo n.º 8
0
        public static bool DrawObjectMember(Type memberType, string memberName, object value, object target,
                                            Action <object, object> setValue)
        {
            if (value == null)
            {
                EditorGUI.BeginChangeCheck();
                {
                    var isUnityObject = memberType == typeof(Object) || memberType.IsSubclassOf(typeof(Object));
                    EditorGUILayout.BeginHorizontal();
                    {
                        if (isUnityObject)
                        {
                            setValue(
                                target,
                                EditorGUILayout.ObjectField(
                                    memberName,
                                    (Object)value,
                                    memberType,
                                    true));
                        }
                        else
                        {
                            EditorGUILayout.LabelField(memberName, "null");
                        }

                        if (EditorGUILayoutTools.MiniButton("new " + memberType.ToCompilableString().ShortTypeName()))
                        {
                            if (CreateDefault(memberType, out var defaultValue))
                            {
                                setValue(target, defaultValue);
                            }
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }

                return(EditorGUI.EndChangeCheck());
            }

            if (!memberType.IsValueType)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.BeginVertical();
            }

            EditorGUI.BeginChangeCheck();
            {
                var typeDrawer = GetTypeDrawer(memberType);
                if (typeDrawer != null)
                {
                    var newValue = typeDrawer.DrawAndGetNewValue(
                        memberType,
                        memberName,
                        value,
                        target);
                    setValue(target, newValue);
                }
                else
                {
                    var targetType = target.GetType();
                    var shouldDraw = !targetType.ImplementsInterface <IComponent>() ||
                                     !Attribute.IsDefined(targetType, typeof(DontDrawComponentAttribute));
                    if (shouldDraw)
                    {
                        EditorGUILayout.LabelField(memberName, value.ToString());

                        var indent = EditorGUI.indentLevel;
                        EditorGUI.indentLevel += 1;

                        EditorGUILayout.BeginVertical();
                        {
                            foreach (var info in memberType.GetPublicMemberInfos())
                            {
                                var mValue = info.GetValue(value);
                                var mType  = mValue == null ? info.type : mValue.GetType();
                                DrawObjectMember(
                                    mType,
                                    info.name,
                                    mValue,
                                    value,
                                    info.SetValue);
                                if (memberType.IsValueType)
                                {
                                    setValue(target, value);
                                }
                            }
                        }
                        EditorGUILayout.EndVertical();

                        EditorGUI.indentLevel = indent;
                    }
                    else
                    {
                        DrawUnsupportedType(memberType, memberName, value);
                    }
                }

                if (!memberType.IsValueType)
                {
                    EditorGUILayout.EndVertical();
                    if (EditorGUILayoutTools.MiniButton("×"))
                    {
                        setValue(target, null);
                    }

                    EditorGUILayout.EndHorizontal();
                }
            }

            return(EditorGUI.EndChangeCheck());
        }
Exemplo n.º 9
0
        public static void DrawComponent(
            bool[] unfoldedComponents,
            string contextName,
            int totalNumberOfComponents,
            int index,
            IComponent component,
            Action <IComponent> removeComponent,
            Action onComponentChanged)
        {
            var componentType = component.GetType();
            var componentName = componentType.Name.RemoveComponentSuffix();
            var boxStyle      = GetColoredBoxStyle(contextName, totalNumberOfComponents, index);

            EditorGUILayout.BeginVertical(boxStyle);

            if (!Attribute.IsDefined(componentType, typeof(DontDrawComponentAttribute)))
            {
                var memberInfos = componentType.GetPublicMemberInfos();
                EditorGUILayout.BeginHorizontal();
                {
                    if (memberInfos.Count == 0)
                    {
                        EditorGUILayout.LabelField(componentName, EditorStyles.boldLabel);
                    }
                    else
                    {
                        unfoldedComponents[index] = EditorGUILayoutTools.Foldout(
                            unfoldedComponents[index],
                            componentName,
                            FoldoutStyle);
                    }

                    if (EditorGUILayoutTools.MiniButton("-"))
                    {
                        removeComponent?.Invoke(component);
                    }
                }
                EditorGUILayout.EndHorizontal();

                if (unfoldedComponents[index])
                {
                    var changed         = false;
                    var componentDrawer = GetComponentDrawer(componentType);
                    if (componentDrawer != null)
                    {
                        EditorGUI.BeginChangeCheck();
                        {
                            componentDrawer.DrawComponent(component);
                        }
                        changed = EditorGUI.EndChangeCheck();
                    }
                    else
                    {
                        foreach (var info in memberInfos)
                        {
                            var memberValue = info.GetValue(component);
                            var memberType  = memberValue == null ? info.type : memberValue.GetType();
                            if (DrawObjectMember(
                                    memberType,
                                    info.name,
                                    memberValue,
                                    component,
                                    info.SetValue))
                            {
                                changed = true;
                            }
                        }
                    }

                    if (changed)
                    {
                        onComponentChanged?.Invoke();
                    }
                }
            }
            else
            {
                EditorGUILayout.LabelField(componentName, "[DontDrawComponent]", EditorStyles.boldLabel);
            }

            EditorGUILayoutTools.EndVerticalBox();
        }
Exemplo n.º 10
0
        public static void DrawComponent(bool[] unfoldedComponents,
                                         string[] componentMemberSearch,
                                         IEntity entity,
                                         int index,
                                         IComponent component)
        {
            var componentType = component.GetType();
            var componentName = componentType.Name.RemoveComponentSuffix();

            if (EditorGUILayoutTools.MatchesSearchString(componentName.ToLower(), ComponentNameSearchString.ToLower()))
            {
                var boxStyle = GetColoredBoxStyle(entity, index);

                EditorGUILayout.BeginVertical(boxStyle);

                if (!Attribute.IsDefined(componentType, typeof(DontDrawComponentAttribute)))
                {
                    var memberInfos = componentType.GetPublicMemberInfos();
                    EditorGUILayout.BeginHorizontal();
                    {
                        if (memberInfos.Count == 0)
                        {
                            EditorGUILayout.LabelField(componentName, EditorStyles.boldLabel);
                        }
                        else
                        {
                            unfoldedComponents[index] = EditorGUILayoutTools.Foldout(
                                unfoldedComponents[index],
                                componentName,
                                FoldoutStyle);
                            if (unfoldedComponents[index])
                            {
                                componentMemberSearch[index] = memberInfos.Count > 5
                                                                        ? EditorGUILayoutTools.SearchTextField(componentMemberSearch[index])
                                                                        : string.Empty;
                            }
                        }

                        if (EditorGUILayoutTools.MiniButton("-"))
                        {
                            entity.RemoveComponent(index);
                        }
                    }
                    EditorGUILayout.EndHorizontal();

                    if (unfoldedComponents[index])
                    {
                        var newComponent = entity.CreateComponent(index, componentType);
                        component.CopyPublicMemberValues(newComponent);

                        var changed         = false;
                        var componentDrawer = GetComponentDrawer(componentType);
                        if (componentDrawer != null)
                        {
                            EditorGUI.BeginChangeCheck();
                            {
                                componentDrawer.DrawComponent(newComponent);
                            }
                            changed = EditorGUI.EndChangeCheck();
                        }
                        else
                        {
                            foreach (var info in memberInfos)
                            {
                                if (EditorGUILayoutTools.MatchesSearchString(
                                        info.name.ToLower(),
                                        componentMemberSearch[index].ToLower()))
                                {
                                    var memberValue = info.GetValue(newComponent);
                                    var memberType  = memberValue == null ? info.type : memberValue.GetType();
                                    if (DrawObjectMember(
                                            memberType,
                                            info.name,
                                            memberValue,
                                            newComponent,
                                            info.SetValue))
                                    {
                                        changed = true;
                                    }
                                }
                            }
                        }

                        if (changed)
                        {
                            entity.ReplaceComponent(index, newComponent);
                        }
                        else
                        {
                            entity.GetComponentPool(index).Push(newComponent);
                        }
                    }
                }
                else
                {
                    EditorGUILayout.LabelField(componentName, "[DontDrawComponent]", EditorStyles.boldLabel);
                }

                EditorGUILayoutTools.EndVerticalBox();
            }
        }
        public object DrawAndGetNewValue(Type memberType, string memberName, object value, object target)
        {
            var dictionary = (IDictionary)value;
            var keyType    = memberType.GetGenericArguments()[0];
            var valueType  = memberType.GetGenericArguments()[1];
            var targetType = target.GetType();

            if (!_keySearchTexts.ContainsKey(targetType))
            {
                _keySearchTexts.Add(targetType, string.Empty);
            }

            EditorGUILayout.BeginHorizontal();
            {
                if (dictionary.Count == 0)
                {
                    EditorGUILayout.LabelField(memberName, "empty");
                    _keySearchTexts[targetType] = string.Empty;
                }
                else
                {
                    EditorGUILayout.LabelField(memberName);
                }

                var keyTypeName   = keyType.ToCompilableString().ShortTypeName();
                var valueTypeName = valueType.ToCompilableString().ShortTypeName();
                if (EditorGUILayoutTools.MiniButton("new <" + keyTypeName + ", " + valueTypeName + ">"))
                {
                    if (EntityDrawer.CreateDefault(keyType, out var defaultKey))
                    {
                        if (EntityDrawer.CreateDefault(valueType, out var defaultValue))
                        {
                            dictionary[defaultKey] = defaultValue;
                        }
                    }
                }
            }
            EditorGUILayout.EndHorizontal();

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

                if (dictionary.Count > 5)
                {
                    EditorGUILayout.Space();
                    _keySearchTexts[targetType] = EditorGUILayoutTools.SearchTextField(_keySearchTexts[targetType]);
                }

                EditorGUILayout.Space();

                var keys = new ArrayList(dictionary.Keys);
                for (var i = 0; i < keys.Count; i++)
                {
                    var key = keys[i];
                    if (EditorGUILayoutTools.MatchesSearchString(key.ToString().ToLower(), _keySearchTexts[targetType].ToLower()))
                    {
                        EntityDrawer.DrawObjectMember(
                            keyType,
                            "key",
                            key,
                            target,
                            (newComponent, newValue) =>
                        {
                            var tmpValue = dictionary[key];
                            dictionary.Remove(key);
                            if (newValue != null)
                            {
                                dictionary[newValue] = tmpValue;
                            }
                        });

                        EntityDrawer.DrawObjectMember(
                            valueType,
                            "value",
                            dictionary[key],
                            target,
                            (newComponent, newValue) => dictionary[key] = newValue);

                        EditorGUILayout.Space();
                    }
                }

                EditorGUI.indentLevel = indent;
            }

            return(dictionary);
        }
        public object DrawAndGetNewValue(Type memberType, string memberName, object value, object target)
        {
            var elementType   = memberType.GetGenericArguments()[0];
            var itemsToRemove = new ArrayList();
            var itemsToAdd    = new ArrayList();
            var isEmpty       = !((IEnumerable)value).GetEnumerator().MoveNext();

            EditorGUILayout.BeginHorizontal();
            {
                if (isEmpty)
                {
                    EditorGUILayout.LabelField(memberName, "empty");
                }
                else
                {
                    EditorGUILayout.LabelField(memberName);
                }

                if (EditorGUILayoutTools.MiniButton("new " + elementType.ToCompilableString().ShortTypeName()))
                {
                    if (EntityDrawer.CreateDefault(elementType, out var defaultValue))
                    {
                        itemsToAdd.Add(defaultValue);
                    }
                }
            }
            EditorGUILayout.EndHorizontal();

            if (!isEmpty)
            {
                EditorGUILayout.Space();
                var indent = EditorGUI.indentLevel;
                EditorGUI.indentLevel = indent + 1;
                foreach (var item in (IEnumerable)value)
                {
                    EditorGUILayout.BeginHorizontal();
                    {
                        EntityDrawer.DrawObjectMember(
                            elementType,
                            string.Empty,
                            item,
                            target,
                            (newComponent, newValue) =>
                        {
                            itemsToRemove.Add(item);
                            itemsToAdd.Add(newValue);
                        });

                        if (EditorGUILayoutTools.MiniButton("-"))
                        {
                            itemsToRemove.Add(item);
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }

                EditorGUI.indentLevel = indent;
            }

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

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

            return(value);
        }