Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Move setStyleJSON/URL to Style; add Map::setStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jun 22, 2017
1 parent 37f77c9 commit 19fb81c
Show file tree
Hide file tree
Showing 25 changed files with 219 additions and 207 deletions.
2 changes: 1 addition & 1 deletion benchmark/api/query.benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class QueryBenchmark {
NetworkStatus::Set(NetworkStatus::Status::Offline);
fileSource.setAccessToken("foobar");

map.setStyleJSON(util::read_file("benchmark/fixtures/api/query_style.json"));
map.getStyle().loadJSON(util::read_file("benchmark/fixtures/api/query_style.json"));
map.setLatLngZoom({ 40.726989, -73.992857 }, 15); // Manhattan
map.getStyle().addImage(std::make_unique<style::Image>("test-icon",
decodeImage(util::read_file("benchmark/fixtures/api/default_marker.png")), 1.0));
Expand Down
3 changes: 2 additions & 1 deletion bin/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <mbgl/gl/offscreen_view.hpp>
#include <mbgl/util/default_thread_pool.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/style/style.hpp>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
Expand Down Expand Up @@ -93,7 +94,7 @@ int main(int argc, char *argv[]) {
style_path = std::string("file://") + style_path;
}

map.setStyleURL(style_path);
map.getStyle().loadURL(style_path);
map.setLatLngZoom({ lat, lon }, zoom);
map.setBearing(bearing);
map.setPitch(pitch);
Expand Down
8 changes: 2 additions & 6 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,11 @@ class Map : private util::noncopyable {
// Main render function.
void render(View&);

// Style
void setStyleURL(const std::string&);
void setStyleJSON(const std::string&);
std::string getStyleURL() const;
std::string getStyleJSON() const;

style::Style& getStyle();
const style::Style& getStyle() const;

void setStyle(std::unique_ptr<style::Style>);

// Transition
void cancelTransitions();
void setGestureInProgress(bool);
Expand Down
6 changes: 6 additions & 0 deletions include/mbgl/style/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class Style {
Style(Scheduler&, FileSource&, float pixelRatio);
~Style();

void loadJSON(const std::string&);
void loadURL(const std::string&);

std::string getJSON() const;
std::string getURL() const;

// Defaults
std::string getName() const;
LatLng getDefaultLatLng() const;
Expand Down
8 changes: 4 additions & 4 deletions platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,19 +344,19 @@ void NativeMapView::resizeFramebuffer(jni::JNIEnv&, int w, int h) {
}

jni::String NativeMapView::getStyleUrl(jni::JNIEnv& env) {
return jni::Make<jni::String>(env, map->getStyleURL());
return jni::Make<jni::String>(env, map->getStyle().getURL());
}

void NativeMapView::setStyleUrl(jni::JNIEnv& env, jni::String url) {
map->setStyleURL(jni::Make<std::string>(env, url));
map->getStyle().loadURL(jni::Make<std::string>(env, url));
}

jni::String NativeMapView::getStyleJson(jni::JNIEnv& env) {
return jni::Make<jni::String>(env, map->getStyleJSON());
return jni::Make<jni::String>(env, map->getStyle().getJSON());
}

void NativeMapView::setStyleJson(jni::JNIEnv& env, jni::String json) {
map->setStyleJSON(jni::Make<std::string>(env, json));
map->getStyle().loadJSON(jni::Make<std::string>(env, json));
}

void NativeMapView::setLatLngBounds(jni::JNIEnv& env, jni::Object<mbgl::android::LatLngBounds> jBounds) {
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/MGLStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ - (instancetype)initWithRawStyle:(mbgl::style::Style *)rawStyle mapView:(MGLMapV
}

- (NSURL *)URL {
return [NSURL URLWithString:@(self.mapView.mbglMap->getStyleURL().c_str())];
return [NSURL URLWithString:@(self.rawStyle->getURL().c_str())];
}

- (NSString *)name {
Expand Down
5 changes: 3 additions & 2 deletions platform/glfw/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <mbgl/util/platform.hpp>
#include <mbgl/util/default_thread_pool.hpp>
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/style/style.hpp>

#include <csignal>
#include <getopt.h>
Expand Down Expand Up @@ -147,7 +148,7 @@ int main(int argc, char *argv[]) {
}

mbgl::util::default_styles::DefaultStyle newStyle = mbgl::util::default_styles::orderedStyles[currentStyleIndex];
map.setStyleURL(newStyle.url);
map.getStyle().loadURL(newStyle.url);
view->setWindowTitle(newStyle.name);

mbgl::Log::Info(mbgl::Event::Setup, "Changed style to: %s", newStyle.name);
Expand Down Expand Up @@ -178,7 +179,7 @@ int main(int argc, char *argv[]) {
}
}

map.setStyleURL(style);
map.getStyle().loadURL(style);

view->run();

Expand Down
6 changes: 3 additions & 3 deletions platform/ios/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ + (void)initialize

- (nonnull NSURL *)styleURL
{
NSString *styleURLString = @(_mbglMap->getStyleURL().c_str()).mgl_stringOrNilIfEmpty;
NSString *styleURLString = @(_mbglMap->getStyle().getURL().c_str()).mgl_stringOrNilIfEmpty;
NSAssert(styleURLString || _isTargetingInterfaceBuilder, @"Invalid style URL string %@", styleURLString);
return styleURLString ? [NSURL URLWithString:styleURLString] : nil;
}
Expand All @@ -389,12 +389,12 @@ - (void)setStyleURL:(nullable NSURL *)styleURL

styleURL = styleURL.mgl_URLByStandardizingScheme;
self.style = nil;
_mbglMap->setStyleURL([[styleURL absoluteString] UTF8String]);
_mbglMap->getStyle().loadURL([[styleURL absoluteString] UTF8String]);
}

- (IBAction)reloadStyle:(__unused id)sender {
NSURL *styleURL = self.styleURL;
_mbglMap->setStyleURL("");
_mbglMap->getStyle().loadURL("");
self.styleURL = styleURL;
}

Expand Down
9 changes: 5 additions & 4 deletions platform/macos/src/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#import <mbgl/map/map.hpp>
#import <mbgl/map/view.hpp>
#import <mbgl/style/style.hpp>
#import <mbgl/annotation/annotation.hpp>
#import <mbgl/map/camera.hpp>
#import <mbgl/storage/reachability.h>
Expand Down Expand Up @@ -239,7 +240,7 @@ - (void)awakeFromNib {

// If the Style URL inspectable was not set, make sure to go through
// -setStyleURL: to load the default style.
if (_mbglMap->getStyleURL().empty()) {
if (_mbglMap->getStyle().getURL().empty()) {
self.styleURL = nil;
}
}
Expand Down Expand Up @@ -607,7 +608,7 @@ - (void)setDelegate:(id<MGLMapViewDelegate>)delegate {
}

- (nonnull NSURL *)styleURL {
NSString *styleURLString = @(_mbglMap->getStyleURL().c_str()).mgl_stringOrNilIfEmpty;
NSString *styleURLString = @(_mbglMap->getStyle().getURL().c_str()).mgl_stringOrNilIfEmpty;
return styleURLString ? [NSURL URLWithString:styleURLString] : [MGLStyle streetsStyleURLWithVersion:MGLStyleDefaultVersion];
}

Expand All @@ -629,12 +630,12 @@ - (void)setStyleURL:(nullable NSURL *)styleURL {

styleURL = styleURL.mgl_URLByStandardizingScheme;
self.style = nil;
_mbglMap->setStyleURL(styleURL.absoluteString.UTF8String);
_mbglMap->getStyle().loadURL(styleURL.absoluteString.UTF8String);
}

- (IBAction)reloadStyle:(__unused id)sender {
NSURL *styleURL = self.styleURL;
_mbglMap->setStyleURL("");
_mbglMap->getStyle().loadURL("");
self.styleURL = styleURL;
}

Expand Down
6 changes: 3 additions & 3 deletions platform/node/src/node_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void NodeMap::Load(const Nan::FunctionCallbackInfo<v8::Value>& info) {
}

try {
nodeMap->map->setStyleJSON(style);
nodeMap->map->getStyle().loadJSON(style);
} catch (const std::exception &ex) {
return Nan::ThrowError(ex.what());
}
Expand Down Expand Up @@ -528,15 +528,15 @@ void NodeMap::Cancel(const Nan::FunctionCallbackInfo<v8::Value>& info) {
}

void NodeMap::cancel() {
auto style = map->getStyleJSON();
auto style = map->getStyle().getJSON();

map = std::make_unique<mbgl::Map>(backend, mbgl::Size{ 256, 256 },
pixelRatio, *this, threadpool, mbgl::MapMode::Still);

// FIXME: Reload the style after recreating the map. We need to find
// a better way of canceling an ongoing rendering on the core level
// without resetting the map, which is way too expensive.
map->setStyleJSON(style);
map->getStyle().loadJSON(style);

error = std::make_exception_ptr(std::runtime_error("Canceled"));
renderFinished();
Expand Down
8 changes: 4 additions & 4 deletions platform/qt/src/qmapboxgl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,12 @@ void QMapboxGL::cycleDebugOptions()
*/
QString QMapboxGL::styleJson() const
{
return QString::fromStdString(d_ptr->mapObj->getStyleJSON());
return QString::fromStdString(d_ptr->mapObj->getStyle().getJSON());
}

void QMapboxGL::setStyleJson(const QString &style)
{
d_ptr->mapObj->setStyleJSON(style.toStdString());
d_ptr->mapObj->getStyle().loadJSON(style.toStdString());
}

/*!
Expand All @@ -500,12 +500,12 @@ void QMapboxGL::setStyleJson(const QString &style)
*/
QString QMapboxGL::styleUrl() const
{
return QString::fromStdString(d_ptr->mapObj->getStyleURL());
return QString::fromStdString(d_ptr->mapObj->getStyle().getURL());
}

void QMapboxGL::setStyleUrl(const QString &url)
{
d_ptr->mapObj->setStyleURL(url.toStdString());
d_ptr->mapObj->getStyle().loadURL(url.toStdString());
}

/*!
Expand Down
Loading

0 comments on commit 19fb81c

Please sign in to comment.