Exemple #1
0
        public static List <MGLPaint> ConvertFillLayer(JsonStyleLayer jsonStyleLayer, MGLSpriteAtlas spriteAtlas)
        {
            var layout = jsonStyleLayer?.Layout;
            var paint  = jsonStyleLayer?.Paint;

            var area = new MGLPaint();
            var line = new MGLPaint();

            // Set defaults
            area.SetFixColor(new SKColor(0, 0, 0, 0));
            area.SetFixOpacity(1);
            area.SetFixStyle(SKPaintStyle.Fill);
            line.SetFixColor(new SKColor(0, 0, 0, 0));
            line.SetFixOpacity(1);
            line.SetFixStyle(SKPaintStyle.Stroke);
            line.SetFixStrokeWidth(0);

            // If we don't have a paint, than there isn't anything that we could do
            if (paint == null)
            {
                return(new List <MGLPaint>()
                {
                    area, line
                });
            }

            // fill-color
            //   Optional color. Defaults to #000000. Disabled by fill-pattern. Exponential.
            //   The color of the filled part of this layer. This color can be specified as
            //   rgba with an alpha component and the color's opacity will not affect the
            //   opacity of the 1px stroke, if it is used.
            if (paint.FillColor != null)
            {
                if (paint.FillColor.Stops != null)
                {
                    area.SetVariableColor((context) => jsonStyleLayer.Paint.FillColor.Evaluate(context.Zoom));
                    line.SetVariableColor((context) => jsonStyleLayer.Paint.FillColor.Evaluate(context.Zoom));
                }
                else
                {
                    area.SetFixColor(jsonStyleLayer.Paint.FillColor.SingleVal);
                    line.SetFixColor(jsonStyleLayer.Paint.FillColor.SingleVal);
                }
            }

            // fill-outline-color
            //   Optional color. Disabled by fill-pattern. Requires fill-antialias = true. Exponential.
            //   The outline color of the fill. Matches the value of fill-color if unspecified.
            if (paint.FillOutlineColor != null && paint.FillAntialias != null)
            {
                if (paint.FillOutlineColor.Stops != null)
                {
                    line.SetVariableColor((context) => jsonStyleLayer.Paint.FillOutlineColor.Evaluate(context.Zoom));
                }
                else
                {
                    line.SetFixColor(jsonStyleLayer.Paint.FillOutlineColor.SingleVal);
                }
            }

            // fill-opacity
            //   Optional number. Defaults to 1. Exponential.
            //   The opacity of the entire fill layer. In contrast to the fill-color, this
            //   value will also affect the 1px stroke around the fill, if the stroke is used.
            if (paint.FillOpacity != null)
            {
                if (paint.FillOpacity.Stops != null)
                {
                    area.SetVariableOpacity((context) => jsonStyleLayer.Paint.FillOpacity.Evaluate(context.Zoom));
                    line.SetVariableOpacity((context) => jsonStyleLayer.Paint.FillOpacity.Evaluate(context.Zoom));
                }
                else
                {
                    area.SetFixOpacity(jsonStyleLayer.Paint.FillOpacity.SingleVal);
                    line.SetFixOpacity(jsonStyleLayer.Paint.FillOpacity.SingleVal);
                }
            }

            // fill-antialias
            //   Optional boolean. Defaults to true. Interval.
            //   Whether or not the fill should be antialiased.
            if (paint.FillAntialias != null)
            {
                if (paint.FillAntialias.Stops != null)
                {
                    area.SetVariableAntialias((context) => jsonStyleLayer.Paint.FillAntialias.Evaluate(context.Zoom));
                    line.SetVariableAntialias((context) => jsonStyleLayer.Paint.FillAntialias.Evaluate(context.Zoom));
                }
                else
                {
                    area.SetFixAntialias(jsonStyleLayer.Paint.FillAntialias.SingleVal == null ? false : (bool)jsonStyleLayer.Paint.FillAntialias.SingleVal);
                    line.SetFixAntialias(jsonStyleLayer.Paint.FillAntialias.SingleVal == null ? false : (bool)jsonStyleLayer.Paint.FillAntialias.SingleVal);
                }
            }

            // fill-translate
            //   Optional array. Units in pixels. Defaults to 0,0. Exponential.
            //   The geometry's offset. Values are [x, y] where negatives indicate left and up,
            //   respectively.

            // TODO: Use matrix of paint object for this

            // fill-translate-anchor
            //   Optional enum. One of map, viewport. Defaults to map. Requires fill-translate. Interval.
            //   Control whether the translation is relative to the map (north) or viewport (screen)

            // TODO: Use matrix of paint object for this

            // fill-pattern
            //   Optional string. Interval.
            //   Name of image in sprite to use for drawing image fills. For seamless patterns,
            //   image width and height must be a factor of two (2, 4, 8, …, 512).
            if (paint.FillPattern != null)
            {
                // FillPattern needs a color. Instead no pattern is drawn.
                area.SetFixColor(SKColors.Black);

                if (paint.FillPattern.Stops == null && !paint.FillPattern.SingleVal.Contains("{"))
                {
                    area.SetVariableShader((context) =>
                    {
                        var name = paint.FillPattern.SingleVal;

                        var sprite = spriteAtlas.GetSprite(name);
                        if (sprite != null && sprite.Image != null)
                        {
                            return(sprite.Image.ToShader(SKShaderTileMode.Repeat, SKShaderTileMode.Repeat, SKMatrix.MakeScale(context.Scale, context.Scale)));
                        }
                        else
                        {
                            // Log information, that no sprite is found
                            // TODO
                            // Logging.Logger.Log(Logging.LogLevel.Information, $"Fill pattern {name} not found");
                            return(null);
                        }
                    });
                }
                else
                {
                    area.SetVariableShader((context) =>
                    {
                        var name = ReplaceFields(jsonStyleLayer.Paint.FillPattern.Evaluate(context.Zoom), context.Tags);

                        var sprite = spriteAtlas.GetSprite(name);
                        if (sprite != null && sprite.Image != null)
                        {
                            return(sprite.Image.ToShader(SKShaderTileMode.Repeat, SKShaderTileMode.Repeat, SKMatrix.MakeScale(context.Scale, context.Scale)));
                        }
                        else
                        {
                            // Log information, that no sprite is found
                            // TODO
                            // Logging.Logger.Log(Logging.LogLevel.Information, $"Fill pattern {name} not found");
                            return(null);
                        }
                    });
                }
            }

            return(new List <MGLPaint>()
            {
                area, line
            });
        }
Exemple #2
0
        public static List <MGLPaint> ConvertBackgroundLayer(JsonStyleLayer jsonStyleLayer, MGLSpriteAtlas spriteAtlas)
        {
            var paint = jsonStyleLayer.Paint;

            var brush = new MGLPaint();

            brush.SetFixColor(new SKColor(0, 0, 0, 0));
            brush.SetFixOpacity(1);

            // background-color
            //   Optional color. Defaults to #000000. Disabled by background-pattern. Transitionable.
            //   The color with which the background will be drawn.
            if (paint.BackgroundColor != null)
            {
                if (paint.BackgroundColor.Stops != null)
                {
                    brush.SetVariableColor((context) => paint.BackgroundColor.Evaluate(context.Zoom));
                }
                else
                {
                    brush.SetFixColor(paint.BackgroundColor.SingleVal);
                }
            }

            // background-pattern
            //   Optional string. Interval.
            //   Name of image in sprite to use for drawing image background. For seamless patterns,
            //   image width and height must be a factor of two (2, 4, 8, …, 512). Note that
            //   zoom -dependent expressions will be evaluated only at integer zoom levels.
            if (paint.BackgroundPattern != null)
            {
                if (paint.BackgroundPattern.Stops == null && !paint.BackgroundPattern.SingleVal.Contains("{"))
                {
                    var sprite = spriteAtlas.GetSprite(paint.BackgroundPattern.SingleVal);
                    if (sprite != null && sprite.Image != null)
                    {
                        brush.SetFixShader(sprite.Image.ToShader(SKShaderTileMode.Repeat, SKShaderTileMode.Repeat));
                    }
                }
                else
                {
                    brush.SetVariableShader((context) =>
                    {
                        var name = ReplaceFields(paint.BackgroundPattern.Evaluate(context.Zoom), null);

                        var sprite = spriteAtlas.GetSprite(name);
                        if (sprite != null && sprite.Image != null)
                        {
                            return(sprite.Image.ToShader(SKShaderTileMode.Repeat, SKShaderTileMode.Repeat));
                        }
                        else
                        {
                            // Log information
                            // TODO
                            // Logging.Logger.Log(Logging.LogLevel.Information, $"Fill pattern {name} not found");
                            // No sprite found
                            return(null);
                        }
                    });
                }
            }

            // background-opacity
            //   Optional number. Defaults to 1.
            //   The opacity at which the background will be drawn.
            if (paint?.BackgroundOpacity != null)
            {
                if (paint.BackgroundOpacity.Stops != null)
                {
                    brush.SetVariableOpacity((context) => paint.BackgroundOpacity.Evaluate(context.Zoom));
                }
                else
                {
                    brush.SetFixOpacity(paint.BackgroundOpacity.SingleVal);
                }
            }

            return(new List <MGLPaint> {
                brush
            });
        }