public static void DrawComponents(IEntityExt entity)
        {
            var unfoldedComponents    = getUnfoldedComponents(entity);
            var componentMemberSearch = getComponentMemberSearch(entity);

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

                    if (EditorLayout.MiniButtonRight("▾"))
                    {
                        for (int 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 = EditorLayout.SearchTextField(componentNameSearchString);

                EditorGUILayout.Space();

                var indices    = entity.GetComponentIndices();
                var components = entity.GetComponents();
                for (int i = 0; i < components.Length; i++)
                {
                    DrawComponent(unfoldedComponents, componentMemberSearch, entity, indices[i], components[i]);
                }
            }
            EditorLayout.EndVerticalBox();
        }
        public static void DrawComponent(bool[] unfoldedComponents, string[] componentMemberSearch, IEntityExt entity, int index, IComponent component)
        {
            var componentType = component.GetType();
            var componentName = componentType.Name.RemoveComponentSuffix();

            if (EditorLayout.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] = EditorLayout.Foldout(unfoldedComponents[index], componentName, foldoutStyle);
                                if (unfoldedComponents[index])
                                {
                                    componentMemberSearch[index] = memberInfos.Count > 5
                                        ? EditorLayout.SearchTextField(componentMemberSearch[index])
                                        : string.Empty;
                                }
                            }

                            if (EditorLayout.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 (EditorLayout.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);
                    }
                }
                EditorLayout.EndVerticalBox();
            }
        }
Beispiel #3
0
        public IComponent CreateComponent(IEntityExt entity)
        {
            if (_type == null)
            {
                _type = fullTypeName.ToType();

                if (_type == null)
                {
                    throw new ComponentBlueprintException(
                              "Type '" + fullTypeName +
                              "' doesn't exist in any assembly!",
                              "Please check the full type name."
                              );
                }

                if (!_type.ImplementsInterface <IComponent>())
                {
                    throw new ComponentBlueprintException(
                              "Type '" + fullTypeName +
                              "' doesn't implement IComponent!",
                              typeof(ComponentBlueprint).Name +
                              " only supports IComponent."
                              );
                }
            }

            var component = entity.CreateComponent(index, _type);

            if (_componentMembers == null)
            {
                var memberInfos = _type.GetPublicMemberInfos();
                _componentMembers = new Dictionary <string, PublicMemberInfo>(
                    memberInfos.Count
                    );
                for (int i = 0; i < memberInfos.Count; i++)
                {
                    var info = memberInfos[i];
                    _componentMembers.Add(info.name, info);
                }
            }

            for (int i = 0; i < members.Length; i++)
            {
                var member = members[i];

                PublicMemberInfo memberInfo;
                if (_componentMembers.TryGetValue(member.name, out memberInfo))
                {
                    memberInfo.SetValue(component, member.value);
                }
                else
                {
                    Console.WriteLine(
                        "Could not find member '" + member.name +
                        "' in type '" + _type.FullName + "'!\n" +
                        "Only non-static public members are supported."
                        );
                }
            }

            return(component);
        }