Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ class Map : private util::noncopyable {
void addLayer(std::unique_ptr<style::Layer>, const optional<std::string>& beforeLayerID = {});
void removeLayer(const std::string& layerID);

// Defaults
std::string getStyleName() const;
LatLng getDefaultLatLng() const;
double getDefaultZoom() const;
double getDefaultBearing() const;
double getDefaultPitch() const;

// Feature queries
std::vector<Feature> queryRenderedFeatures(const ScreenCoordinate&, const optional<std::vector<std::string>>& layerIDs = {});
std::vector<Feature> queryRenderedFeatures(const ScreenBox&, const optional<std::vector<std::string>>& layerIDs = {});
Expand Down
22 changes: 22 additions & 0 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,28 @@ void Map::removeLayer(const std::string& id) {
impl->view.deactivate();
}

#pragma mark - Defaults

std::string Map::getStyleName() const {
return impl->style->getName();
}

LatLng Map::getDefaultLatLng() const {
return impl->style->getDefaultLatLng();
}

double Map::getDefaultZoom() const {
return impl->style->getDefaultZoom();
}

double Map::getDefaultBearing() const {
return impl->style->getDefaultBearing();
}

double Map::getDefaultPitch() const {
return impl->style->getDefaultPitch();
}

#pragma mark - Toggles

void Map::setDebug(MapDebugOptions debugOptions) {
Expand Down
37 changes: 37 additions & 0 deletions src/mbgl/style/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,43 @@ void Parser::parse(const std::string& json) {
}
}

if (document.HasMember("name")) {
const JSValue& value = document["name"];
if (value.IsString()) {
name = { value.GetString(), value.GetStringLength() };
}
}

if (document.HasMember("center")) {
const JSValue& value = document["center"];
if (value.IsArray() && value.Size() >= 2) {
// Style spec uses lon/lat order
latLng.longitude = value[0].IsNumber() ? value[0].GetDouble() : 0;
latLng.latitude = value[1].IsNumber() ? value[1].GetDouble() : 0;
}
}

if (document.HasMember("zoom")) {
const JSValue& value = document["zoom"];
if (value.IsNumber()) {
zoom = value.GetDouble();
}
}

if (document.HasMember("bearing")) {
const JSValue& value = document["bearing"];
if (value.IsNumber()) {
bearing = value.GetDouble();
}
}

if (document.HasMember("pitch")) {
const JSValue& value = document["pitch"];
if (value.IsNumber()) {
pitch = value.GetDouble();
}
}

if (document.HasMember("sources")) {
parseSources(document["sources"]);
}
Expand Down
7 changes: 7 additions & 0 deletions src/mbgl/style/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <mbgl/util/rapidjson.hpp>
#include <mbgl/util/font_stack.hpp>
#include <mbgl/util/geo.hpp>

#include <vector>
#include <memory>
Expand All @@ -27,6 +28,12 @@ class Parser {
std::vector<std::unique_ptr<Source>> sources;
std::vector<std::unique_ptr<Layer>> layers;

std::string name;
LatLng latLng;
double zoom = 0;
double bearing = 0;
double pitch = 0;

// Statically evaluate layer properties to determine what font stacks are used.
std::vector<FontStack> fontStacks() const;

Expand Down
26 changes: 26 additions & 0 deletions src/mbgl/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ void Style::setJSON(const std::string& json) {
addLayer(std::move(layer));
}

name = parser.name;
defaultLatLng = parser.latLng;
defaultZoom = parser.zoom;
defaultBearing = parser.bearing;
defaultPitch = parser.pitch;

glyphStore->setURL(parser.glyphURL);
spriteStore->load(parser.spriteURL, fileSource);

Expand Down Expand Up @@ -163,6 +169,26 @@ void Style::removeLayer(const std::string& id) {
layers.erase(it);
}

std::string Style::getName() const {
return name;
}

LatLng Style::getDefaultLatLng() const {
return defaultLatLng;
}

double Style::getDefaultZoom() const {
return defaultZoom;
}

double Style::getDefaultBearing() const {
return defaultBearing;
}

double Style::getDefaultPitch() const {
return defaultPitch;
}

void Style::update(const UpdateParameters& parameters) {
bool allTilesUpdated = true;

Expand Down
14 changes: 14 additions & 0 deletions src/mbgl/style/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <mbgl/util/worker.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/geo.hpp>

#include <cstdint>
#include <string>
Expand Down Expand Up @@ -68,6 +69,12 @@ class Style : public GlyphStoreObserver,
optional<std::string> beforeLayerID = {});
void removeLayer(const std::string& layerID);

std::string getName() const;
LatLng getDefaultLatLng() const;
double getDefaultZoom() const;
double getDefaultBearing() const;
double getDefaultPitch() const;

bool addClass(const std::string&, const TransitionOptions& = {});
bool removeClass(const std::string&, const TransitionOptions& = {});
bool hasClass(const std::string&) const;
Expand Down Expand Up @@ -98,6 +105,13 @@ class Style : public GlyphStoreObserver,
std::vector<std::string> classes;
optional<TransitionOptions> transitionProperties;

// Defaults
std::string name;
LatLng defaultLatLng;
double defaultZoom;
double defaultBearing;
double defaultPitch;

std::vector<std::unique_ptr<Layer>>::const_iterator findLayer(const std::string& layerID) const;

// GlyphStoreObserver implementation.
Expand Down
34 changes: 34 additions & 0 deletions test/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,37 @@ TEST(Style, UnusedSourceActiveViaClassUpdate) {
EXPECT_TRUE(unusedSource);
EXPECT_FALSE(unusedSource->baseImpl->isLoaded());
}

TEST(Style, Properties) {
util::RunLoop loop;

StubFileSource fileSource;
Style style { fileSource, 1.0 };

style.setJSON(R"STYLE({"name": "Test"})STYLE");
ASSERT_EQ("Test", style.getName());

style.setJSON(R"STYLE({"center": [10, 20]})STYLE");
ASSERT_EQ("", style.getName());
ASSERT_EQ((LatLng{20, 10}), style.getDefaultLatLng());

style.setJSON(R"STYLE({"bearing": 24})STYLE");
ASSERT_EQ("", style.getName());
ASSERT_EQ((LatLng{0, 0}), style.getDefaultLatLng());
ASSERT_EQ(24, style.getDefaultBearing());

style.setJSON(R"STYLE({"zoom": 13.3})STYLE");
ASSERT_EQ("", style.getName());
ASSERT_EQ(13.3, style.getDefaultZoom());

style.setJSON(R"STYLE({"pitch": 60})STYLE");
ASSERT_EQ("", style.getName());
ASSERT_EQ(60, style.getDefaultPitch());

style.setJSON(R"STYLE({"name": 23, "center": {}, "bearing": "north", "zoom": null, "pitch": "wide"})STYLE");
ASSERT_EQ("", style.getName());
ASSERT_EQ((LatLng{0, 0}), style.getDefaultLatLng());
ASSERT_EQ(0, style.getDefaultBearing());
ASSERT_EQ(0, style.getDefaultZoom());
ASSERT_EQ(0, style.getDefaultPitch());
}