Exemplo n.º 1
0
        public CircularReferenceElement(PropertyElement root, IProperty property, T value, PropertyPath path, PropertyPath pathToReference)
        {
            binding           = this;
            m_Root            = root;
            m_Path            = path;
            m_PathToReference = pathToReference;
            m_Value           = value;
            name = m_PathToReference.ToString();
            style.flexDirection = FlexDirection.Row;
            Resources.Templates.CircularReference.Clone(this);

            var label = this.Q <Label>(className: UssClasses.CircularReferenceElement.Label);

            label.text = GuiFactory.GetDisplayName(property);
            label.AddManipulator(
                new ContextualMenuManipulator(evt =>
            {
                var prop = GetProperty();
                if (null == prop)
                {
                    return;
                }

                var inspectorOptions = property.GetAttribute <InspectorOptionsAttribute>();

                if (property.IsReadOnly || true == inspectorOptions?.HideResetToDefault)
                {
                    return;
                }

                evt.menu.AppendAction(
                    "Reset to default",
                    p => ReloadWithInstance(default),
Exemplo n.º 2
0
        public InspectorContext(
            PropertyElement root,
            PropertyPath propertyPath,
            IProperty property,
            IEnumerable <Attribute> attributes = null
            )
        {
            Root         = root;
            PropertyPath = propertyPath;
            BasePath     = new PropertyPath();
            BasePath.PushPath(PropertyPath);
            if (BasePath.PartsCount > 0)
            {
                BasePath.Pop();
            }

            Name = property.Name;
            Part = PropertyPath.PartsCount > 0 ? PropertyPath[PropertyPath.PartsCount - 1] : default;
            var attributeList = new List <Attribute>(attributes ?? property.GetAttributes());

            Attributes  = attributeList;
            Tooltip     = property.GetAttribute <TooltipAttribute>()?.tooltip;
            DisplayName = GuiFactory.GetDisplayName(property);
            IsDelayed   = property.HasAttribute <DelayedAttribute>();
        }
        public NullElement(PropertyElement root, IProperty property, PropertyPath path)
        {
            m_PotentialTypes = new List <Type> {
                typeof(Null)
            };
            binding = this;
            m_Root  = root;
            m_Path  = path;
            name    = m_Path.ToString();

            TypeConstruction.GetAllConstructableTypes <T>(m_PotentialTypes);

            if (typeof(T).IsArray)
            {
                Resources.Templates.NullStringField.Clone(this);
                this.Q <Label>().text = GuiFactory.GetDisplayName(property);
                var button = this.Q <Button>();
                button.text = $"Null ({GetTypeName(typeof(T))})";
                button.clickable.clicked += ReloadWithArrayType;
                if (property.IsReadOnly)
                {
                    button.SetEnabledSmart(false);
                }
                return;
            }

            if (m_PotentialTypes.Count == 2)
            {
                Resources.Templates.NullStringField.Clone(this);
                this.Q <Label>().text = GuiFactory.GetDisplayName(property);
                var button = this.Q <Button>();
                button.text = $"Null ({GetTypeName(typeof(T))})";
                button.clickable.clicked += ReloadWithFirstType;
                if (property.IsReadOnly)
                {
                    button.SetEnabledSmart(false);
                }
                return;
            }

            var typeSelector = new PopupField <Type>(
                GuiFactory.GetDisplayName(property),
                m_PotentialTypes,
                typeof(Null),
                GetTypeName,
                GetTypeName);

            typeSelector.RegisterValueChangedCallback(OnCreateItem);
            if (property.IsReadOnly)
            {
                typeSelector.pickingMode = PickingMode.Ignore;
                typeSelector.Q(className: UssClasses.Unity.BasePopupFieldInput).SetEnabledSmart(false);
            }

            Add(typeSelector);
        }
Exemplo n.º 4
0
 public void DefaultPropertyVisit <TValue>(
     IProperty property,
     ref TValue value,
     PropertyPath path)
 {
     if (path.PartsCount > 0)
     {
         if (string.IsNullOrEmpty(GuiFactory.GetDisplayName(property)))
         {
             property.Visit(this, ref value);
         }
         else
         {
             var foldout = GuiFactory.Foldout <TValue>(property, path, VisitorContext);
             using (VisitorContext.MakeParentScope(foldout))
                 property.Visit(this, ref value);
         }
     }
     else
     {
         property.Visit(this, ref value);
     }
 }
Exemplo n.º 5
0
        void IPropertyVisitor.Visit <TContainer, TValue>(
            Property <TContainer, TValue> property,
            ref TContainer container)
        {
            if (IsExcluded(property))
            {
                return;
            }

            var value     = property.GetValue(ref container);
            var isWrapper = container is PropertyWrapper <TValue>;

            Context.IsRootObject = isWrapper;

            // Null values
            if (RuntimeTypeInfoCache <TValue> .CanBeNull &&
                ShouldBeTreatedAsNull(property, ref container, ref value) &&
                ShouldStayNull(property, ref container, ref value))
            {
                using (Context.MakePathScope(property))
                {
                    if (VisitNull(Context, property, ref value, Context.CopyCurrentPath()))
                    {
                        return;
                    }
                }
            }

            foreach (var adapter in m_AdapterOverrides)
            {
                if (!(adapter is IInspectorVisit <TValue> partiallyTyped))
                {
                    continue;
                }

                using (Context.MakePathScope(property))
                {
                    if (partiallyTyped.Visit(Context, property, ref value, Context.CopyCurrentPath()))
                    {
                        return;
                    }
                }
            }

            // Primitives
            if (this is IInspectorVisit <TValue> typed)
            {
                using (Context.MakePathScope(property))
                {
                    if (typed.Visit(Context, property, ref value, Context.CopyCurrentPath()))
                    {
                        return;
                    }
                }
            }

            // UnityEngine.Object
            if (this is IInspectorContravariantVisit <TValue> contravariant)
            {
                using (Context.MakePathScope(property))
                {
                    if (contravariant.Visit(Context, property, value, Context.CopyCurrentPath()))
                    {
                        return;
                    }
                }
            }

            // Enums
            if (RuntimeTypeInfoCache <TValue> .IsEnum)
            {
                using (Context.MakePathScope(property))
                {
                    if (VisitEnum(Context, property, ref value, Context.CopyCurrentPath(), isWrapper))
                    {
                        return;
                    }
                }
            }

            try
            {
                // Regular visit
                if (!isWrapper)
                {
                    Context.AddToPath(property);
                }

                var path = Context.CopyCurrentPath();
                using (var references = Context.MakeVisitedReferencesScope(ref value, path))
                {
                    if (references.VisitedOnCurrentBranch)
                    {
                        Context.Parent.Add(new CircularReferenceElement <TValue>(Context.Root, property, value, path,
                                                                                 references.GetReferencePath()));
                        return;
                    }

                    var inspector = GetCustomInspector(property, ref value, path, isWrapper);
                    if (null != inspector)
                    {
                        var customInspector = new CustomInspectorElement(path, inspector, Context.Root);
                        Context.Parent.contentContainer.Add(customInspector);
                        return;
                    }

                    if (path.PartsCount > 0)
                    {
                        if (string.IsNullOrEmpty(GuiFactory.GetDisplayName(property)))
                        {
                            PropertyContainer.TryAccept(this, ref value);
                        }
                        else
                        {
                            // Give a chance for different property bags to handle themselves
                            if (PropertyBagStore.TryGetPropertyBagForValue(ref value, out var valuePropertyBag))
                            {
                                switch (valuePropertyBag)
                                {
                                case IDictionaryPropertyAccept <TValue> accept:
                                    accept.Accept(this, property, ref container, ref value);
                                    break;

                                case IListPropertyAccept <TValue> accept:
                                    accept.Accept(this, property, ref container, ref value);
                                    break;

                                case ISetPropertyAccept <TValue> accept:
                                    accept.Accept(this, property, ref container, ref value);
                                    break;

                                case ICollectionPropertyAccept <TValue> accept:
                                    accept.Accept(this, property, ref container, ref value);
                                    break;

                                default:
                                    var foldout = GuiFactory.Foldout <TValue>(property, path, Context);
                                    using (Context.MakeParentScope(foldout))
                                        PropertyContainer.TryAccept(this, ref value);
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        PropertyContainer.TryAccept(this, ref value);
                    }
                }
            }
            finally
            {
                if (!isWrapper)
                {
                    Context.RemoveFromPath(property);
                }
            }
        }