From afc54b7956ca77dded9c80194954b22148258f5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 14 May 2014 16:51:27 +0200 Subject: [PATCH] add translate-anchor for fills --- include/llmr/style/properties.hpp | 2 ++ src/renderer/painter_fill.cpp | 12 +++++------- src/style/style.cpp | 1 + src/style/style_parser.cpp | 30 +++++++++++++++++------------- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/include/llmr/style/properties.hpp b/include/llmr/style/properties.hpp index eae2351f4..c2fce21e1 100644 --- a/include/llmr/style/properties.hpp +++ b/include/llmr/style/properties.hpp @@ -105,6 +105,7 @@ struct LineProperties { struct FillClass { FunctionProperty enabled = true; std::array translate = {{ 0, 0 }}; + TranslateAnchor translateAnchor = TranslateAnchor::Map; Winding winding = Winding::NonZero; FunctionProperty antialias = true; Color fill_color = {{ 0, 0, 0, 1 }}; @@ -116,6 +117,7 @@ struct FillClass { struct FillProperties { bool enabled = true; std::array translate = {{ 0, 0 }}; + TranslateAnchor translateAnchor = TranslateAnchor::Map; Winding winding = Winding::NonZero; bool antialias = true; Color fill_color = {{ 0, 0, 0, 1 }}; diff --git a/src/renderer/painter_fill.cpp b/src/renderer/painter_fill.cpp index b412d8570..543df440d 100644 --- a/src/renderer/painter_fill.cpp +++ b/src/renderer/painter_fill.cpp @@ -35,13 +35,13 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons stroke_color = fill_color; } - translateLayer(properties.translate, id); + const mat4 vtxMatrix = translatedMatrix(properties.translate, id, properties.translateAnchor); // Because we're drawing top-to-bottom, and we update the stencil mask // below, we have to draw the outline first (!) if (outline && pass == Translucent) { useProgram(outlineShader->program); - outlineShader->setMatrix(matrix); + outlineShader->setMatrix(vtxMatrix); lineWidth(2.0f); // This is always fixed and does not depend on the pixelRatio! outlineShader->setColor(stroke_color); @@ -93,7 +93,7 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons }; useProgram(patternShader->program); - patternShader->setMatrix(matrix); + patternShader->setMatrix(vtxMatrix); patternShader->setOffset(offset); patternShader->setPatternSize(imageSize); patternShader->setPatternTopLeft({{ imagePos.tl.x, imagePos.tl.y }}); @@ -111,7 +111,7 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons // fragments // Draw filling rectangle. useProgram(plainShader->program); - plainShader->setMatrix(matrix); + plainShader->setMatrix(vtxMatrix); plainShader->setColor(fill_color); // Draw the actual triangles into the color & stencil buffer. @@ -124,7 +124,7 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons // below, we have to draw the outline first (!) if (fringeline && pass == Translucent) { useProgram(outlineShader->program); - outlineShader->setMatrix(matrix); + outlineShader->setMatrix(vtxMatrix); lineWidth(2.0f); // This is always fixed and does not depend on the pixelRatio! outlineShader->setColor(fill_color); @@ -138,6 +138,4 @@ void Painter::renderFill(FillBucket& bucket, const std::string& layer_name, cons glDepthRange(strata + strata_epsilon, 1.0f); bucket.drawVertices(*outlineShader); } - - translateLayer(properties.translate, id, true); } diff --git a/src/style/style.cpp b/src/style/style.cpp index ecb86108e..b6f22ea20 100644 --- a/src/style/style.cpp +++ b/src/style/style.cpp @@ -40,6 +40,7 @@ void Style::cascade(float z) { fill.enabled = layer.enabled.evaluate(z); fill.translate = {{ layer.translate[0].evaluate(z), layer.translate[1].evaluate(z) }}; + fill.translateAnchor = layer.translateAnchor; fill.winding = layer.winding; fill.antialias = layer.antialias.evaluate(z); fill.fill_color = layer.fill_color; diff --git a/src/style/style_parser.cpp b/src/style/style_parser.cpp index 83c408dd3..3f9370919 100644 --- a/src/style/style_parser.cpp +++ b/src/style/style_parser.cpp @@ -194,6 +194,19 @@ void StyleParser::parseConstants(JSVal value) { } } +TranslateAnchor parseTranslateAnchor(JSVal anchor) { + if (anchor.IsString()) { + std::string a { anchor.GetString(), anchor.GetStringLength() }; + if (a == "viewport") { + return TranslateAnchor::Viewport; + } else { + return TranslateAnchor::Map; + } + } else { + throw Style::exception("translate anchor must be a string"); + } +} + void StyleParser::parseClasses(JSVal value, std::map& classes, std::map& buckets, std::vector& layers) { if (value.IsArray()) { for (rapidjson::SizeType i = 0; i < value.Size(); ++i) { @@ -434,6 +447,10 @@ FillClass StyleParser::parseFillClass(JSVal value) { klass.translate = std::array {{ values[0], values[1] }}; } + if (value.HasMember("translate-anchor")) { + klass.translateAnchor = parseTranslateAnchor(value["translate-anchor"]); + } + if (value.HasMember("color")) { klass.fill_color = parseColor(value["color"]); } @@ -530,19 +547,6 @@ PointClass StyleParser::parsePointClass(JSVal value) { return klass; } -TranslateAnchor parseTranslateAnchor(JSVal anchor) { - if (anchor.IsString()) { - std::string a { anchor.GetString(), anchor.GetStringLength() }; - if (a == "viewport") { - return TranslateAnchor::Viewport; - } else { - return TranslateAnchor::Map; - } - } else { - throw Style::exception("translate anchor must be a string"); - } -} - TextClass StyleParser::parseTextClass(JSVal value) { TextClass klass;