diff --git a/include/mbgl/map/source.hpp b/include/mbgl/map/source.hpp index be5fad3af..95a141ff5 100644 --- a/include/mbgl/map/source.hpp +++ b/include/mbgl/map/source.hpp @@ -23,10 +23,7 @@ class Texturepool; class Source : public std::enable_shared_from_this, private util::noncopyable { public: - Source(StyleSource style_source, const std::string &access_token = ""); - Source(SourceType type = SourceType::Vector, const std::string &url = "", - uint32_t tile_size = 512, uint32_t min_zoom = 0, uint32_t max_zoom = 22, - const std::string &access_token = ""); + Source(SourceInfo info, const std::string &access_token = ""); bool update(Map &map); void updateMatrices(const mat4 &projMatrix, const TransformState &transform); @@ -41,11 +38,7 @@ public: static std::string normalizeSourceURL(const std::string &url, const std::string &access_token); public: - const SourceType type; - const std::string url; - const uint32_t tile_size; - const int32_t min_zoom; - const int32_t max_zoom; + const SourceInfo info; private: bool findLoadedChildren(const Tile::ID& id, int32_t maxCoveringZoom, std::forward_list& retain); diff --git a/include/mbgl/style/style_source.hpp b/include/mbgl/style/style_source.hpp index 6b86c3090..fa5c8dfb3 100644 --- a/include/mbgl/style/style_source.hpp +++ b/include/mbgl/style/style_source.hpp @@ -9,7 +9,7 @@ namespace mbgl { class Source; -class StyleSource { +class SourceInfo { public: const SourceType type; const std::string url; @@ -17,13 +17,21 @@ public: const int32_t min_zoom; const int32_t max_zoom; + SourceInfo(SourceType type = SourceType::Vector, const std::string &url = "", + uint32_t tile_size = 512, int32_t min_zoom = 0, int32_t max_zoom = 22) + : type(type), url(url), tile_size(tile_size), min_zoom(min_zoom), max_zoom(max_zoom) {} +}; + + +class StyleSource { +public: + const SourceInfo info; + bool enabled = false; std::shared_ptr source; - StyleSource(SourceType type = SourceType::Vector, const std::string &url = "", - uint32_t tile_size = 512, uint32_t min_zoom = 0, uint32_t max_zoom = 22) - : type(type), url(url), tile_size(tile_size), min_zoom(min_zoom), max_zoom(max_zoom) {} -}; + StyleSource(const SourceInfo &info) : info(info) {} }; +} #endif diff --git a/src/map/map.cpp b/src/map/map.cpp index c7039cc92..88fe8cf85 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -421,13 +421,13 @@ void Map::updateSources() { updateSources(style->layers); // Then, construct or destroy the actual source object, depending on enabled state. - for (const std::shared_ptr &source : activeSources) { - if (source->enabled) { - if (!source->source) { - source->source = std::make_shared(*source, getAccessToken()); + for (const std::shared_ptr &style_source : activeSources) { + if (style_source->enabled) { + if (!style_source->source) { + style_source->source = std::make_shared(style_source->info, getAccessToken()); } } else { - source->source.reset(); + style_source->source.reset(); } } diff --git a/src/map/source.cpp b/src/map/source.cpp index e46bb0abd..b885ee764 100644 --- a/src/map/source.cpp +++ b/src/map/source.cpp @@ -18,21 +18,14 @@ namespace mbgl { -Source::Source(StyleSource style_source, const std::string &access_token) - : type(style_source.type), - url(normalizeSourceURL(style_source.url, access_token)), - tile_size(style_source.tile_size), - min_zoom(style_source.min_zoom), - max_zoom(style_source.max_zoom) {} - -Source::Source(SourceType type, const std::string &url, - uint32_t tile_size, uint32_t min_zoom, uint32_t max_zoom, - const std::string &access_token) - : type(type), - url(normalizeSourceURL(url, access_token)), - tile_size(tile_size), - min_zoom(min_zoom), - max_zoom(max_zoom) {} +Source::Source(SourceInfo info, const std::string &access_token) + : info( + info.type, + normalizeSourceURL(info.url, access_token), + info.tile_size, + info.min_zoom, + info.max_zoom + ) {} std::string Source::normalizeSourceURL(const std::string &url, const std::string &access_token) { const std::string t = "mapbox://"; @@ -153,7 +146,7 @@ TileData::State Source::addTile(Map &map, const Tile::ID& id) { if (!new_tile.data) { // If we don't find working tile data, we're just going to load it. - const std::string formed_url = util::replaceTokens(url, [&](const std::string &token) -> std::string { + const std::string formed_url = util::replaceTokens(info.url, [&](const std::string &token) -> std::string { if (token == "z") return std::to_string(normalized_id.z); if (token == "x") return std::to_string(normalized_id.x); if (token == "y") return std::to_string(normalized_id.y); @@ -161,9 +154,9 @@ TileData::State Source::addTile(Map &map, const Tile::ID& id) { return ""; }); - if (type == SourceType::Vector) { + if (info.type == SourceType::Vector) { new_tile.data = std::make_shared(normalized_id, map, formed_url); - } else if (type == SourceType::Raster) { + } else if (info.type == SourceType::Raster) { new_tile.data = std::make_shared(normalized_id, map, formed_url); } else { throw std::runtime_error("source type not implemented"); @@ -232,14 +225,14 @@ bool Source::updateTiles(Map &map) { // Figure out what tiles we need to load int32_t clamped_zoom = map.getState().getIntegerZoom(); - if (clamped_zoom > max_zoom) clamped_zoom = max_zoom; - if (clamped_zoom < min_zoom) clamped_zoom = min_zoom; + if (clamped_zoom > info.max_zoom) clamped_zoom = info.max_zoom; + if (clamped_zoom < info.min_zoom) clamped_zoom = info.min_zoom; int32_t max_covering_zoom = clamped_zoom + 1; - if (max_covering_zoom > max_zoom) max_covering_zoom = max_zoom; + if (max_covering_zoom > info.max_zoom) max_covering_zoom = info.max_zoom; int32_t min_covering_zoom = clamped_zoom - 10; - if (min_covering_zoom < min_zoom) min_covering_zoom = min_zoom; + if (min_covering_zoom < info.min_zoom) min_covering_zoom = info.min_zoom; // Map four viewport corners to pixel coordinates box box = map.getState().cornersToBox(clamped_zoom); @@ -379,7 +372,7 @@ void _scanTriangle(const mbgl::vec2 a, const mbgl::vec2 b, const } double Source::getZoom(const TransformState &state) const { - double offset = log(util::tileSize / tile_size) / log(2); + double offset = log(util::tileSize / info.tile_size) / log(2); offset += (state.getPixelRatio() > 1.0 ? 1 :0); return state.getZoom() + offset; } @@ -387,7 +380,7 @@ double Source::getZoom(const TransformState &state) const { std::forward_list Source::covering_tiles(const TransformState &state, int32_t clamped_zoom, const box& points) { int32_t dim = std::pow(2, clamped_zoom); std::forward_list tiles; - bool is_raster = (type == SourceType::Raster); + bool is_raster = (info.type == SourceType::Raster); double search_zoom = getZoom(state); auto scanLine = [&tiles, clamped_zoom, is_raster, search_zoom](int32_t x0, int32_t x1, int32_t y, int32_t ymax) { diff --git a/src/style/style_parser.cpp b/src/style/style_parser.cpp index c037e5172..5b0bf6f68 100644 --- a/src/style/style_parser.cpp +++ b/src/style/style_parser.cpp @@ -175,7 +175,7 @@ void StyleParser::parseSources(JSVal value) { parseRenderProperty(itr->value, min_zoom, "minZoom"); parseRenderProperty(itr->value, max_zoom, "maxZoom"); - sources.emplace(std::move(name), std::make_shared(type, url, tile_size, min_zoom, max_zoom)); + sources.emplace(std::move(name), std::make_shared(SourceInfo { type, url, tile_size, min_zoom, max_zoom })); } } else { throw Style::exception("sources must be an object");