Exemple #1
0
        public bool OnFieldValueChange(StyleProperty styleProperty, StyleSheet styleSheet)
        {
            var stylePropertyValueCount = styleProperty.values.Length;
            var isNewValue = stylePropertyValueCount == 0;

            if (!isNewValue && (
                    stylePropertyValueCount < 2 ||
                    styleProperty.values[0].valueType != StyleValueType.Dimension ||
                    styleProperty.values[1].valueType != StyleValueType.Dimension))
            {
                Undo.RegisterCompleteObjectUndo(styleSheet, BuilderConstants.ChangeUIStyleValueUndoMessage);
                styleProperty.values = new StyleValueHandle[0];
                isNewValue           = true;
            }

            // if the translate property was already defined then ...
            if (!isNewValue)
            {
                styleSheet.SetValue(styleProperty.values[0], value.x);
                styleSheet.SetValue(styleProperty.values[1], value.y);
            }
            else
            {
                styleSheet.AddValue(styleProperty, value.x);
                styleSheet.AddValue(styleProperty, value.y);
            }
            return(isNewValue);
        }
        StyleValueHandle AddTypedValue <T>(T value, StyleValueType type)
        {
            switch (type)
            {
            case StyleValueType.Keyword:
                return(styleSheet.AddValue(styleProperty, (StyleValueKeyword)(object)value));

            case StyleValueType.Float:
                return(styleSheet.AddValue(styleProperty, (float)(object)value));

            case StyleValueType.Dimension:
                return(styleSheet.AddValue(styleProperty, (Dimension)(object)value));

            case StyleValueType.Color:
                return(styleSheet.AddValue(styleProperty, (Color)(object)value));

            case StyleValueType.ResourcePath:
                return(styleSheet.AddValue(styleProperty, (string)(object)value));

            case StyleValueType.AssetReference:
                return(styleSheet.AddValue(styleProperty, (UnityEngine.Object)(object) value));

            case StyleValueType.Enum:
            {
                if (value is string strValue)
                {
                    // Add value data to data array.
                    var index = styleSheet.AddValueToArray(strValue);

                    // Add value object to property.
                    return(styleSheet.AddValueHandle(styleProperty, index, StyleValueType.Enum));
                }
                return(styleSheet.AddValue(styleProperty, (Enum)(object)value));
            }

            case StyleValueType.String:
                return(styleSheet.AddValue(styleProperty, (string)(object)value));

            case StyleValueType.MissingAssetReference:
                return(styleSheet.AddValue(styleProperty, (string)(object)value));

            case StyleValueType.ScalableImage:
            // Not actually supported
            //return styleSheet.AddValue(styleProperty, (ScalableImage)(object) value);
            // These are not "values".
            case StyleValueType.Invalid:
            case StyleValueType.Variable:
            case StyleValueType.Function:
            case StyleValueType.CommaSeparator:
            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
Exemple #3
0
        // StyleSheet Value Setters

        static public void SetStyleSheetRuleValue(StyleSheet styleSheet, StyleRule styleRule, string styleName, float value)
        {
            var styleProperty = GetOrCreateStylePropertyByStyleName(styleSheet, styleRule, styleName);
            var isNewValue    = styleProperty.values.Length == 0;

            // If the current style property is saved as a float instead of a dimension,
            // it means it's a user file where they left out the unit. We need to resave
            // it here as a dimension to create final proper uss.
            if (!isNewValue && styleProperty.values[0].valueType != StyleValueType.Dimension)
            {
                styleProperty.values = new StyleValueHandle[0];
                isNewValue           = true;
            }

            var dimension = new Dimension();

            dimension.unit  = Dimension.Unit.Pixel;
            dimension.value = value;

            if (isNewValue)
            {
                styleSheet.AddValue(styleProperty, dimension);
            }
            else // TODO: Assume only one value.
            {
                styleSheet.SetValue(styleProperty.values[0], dimension);
            }
        }
Exemple #4
0
        public bool OnFieldValueChange(StyleProperty styleProperty, StyleSheet styleSheet)
        {
            var stylePropertyValueCount = styleProperty.values.Length;
            var isNewValue = stylePropertyValueCount == 0;

            // Assume that TextShadow style property is made of 2+ values at all times at this point
            // If the current style property is saved as a different type than the new style type,
            // we need to re-save it here as the new type. We do this by just removing the current value.
            if (!isNewValue && stylePropertyValueCount != 4 ||
                !isNewValue && styleProperty.values[0].valueType != StyleValueType.Dimension ||
                !isNewValue && styleProperty.values[1].valueType != StyleValueType.Dimension ||
                !isNewValue && styleProperty.values[2].valueType != StyleValueType.Dimension ||
                !isNewValue && styleProperty.values[3].valueType != StyleValueType.Color)
            {
                Undo.RegisterCompleteObjectUndo(styleSheet, BuilderConstants.ChangeUIStyleValueUndoMessage);

                styleProperty.values = new StyleValueHandle[0];
                isNewValue           = true;
            }

            var offsetX = new Dimension {
                value = m_OffsetXField.length, unit = m_OffsetXField.unit
            };
            var offsetY = new Dimension {
                value = m_OffsetYField.length, unit = m_OffsetYField.unit
            };
            var blurRadius = new Dimension {
                value = m_BlurRadiusField.length, unit = m_BlurRadiusField.unit
            };

            if (isNewValue)
            {
                styleSheet.AddValue(styleProperty, offsetX);
                styleSheet.AddValue(styleProperty, offsetY);
                styleSheet.AddValue(styleProperty, blurRadius);
                styleSheet.AddValue(styleProperty, m_ColorField.value);
            }
            else
            {
                styleSheet.SetValue(styleProperty.values[0], offsetX);
                styleSheet.SetValue(styleProperty.values[1], offsetY);
                styleSheet.SetValue(styleProperty.values[2], blurRadius);
                styleSheet.SetValue(styleProperty.values[3], m_ColorField.value);
            }

            return(isNewValue);
        }
        public static void TransferRulePropertiesToSelector(this StyleSheet toStyleSheet, StyleComplexSelector toSelector, StyleSheet fromStyleSheet, StyleRule fromRule)
        {
            foreach (var property in fromRule.properties)
            {
                var newProperty = toStyleSheet.AddProperty(toSelector, property.name);
                foreach (var value in property.values)
                {
                    switch (value.valueType)
                    {
                    case StyleValueType.Float: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetFloat(value)); break;

                    case StyleValueType.Dimension: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetDimension(value)); break;

                    case StyleValueType.Enum: toStyleSheet.AddValueAsEnum(newProperty, fromStyleSheet.GetEnum(value)); break;

                    case StyleValueType.String: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetString(value)); break;

                    case StyleValueType.Color: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetColor(value)); break;

                    case StyleValueType.AssetReference: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetAsset(value)); break;

                    case StyleValueType.ResourcePath: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetAsset(value)); break;

                    case StyleValueType.Variable: toStyleSheet.AddVariable(newProperty, fromStyleSheet.GetString(value)); break;

                    case StyleValueType.Keyword: toStyleSheet.AddValue(newProperty, fromStyleSheet.GetKeyword(value)); break;
                    }
                }
            }
            foreach (var property in fromRule.properties)
            {
                fromStyleSheet.RemoveProperty(fromRule, property);
            }
        }
        public static void AddStyleComplexSelectorToSelection(StyleSheet styleSheet, StyleComplexSelector scs)
        {
            var selectionProp = styleSheet.AddProperty(
                scs,
                BuilderConstants.SelectedStyleRulePropertyName,
                BuilderConstants.ChangeSelectionUndoMessage);

            // Need to add at least one dummy value because lots of code will die
            // if it encounters a style property with no values.
            styleSheet.AddValue(
                selectionProp, 42.0f, BuilderConstants.ChangeSelectionUndoMessage);
        }
Exemple #7
0
        // StyleSheet Value Setters

        static void SetStyleSheetRuleValue(StyleSheet styleSheet, StyleRule styleRule, string styleName, float value)
        {
            var styleProperty = GetOrCreateStylePropertyByStyleName(styleSheet, styleRule, styleName);
            var isNewValue    = styleProperty.values.Length == 0;

            if (isNewValue)
            {
                styleSheet.AddValue(styleProperty, value);
            }
            else // TODO: Assume only one value.
            {
                styleSheet.SetValue(styleProperty.values[0], value);
            }
        }
Exemple #8
0
        static public void SetStyleSheetRuleValue(StyleSheet styleSheet, StyleRule styleRule, string styleName, Enum value)
        {
            var styleProperty = GetOrCreateStylePropertyByStyleName(styleSheet, styleRule, styleName);
            var isNewValue    = styleProperty.values.Length == 0;

            if (!isNewValue && styleProperty.IsVariable())
            {
                styleProperty.values = new StyleValueHandle[0];
                isNewValue           = true;
            }

            if (isNewValue)
            {
                styleSheet.AddValue(styleProperty, value);
            }
            else // TODO: Assume only one value.
            {
                styleSheet.SetValue(styleProperty.values[0], value);
            }
        }
        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);
            }
        }
Exemple #10
0
        public bool OnFieldValueChange(StyleProperty styleProperty, StyleSheet styleSheet)
        {
            var stylePropertyValueCount = styleProperty.values.Length;
            var isNewValue       = stylePropertyValueCount == 0;
            var newXOriginOffset = GetOffsetFromDim(value.x, true);
            var newYOriginOffset = GetOffsetFromDim(value.y, false);

            // if the transform-origin property was already defined then ...
            if (!isNewValue)
            {
                // if only one value is specified
                // eg:
                //    - transform-origin: 10px
                //    - transform-origin: 40%
                //    - transform-origin: left
                //    - transform-origin: top
                if (stylePropertyValueCount == 1)
                {
                    // If the current value is a dimension then it means that only the x value was specified and y is defaulted to 'center'.
                    // eg: transform-origin: 20px;
                    if (styleProperty.values[0].valueType == StyleValueType.Dimension)
                    {
                        // If the new x value is an enum then clear data
                        // or if the new y value is anything but 'center' (default value) then clear data
                        if (newXOriginOffset != k_TransformOriginOffset_None || newYOriginOffset != TransformOriginOffset.Center)
                        {
                            isNewValue = true;
                        }
                        else
                        {
                            styleSheet.SetValue(styleProperty.values[0], value.x);
                        }
                    }
                    // if the current value is a enum then the value can be x or y depending on the value. In this case, the other value is defaulted to 'center'
                    // eg:
                    //     - transform-origin: left
                    //     - transform-origin: top
                    else
                    {
                        var offset = GetOffsetFromEnumString(styleSheet.ReadEnum(styleProperty.values[0]).ToLower());

                        // if the current specified enum value is x
                        if (IsOffsetHorizontal(offset))
                        {
                            // if the new x value is a dimension then clear data
                            if (newXOriginOffset == k_TransformOriginOffset_None)
                            {
                                isNewValue = true;
                            }
                            // otherwise, if it is an enum value
                            else
                            {
                                // If the new x value is 'center' and that the new y is an enum but 'center' then write the new y instead
                                if (newXOriginOffset == TransformOriginOffset.Center && newYOriginOffset != k_TransformOriginOffset_None && newYOriginOffset != TransformOriginOffset.Center)
                                {
                                    styleSheet.SetValue(styleProperty.values[0], newYOriginOffset);
                                }
                                // if the new y value is anything but 'center' (default value) then clear data
                                else if (newYOriginOffset != TransformOriginOffset.Center)
                                {
                                    isNewValue = true;
                                }
                                else
                                {
                                    styleSheet.SetValue(styleProperty.values[0], newXOriginOffset);
                                }
                            }
                        }
                        // If the current specified enum value is on y
                        else
                        {
                            // If the new y value is a dimension then clear data
                            if (newYOriginOffset == k_TransformOriginOffset_None)
                            {
                                isNewValue = true;
                            }
                            else
                            {
                                // If the new y value is 'center' then clear data
                                if (newYOriginOffset == TransformOriginOffset.Center)
                                {
                                    // If the new x value is an enum then write it instead
                                    if (newXOriginOffset == k_TransformOriginOffset_None)
                                    {
                                        styleSheet.SetValue(styleProperty.values[0], newXOriginOffset);
                                    }
                                    else
                                    {
                                        isNewValue = true;
                                    }
                                }
                                // if the new x value is anything but 'center' then clear data...
                                else if (newXOriginOffset != TransformOriginOffset.Center)
                                {
                                    isNewValue = true;
                                }
                                // otherwise if it is a enum...
                                else
                                {
                                    styleSheet.SetValue(styleProperty.values[0], newYOriginOffset);
                                }
                            }
                        }
                    }
                }
                // if two values are specified
                // eg:
                //    - transform-origin: 10px 40% [z optionally]
                //    - transform-origin: 40% top
                //    - transform-origin: left 30%
                //    - transform-origin: top
                else if (stylePropertyValueCount <= 3)
                {
                    int xIndex = 0;
                    int yIndex = 1;

                    // If the first value is enum but a horizontal enum then flip the indexes
                    if (styleProperty.values[0].valueType == StyleValueType.Enum && !IsOffsetHorizontal(GetOffsetFromEnumString(styleSheet.ReadEnum(styleProperty.values[0]).ToLower())))
                    {
                        xIndex = 1;
                        yIndex = 0;
                    }

                    // if the current x is a dimension and the new x is an enum
                    // or if the current x is an enum and the new x is an dimension
                    // or if the current y is a dimension and the new y is an enum
                    // or if the current y is an enum and the new y is an dimension
                    // then clear data
                    if ((styleProperty.values[xIndex].valueType == StyleValueType.Dimension && newXOriginOffset != k_TransformOriginOffset_None) ||
                        (styleProperty.values[xIndex].valueType == StyleValueType.Enum && newXOriginOffset == k_TransformOriginOffset_None) ||
                        (styleProperty.values[yIndex].valueType == StyleValueType.Dimension && newYOriginOffset != k_TransformOriginOffset_None) ||
                        (styleProperty.values[yIndex].valueType == StyleValueType.Enum && newYOriginOffset == k_TransformOriginOffset_None))
                    {
                        isNewValue = true;
                    }
                    // if the y value is 'center' then clear data to ignore it because it matches the default value
                    else if (newYOriginOffset == TransformOriginOffset.Center)
                    {
                        isNewValue = true;
                    }
                    // The x value is 'center' and the y value is any enum but 'center' then clear data to only add the y value
                    else if (xIndex == 0 && newXOriginOffset == TransformOriginOffset.Center && newYOriginOffset != k_TransformOriginOffset_None && newYOriginOffset != TransformOriginOffset.Center)
                    {
                        isNewValue = true;
                    }
                    else
                    {
                        if (styleProperty.values[xIndex].valueType == StyleValueType.Dimension)
                        {
                            styleSheet.SetValue(styleProperty.values[xIndex], value.x);
                        }
                        else
                        {
                            styleSheet.SetValue(styleProperty.values[xIndex], newXOriginOffset);
                        }

                        if (styleProperty.values[yIndex].valueType == StyleValueType.Dimension)
                        {
                            styleSheet.SetValue(styleProperty.values[yIndex], value.y);
                        }
                        else
                        {
                            styleSheet.SetValue(styleProperty.values[yIndex], newYOriginOffset);
                        }
                    }
                }
                else
                {
                    isNewValue = true;
                }

                if (isNewValue)
                {
                    Undo.RegisterCompleteObjectUndo(styleSheet, BuilderConstants.ChangeUIStyleValueUndoMessage);

                    styleProperty.values = new StyleValueHandle[0];
                }
            }

            // if the property is not already specified then ...
            if (isNewValue)
            {
                // if the x value is  a dimension than add it
                if (newXOriginOffset == k_TransformOriginOffset_None)
                {
                    styleSheet.AddValue(styleProperty, value.x);
                }
                // otherwise, if it is an enum ...
                else
                {
                    // The x value is 'center' and the y value is any enum but 'center' then only add the y value
                    if (newXOriginOffset == TransformOriginOffset.Center && newYOriginOffset != k_TransformOriginOffset_None && newYOriginOffset != TransformOriginOffset.Center)
                    {
                        styleSheet.AddValue(styleProperty, newYOriginOffset);
                        return(true);
                    }
                    styleSheet.AddValue(styleProperty, newXOriginOffset);
                }

                if (newYOriginOffset == k_TransformOriginOffset_None)
                {
                    styleSheet.AddValue(styleProperty, value.y);
                }
                // otherwise, if it is an enum ...
                else
                {
                    // The y value is 'center' then ignore it as it matches the default value
                    if (newYOriginOffset != TransformOriginOffset.Center)
                    {
                        styleSheet.AddValue(styleProperty, newYOriginOffset);
                        return(true);
                    }
                }
            }
            return(isNewValue);
        }