summaryrefslogtreecommitdiff
path: root/src/mbgl/text/placement.hpp
blob: 2a6a2e1d6e293f1bae64d77d76c1999e8fcab86b (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#pragma once

#include <string>
#include <unordered_map>
#include <mbgl/util/chrono.hpp>
#include <mbgl/text/collision_index.hpp>
#include <mbgl/layout/symbol_projection.hpp>
#include <mbgl/style/transition_options.hpp>
#include <unordered_set>

namespace mbgl {

class SymbolBucket;
class SymbolInstance;

class OpacityState {
public:
    OpacityState(bool placed, bool skipFade);
    OpacityState(const OpacityState& prevOpacityState, float increment, bool placed);
    bool isHidden() const;
    float opacity;
    bool placed;
};

class JointOpacityState {
public:
    JointOpacityState(bool placedIcon, bool placedText, bool skipFade);
    JointOpacityState(const JointOpacityState& prevOpacityState, float increment, bool placedIcon, bool placedText);
    bool isHidden() const;
    OpacityState icon;
    OpacityState text;
};

class VariableOffset {
public:
    float radialOffset;
    float width;
    float height;
    style::TextVariableAnchorType anchor;
    float textBoxScale;
    optional<style::TextVariableAnchorType> prevAnchor;
};

class JointPlacement {
public:
    JointPlacement(bool text_, bool icon_, bool skipFade_)
        : text(text_), icon(icon_), skipFade(skipFade_)
    {}

    const bool text;
    const bool icon;
    // skipFade = outside viewport, but within CollisionIndex::viewportPadding px of the edge
    // Because these symbols aren't onscreen yet, we can skip the "fade in" animation,
    // and if a subsequent viewport change brings them into view, they'll be fully
    // visible right away.
    const bool skipFade;
};
  
struct RetainedQueryData {
    uint32_t bucketInstanceId;
    std::shared_ptr<FeatureIndex> featureIndex;
    OverscaledTileID tileID;
    std::shared_ptr<std::vector<size_t>> featureSortOrder;
    
    RetainedQueryData(uint32_t bucketInstanceId_,
                      std::shared_ptr<FeatureIndex> featureIndex_,
                      OverscaledTileID tileID_)
        : bucketInstanceId(bucketInstanceId_)
        , featureIndex(std::move(featureIndex_))
        , tileID(std::move(tileID_)) {}
};
    
class CollisionGroups {
public:
    using Predicate = std::function<bool(const IndexedSubfeature&)>;
    using CollisionGroup = std::pair<uint16_t, optional<Predicate>>;
    
    CollisionGroups(const bool crossSourceCollisions_)
        : maxGroupID(0)
        , crossSourceCollisions(crossSourceCollisions_)
    {}
    
    const CollisionGroup& get(const std::string& sourceID);
    
private:
    std::map<std::string, CollisionGroup> collisionGroups;
    uint16_t maxGroupID;
    bool crossSourceCollisions;
};

class BucketPlacementParameters {
public:
    const RenderTile& tile;
    const mat4& projMatrix;
    std::string sourceId;
    std::shared_ptr<FeatureIndex> featureIndex;
    bool showCollisionBoxes;
};
    
class Placement {
public:
    Placement(const TransformState&, MapMode, style::TransitionOptions, const bool crossSourceCollisions, std::unique_ptr<Placement> prevPlacementOrNull = nullptr);
    void placeLayer(const RenderLayer&, const mat4&, bool showCollisionBoxes);
    void commit(TimePoint);
    void updateLayerBuckets(const RenderLayer&, const TransformState&,  bool updateOpacities);
    float symbolFadeChange(TimePoint now) const;
    bool hasTransitions(TimePoint now) const;

    const CollisionIndex& getCollisionIndex() const;

    bool stillRecent(TimePoint now) const;
    void setRecent(TimePoint now);
    void setStale();
    
    const RetainedQueryData& getQueryData(uint32_t bucketInstanceId) const;
private:
    friend SymbolBucket;
    void placeBucket(
            const SymbolBucket&,
            const BucketPlacementParameters&,
            std::set<uint32_t>& seenCrossTileIDs);
    // Returns `true` if bucket vertices were updated; returns `false` otherwise.
    bool updateBucketDynamicVertices(SymbolBucket&, const TransformState&, const RenderTile& tile) const;
    void updateBucketOpacities(SymbolBucket&, const TransformState&, std::set<uint32_t>&);
    void markUsedJustification(SymbolBucket&, style::TextVariableAnchorType, SymbolInstance&);

    CollisionIndex collisionIndex;

    MapMode mapMode;
    style::TransitionOptions transitionOptions;

    TimePoint fadeStartTime;
    TimePoint commitTime;

    std::unordered_map<uint32_t, JointPlacement> placements;
    std::unordered_map<uint32_t, JointOpacityState> opacities;
    std::unordered_map<uint32_t, VariableOffset> variableOffsets;

    bool stale = false;
    
    std::unordered_map<uint32_t, RetainedQueryData> retainedQueryData;
    CollisionGroups collisionGroups;
    std::unique_ptr<Placement> prevPlacement;

    // Used for debug purposes.
    std::unordered_map<const CollisionFeature*, std::vector<ProjectedCollisionBox>> collisionCircles;
};

} // namespace mbgl