public VisitStatus Visit <TContainer, TValue>(Property <TContainer, TValue> property, ref TContainer container,
                                                      ref TValue value)
        {
            if (RuntimeTypeInfoCache <TValue> .IsEnum)
            {
                Visitor.AddToPath(property);
                try
                {
                    var path      = Visitor.GetCurrentPath();
                    var inspector = AllowInspector ? GetInspector <TValue>(property, VisitorContext.Root, path) : null;
                    NoReentrace = true;
                    if (null != inspector)
                    {
                        var customInspector = new CustomInspectorElement(path, inspector, Visitor.VisitorContext.Root);
                        Visitor.VisitorContext.Parent.contentContainer.Add(customInspector);
                        return(VisitStatus.Stop);
                    }
                }
                finally
                {
                    NoReentrace = false;
                    Visitor.RemoveFromPath(property);
                }

                return(RuntimeTypeInfoCache <TValue> .IsEnumFlags
                    ? VisitPrimitive(property, ref value, GuiFactory.FlagsField)
                    : VisitPrimitive(property, ref value, GuiFactory.EnumField));
            }
            return(VisitStatus.Unhandled);
        }
        VisitStatus VisitPrimitive <TContainer, TValue, TElement>(
            IProperty <TContainer> property,
            ref TValue value,
            DrawHandler <TContainer, TValue, TElement> handler
            )
        {
            Visitor.AddToPath(property);
            try
            {
                var path = Visitor.GetCurrentPath();

                var inspector = AllowInspector ? GetPropertyDrawer <TValue>(property, Visitor.VisitorContext.Root, path) : null;
                NoReentrace = true;
                if (null == inspector)
                {
                    handler(property, ref value, path, VisitorContext);
                }
                else
                {
                    var customInspector = new CustomInspectorElement(path, inspector, Visitor.VisitorContext.Root);
                    Visitor.VisitorContext.Parent.contentContainer.Add(customInspector);
                }
            }
            finally
            {
                Visitor.RemoveFromPath(property);
                NoReentrace = false;
            }
            return(VisitStatus.Stop);
        }
Exemple #3
0
        public VisitStatus Visit <TContainer, TValue>(Property <TContainer, TValue> property, ref TContainer container,
                                                      ref TValue value)
        {
            if (!RuntimeTypeInfoCache <TValue> .CanBeNull || null != value)
            {
                return(VisitStatus.Unhandled);
            }

            if (typeof(UnityEngine.Object).IsAssignableFrom(typeof(TValue)))
            {
                return(VisitStatus.Unhandled);
            }

            if (!property.IsReadOnly && property.HasAttribute <CreateInstanceOnInspectionAttribute>() && !(property is ICollectionElementProperty))
            {
                var attribute = property.GetAttribute <CreateInstanceOnInspectionAttribute>();
                if (null == attribute.Type)
                {
                    if (TypeConstruction.CanBeConstructed <TValue>())
                    {
                        value = TypeConstruction.Construct <TValue>();
                        property.SetValue(ref container, value);
                        return(VisitStatus.Unhandled);
                    }

                    Debug.LogWarning(PropertyChecks.GetNotConstructableWarningMessage(typeof(TValue)));
                }
                else
                {
                    var isAssignable    = typeof(TValue).IsAssignableFrom(attribute.Type);
                    var isConstructable = TypeConstruction.GetAllConstructableTypes(typeof(TValue))
                                          .Contains(attribute.Type);
                    if (isAssignable && isConstructable)
                    {
                        value = TypeConstruction.Construct <TValue>(attribute.Type);
                        property.SetValue(ref container, value);
                        return(VisitStatus.Unhandled);
                    }

                    Debug.LogWarning(isAssignable
                        ? PropertyChecks.GetNotConstructableWarningMessage(attribute.Type)
                        : PropertyChecks.GetNotAssignableWarningMessage(attribute.Type, typeof(TValue)));
                }
            }

            Visitor.AddToPath(property);
            try
            {
                var path    = Visitor.GetCurrentPath();
                var element = new NullElement <TValue>(VisitorContext.Root, property, path);
                VisitorContext.Parent.contentContainer.Add(element);
            }
            finally
            {
                Visitor.RemoveFromPath(property);
            }
            return(VisitStatus.Stop);
        }
        public VisitStatus Visit <TContainer, TValue>(Property <TContainer, TValue> property, ref TContainer container,
                                                      ref TValue value)
        {
            if (RuntimeTypeInfoCache <TValue> .IsEnum)
            {
                return(RuntimeTypeInfoCache <TValue> .IsEnumFlags
                    ? VisitPrimitive(property, ref value, GuiFactory.FlagsField)
                    : VisitPrimitive(property, ref value, GuiFactory.EnumField));
            }

            if (RuntimeTypeInfoCache <TValue> .IsLazyLoadReference)
            {
                Visitor.AddToPath(property);
                try
                {
                    var path      = Visitor.GetCurrentPath();
                    var inspector = CustomInspectorDatabase.GetBestInspectorType <TValue>(property);
                    if (null == inspector)
                    {
                        var assetType     = typeof(TValue).GetGenericArguments()[0];
                        var inspectorType = typeof(LazyLoadReferenceInspector <>).MakeGenericType(assetType);
                        inspector = (Inspector <TValue>)Activator.CreateInstance(inspectorType);
                    }
                    inspector.Context = new InspectorContext <TValue>(
                        Visitor.VisitorContext.Root,
                        path,
                        property,
                        property.GetAttributes()
                        );
                    Visitor.VisitorContext.Parent.contentContainer.Add(
                        new CustomInspectorElement(path, inspector, Visitor.VisitorContext.Root));
                }
                finally
                {
                    Visitor.RemoveFromPath(property);
                }

                return(VisitStatus.Stop);
            }

            return(VisitStatus.Unhandled);
        }