public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
 {
     base.Init(ve, bag, cc);
     ((INotifyValueChanged <TValueType>)ve).SetValueWithoutNotify(this.m_Value.GetValueFromBag(bag, cc));
 }
Ejemplo n.º 2
0
 public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
 {
     base.Init(ve, bag, cc);
     ((BaseField <TValueType>)ve).label = m_Label.GetValueFromBag(bag, cc);
 }
Ejemplo n.º 3
0
 public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
 {
     base.Init(ve, bag, cc);
     ((ITextElement)ve).text = m_Text.GetValueFromBag(bag, cc);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Returns true.
 /// </summary>
 /// <param name="bag">The attribute bag.</param>
 /// <remarks>
 /// By default, accepts any attribute bags. Override this function if you want to make specific checks on the attribute bag.
 /// </remarks>
 /// <returns>Always true.</returns>
 public virtual bool AcceptsAttributeBag(IUxmlAttributes bag, CreationContext cc)
 {
     return(true);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Initialize a <see cref="VisualElement"/> instance with values from the UXML element attributes.
 /// </summary>
 /// <param name="ve">The VisualElement to initialize.</param>
 /// <param name="bag">A bag of name-value pairs, one for each attribute of the UXML element.</param>
 /// <param name="cc">When the element is created as part of a template instance inserted in another document, this contains information about the insertion point.</param>
 /// <remarks>
 /// Override this function in your traits class to initialize your C# object with values read from the UXML document.
 /// </remarks>
 public virtual void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
 {
 }
 public bool TryGetValueFromBag(IUxmlAttributes bag, CreationContext cc, ref T value)
 {
     return(TryGetValueFromBag(bag, cc, ConvertValueToEnum, defaultValue, ref value));
 }
        internal bool TryGetValueFromBagAsString(IUxmlAttributes bag, CreationContext cc, out string value)
        {
            // Regardless of whether the attribute is overwridden or not, we want to error here
            // if there is no valid name.
            if (name == null && (m_ObsoleteNames == null || m_ObsoleteNames.Length == 0))
            {
                Debug.LogError("Attribute description has no name.");
                value = null;
                return(false);
            }

            string elementName;

            bag.TryGetAttributeValue("name", out elementName);
            if (!string.IsNullOrEmpty(elementName) && cc.attributeOverrides != null)
            {
                for (var i = 0; i < cc.attributeOverrides.Count; ++i)
                {
                    if (cc.attributeOverrides[i].m_ElementName != elementName)
                    {
                        continue;
                    }

                    if (cc.attributeOverrides[i].m_AttributeName != name)
                    {
                        if (m_ObsoleteNames != null)
                        {
                            bool matchedObsoleteName = false;
                            for (var j = 0; j < m_ObsoleteNames.Length; j++)
                            {
                                if (cc.attributeOverrides[i].m_AttributeName == m_ObsoleteNames[j])
                                {
                                    matchedObsoleteName = true;
                                    break;
                                }
                            }

                            if (!matchedObsoleteName)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }

                    value = cc.attributeOverrides[i].m_Value;
                    return(true);
                }
            }

            if (name == null)
            {
                // Check for:
                // (m_ObsoleteNames == null || m_ObsoleteNames.Length == 0)
                // moved at the top of method so we can assume we have obsolete names here.

                for (var i = 0; i < m_ObsoleteNames.Length; i++)
                {
                    if (bag.TryGetAttributeValue(m_ObsoleteNames[i], out value))
                    {
                        if (cc.visualTreeAsset != null)
                        {
                        }

                        return(true);
                    }
                }

                value = null;
                return(false);
            }

            if (!bag.TryGetAttributeValue(name, out value))
            {
                if (m_ObsoleteNames != null)
                {
                    for (var i = 0; i < m_ObsoleteNames.Length; i++)
                    {
                        if (bag.TryGetAttributeValue(m_ObsoleteNames[i], out value))
                        {
                            if (cc.visualTreeAsset != null)
                            {
                            }

                            return(true);
                        }
                    }
                }

                value = null;
                return(false);
            }

            return(true);
        }
 public override T GetValueFromBag(IUxmlAttributes bag, CreationContext cc)
 {
     return(GetValueFromBag(bag, cc, ConvertValueToEnum, defaultValue));
 }
 public abstract T GetValueFromBag(IUxmlAttributes bag, CreationContext cc);
Ejemplo n.º 10
0
 public override VisualElement Create(IUxmlAttributes bag, CreationContext cc)
 {
     return(null);
 }
Ejemplo n.º 11
0
        private VisualElement CloneSetupRecursively(VisualElementAsset root,
                                                    Dictionary <int, List <VisualElementAsset> > idToChildren, CreationContext context)
        {
            VisualElement ve = Create(root, context);

            if (ve == null)
            {
                return(null);
            }

            // context.target is the created templateContainer
            if (root.id == context.visualTreeAsset.contentContainerId)
            {
                if (context.target is TemplateContainer)
                {
                    ((TemplateContainer)context.target).SetContentContainer(ve);
                }
                else
                {
                    Debug.LogError(
                        "Trying to clone a VisualTreeAsset with a custom content container into a element which is not a template container");
                }
            }

            // if the current element had a slot-name attribute, put it in the resulting slot mapping
            string slotName;

            if (context.slotInsertionPoints != null && TryGetSlotInsertionPoint(root.id, out slotName))
            {
                context.slotInsertionPoints.Add(slotName, ve);
            }

            if (root.ruleIndex != -1)
            {
                if (inlineSheet == null)
                {
                    Debug.LogWarning("VisualElementAsset has a RuleIndex but no inlineStyleSheet");
                }
                else
                {
                    StyleRule r = inlineSheet.rules[root.ruleIndex];
                    ve.SetInlineRule(inlineSheet, r);
                }
            }

            var templateAsset = root as TemplateAsset;
            List <VisualElementAsset> children;

            if (idToChildren.TryGetValue(root.id, out children))
            {
                children.Sort(CompareForOrder);

                foreach (VisualElementAsset childVea in children)
                {
                    // this will fill the slotInsertionPoints mapping
                    VisualElement childVe = CloneSetupRecursively(childVea, idToChildren, context);
                    if (childVe == null)
                    {
                        continue;
                    }

                    // if the parent is not a template asset, just add the child to whatever hierarchy we currently have
                    // if ve is a scrollView (with contentViewport as contentContainer), this will go to the right place
                    if (templateAsset == null)
                    {
                        ve.Add(childVe);
                        continue;
                    }

                    int index = templateAsset.slotUsages == null
                        ? -1
                        : templateAsset.slotUsages.FindIndex(u => u.assetId == childVea.id);
                    if (index != -1)
                    {
                        VisualElement parentSlot;
                        string        key = templateAsset.slotUsages[index].slotName;
                        Assert.IsFalse(String.IsNullOrEmpty(key),
                                       "a lost name should not be null or empty, this probably points to an importer or serialization bug");
                        if (context.slotInsertionPoints == null ||
                            !context.slotInsertionPoints.TryGetValue(key, out parentSlot))
                        {
                            Debug.LogErrorFormat("Slot '{0}' was not found. Existing slots: {1}", key,
                                                 context.slotInsertionPoints == null
                                ? String.Empty
                                : String.Join(", ",
                                              System.Linq.Enumerable.ToArray(context.slotInsertionPoints.Keys)));
                            ve.Add(childVe);
                        }
                        else
                        {
                            parentSlot.Add(childVe);
                        }
                    }
                    else
                    {
                        ve.Add(childVe);
                    }
                }
            }

            if (templateAsset != null && context.slotInsertionPoints != null)
            {
                context.slotInsertionPoints.Clear();
            }

            return(ve);
        }
Ejemplo n.º 12
0
        internal static VisualElement Create(VisualElementAsset asset, CreationContext ctx)
        {
            List <IUxmlFactory> factoryList;

            if (!VisualElementFactoryRegistry.TryGetValue(asset.fullTypeName, out factoryList))
            {
                if (asset.fullTypeName.StartsWith("UnityEngine.Experimental.UIElements.") || asset.fullTypeName.StartsWith("UnityEditor.Experimental.UIElements."))
                {
                    string experimentalTypeName = asset.fullTypeName.Replace(".Experimental.UIElements", ".UIElements");
                    if (!VisualElementFactoryRegistry.TryGetValue(experimentalTypeName, out factoryList))
                    {
                        Debug.LogErrorFormat("Element '{0}' has no registered factory method.", asset.fullTypeName);
                        return(new Label(string.Format("Unknown type: '{0}'", asset.fullTypeName)));
                    }
                }
                else
                {
                    Debug.LogErrorFormat("Element '{0}' has no registered factory method.", asset.fullTypeName);
                    return(new Label(string.Format("Unknown type: '{0}'", asset.fullTypeName)));
                }
            }

            IUxmlFactory factory = null;

            foreach (IUxmlFactory f in factoryList)
            {
                if (f.AcceptsAttributeBag(asset, ctx))
                {
                    factory = f;
                    break;
                }
            }

            if (factory == null)
            {
                Debug.LogErrorFormat("Element '{0}' has a no factory that accept the set of XML attributes specified.", asset.fullTypeName);
                return(new Label(string.Format("Type with no factory: '{0}'", asset.fullTypeName)));
            }

            if (factory is UxmlRootElementFactory)
            {
                return(null);
            }

            VisualElement res = factory.Create(asset, ctx);

            if (res == null)
            {
                Debug.LogErrorFormat("The factory of Visual Element Type '{0}' has returned a null object", asset.fullTypeName);
                return(new Label(string.Format("The factory of Visual Element Type '{0}' has returned a null object", asset.fullTypeName)));
            }

            if (asset.classes != null)
            {
                for (int i = 0; i < asset.classes.Length; i++)
                {
                    res.AddToClassList(asset.classes[i]);
                }
            }

            if (asset.stylesheetPaths != null)
            {
                for (int i = 0; i < asset.stylesheetPaths.Count; i++)
                {
                    res.AddStyleSheetPath(asset.stylesheetPaths[i]);
                }
            }

            if (asset.stylesheets != null)
            {
                for (int i = 0; i < asset.stylesheets.Count; ++i)
                {
                    res.styleSheets.Add(asset.stylesheets[i]);
                }
            }

            return(res);
        }