public static StyleComplexSelector Swallow(this StyleSheet toStyleSheet, StyleSheet fromStyleSheet, StyleComplexSelector fromSelector)
        {
            var toSelector = toStyleSheet.AddSelector(StyleSheetToUss.ToUssSelector(fromSelector));

            SwallowStyleRule(toStyleSheet, toSelector, fromStyleSheet, fromSelector);
            return(toSelector);
        }
 public static void Swallow(this StyleSheet toStyleSheet, StyleSheet fromStyleSheet)
 {
     foreach (var fromSelector in fromStyleSheet.complexSelectors)
     {
         var toSelector = toStyleSheet.AddSelector(StyleSheetToUss.ToUssSelector(fromSelector));
         SwallowStyleRule(toStyleSheet, toSelector, fromStyleSheet, fromSelector);
     }
 }
Beispiel #3
0
        internal static StyleComplexSelector CreateNewSelector(VisualElement selectorContainerElement, StyleSheet styleSheet, string selectorStr)
        {
            var complexSelector = styleSheet.AddSelector(selectorStr);
            var ssVE            = CreateNewSelectorElement(complexSelector);

            selectorContainerElement.Add(ssVE);

            return(complexSelector);
        }
        internal static StyleComplexSelector CreateNewSelector(VisualElement selectorContainerElement, StyleSheet styleSheet, string selectorStr)
        {
            var complexSelector = styleSheet.AddSelector(selectorStr);

            VisualElement styleSheetElement = null;

            foreach (var child in selectorContainerElement.Children())
            {
                if (child.GetStyleSheet() == styleSheet)
                {
                    styleSheetElement = child;
                    break;
                }
            }

            if (styleSheetElement != null)
            {
                var ssVE = CreateNewSelectorElement(styleSheet, complexSelector);
                styleSheetElement.Add(ssVE);
            }

            return(complexSelector);
        }
        void CloneTree()
        {
            m_Container.Clear();
            m_StyleSheetContents.Clear();
            m_VisualTreeAssetContents.Clear();

            m_Container.styleSheets.Clear();

            if (m_VisualTreeAsset != null)
            {
                m_VisualTreeAsset.LinkedCloneTree(m_Container);

                var canvas      = m_Container.Q("sample-canvas");
                var canvasAsset = canvas.GetVisualElementAsset();

                var newButton = m_VisualTreeAsset.AddElement(canvasAsset, "UnityEngine.UIElements.Button");
                newButton.AddProperty("name", "new-guy");
                newButton.AddProperty("text", "Canvas Button 2!");
                newButton.AddStyleClass("new-guy-type");
                newButton.AddStyleClass("some-button");
                newButton.RemoveStyleClass("some-button");
                newButton.AddStyleClass("some-fancy-button");
                { // Add max-width to newButton.
                    var rule = m_VisualTreeAsset.GetOrCreateInlineStyleRule(newButton);
                    var prop = m_VisualTreeAsset.inlineSheet.AddProperty(rule, "max-width");
                    var val  = m_VisualTreeAsset.inlineSheet.AddValue(prop, 200);
                }

                { // Add max-width to canvas.
                    var rule = m_VisualTreeAsset.GetOrCreateInlineStyleRule(canvasAsset);
                    var prop = m_VisualTreeAsset.inlineSheet.AddProperty(rule, "max-width");
                    var val  = m_VisualTreeAsset.inlineSheet.AddValue(prop, 500);
                }
                { // Change border of canvas.
                    var rule = m_VisualTreeAsset.GetOrCreateInlineStyleRule(canvasAsset);
                    var prop = m_VisualTreeAsset.inlineSheet.FindLastProperty(rule, "border-width");
                    m_VisualTreeAsset.inlineSheet.SetValue(prop.values[0], 10);
                }
                { // Remove max-width
                    var rule = m_VisualTreeAsset.GetOrCreateInlineStyleRule(canvasAsset);
                    var prop = m_VisualTreeAsset.inlineSheet.FindLastProperty(rule, "max-width");
                    m_VisualTreeAsset.inlineSheet.RemoveProperty(rule, prop);
                }

                var newButton2 = m_VisualTreeAsset.AddElement(canvasAsset, "UnityEngine.UIElements.Button");
                m_VisualTreeAsset.RemoveElement(newButton2);

                var newInstance = m_VisualTreeAsset.AddTemplateInstance(canvasAsset, "SampleSection");
                newInstance.SetAttributeOverride("section-text-field", "label", "label programmatically written!");
                newInstance.SetAttributeOverride("section-text-field", "text", "text programmatically written!");
                newInstance.RemoveAttributeOverride("section-text-field", "text");
                newInstance.AddStyleSheetPath(s_CanvasInstanceUSSPath);

                var overriddenSection = m_VisualTreeAsset.FindElementByName("overridden-section");
                if (overriddenSection != null)
                {
                    overriddenSection.RemoveStyleSheetPath(s_CanvasInstanceUSSPath);
                }

                // Add UXML string.
                var uxmlString = m_VisualTreeAsset.GenerateUXML(null);
                m_VisualTreeAssetContents.Add(new Label(uxmlString));

                // Add inline stylesheet.
                var inlineBuilder = new StringBuilder();
                foreach (var rule in m_VisualTreeAsset.inlineSheet.rules)
                {
                    inlineBuilder.Append("{\n");

                    var exportOptions = new UssExportOptions();
                    StyleSheetToUss.ToUssString(m_VisualTreeAsset.inlineSheet, exportOptions, rule, inlineBuilder);

                    inlineBuilder.Append("}\n");
                }
                var inlineStyleSheetString = inlineBuilder.ToString();
                m_VisualTreeAssetContents.Add(new Label(inlineStyleSheetString));

                m_Container.Clear();
                m_VisualTreeAsset.LinkedCloneTree(m_Container);
            }

            if (m_StyleSheet != null)
            {
                // Add width
                //var firstSelector = m_StyleSheet.complexSelectors.First();
                //var firstSelector = m_StyleSheet.FindSelector(".blue#red > .green .pink");
                var firstSelector = m_StyleSheet.FindSelector(".unity-button");
                if (firstSelector != null)
                {
                    var widthProperty = m_StyleSheet.AddProperty(firstSelector, "width");
                    var widthValue    = m_StyleSheet.AddValue(widthProperty, 62);
                    m_StyleSheet.SetValue(widthValue, 82);
                    m_StyleSheet.RemoveProperty(firstSelector, widthProperty);

                    var borderWidthProperty = m_StyleSheet.AddProperty(firstSelector, "border-width");
                    m_StyleSheet.AddValue(borderWidthProperty, 1);
                    m_StyleSheet.AddValue(borderWidthProperty, 2);
                    m_StyleSheet.AddValue(borderWidthProperty, 5);
                    var leftBorderWidthValue = m_StyleSheet.AddValue(borderWidthProperty, 8);
                    m_StyleSheet.RemoveValue(borderWidthProperty, leftBorderWidthValue);

                    var borderColorProperty = m_StyleSheet.AddProperty(firstSelector, "border-color");
                    var borderColorValue    = m_StyleSheet.AddValue(borderColorProperty, Color.red);
                    m_StyleSheet.SetValue(borderColorValue, Color.green);
                }

                var newSelector = m_StyleSheet.AddSelector(".unity-button Label");
                {
                    var widthProperty = m_StyleSheet.AddProperty(newSelector, "width");
                    var widthValue    = m_StyleSheet.AddValue(widthProperty, 62);
                    m_StyleSheet.SetValue(widthValue, 82);
                }

                //

                // Add USS contents.
                //var selectorStrings = m_StyleSheet.GetSelectorStrings();
                //foreach (var selectorString in selectorStrings)
                //m_StyleSheetContents.Add(new Label(selectorString));

                // Add USS string.
                var ussString = m_StyleSheet.GenerateUSS();
                m_StyleSheetContents.Add(new Label(ussString));

                //m_Container.styleSheets.Add(m_StyleSheet);
            }
        }