summaryrefslogtreecommitdiff
path: root/src/mbgl/layer/line_layer.cpp
blob: a27cf65ee2a320502a70b6dadef6ee98feb05a25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <mbgl/layer/line_layer.hpp>
#include <mbgl/style/property_parsing.hpp>
#include <mbgl/style/style_bucket_parameters.hpp>
#include <mbgl/renderer/line_bucket.hpp>
#include <mbgl/map/tile_id.hpp>

namespace mbgl {

void LineLayer::parseLayout(const JSVal& value) {
    parseProperty<Function<CapType>>("line-cap", PropertyKey::LineCap, layout, value);
    parseProperty<Function<JoinType>>("line-join", PropertyKey::LineJoin, layout, value);
    parseProperty<Function<float>>("line-miter-limit", PropertyKey::LineMiterLimit, layout, value);
    parseProperty<Function<float>>("line-round-limit", PropertyKey::LineRoundLimit, layout, value);
}

void LineLayer::parsePaints(const JSVal& layer) {
    paints.parseEach(layer, [&] (ClassProperties& paint, const JSVal& value) {
        parseProperty<Function<float>>("line-opacity", PropertyKey::LineOpacity, paint, value);
        parseProperty<PropertyTransition>("line-opacity-transition", PropertyKey::LineOpacity, paint, value);
        parseProperty<Function<Color>>("line-color", PropertyKey::LineColor, paint, value);
        parseProperty<PropertyTransition>("line-color-transition", PropertyKey::LineColor, paint, value);
        parseProperty<Function<std::array<float,2>>>("line-translate", PropertyKey::LineTranslate, paint, value);
        parseProperty<PropertyTransition>("line-translate-transition", PropertyKey::LineTranslate, paint, value);
        parseProperty<Function<TranslateAnchorType>>("line-translate-anchor", PropertyKey::LineTranslateAnchor, paint, value);
        parseProperty<Function<float>>("line-width", PropertyKey::LineWidth, paint, value);
        parseProperty<PropertyTransition>("line-width-transition", PropertyKey::LineWidth, paint, value);
        parseProperty<Function<float>>("line-gap-width", PropertyKey::LineGapWidth, paint, value);
        parseProperty<PropertyTransition>("line-gap-width-transition", PropertyKey::LineGapWidth, paint, value);
        parseProperty<Function<float>>("line-blur", PropertyKey::LineBlur, paint, value);
        parseProperty<PropertyTransition>("line-blur-transition", PropertyKey::LineBlur, paint, value);
        parseProperty<PiecewiseConstantFunction<Faded<std::vector<float>>>>("line-dasharray", PropertyKey::LineDashArray, paint, value, "line-dasharray-transition");
        parseProperty<PiecewiseConstantFunction<Faded<std::string>>>("line-pattern", PropertyKey::LineImage, paint, value, "line-pattern-transition");
    });
}

void LineLayer::recalculate(const StyleCalculationParameters& parameters) {
    paints.removeExpiredTransitions(parameters.now);

    paints.calculateTransitioned(PropertyKey::LineOpacity, properties.opacity, parameters);
    paints.calculateTransitioned(PropertyKey::LineColor, properties.color, parameters);
    paints.calculateTransitioned(PropertyKey::LineTranslate, properties.translate, parameters);
    paints.calculate(PropertyKey::LineTranslateAnchor, properties.translateAnchor, parameters);
    paints.calculateTransitioned(PropertyKey::LineWidth, properties.width, parameters);
    paints.calculateTransitioned(PropertyKey::LineGapWidth, properties.gap_width, parameters);
    paints.calculateTransitioned(PropertyKey::LineBlur, properties.blur, parameters);
    paints.calculate(PropertyKey::LineDashArray, properties.dash_array, parameters);
    paints.calculate(PropertyKey::LineImage, properties.image, parameters);

    // for scaling dasharrays
    StyleCalculationParameters dashArrayParams = parameters;
    dashArrayParams.z = std::floor(dashArrayParams.z);
    paints.calculate(PropertyKey::LineWidth, properties.dash_line_width, dashArrayParams);

    passes = properties.isVisible() ? RenderPass::Translucent : RenderPass::None;
}

std::unique_ptr<Bucket> LineLayer::createBucket(StyleBucketParameters& parameters) const {
    auto bucket = std::make_unique<LineBucket>();

    const float z = parameters.tileID.z;

    layout.calculate(PropertyKey::LineCap, bucket->layout.cap, z);
    layout.calculate(PropertyKey::LineJoin, bucket->layout.join, z);
    layout.calculate(PropertyKey::LineMiterLimit, bucket->layout.miter_limit, z);
    layout.calculate(PropertyKey::LineRoundLimit, bucket->layout.round_limit, z);

    parameters.eachFilteredFeature(filter, [&] (const auto& feature) {
        bucket->addGeometry(feature.getGeometries());
    });

    return std::move(bucket);
}

}