Beispiel #1
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();
        }
Beispiel #2
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();
            }
        }