void RegisterValue <T>(List <T> list, StyleValueType type, T value)
 {
     Log("Add value of type " + type + " : " + value);
     Debug.Assert(m_BuilderState == BuilderState.Property);
     list.Add(value);
     m_CurrentValues.Add(new StyleValueHandle(list.Count - 1, type));
 }
        void SetValue <T>(Index indices, T value, StyleValueType valueType)
        {
            var part = stylePropertyParts[indices.partIndex];

            if (part.isVariable)
            {
                OverrideVariableWithValue(indices, value, valueType);
            }
            // The assumption here is that an individual value which is not a variable will always have a single
            // resolved value
            else
            {
                if (part.handles[0].handle.valueType == valueType)
                {
                    var valueHandle = part.handles[0];
                    SetTypedValue(valueHandle, value);
                }
                else
                {
                    var initialOffset = part.offset;
                    var list          = styleProperty.values.ToList();
                    list.RemoveAt(initialOffset);
                    var handle = AddTypedValue(value, valueType);
                    list.Insert(initialOffset, handle);
                    styleProperty.values = list.ToArray();
                    var valueContext = part.handles[0];
                    valueContext.handle = handle;
                    part.handles[0]     = valueContext;
                }
            }
        }
Exemple #3
0
        private static T CheckAccess <T>(T[] list, StyleValueType type, StyleValueHandle handle)
        {
            T    result = default(T);
            bool flag   = handle.valueType != type;

            if (flag)
            {
                Debug.LogErrorFormat("Trying to read value of type {0} while reading a value of type {1}", new object[]
                {
                    type,
                    handle.valueType
                });
            }
            else
            {
                bool flag2 = list == null || handle.valueIndex < 0 || handle.valueIndex >= list.Length;
                if (flag2)
                {
                    Debug.LogError("Accessing invalid property");
                }
                else
                {
                    result = list[handle.valueIndex];
                }
            }
            return(result);
        }
        public bool IsValueType(int index, StyleValueType type)
        {
            if (type == StyleValueType.Keyword)
            {
                return(m_CurrentStyleValue.keyword != StyleKeyword.Undefined);
            }

            return(m_CurrentStyleValue.keyword == StyleKeyword.Undefined);
        }
Exemple #5
0
 public void AddValue(string value, StyleValueType type)
 {
     if (type == StyleValueType.Variable)
     {
         RegisterVariable(value);
     }
     else
     {
         RegisterValue(m_Strings, type, value);
     }
 }
 private void RegisterValue <T>(List <T> list, StyleValueType type, T value)
 {
     StyleSheetBuilder.Log(string.Concat(new object[]
     {
         "Add value of type ",
         type,
         " : ",
         value
     }));
     list.Add(value);
     this.m_CurrentValues.Add(new StyleValueHandle(list.Count - 1, type));
 }
        private void RegisterValue <T>(List <T> list, StyleValueType type, T value)
        {
            string arg_32_0 = "Add value of type ";
            string arg_32_1 = type.ToString();
            string arg_32_2 = " : ";
            T      t        = value;

            StyleSheetBuilder.Log(arg_32_0 + arg_32_1 + arg_32_2 + ((t != null) ? t.ToString() : null));
            Debug.Assert(this.m_BuilderState == StyleSheetBuilder.BuilderState.Property);
            list.Add(value);
            this.m_CurrentValues.Add(new StyleValueHandle(list.Count - 1, type));
        }
        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);
            }
        }
        private static bool TryCheckAccess <T>(T[] list, StyleValueType type, StyleValueHandle handle, out T value)
        {
            bool result = false;

            value = default(T);
            if (handle.valueType == type && handle.valueIndex >= 0 && handle.valueIndex < list.Length)
            {
                value  = list[handle.valueIndex];
                result = true;
            }
            return(result);
        }
Exemple #10
0
        public static StyleValueHandle AddValueHandle(
            this StyleSheet styleSheet, StyleProperty property, int index, StyleValueType type)
        {
            // Add value object to property.
            var newValues = property.values.ToList();
            var newValue  = new StyleValueHandle(index, type);

            newValues.Add(newValue);
            property.values = newValues.ToArray();

            return(newValue);
        }
        public void AddValue(string value, StyleValueType type)
        {
            bool flag = type == StyleValueType.Variable;

            if (flag)
            {
                this.RegisterVariable(value);
            }
            else
            {
                this.RegisterValue <string>(this.m_Strings, type, value);
            }
        }
        public void SetValueAtIndex <T>(int index, T value, StyleValueType type)
        {
            if (null == styleProperty)
            {
                throw new InvalidOperationException();
            }

            var indices = GetInternalIndices(index);

            // Undo/Redo
            Undo.RegisterCompleteObjectUndo(styleSheet, BuilderConstants.ChangeUIStyleValueUndoMessage);

            SetValue(indices, value, type);
            UpdateStylesheet();
        }
Exemple #13
0
        static bool TryCheckAccess <T>(T[] list, StyleValueType type, StyleValueHandle handle, out T value)
        {
            bool result = false;

            value = default(T);

            if (handle.valueType == type && handle.valueIndex >= 0 && handle.valueIndex < list.Length)
            {
                value  = list[handle.valueIndex];
                result = true;
            }
            else
            {
                Debug.LogErrorFormat("Trying to read value of type {0} while reading a value of type {1}", type, handle.valueType);
            }
            return(result);
        }
Exemple #14
0
        private bool TryGetValue(string propertyName, StyleValueType valueType, out StylePropertyValue customProp)
        {
            customProp = new StylePropertyValue();
            if (m_CustomProperties != null && m_CustomProperties.TryGetValue(propertyName, out customProp))
            {
                // CustomProperty only support one value
                var handle = customProp.handle;
                if (handle.valueType != valueType)
                {
                    LogCustomPropertyWarning(propertyName, valueType, customProp);
                    return(false);
                }

                return(true);
            }

            return(false);
        }
Exemple #15
0
        static T CheckAccess <T>(T[] list, StyleValueType type, StyleValueHandle handle)
        {
            T value = default(T);

            if (handle.valueType != type)
            {
                Debug.LogErrorFormat("Trying to read value of type {0} while reading a value of type {1}", type, handle.valueType);
            }
            else if (list == null || handle.valueIndex < 0 || handle.valueIndex >= list.Length)
            {
                Debug.LogError("Accessing invalid property");
            }
            else
            {
                value = list[handle.valueIndex];
            }
            return(value);
        }
        private bool TryGetValue(string propertyName, StyleValueType valueType, out CustomPropertyHandle customPropertyHandle)
        {
            customPropertyHandle = new CustomPropertyHandle();
            if (m_CustomProperties != null && m_CustomProperties.TryGetValue(propertyName, out customPropertyHandle))
            {
                // CustomProperty only support one value
                var handle = customPropertyHandle.value.handle;
                if (handle.valueType != valueType)
                {
                    Debug.LogWarning(string.Format("Trying to read value as {0} while parsed type is {1}", valueType, handle.valueType));
                    return(false);
                }

                return(true);
            }

            return(false);
        }
        public void AddValue <T>(T value, StyleValueType valueType)
        {
            Undo.RegisterCompleteObjectUndo(styleSheet, BuilderConstants.ChangeUIStyleValueUndoMessage);

            EnsureStylePropertyExists();

            var offset = styleProperty.values.Length;

            if (offset > 0)
            {
                AddCommaSeparator();
                ++offset;
            }

            var handle = AddTypedValue(value, valueType);
            var part   = CreatePart(styleSheet, handle, offset);

            stylePropertyParts.Add(part);
            UpdateStylesheet();
        }
Exemple #18
0
        public StyleFont ReadStyleFont(int index)
        {
            Font font = null;
            StylePropertyValue stylePropertyValue = this.m_Values[this.m_CurrentValueIndex + index];
            StyleValueType     valueType          = stylePropertyValue.handle.valueType;
            StyleValueType     styleValueType     = valueType;

            if (styleValueType != StyleValueType.ResourcePath)
            {
                if (styleValueType != StyleValueType.AssetReference)
                {
                    Debug.LogWarning("Invalid value for font " + stylePropertyValue.handle.valueType.ToString());
                }
                else
                {
                    font = (stylePropertyValue.sheet.ReadAssetReference(stylePropertyValue.handle) as Font);
                    bool flag = font == null;
                    if (flag)
                    {
                        Debug.LogWarning("Invalid font reference");
                    }
                }
            }
            else
            {
                string text  = stylePropertyValue.sheet.ReadResourcePath(stylePropertyValue.handle);
                bool   flag2 = !string.IsNullOrEmpty(text);
                if (flag2)
                {
                    font = (Panel.LoadResource(text, typeof(Font), this.dpiScaling) as Font);
                }
                bool flag3 = font == null;
                if (flag3)
                {
                    Debug.LogWarning(string.Format("Font not found for path: {0}", text));
                }
            }
            return(new StyleFont(font));
        }
Exemple #19
0
        private static bool TryCheckAccess <T>(T[] list, StyleValueType type, StyleValueHandle[] handles, int index, out T value)
        {
            bool result = false;

            value = default(T);
            if (index < handles.Length)
            {
                StyleValueHandle styleValueHandle = handles[index];
                if (styleValueHandle.valueType == type && styleValueHandle.valueIndex >= 0 && styleValueHandle.valueIndex < list.Length)
                {
                    value  = list[styleValueHandle.valueIndex];
                    result = true;
                }
                else
                {
                    Debug.LogErrorFormat("Trying to read value of type {0} while reading a value of type {1}", new object[]
                    {
                        type,
                        styleValueHandle.valueType
                    });
                }
            }
            return(result);
        }
 public Value(StyleValueType type, object obj)
 {
     ValueType = type;
     Obj       = obj;
 }
 public bool IsValueType(int index, StyleValueType type)
 {
     return(m_Values[index].handle.valueType == type);
 }
 public bool IsValueType(int index, StyleValueType type)
 {
     return(m_Values[m_CurrentValueIndex + index].handle.valueType == type);
 }
 public StyleValue(T value, StyleValueType type)
 {
     Value = value;
     Type  = type;
 }
 public StyleValue()
 {
     Value = default(T);
     Type  = StyleValueType.Default;
 }
Exemple #25
0
        internal static bool TryGetImageSourceFromValue(StylePropertyValue propertyValue, float dpiScaling, out ImageSource source)
        {
            source = default(ImageSource);
            StyleValueType valueType      = propertyValue.handle.valueType;
            StyleValueType styleValueType = valueType;
            bool           result;

            if (styleValueType <= StyleValueType.AssetReference)
            {
                if (styleValueType != StyleValueType.ResourcePath)
                {
                    if (styleValueType == StyleValueType.AssetReference)
                    {
                        UnityEngine.Object @object = propertyValue.sheet.ReadAssetReference(propertyValue.handle);
                        source.texture     = (@object as Texture2D);
                        source.vectorImage = (@object as VectorImage);
                        bool flag = source.texture == null && source.vectorImage == null;
                        if (flag)
                        {
                            Debug.LogWarning("Invalid image specified");
                            result = false;
                            return(result);
                        }
                        goto IL_24E;
                    }
                }
                else
                {
                    string text  = propertyValue.sheet.ReadResourcePath(propertyValue.handle);
                    bool   flag2 = !string.IsNullOrEmpty(text);
                    if (flag2)
                    {
                        source.texture = (Panel.LoadResource(text, typeof(Texture2D), dpiScaling) as Texture2D);
                        bool flag3 = source.texture == null;
                        if (flag3)
                        {
                            source.vectorImage = (Panel.LoadResource(text, typeof(VectorImage), dpiScaling) as VectorImage);
                        }
                    }
                    bool flag4 = source.texture == null && source.vectorImage == null;
                    if (flag4)
                    {
                        Debug.LogWarning(string.Format("Image not found for path: {0}", text));
                        result = false;
                        return(result);
                    }
                    goto IL_24E;
                }
            }
            else if (styleValueType != StyleValueType.ScalableImage)
            {
                if (styleValueType == StyleValueType.MissingAssetReference)
                {
                    result = false;
                    return(result);
                }
            }
            else
            {
                ScalableImage scalableImage = propertyValue.sheet.ReadScalableImage(propertyValue.handle);
                bool          flag5         = scalableImage.normalImage == null && scalableImage.highResolutionImage == null;
                if (flag5)
                {
                    Debug.LogWarning("Invalid scalable image specified");
                    result = false;
                    return(result);
                }
                bool flag6 = dpiScaling > 1f;
                if (flag6)
                {
                    source.texture = scalableImage.highResolutionImage;
                    source.texture.pixelsPerPoint = 2f;
                }
                else
                {
                    source.texture = scalableImage.normalImage;
                }
                bool flag7 = !Mathf.Approximately(dpiScaling % 1f, 0f);
                if (flag7)
                {
                    source.texture.filterMode = FilterMode.Bilinear;
                }
                goto IL_24E;
            }
            Debug.LogWarning("Invalid value for image texture " + propertyValue.handle.valueType.ToString());
            result = false;
            return(result);

IL_24E:
            result = true;
            return(result);
        }
Exemple #26
0
        public StyleCursor ReadStyleCursor(int index)
        {
            float          x = 0f;
            float          y = 0f;
            int            defaultCursorId = 0;
            Texture2D      texture         = null;
            StyleValueType valueType       = this.GetValueType(index);
            bool           flag            = valueType == StyleValueType.ResourcePath || valueType == StyleValueType.AssetReference || valueType == StyleValueType.ScalableImage;
            bool           flag2           = flag;

            if (flag2)
            {
                bool flag3 = this.valueCount < 1;
                if (flag3)
                {
                    Debug.LogWarning(string.Format("USS 'cursor' has invalid value at {0}.", index));
                }
                else
                {
                    ImageSource        imageSource = default(ImageSource);
                    StylePropertyValue value       = this.GetValue(index);
                    bool flag4 = StylePropertyReader.TryGetImageSourceFromValue(value, this.dpiScaling, out imageSource);
                    if (flag4)
                    {
                        texture = imageSource.texture;
                        bool flag5 = this.valueCount >= 3;
                        if (flag5)
                        {
                            StylePropertyValue value2 = this.GetValue(index + 1);
                            StylePropertyValue value3 = this.GetValue(index + 2);
                            bool flag6 = value2.handle.valueType != StyleValueType.Float || value3.handle.valueType != StyleValueType.Float;
                            if (flag6)
                            {
                                Debug.LogWarning("USS 'cursor' property requires two integers for the hot spot value.");
                            }
                            else
                            {
                                x = value2.sheet.ReadFloat(value2.handle);
                                y = value3.sheet.ReadFloat(value3.handle);
                            }
                        }
                    }
                }
            }
            else
            {
                bool flag7 = StylePropertyReader.getCursorIdFunc != null;
                if (flag7)
                {
                    StylePropertyValue value4 = this.GetValue(index);
                    defaultCursorId = StylePropertyReader.getCursorIdFunc(value4.sheet, value4.handle);
                }
            }
            UnityEngine.UIElements.Cursor v = new UnityEngine.UIElements.Cursor
            {
                texture         = texture,
                hotspot         = new Vector2(x, y),
                defaultCursorId = defaultCursorId
            };
            return(new StyleCursor(v));
        }
 internal StyleValueHandle(int valueIndex, StyleValueType valueType)
 {
     this.valueIndex = valueIndex;
     m_ValueType     = valueType;
 }
 public void AddValue(string value, StyleValueType type)
 {
     this.RegisterValue <string>(this.m_Strings, type, value);
 }
        void OverrideVariableWithValue <T>(Index indices, T value, StyleValueType valueType)
        {
            if (!stylePropertyParts[indices.partIndex].isVariable)
            {
                return;
            }

            var part          = stylePropertyParts[indices.partIndex];
            var initialOffset = part.offset;
            var nextOffset    = indices.partIndex + 1 >= stylePropertyParts.Count
                ? -1
                : stylePropertyParts[indices.partIndex + 1].offset;

            // Range of handles to remove in the StyleProperty.values array
            var range = nextOffset < 0
                ? styleProperty.values.Length - initialOffset
                : nextOffset - initialOffset;

            var newParts = new List <StylePropertyPart>();

            // To set an explicit value on top of a variable, we must first remove the variable. In the case where
            // the variable points to a list of values, we must remove all values of the list and set them as
            // explicit values of the same type.
            var list = styleProperty.values.ToList();

            list.RemoveRange(initialOffset, range);

            var currentOffset = initialOffset;

            for (var i = 0; i < part.handles.Count; ++i, ++currentOffset)
            {
                var propertyValue = StylePropertyPart.Create();
                propertyValue.offset = currentOffset;

                if (i == indices.valueIndex && typeof(Variable).IsAssignableFrom(typeof(T)) && value is Variable variable)
                {
                    var handles = AddVariableToStyleSheet(variable.name);

                    var property = new StyleProperty
                    {
                        name   = styleProperty.name,
                        values = handles
                    };

                    var ib      = 0;
                    var newPart = ResolveValueOrVariable(styleSheet, element, styleRule, property, ref ib,
                                                         editorExtensionMode);

                    list.InsertRange(currentOffset, handles);
                    currentOffset += 2;
                    if (i < part.handles.Count - 1 || indices.partIndex < stylePropertyParts.Count - 1)
                    {
                        list.Insert(++currentOffset, new StyleValueHandle(-1, StyleValueType.CommaSeparator));
                    }

                    newParts.Add(newPart);
                }
                else
                {
                    var handle = i == indices.valueIndex
                        ? AddTypedValue(value, valueType)
                        : TransferTypedValue(part.handles[i]);
                    list.Insert(currentOffset, handle);

                    if (i < part.handles.Count - 1 || indices.partIndex < stylePropertyParts.Count - 1)
                    {
                        list.Insert(++currentOffset, new StyleValueHandle(-1, StyleValueType.CommaSeparator));
                    }

                    propertyValue.handles.Add(new StyleValueHandleContext {
                        styleSheet = styleSheet, handle = handle
                    });

                    newParts.Add(propertyValue);
                }
            }

            if (part.isVariableUnresolved && indices.valueIndex == -1)
            {
                if (typeof(Variable).IsAssignableFrom(typeof(T)) && value is Variable variable)
                {
                    var handles = AddVariableToStyleSheet(variable.name);
                    var newPart = StylePropertyPart.Create();
                    newPart.offset = currentOffset;
                    newPart.handles.Add(new StyleValueHandleContext
                    {
                        styleSheet = styleSheet,
                        handle     = handles[2],
                    });
                    list.InsertRange(currentOffset, handles);
                    newParts.Add(newPart);
                }
                else
                {
                    var handle = AddTypedValue(value, valueType);

                    var newPart = StylePropertyPart.Create();
                    newPart.offset = currentOffset;
                    newPart.handles.Add(new StyleValueHandleContext
                    {
                        styleSheet = styleSheet,
                        handle     = handle,
                    });
                    list.Insert(currentOffset, handle);
                    newParts.Add(newPart);
                }
            }

            styleProperty.values = list.ToArray();
            stylePropertyParts.RemoveAt(indices.partIndex);
            // Manually dispose removed part
            part.Dispose();

            stylePropertyParts.InsertRange(indices.partIndex, newParts);
            var partsCount = indices.partIndex + newParts.Count;

            var offset = currentOffset - nextOffset;

            for (var i = partsCount; i < stylePropertyParts.Count; ++i)
            {
                var nextPart = stylePropertyParts[i];
                nextPart.offset      += offset;
                stylePropertyParts[i] = nextPart;
            }
        }
 public void AddValue(string value, StyleValueType type)
 {
     RegisterValue(m_Strings, type, value);
 }