Exemplo n.º 1
0
        private void LookAtAnchor(XmlElement xml, ref Vector4 result)
        {
            if (!xml.Attributes.ContainsKey("Anchor"))
            {
                return;
            }

            Vector4Converter.Convert(xml.Attributes["Anchor"], ref result);
        }
Exemplo n.º 2
0
        public static bool HasVector4(this XmlElement xml, string attribute, out Vector4 value)
        {
            value = Vector4.zero;
            if (!xml.HasAttribute(attribute))
            {
                return(false);
            }

            return(Vector4Converter.Convert(xml.Attributes[attribute], ref value));
        }
Exemplo n.º 3
0
        protected override bool TryCopyTo(BitmapContent destinationBitmap, Rectangle sourceRegion, Rectangle destinationRegion)
        {
            SurfaceFormat destinationFormat;

            if (!destinationBitmap.TryGetFormat(out destinationFormat))
            {
                return(false);
            }

            // A shortcut for copying the entire bitmap to another bitmap of the same type and format
            if (_format == destinationFormat && (sourceRegion == new Rectangle(0, 0, Width, Height)) && sourceRegion == destinationRegion)
            {
                destinationBitmap.SetPixelData(GetPixelData());
                return(true);
            }

            // If the destination is not Vector4 or requires resizing, send it through BitmapContent.Copy
            if (!(destinationBitmap is PixelBitmapContent <Vector4>) || sourceRegion.Width != destinationRegion.Width || sourceRegion.Height != destinationRegion.Height)
            {
                try
                {
                    BitmapContent.Copy(this, sourceRegion, destinationBitmap, destinationRegion);
                    return(true);
                }
                catch (InvalidOperationException)
                {
                    return(false);
                }
            }

            // Convert to a Vector4 format
            var dest = destinationBitmap as PixelBitmapContent <Vector4>;

            if (default(T) is IPackedVector)
            {
                Parallel.For(0, sourceRegion.Height, (y) =>
                {
                    var row = GetRow(sourceRegion.Top + y);
                    for (int x = 0; x < sourceRegion.Width; ++x)
                    {
                        dest.SetPixel(destinationRegion.Left + x, destinationRegion.Top + y, ((IPackedVector)row[sourceRegion.Left + x]).ToVector4());
                    }
                });
            }
            else
            {
                var converter = new Vector4Converter() as IVector4Converter <T>;
                // If no converter could be created, converting from this format is not supported
                if (converter == null)
                {
                    return(false);
                }

                Parallel.For(0, sourceRegion.Height, (y) =>
                {
                    var row = GetRow(sourceRegion.Top + y);
                    for (int x = 0; x < sourceRegion.Width; ++x)
                    {
                        dest.SetPixel(destinationRegion.Left + x, destinationRegion.Top + y, converter.ToVector4(row[sourceRegion.Left + x]));
                    }
                });
            }

            return(true);
        }
Exemplo n.º 4
0
        public override object Convert(object value, Type type, IFormatProvider formatProvider)
        {
            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                if (value == null)
                {
                    return(null);
                }

                Type underlyingType = type.GetGenericArguments()[0];
                return(Convert(value, underlyingType, formatProvider));
            }


            if (type == YogaValueType)
            {
                var res = YogaValueConverter.NormalizeYogaValue(value);
                if (res.HasValue)
                {
                    return(res.Value);
                }
            }

            if (value == null)
            {
                if (TypeConverter.TypeIsNullable(type))
                {
                    return(null);
                }
                throw new NotSupportedException($"Unable to convert null to '{type.FullName}'");
            }

            if (type == ColorType)
            {
                if (value is Color v)
                {
                    return(v);
                }
                var res = ColorConverter.FromJsValue(JsValue.FromObject(engine, value));
                if (res.HasValue)
                {
                    return(res.Value);
                }
            }
            else if (type == Vector2Type)
            {
                if (value is Vector2 v)
                {
                    return(v);
                }
                var res = Vector2Converter.FromJsValue(JsValue.FromObject(engine, value));
                if (res.HasValue)
                {
                    return(res.Value);
                }
            }
            else if (type == Vector4Type)
            {
                if (value is Vector4 v)
                {
                    return(v);
                }
                var res = Vector4Converter.FromJsValue(JsValue.FromObject(engine, value));
                if (res.HasValue)
                {
                    return(res.Value);
                }
            }
            else if (type.IsEnum && value is string s)
            {
                return(Enum.Parse(type, s, true));
            }

            return(base.Convert(value, type, formatProvider));
        }