From 33b40ca7af01d94f08dc85e0dbecb49ab18df749 Mon Sep 17 00:00:00 2001 From: Hannes Janetzek Date: Tue, 20 Dec 2016 17:54:59 +0100 Subject: [PATCH] add attribution overlay --- core/src/labels/label.h | 2 + core/src/style/textStyleBuilder.cpp | 4 +- core/src/tangram.cpp | 73 +++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/core/src/labels/label.h b/core/src/labels/label.h index 987bc97151..48233d9286 100644 --- a/core/src/labels/label.h +++ b/core/src/labels/label.h @@ -186,6 +186,8 @@ class Label { void setAlpha(float _alpha); + void setScreenPosition(glm::vec2 _pos) { m_screenTransform.position = _pos; } + private: virtual void applyAnchor(LabelProperty::Anchor _anchor) = 0; diff --git a/core/src/style/textStyleBuilder.cpp b/core/src/style/textStyleBuilder.cpp index c344065002..c33fed2d97 100644 --- a/core/src/style/textStyleBuilder.cpp +++ b/core/src/style/textStyleBuilder.cpp @@ -27,7 +27,9 @@ const static std::string key_name("name"); TextStyleBuilder::TextStyleBuilder(const TextStyle& _style) : StyleBuilder(_style), - m_style(_style) {} + m_style(_style) { + m_textLabels = std::make_unique(m_style); +} void TextStyleBuilder::setup(const Tile& _tile){ m_tileSize = _tile.getProjection()->TileSize(); diff --git a/core/src/tangram.cpp b/core/src/tangram.cpp index 5d9ed69e17..ce4924610a 100644 --- a/core/src/tangram.cpp +++ b/core/src/tangram.cpp @@ -5,6 +5,7 @@ #include "scene/sceneLoader.h" #include "style/material.h" #include "style/style.h" +#include "style/textStyleBuilder.h" #include "labels/labels.h" #include "text/fontContext.h" #include "tile/tileManager.h" @@ -93,6 +94,15 @@ class Map::Impl { std::vector featureSelectionQueries; std::vector labelSelectionQueries; + + struct { + std::unique_ptr mesh = nullptr; + std::unique_ptr style = nullptr; + bool ready = false;; + } overlay; + void initOverlay(Scene& scene); + void renderOverlay(); + }; void Map::Impl::setEase(EaseField _f, Ease _e) { @@ -124,6 +134,62 @@ Map::~Map() { Primitives::deinit(); } +void Map::Impl::initOverlay(Scene& scene) { + + overlay.mesh = nullptr; + overlay.style = nullptr; + + if (scene.fontContext()) { + overlay.style = std::make_unique("overlay", scene.fontContext(), true); + overlay.style->build(scene); + auto builder = overlay.style->createBuilder(); + + TextStyle::Parameters p; + p.font = scene.fontContext()->getFont("default", "normal", "400", 18); + p.text = "© Mapzen. © OpenStreetMap"; + p.strokeWidth = 4; + p.fontSize = 18; + p.fill = 0xff444444; + p.strokeColor = 0xaaffffff; + p.wordWrap = false; + p.labelOptions.anchors.anchor = { {LabelProperty::Anchor::top_right} }; + p.labelOptions.anchors.count = 1; + + TextStyleBuilder* b = static_cast(builder.get()); + b->prepareLabel(p, Label::Type::point); + b->addLabel(p, Label::Type::point, Label::WorldTransform(glm::vec3(0.f)), 0); + overlay.mesh = builder->build(); + + auto* mesh = dynamic_cast(overlay.mesh.get()); + if (!mesh || mesh->getLabels().empty()) { + LOGE("Failed to create overlay"); + overlay.mesh = nullptr; + overlay.style = nullptr; + } + + overlay.ready = false; + } +} + +void Map::Impl::renderOverlay() { + if (overlay.mesh) { + if (!overlay.ready) { + overlay.ready = true; + overlay.style->onBeginUpdate(); + + auto* mesh = dynamic_cast(overlay.mesh.get()); + auto& l = mesh->getLabels()[0]; + l->setScreenPosition({10, view.getHeight()}); + l->enterState(Label::State::visible); + l->evalState(0); + l->addVerticesToMesh(); + } + overlay.style->onBeginFrame(renderState); + overlay.style->onBeginDrawFrame(renderState, view, *scene); + overlay.style->onEndDrawFrame(); + } +} + void Map::Impl::setScene(std::shared_ptr& _scene) { { std::lock_guard lock(sceneMutex); @@ -181,6 +247,8 @@ void Map::Impl::setScene(std::shared_ptr& _scene) { if (animated != isContinuousRendering()) { setContinuousRendering(animated); } + + initOverlay(*scene); } // NB: Not thread-safe. Must be called on the main/render thread! @@ -316,6 +384,8 @@ void Map::resize(int _newWidth, int _newHeight) { impl->selectionBuffer = std::make_unique(_newWidth/2, _newHeight/2); + impl->overlay.ready = false; + Primitives::setResolution(impl->renderState, _newWidth, _newHeight); } @@ -556,6 +626,8 @@ void Map::render() { } } + impl->renderOverlay(); + impl->labels.drawDebug(impl->renderState, impl->view); FrameInfo::draw(impl->renderState, impl->view, impl->tileManager); @@ -900,6 +972,7 @@ void Map::setupGL() { // created, so we invalidate all data that depends on OpenGL object handles. impl->renderState.increaseGeneration(); impl->renderState.invalidate(); + impl->overlay.ready = false; // Set default primitive render color Primitives::setColor(impl->renderState, 0xffffff);