Skip to content

Commit

Permalink
refactor: pass just selection color to addLabel instead of DrawRule
Browse files Browse the repository at this point in the history
  • Loading branch information
hjanetzek authored and tallytalwar committed Dec 21, 2016
1 parent 0b57df9 commit 4a842f4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
2 changes: 0 additions & 2 deletions core/src/labels/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,6 @@ class Label {

int m_anchorIndex;

uint32_t m_selectionColor;

protected:

bool m_occludedLastFrame;
Expand Down
2 changes: 1 addition & 1 deletion core/src/style/debugTextStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ std::unique_ptr<StyledMesh> DebugTextStyleBuilder::build() {
}

DrawRule rule({"", 0, {}}, "", 0);
addLabel(params, Label::Type::debug, { glm::vec3(0.5f, 0.5f, 0.f) }, rule);
addLabel(params, Label::Type::debug, { glm::vec3(0.5f, 0.5f, 0.f) }, 0);

m_textLabels->setLabels(m_labels);

Expand Down
33 changes: 19 additions & 14 deletions core/src/style/textStyleBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,27 @@ bool TextStyleBuilder::addFeatureCommon(const Feature& _feat, const DrawRule& _r
size_t quadsStart = m_quads.size();
size_t numLabels = m_labels.size();

auto selectionColor = [&]() {
return params.interactive ? _rule.featureSelection->nextColorIdentifier() : 0;
};

if (!prepareLabel(params, labelType)) { return false; }

if (_feat.geometryType == GeometryType::points) {
for (auto& point : _feat.points) {
auto p = glm::vec2(point);
addLabel(params, Label::Type::point, { p, p }, _rule);
addLabel(params, Label::Type::point, { p, p }, selectionColor());
}

} else if (_feat.geometryType == GeometryType::polygons) {
for (auto& polygon : _feat.polygons) {
if (_iconText) {
auto p = centroid(polygon);
addLabel(params, Label::Type::point, { p, p }, _rule);
addLabel(params, Label::Type::point, { p, p }, selectionColor());
} else {
for (auto& line : polygon) {
for (auto& point : line) {
addLabel(params, Label::Type::point, { point }, _rule);
addLabel(params, Label::Type::point, { point }, selectionColor());
}
}
}
Expand All @@ -206,7 +210,7 @@ bool TextStyleBuilder::addFeatureCommon(const Feature& _feat, const DrawRule& _r
if (_iconText) {
for (auto& line : _feat.lines) {
for (auto& point : line) {
addLabel(params, Label::Type::point, { point }, _rule);
addLabel(params, Label::Type::point, { point }, selectionColor());
}
}
} else {
Expand All @@ -221,12 +225,19 @@ bool TextStyleBuilder::addFeatureCommon(const Feature& _feat, const DrawRule& _r
return true;
}

void TextStyleBuilder::addLineTextLabels(const Feature& _feat, const TextStyle::Parameters& _params, const DrawRule& _rule) {
void TextStyleBuilder::addLineTextLabels(const Feature& _feat,
const TextStyle::Parameters& _params,
const DrawRule& _rule) {

float pixelScale = 1.0/m_tileSize;
float minLength = m_attributes.width * pixelScale;

float tolerance = pow(pixelScale * 2, 2);

auto selectionColor = [&]() {
return _params.interactive ? _rule.featureSelection->nextColorIdentifier() : 0;
};

for (auto& line : _feat.lines) {

for (size_t i = 0; i < line.size() - 1; i++) {
Expand All @@ -243,7 +254,7 @@ void TextStyleBuilder::addLineTextLabels(const Feature& _feat, const TextStyle::

if (j == next) {
if (segmentLength > minLength) {
addLabel(_params, Label::Type::line, { p1, p }, _rule);
addLabel(_params, Label::Type::line, { p1, p }, selectionColor());
}
} else {
glm::vec2 pp = glm::vec2(line[j-1]);
Expand All @@ -267,7 +278,7 @@ void TextStyleBuilder::addLineTextLabels(const Feature& _feat, const TextStyle::
glm::vec2 b = glm::vec2(p2 - p1) / float(run);

for (int r = 0; r < run; r++) {
addLabel(_params, Label::Type::line, { a, a+b }, _rule);
addLabel(_params, Label::Type::line, { a, a+b }, selectionColor());
a += b;
}
run *= 2;
Expand Down Expand Up @@ -527,13 +538,7 @@ bool TextStyleBuilder::prepareLabel(TextStyle::Parameters& _params, Label::Type
}

void TextStyleBuilder::addLabel(const TextStyle::Parameters& _params, Label::Type _type,
Label::WorldTransform _transform, const DrawRule& _rule) {

uint32_t selectionColor = 0;

if (_params.interactive) {
selectionColor = _rule.featureSelection->nextColorIdentifier();
}
Label::WorldTransform _transform, uint32_t selectionColor) {

m_labels.emplace_back(new TextLabel(_transform, _type, _params.labelOptions,
{m_attributes.fill,
Expand Down
2 changes: 1 addition & 1 deletion core/src/style/textStyleBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TextStyleBuilder : public StyleBuilder {

// Add label to the mesh using the prepared label data
void addLabel(const TextStyle::Parameters& _params, Label::Type _type,
Label::WorldTransform _transform, const DrawRule& _rule);
Label::WorldTransform _transform, uint32_t _selectionColor);

void addLineTextLabels(const Feature& _feature, const TextStyle::Parameters& _params, const DrawRule& _rule);

Expand Down

0 comments on commit 4a842f4

Please sign in to comment.