Example #1
0
        public CircularReferenceElement(BindingContextElement 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),
Example #2
0
        public CustomInspectorElement(PropertyPath basePath, IInspector inspector, BindingContextElement root)
        {
            m_Root     = root;
            binding    = this;
            m_BasePath = basePath;
            name       = TypeUtility.GetTypeDisplayName(inspector.Type);
            Inspector  = inspector;
            try
            {
                m_Content = Inspector.Build();

                if (null == m_Content)
                {
                    return;
                }

                HasInspector = true;

                // If `IInspector.Build` was not overridden, it returns this element as its content.
                if (this != m_Content)
                {
                    Add(m_Content);
                    RegisterBindings(m_Content);
                    RegisterSearchHandlers(m_Content);
                }
            }
            catch (Exception exception)
            {
                Debug.LogException(exception);
            }
        }
        public InspectorContext(
            BindingContextElement 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>();
            IsReadOnly  = property.IsReadOnly;
        }
Example #4
0
 internal static void SetCallbacks <TValue>(
     ref TValue value,
     PropertyPath path,
     BindingContextElement root,
     Label label)
 {
     label.text    = TypeConversion.TryConvert(ref value, out string strValue) ? strValue : value.ToString();
     label.binding = new LabelBinding <TValue>(label, root, path);
 }
Example #5
0
        public NullElement(BindingContextElement root, IProperty property, PropertyPath path)
        {
            m_PotentialTypes = new List <Type> {
                typeof(Null)
            };
            binding = this;
            m_Root  = root;
            m_Path  = path;
            name    = m_Path.ToString();

            TypeUtility.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);
        }
Example #6
0
        internal static void SetCallbacks <TValue>(
            ref TValue value,
            PropertyPath path,
            BindingContextElement root,
            BindableElement element)
        {
            if (!TypeConversion.TryConvert(ref value, out Texture2D texture))
            {
                return;
            }

            element.style.backgroundImage = texture;
            element.binding = new TextureBinding <TValue>(element, root, path);
        }
Example #7
0
        internal static void SetCallbacks <TFieldType, TValue>(
            ref TValue value,
            PropertyPath path,
            BindingContextElement root,
            BaseField <TFieldType> field)
        {
            if (TypeConversion.TryConvert(ref value, out TFieldType fieldValue))
            {
                field.SetValueWithoutNotify(fieldValue);
                field.binding = new Binding <TFieldType, TValue>(field, root, path);
            }

            field.RegisterCallback <ChangeEvent <TFieldType>, PropertyPath>(ValueChanged <TFieldType, TValue>, path);
        }
Example #8
0
        internal static void SetCallbacks <TValue>(
            ref TValue value,
            PropertyPath path,
            BindingContextElement root,
            BindingContextElement field)
        {
            field.SetRoot(root);
            field.SetTarget(value);
            field.binding = new PropertyBinding <TValue>(field, root, path);

            var r = root;
            var p = path;

            field.OnChanged += (element, propertyPath) =>
            {
                r.SetValue(p, element.GetTarget <TValue>());
                element.SetTarget(r.GetValue <TValue>(p));
                var fullPath = new PropertyPath();
                fullPath.PushPath(p);
                fullPath.PushPath(propertyPath);
                r.NotifyChanged(fullPath);
            };
        }
 public InspectorContext(BindingContextElement root)
 {
     Root          = root;
     m_ParentStack = new Stack <VisualElement>();
     m_References  = new InspectedReferences();
 }
 void OnValueChanged(BindingContextElement element, PropertyPath path)
 {
     DictionaryElement.SetAtKey(m_Key.GetTarget <KeyContainer>().Key, m_Value.GetTarget <ValueContainer>().Value);
 }
Example #11
0
 void IContextElement.SetContext(BindingContextElement root, PropertyPath path)
 {
     Root = root;
     Path = path;
     OnContextReady();
 }
 internal ChangeContext(BindingContextElement binding)
 {
     m_Binding = binding;
 }
Example #13
0
 public TextureBinding(BindableElement element, BindingContextElement root, PropertyPath path) : base(element, root, path)
 {
 }
Example #14
0
        static void RegisterBindings(IInspector inspector, PropertyPath pathToValue, VisualElement toBind, BindingContextElement root)
        {
            var fullPath = new PropertyPath();

            fullPath.PushPath(inspector.PropertyPath);
            fullPath.PushPath(pathToValue);
            root.RegisterBindings(fullPath, toBind);
        }
Example #15
0
 public PropertyBinding(BindingContextElement element, BindingContextElement root, PropertyPath path) : base(element, root, path)
 {
 }
        static void TrySetCallbacksThroughReflection <TValue>(VisualElement element, ref TValue value, PropertyPath path, BindingContextElement root)
        {
            var type          = element.GetType();
            var baseFieldType = GetBaseFieldType(type);

            if (null == baseFieldType)
            {
                return;
            }

            var fieldType = baseFieldType.GenericTypeArguments[0];
            var key       = new TypePairKey(fieldType, typeof(TValue));

            if (!s_RegistrationMethods.TryGetValue(key, out var method))
            {
                s_RegistrationMethods[key] = method = BaseBinderMethod.MakeGenericMethod(fieldType, typeof(TValue));
            }

            method.Invoke(null, new object[] { value, element, path, root });
        }
        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;
            }
        }
 public override void Reset()
 {
     base.Reset();
     Binding = null;
     Element = null;
 }
Example #19
0
 public Binding(BaseField <TFieldType> element, BindingContextElement root, PropertyPath path) : base(element, root, path)
 {
 }
Example #20
0
 protected UIBinding(TElement element, BindingContextElement root, PropertyPath path)
 {
     Element = element;
     Root    = root;
     Path    = path;
 }
 static void SetCallbacks <TFieldType, TValue>(ref TValue value, BaseField <TFieldType> field, PropertyPath path, BindingContextElement root)
 {
     GuiFactory.SetCallbacks(ref value, path, root, field);
 }
 public InspectorVisitor(BindingContextElement root)
 {
     Context = new InspectorContext(root);
 }
Example #23
0
 public LabelBinding(Label element, BindingContextElement root, PropertyPath path) : base(element, root, path)
 {
 }