Skip to content

Commit

Permalink
use fixed array for Label::Options::anchors
Browse files Browse the repository at this point in the history
  • Loading branch information
hjanetzek committed Jul 22, 2016
1 parent 91a721d commit ae36dc7
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 76 deletions.
52 changes: 21 additions & 31 deletions core/src/labels/label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ namespace Tangram {

const float Label::activation_distance_threshold = 2;

Label::Label(Label::Transform _transform, glm::vec2 _size, Type _type, Options _options, LabelProperty::Anchor _anchor)
Label::Label(Label::Transform _transform, glm::vec2 _size, Type _type, Options _options)
: m_type(_type),
m_transform(_transform),
m_dim(_size),
m_options(_options),
m_anchorType(_anchor) {
m_options(_options) {

if (!m_options.collide || m_type == Type::debug){
enterState(State::visible, 1.0);
Expand All @@ -26,7 +25,7 @@ Label::Label(Label::Transform _transform, glm::vec2 _size, Type _type, Options _
m_occludedLastFrame = false;
m_occluded = false;
m_parent = nullptr;
m_anchorFallbackIndex = 0;
m_anchorIndex = 0;
}

Label::~Label() {}
Expand Down Expand Up @@ -107,7 +106,7 @@ void Label::alignFromParent(const Label& _parent) {
glm::vec2 anchorDir = LabelProperty::anchorDirection(_parent.anchorType());
glm::vec2 anchorOrigin = anchorDir * _parent.dimension() * 0.5f;

applyAnchor(m_dim + _parent.dimension(), anchorOrigin, m_anchorType);
applyAnchor(m_dim + _parent.dimension(), anchorOrigin, m_options.anchors[m_anchorIndex]);
m_options.offset += _parent.options().offset;
}

Expand Down Expand Up @@ -176,7 +175,7 @@ void Label::enterState(const State& _state, float _alpha) {
setAlpha(_alpha);

// Reset anchor fallback index
m_anchorFallbackIndex = 0;
m_anchorIndex = 0;
}

void Label::setAlpha(float _alpha) {
Expand Down Expand Up @@ -209,7 +208,7 @@ void Label::print() const {
case State::out_of_screen: state = "out_of_screen"; break;
}
LOG("\tm_state: %s", state.c_str());
LOG("\tm_options.anchorFallback: %s", m_options.anchorFallbacks.to_string().c_str());
//LOG("\tm_options.anchorFallback: %s", m_options.anchorFallbacks.to_string().c_str());
}

bool Label::update(const glm::mat4& _mvp, const glm::vec2& _screenSize, float _zoomFract, bool _drawAllLabels) {
Expand Down Expand Up @@ -268,7 +267,7 @@ bool Label::evalState(const glm::vec2& _screenSize, float _dt) {
switch (m_state) {
case State::visible:
if (m_occluded || m_occludedLastFrame) {
if (m_options.anchorFallbacks != 0) {
if (m_options.anchorCount > 1) {
enterState(State::anchor_fallback, 0.0);
} else {
m_fade.reset(false, m_options.hideTransition.ease,
Expand All @@ -282,28 +281,19 @@ bool Label::evalState(const glm::vec2& _screenSize, float _dt) {
break;
case State::anchor_fallback:
if (m_occluded) {
if (m_anchorFallbackIndex >= m_options.anchorFallbacks.size()) {
if (m_anchorIndex >= int(m_options.anchorCount)-1) {
m_fade.reset(false, m_options.hideTransition.ease,
m_options.hideTransition.time);

enterState(State::fading_out, m_transform.state.alpha);
} else {
// Find next valid anchor
for (; m_anchorFallbackIndex < m_options.anchorFallbacks.size(); ++m_anchorFallbackIndex) {
if (m_options.anchorFallbacks.test(m_anchorFallbackIndex)) {
m_anchorType = (LabelProperty::Anchor) m_anchorFallbackIndex;

// Apply new alignment
if (m_parent) {
alignFromParent(*m_parent);
} else {
applyAnchor(m_dim, glm::vec2(0.0), m_anchorType);
}

// Move to next one for upcoming frame
m_anchorFallbackIndex++;
break;
}
// Move to next one for upcoming frame
m_anchorIndex++;
// Apply new alignment
if (m_parent) {
alignFromParent(*m_parent);
} else {
applyAnchor(m_dim, glm::vec2(0.0), m_options.anchors[m_anchorIndex]);
}
}
} else {
Expand Down Expand Up @@ -339,12 +329,12 @@ bool Label::evalState(const glm::vec2& _screenSize, float _dt) {
break;
case State::none:
if (m_occluded) {
if (m_options.anchorFallbacks != 0) {
enterState(State::anchor_fallback, 0.0);
animate = true;
} else {
enterState(State::sleep, 0.0);
}
// if (m_options.anchorCount > 0) {
// enterState(State::anchor_fallback, 0.0);
// animate = true;
// } else {
enterState(State::sleep, 0.0);
// }
} else {
m_fade.reset(true, m_options.showTransition.ease,
m_options.showTransition.time);
Expand Down
13 changes: 7 additions & 6 deletions core/src/labels/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ class Label {
// the label hash based on its styling parameters
size_t paramHash = 0;

std::bitset<9> anchorFallbacks = 0;
static const int max_anchors = 9;
std::array<LabelProperty::Anchor, max_anchors> anchors;
int anchorCount = 0;
};

static const float activation_distance_threshold;

Label(Transform _transform, glm::vec2 _size, Type _type, Options _options, LabelProperty::Anchor _anchor);
Label(Transform _transform, glm::vec2 _size, Type _type, Options _options);

virtual ~Label();

Expand Down Expand Up @@ -130,7 +132,7 @@ class Label {

void alignFromParent(const Label& _parent);

LabelProperty::Anchor anchorType() const { return m_anchorType; }
LabelProperty::Anchor anchorType() const { return m_options.anchors[0]; }

virtual glm::vec2 center() const;

Expand All @@ -143,7 +145,7 @@ class Label {
private:

virtual void applyAnchor(const glm::vec2& _dimension, const glm::vec2& _origin,
LabelProperty::Anchor _anchor) = 0;
LabelProperty::Anchor _anchor) = 0;

bool offViewport(const glm::vec2& _screenSize);

Expand All @@ -154,7 +156,7 @@ class Label {
// the label fade effect
FadeEffect m_fade;

int m_anchorFallbackIndex;
int m_anchorIndex;

protected:

Expand All @@ -173,7 +175,6 @@ class Label {
// label options
Options m_options;

LabelProperty::Anchor m_anchorType;
glm::vec2 m_anchor;

const Label* m_parent;
Expand Down
2 changes: 1 addition & 1 deletion core/src/labels/labelProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Tangram {

namespace LabelProperty {

enum Anchor {
enum Anchor : uint8_t {
center = 0,
top,
bottom,
Expand Down
2 changes: 1 addition & 1 deletion core/src/labels/labels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ void Labels::drawDebug(const View& _view) {
Primitives::setColor(0x0000ff);
Primitives::drawRect(sp - glm::vec2(1.f), sp + glm::vec2(1.f));

if (label->options().anchorFallbacks != 0) {
if (label->options().anchorCount > 1) {
Primitives::setColor(0xffffff);
Primitives::drawPoly(&(label->obb().getQuad())[0], 4);
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/labels/spriteLabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ const float SpriteVertex::alpha_scale = 65535.0f;
const float SpriteVertex::texture_scale = 65535.0f;

SpriteLabel::SpriteLabel(Label::Transform _transform, glm::vec2 _size, Label::Options _options,
float _extrudeScale, LabelProperty::Anchor _anchor,
SpriteLabels& _labels, size_t _labelsPos)
: Label(_transform, _size, Label::Type::point, _options, _anchor),
float _extrudeScale, SpriteLabels& _labels, size_t _labelsPos)
: Label(_transform, _size, Label::Type::point, _options),
m_labels(_labels),
m_labelsPos(_labelsPos),
m_extrudeScale(_extrudeScale) {

applyAnchor(m_dim, glm::vec2(0.0), _anchor);
applyAnchor(m_dim, glm::vec2(0.0), m_options.anchors[0]);
}

void SpriteLabel::applyAnchor(const glm::vec2& _dimension, const glm::vec2& _origin,
LabelProperty::Anchor _anchor) {

// _dimension is not applied to the sprite anchor since fractionnal zoom
// level would result in scaling the sprite size dynamically, instead we
// store a factor between 0..1 to scale the sprite accordingly
Expand Down
5 changes: 2 additions & 3 deletions core/src/labels/spriteLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class SpriteLabel : public Label {
public:

SpriteLabel(Label::Transform _transform, glm::vec2 _size, Label::Options _options,
float _extrudeScale, LabelProperty::Anchor _anchor,
SpriteLabels& _labels, size_t _labelsPos);
float _extrudeScale, SpriteLabels& _labels, size_t _labelsPos);

void updateBBoxes(float _zoomFract) override;

Expand All @@ -36,7 +35,7 @@ class SpriteLabel : public Label {
private:

void applyAnchor(const glm::vec2& _dimension, const glm::vec2& _origin,
LabelProperty::Anchor _anchor) override;
LabelProperty::Anchor _anchor) override;

// Back-pointer to owning container and position
const SpriteLabels& m_labels;
Expand Down
6 changes: 3 additions & 3 deletions core/src/labels/textLabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ const float TextVertex::position_scale = 4.0f;
const float TextVertex::alpha_scale = 65535.0f;

TextLabel::TextLabel(Label::Transform _transform, Type _type, Label::Options _options,
Anchor _anchor, TextLabel::FontVertexAttributes _attrib,
TextLabel::FontVertexAttributes _attrib,
glm::vec2 _dim, TextLabels& _labels, std::vector<TextRange> _textRanges,
Align _preferedAlignment)
: Label(_transform, _dim, _type, _options, _anchor),
: Label(_transform, _dim, _type, _options),
m_textLabels(_labels),
m_textRanges(_textRanges),
m_fontAttrib(_attrib),
m_preferedAlignment(_preferedAlignment) {

applyAnchor(_dim, glm::vec2(0.0), _anchor);
applyAnchor(_dim, glm::vec2(0.0), m_options.anchors[0]);
m_textRangeIndex = 0;
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/labels/textLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TextLabel : public Label {
};

TextLabel(Label::Transform _transform, Type _type, Label::Options _options,
LabelProperty::Anchor _anchor, TextLabel::FontVertexAttributes _attrib,
TextLabel::FontVertexAttributes _attrib,
glm::vec2 _dim, TextLabels& _labels, std::vector<TextRange> _textRanges,
TextLabelProperty::Align _preferedAlignment);

Expand Down
4 changes: 3 additions & 1 deletion core/src/style/pointStyleBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ auto PointStyleBuilder::applyRule(const DrawRule& _rule, const Properties& _prop

LabelProperty::anchor(anchor, p.anchor);

p.labelOptions.anchors[0] = p.anchor;
p.labelOptions.anchorCount = 1;

if (p.labelOptions.interactive) {
p.labelOptions.properties = std::make_shared<Properties>(_props);
}
Expand All @@ -153,7 +156,6 @@ void PointStyleBuilder::addLabel(const Point& _point, const glm::vec4& _quad,
_params.size,
_params.labelOptions,
_params.extrudeScale,
_params.anchor,
*m_spriteLabels,
m_quads.size()));

Expand Down
19 changes: 9 additions & 10 deletions core/src/style/textStyleBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,17 @@ TextStyle::Parameters TextStyleBuilder::applyRule(const DrawRule& _rule,
a.erase(std::remove(a.begin(), a.end(), ' '), a.end());
std::vector<std::string> anchors = splitString(a, ',');

if (anchors.size() > 1) {
for (size_t i = 0; i < anchors.size(); ++i) {
LabelProperty::Anchor labelAnchor;
if (LabelProperty::anchor(anchors[i], labelAnchor)) {
p.labelOptions.anchorFallbacks.set(labelAnchor);
} else {
LOG("Invalid anchor %s", anchors[i].c_str());
}
for (size_t i = 0; i < anchors.size() && i < Label::Options::max_anchors; ++i) {
LabelProperty::Anchor labelAnchor;
if (LabelProperty::anchor(anchors[i], labelAnchor)) {
p.labelOptions.anchors[i] = labelAnchor;
p.labelOptions.anchorCount++;
//p.labelOptions.anchorFallbacks.set(labelAnchor);
} else {
LOG("Invalid anchor %s", anchors[i].c_str());
}
}

LabelProperty::anchor(anchors[0], p.anchor);
}

if (auto* transform = _rule.get<std::string>(StyleParamKey::text_transform)) {
Expand Down Expand Up @@ -525,7 +524,7 @@ void TextStyleBuilder::addLabel(const TextStyle::Parameters& _params, Label::Typ
// int quadsStart = m_attributes.quadsStart;
// int quadsCount = m_quads.size() - quadsStart;

m_labels.emplace_back(new TextLabel(_transform, _type, _params.labelOptions, _params.anchor,
m_labels.emplace_back(new TextLabel(_transform, _type, _params.labelOptions,
{m_attributes.fill, m_attributes.stroke, m_attributes.fontScale},
{m_attributes.width, m_attributes.height},
*m_textLabels, std::move(m_attributes.textRanges),
Expand Down
11 changes: 5 additions & 6 deletions core/src/text/fontContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,10 @@ bool FontContext::layoutText(TextStyle::Parameters& _params, const std::string&

// Collect possible alignment from anchor fallbacks
std::vector<TextLabelProperty::Align> alignments;
for (int i = 0; i < _params.labelOptions.anchorFallbacks.size(); ++i) {
if (!_params.labelOptions.anchorFallbacks[i]) {
continue;
}
TextLabelProperty::Align alignmentFallback = TextLabelProperty::alignFromAnchor((LabelProperty::Anchor)i);
for (int i = 0; i < _params.labelOptions.anchorCount; ++i) {
// TODO only need center/right/left align once!
TextLabelProperty::Align alignmentFallback =
TextLabelProperty::alignFromAnchor(_params.labelOptions.anchors[i]);
alignments.push_back(alignmentFallback);
}

Expand All @@ -217,7 +216,7 @@ bool FontContext::layoutText(TextStyle::Parameters& _params, const std::string&

if (_params.wordWrap) {
m_textWrapper.clearWraps();

float width = m_textWrapper.getShapeRangeWidth(line, MIN_LINE_WIDTH, _params.maxLineWidth);

for (auto& align : alignments) {
Expand Down
19 changes: 11 additions & 8 deletions tests/unit/labelTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,34 @@ TextLabels dummy(dummyStyle);
TextLabel makeLabel(Label::Transform _transform, Label::Type _type) {
Label::Options options;
options.offset = {0.0f, 0.0f};
options.anchors[0] = LabelProperty::Anchor::center;
options.anchorCount = 1;

std::vector<TextRange> textRanges;
textRanges.push_back({TextLabelProperty::Align::none, {}});

return TextLabel(_transform, _type, options,
LabelProperty::Anchor::center, {}, {0, 0}, dummy, textRanges,
{}, {0, 0}, dummy, textRanges,
TextLabelProperty::Align::none);
}

TextLabel makeLabelWithAnchorFallbacks() {
Label::Options options;

options.anchorFallbacks.set(LabelProperty::Anchor::right);
options.anchorFallbacks.set(LabelProperty::Anchor::bottom);
options.anchorFallbacks.set(LabelProperty::Anchor::left);
options.anchorFallbacks.set(LabelProperty::Anchor::top);
options.anchors[0] = LabelProperty::Anchor::center;
options.anchors[1] = LabelProperty::Anchor::right;
options.anchors[2] = LabelProperty::Anchor::bottom;
options.anchors[3] = LabelProperty::Anchor::left;
options.anchors[4] = LabelProperty::Anchor::top;
options.anchorCount = 5;

options.offset = {0.0f, 0.0f};

std::vector<TextRange> textRanges;

textRanges.push_back({TextLabelProperty::Align::none, {}});
return TextLabel({screenSize/2.f}, Label::Type::point, options,
LabelProperty::Anchor::right, {}, {0, 0},
dummy, textRanges, TextLabelProperty::Align::none);
{}, {0, 0}, dummy, textRanges, TextLabelProperty::Align::none);
}

TEST_CASE( "Ensure the transition from wait -> sleep when occlusion happens", "[Core][Label]" ) {
Expand Down Expand Up @@ -272,4 +276,3 @@ TEST_CASE( "Ensure anchor fallback behavior when looping over all fallback and f
REQUIRE(l.state() == Label::State::visible);
}
#endif

5 changes: 4 additions & 1 deletion tests/unit/labelsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ std::unique_ptr<TextLabel> makeLabel(Label::Transform _transform, Label::Type _t
options.properties = std::make_shared<Properties>();
options.properties->set("id", id);
options.interactive = true;
options.anchors[0] = LabelProperty::Anchor::center;
options.anchorCount = 1;

std::vector<TextRange> textRanges;
textRanges.push_back({TextLabelProperty::Align::none, {}});

return std::unique_ptr<TextLabel>(new TextLabel(_transform, _type, options,
LabelProperty::Anchor::center, {}, {10, 10}, dummy, textRanges,
{}, {10, 10}, dummy, textRanges,
TextLabelProperty::Align::none));
}

Expand Down

0 comments on commit ae36dc7

Please sign in to comment.