Ejemplo n.º 1
0
        public object ConvertFrom(object obj, TEnum value)
        {
            var enumRef = (UTinyEnum.Reference)obj;

            ValidateType(enumRef);

            var enumType = enumRef.Type.Dereference(UTinyEditorApplication.Registry ?? ObjectConverter <TEnum> .TestRegistry);

            var defaultValue     = enumType.DefaultValue as UTinyObject;
            var defaultContainer = defaultValue.Properties as IPropertyContainer;

            // Try to match by name.
            var prop = defaultContainer.PropertyBag.FindProperty(value.ToString());

            if (null == prop)
            {
                // Or by value.
                foreach (var property in defaultContainer.PropertyBag.Properties)
                {
                    int intValue;
                    if (s_ConvertedValueFrom.TryGetValue(value, out intValue) && (int)property.GetObjectValue(defaultContainer) == intValue)
                    {
                        prop = property;
                        break;
                    }
                }
            }

            if (null == prop)
            {
                throw new InvalidOperationException($"{UTinyConstants.ApplicationName}: Could not convert from type '{typeof(TEnum).Name}', no mapping from value {value} found.");
            }
            obj = new UTinyEnum.Reference(enumType, (int)prop.GetObjectValue(defaultContainer));
            return(obj);
        }
Ejemplo n.º 2
0
        public void FlatJsonOverrideValue()
        {
            // Output registry
            var registry = new UTinyRegistry();

            var component = m_Entity.GetComponent((UTinyType.Reference)m_ComponentType);

            component["TestEnumField"] = new UTinyEnum.Reference(m_EnumType, 3);

            using (var json = new MemoryStream())
                using (var command = new MemoryStream())
                {
                    // Write our data to json
                    Serialization.FlatJson.BackEnd.Persist(json, m_EnumType, m_ComponentType, m_Entity);

                    json.Position = 0;

                    // Read from json to command stream
                    Serialization.FlatJson.FrontEnd.Accept(json, command);

                    command.Position = 0;

                    // Read from command stream to registry
                    Serialization.CommandStream.FrontEnd.Accept(command, registry);
                }

            AssertOverrideValue(registry);
        }
Ejemplo n.º 3
0
        private void ValidateType(UTinyEnum.Reference reference)
        {
            ValidateTypeEnumType();

            if (null == reference.Type.Dereference(UTinyEditorApplication.Registry ?? ObjectConverter <TEnum> .TestRegistry))
            {
                throw new ArgumentNullException("reference");
            }
        }
Ejemplo n.º 4
0
        public UTinyObject ConvertFrom(UTinyObject @object, Gradient gradient)
        {
            ValidateType(@object);
            var registry = @object.Registry;
            var type     = @object.Type.Dereference(registry).Fields.First(f => f.Name == "mode").FieldType.Dereference(registry);

            @object["mode"] = new UTinyEnum.Reference(type, (int)gradient.mode);
            var stops = @object["stops"] as UTinyList;

            stops.Clear();
            Color currentColor  = Color.white;
            float currentOffset = float.MaxValue;

            for (int i = 0, j = 0; i < gradient.alphaKeys.Length || j < gradient.colorKeys.Length;)
            {
                var alphaOffset = float.MaxValue;
                var colorOffset = float.MaxValue;

                if (i < gradient.alphaKeys.Length)
                {
                    alphaOffset = gradient.alphaKeys[i].time;
                }

                if (j < gradient.colorKeys.Length)
                {
                    colorOffset = gradient.colorKeys[j].time;
                }

                if (alphaOffset == colorOffset)
                {
                    currentColor   = gradient.colorKeys[j].color;
                    currentColor.a = gradient.alphaKeys[i].alpha;
                    currentOffset  = colorOffset;
                    ++i;
                    ++j;
                }
                else if (alphaOffset < colorOffset)
                {
                    currentColor.a = gradient.alphaKeys[i].alpha;
                    currentOffset  = alphaOffset;
                    ++i;
                }
                else // if (alpha > color)
                {
                    var alpha = currentColor.a;
                    currentColor   = gradient.colorKeys[j].color;
                    currentColor.a = alpha;
                    currentOffset  = colorOffset;
                    ++j;
                }

                var stop = new UTinyObject(registry, registry.GetGradientStopType(), @object.VersionStorage)
                {
                    ["color"] = new UTinyObject(registry, registry.GetColorType(), @object.VersionStorage)
                    {
                        ["r"] = currentColor.r,
                        ["g"] = currentColor.g,
                        ["b"] = currentColor.b,
                        ["a"] = currentColor.a
                    },
                    ["offset"] = currentOffset
                };

                stops.Add(stop);
            }
            return(@object);
        }