Esempio 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),
        public static void Bind <TValue>(VisualElement element, ref TValue value, PropertyPath path, BindingContextElement root)
        {
            switch (element)
            {
            case BindingContextElement propertyElement:
                GuiFactory.SetCallbacks(ref value, path, root, propertyElement);
                break;

            case BaseField <TValue> field:
                GuiFactory.SetCallbacks(ref value, path, root, field);
                break;

            case Label label:
                GuiFactory.SetCallbacks(ref value, path, root, label);
                break;

            case BindableElement bindable when element.GetType() == typeof(BindableElement) && TypeConversion.TryConvert(ref value, out Texture2D _):
                GuiFactory.SetCallbacks(ref value, path, root, bindable);

                break;

            default:
                // Use reflection to figure out if we can map it.
                TrySetCallbacksThroughReflection(element, ref value, path, root);
                break;
            }
        }
Esempio n. 3
0
        protected override void VisitPath <TContainer, TValue>(Property <TContainer, TValue> property,
                                                               ref TContainer container, ref TValue value)
        {
            switch (Element)
            {
            case BaseField <TValue> field:
                GuiFactory.SetCallbacks(ref value, Path, Root, field);
                break;

            case Label label when TypeConversion.TryConvert(value, out string _):
                GuiFactory.SetCallbacks(ref value, Path, Root, label);

                break;

            case BindableElement bindable when TypeConversion.TryConvert(value, out Texture2D _):
                GuiFactory.SetCallbacks(ref value, Path, Root, bindable);

                break;

            default:
                // Use reflection to figure out if we can map it.
                TrySetCallbacksThroughReflection(ref value);
                break;
            }
        }
        protected override void VisitSet <TContainer, TSet, TValue>(Property <TContainer, TSet> property,
                                                                    ref TContainer container, ref TSet value)
        {
            AddToPath(property);
            try
            {
                var path = GetCurrentPath();
                using (var references = VisitorContext.MakeVisitedReferencesScope(this, ref value, path))
                {
                    if (references.VisitedOnCurrentBranch)
                    {
                        VisitorContext.Parent.Add(new CircularReferenceElement <TSet>(VisitorContext.Root, property, value, path, references.GetReferencePath()));
                        return;
                    }

                    var element =
                        GuiFactory.SetFoldout <TContainer, TSet, TValue>(property, path, VisitorContext);
                    VisitorContext.Parent.contentContainer.Add(element);
                    using (VisitorContext.MakeParentScope(element))
                    {
                        element.Reload(property);
                    }
                }
            }
            finally
            {
                RemoveFromPath(property);
            }
        }
        bool VisitEnum <TContainer, TValue>(
            InspectorContext inspectorContext,
            IProperty <TContainer> property,
            ref TValue value,
            PropertyPath path,
            bool isWrapper)
        {
            var inspector = GetCustomInspector(property, ref value, path, isWrapper);

            if (null == inspector)
            {
                if (RuntimeTypeInfoCache <TValue> .IsEnumFlags)
                {
                    GuiFactory.FlagsField(property, ref value, path, inspectorContext);
                }
                else
                {
                    GuiFactory.EnumField(property, ref value, path, inspectorContext);
                }
            }
            else
            {
                using (inspectorContext.MakeIgnoreInspectorScope())
                {
                    var customInspector = new CustomInspectorElement(path, inspector, inspectorContext.Root);
                    inspectorContext.Parent.contentContainer.Add(customInspector);
                }
            }

            return(true);
        }
Esempio n. 6
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);
        }
Esempio n. 8
0
        void IListPropertyVisitor.Visit <TContainer, TList, TElement>(
            Property <TContainer, TList> property,
            ref TContainer container,
            ref TList list)
        {
            var path    = Context.CopyCurrentPath();
            var foldout = GuiFactory.Foldout <TList, TElement>(property, path, Context);

            using (Context.MakeParentScope(foldout))
                foldout.Reload(property);
        }
Esempio n. 9
0
        public void Visit <TContainer, TDictionary, TKey, TValue>(
            Property <TContainer, TDictionary> property,
            ref TContainer container,
            ref TDictionary dictionary) where TDictionary : IDictionary <TKey, TValue>
        {
            var path    = Context.CopyCurrentPath();
            var foldout = GuiFactory.Foldout <TDictionary, TKey, TValue>(property, path, Context);

            using (Context.MakeParentScope(foldout))
                foldout.Reload(property);
        }
Esempio n. 10
0
        void ISetPropertyVisitor.Visit <TContainer, TSet, TValue>(
            Property <TContainer, TSet> property,
            ref TContainer container,
            ref TSet set)
        {
            var path    = Context.CopyCurrentPath();
            var foldout = GuiFactory.SetFoldout <TSet, TValue>(property, path, Context);

            using (Context.MakeParentScope(foldout))
                foldout.Reload(property);
        }
Esempio n. 11
0
        void ICollectionPropertyVisitor.Visit <TContainer, TCollection, TElement>(
            Property <TContainer, TCollection> property,
            ref TContainer container,
            ref TCollection collection)
        {
            var path    = Context.CopyCurrentPath();
            var foldout = GuiFactory.Foldout <TCollection>(property, path, Context);

            using (Context.MakeParentScope(foldout))
                PropertyContainer.TryAccept(this, ref collection);
        }
Esempio n. 12
0
        void DefaultSetVisit <TSet, TValue>(IProperty property,
                                            ref TSet value,
                                            PropertyPath path)
            where TSet : ISet <TValue>
        {
            var element = GuiFactory.SetFoldout <TSet, TValue>(property, path, VisitorContext);

            VisitorContext.Parent.contentContainer.Add(element);
            using (VisitorContext.MakeParentScope(element))
            {
                element.Reload(property);
            }
        }
Esempio n. 13
0
        void DefaultDictionaryVisit <TDictionary, TKey, TValue>(
            IProperty property,
            ref TDictionary value,
            PropertyPath path)
            where TDictionary : IDictionary <TKey, TValue>
        {
            var element = GuiFactory.Foldout <TDictionary, TKey, TValue>(property, path, VisitorContext);

            VisitorContext.Parent.contentContainer.Add(element);
            using (VisitorContext.MakeParentScope(element))
            {
                element.Reload(property);
            }
        }
Esempio n. 14
0
        void DefaultListVisit <TList, TElement>(
            IProperty property,
            ref TList value,
            PropertyPath path)
            where TList : IList <TElement>
        {
            var element = GuiFactory.Foldout <TList, TElement>(property, path, VisitorContext);

            VisitorContext.Parent.contentContainer.Add(element);
            using (VisitorContext.MakeParentScope(element))
            {
                element.Reload(property);
            }
        }
 public void RecurseProperty <TValue>(ref TValue value, IProperty property, PropertyPath path)
 {
     if (path.PartsCount > 0)
     {
         var foldout = GuiFactory.Foldout <TValue>(property, path, VisitorContext);
         using (VisitorContext.MakeParentScope(foldout))
         {
             property.Visit(this, ref value);
         }
     }
     else
     {
         property.Visit(this, ref value);
     }
 }
Esempio n. 16
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);
     }
 }
Esempio n. 17
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);
                }
            }
        }
Esempio n. 18
0
 void SetCallbacks <TFieldType, TValue>(ref TValue value, BaseField <TFieldType> field)
 {
     GuiFactory.SetCallbacks(ref value, Path, Root, field);
 }
 static void SetCallbacks <TFieldType, TValue>(ref TValue value, BaseField <TFieldType> field, PropertyPath path, BindingContextElement root)
 {
     GuiFactory.SetCallbacks(ref value, path, root, field);
 }