Merge pull request #1443 from mapbox/style_change
Add style change functionality to the Linux test app
This commit is contained in:
@@ -25,19 +25,28 @@ public:
|
||||
static void onMouseClick(GLFWwindow *window, int button, int action, int modifiers);
|
||||
static void onMouseMove(GLFWwindow *window, double x, double y);
|
||||
|
||||
// Callback called when the user presses the key mapped to style change.
|
||||
// The expected action is to set a new style, different to the current one.
|
||||
void setChangeStyleCallback(std::function<void()> callback);
|
||||
|
||||
void setShouldClose();
|
||||
|
||||
void setWindowTitle(const std::string&);
|
||||
|
||||
void run();
|
||||
void fps();
|
||||
|
||||
public:
|
||||
private:
|
||||
bool fullscreen = false;
|
||||
|
||||
double lastX = 0, lastY = 0;
|
||||
bool tracking = false;
|
||||
|
||||
bool rotating = false;
|
||||
|
||||
double lastX = 0, lastY = 0;
|
||||
|
||||
double lastClick = -1;
|
||||
|
||||
std::function<void()> changeStyleCallback;
|
||||
|
||||
GLFWwindow *window = nullptr;
|
||||
};
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <mbgl/mbgl.hpp>
|
||||
#include "../platform/default/default_styles.hpp"
|
||||
#include <mbgl/util/std.hpp>
|
||||
#include <mbgl/util/uv.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
@@ -13,14 +14,16 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
namespace {
|
||||
|
||||
std::unique_ptr<GLFWView> view;
|
||||
|
||||
}
|
||||
|
||||
void quit_handler(int) {
|
||||
if (view) {
|
||||
mbgl::Log::Info(mbgl::Event::Setup, "waiting for quit...");
|
||||
glfwSetWindowShouldClose(view->window, true);
|
||||
glfwPostEmptyEvent();
|
||||
view->setShouldClose();
|
||||
} else {
|
||||
exit(0);
|
||||
}
|
||||
@@ -75,6 +78,20 @@ int main(int argc, char *argv[]) {
|
||||
map.setBearing(settings.bearing);
|
||||
map.setDebug(settings.debug);
|
||||
|
||||
view->setChangeStyleCallback([&map] () {
|
||||
static uint8_t currentStyleIndex;
|
||||
|
||||
if (++currentStyleIndex == mbgl::util::defaultStyles.size()) {
|
||||
currentStyleIndex = 0;
|
||||
}
|
||||
|
||||
const auto& newStyle = mbgl::util::defaultStyles[currentStyleIndex];
|
||||
map.setStyleURL(newStyle.first);
|
||||
view->setWindowTitle(newStyle.second);
|
||||
|
||||
mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.first);
|
||||
});
|
||||
|
||||
// Set access token if present
|
||||
const char *token = getenv("MAPBOX_ACCESS_TOKEN");
|
||||
if (token == nullptr) {
|
||||
@@ -85,7 +102,9 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
// Load style
|
||||
if (style.empty()) {
|
||||
style = std::string("asset://") + std::string("styles/bright-v7.json");
|
||||
const auto& newStyle = mbgl::util::defaultStyles.front();
|
||||
style = newStyle.first;
|
||||
view->setWindowTitle(newStyle.second);
|
||||
}
|
||||
|
||||
map.setStyleURL(style);
|
||||
|
||||
@@ -20,8 +20,11 @@
|
||||
'sources': [
|
||||
'main.cpp',
|
||||
'../platform/default/settings_json.cpp',
|
||||
'../platform/default/glfw_view.hpp',
|
||||
'../platform/default/glfw_view.cpp',
|
||||
'../platform/default/log_stderr.cpp',
|
||||
'../platform/default/default_styles.hpp',
|
||||
'../platform/default/default_styles.cpp',
|
||||
],
|
||||
|
||||
'variables' : {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <mbgl/platform/log.hpp>
|
||||
#include "../platform/default/default_styles.hpp"
|
||||
#include <mbgl/platform/platform.hpp>
|
||||
#include <mbgl/platform/darwin/settings_nsuserdefaults.hpp>
|
||||
#include <mbgl/platform/darwin/Reachability.h>
|
||||
@@ -125,13 +126,29 @@ int main() {
|
||||
map.setBearing(settings.bearing);
|
||||
map.setDebug(settings.debug);
|
||||
|
||||
view.setChangeStyleCallback([&map, &view] () {
|
||||
static uint8_t currentStyleIndex;
|
||||
|
||||
if (++currentStyleIndex == mbgl::util::defaultStyles.size()) {
|
||||
currentStyleIndex = 0;
|
||||
}
|
||||
|
||||
const auto& newStyle = mbgl::util::defaultStyles[currentStyleIndex];
|
||||
map.setStyleURL(newStyle.first);
|
||||
view.setWindowTitle(newStyle.second);
|
||||
|
||||
mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.first);
|
||||
});
|
||||
|
||||
// Set access token if present
|
||||
NSString *accessToken = [[NSProcessInfo processInfo] environment][@"MAPBOX_ACCESS_TOKEN"];
|
||||
if (!accessToken) mbgl::Log::Warning(mbgl::Event::Setup, "No access token set. Mapbox vector tiles won't work.");
|
||||
if (accessToken) map.setAccessToken([accessToken cStringUsingEncoding:[NSString defaultCStringEncoding]]);
|
||||
|
||||
// Load style
|
||||
map.setStyleURL("asset://styles/bright-v7.json");
|
||||
const auto& newStyle = mbgl::util::defaultStyles.front();
|
||||
map.setStyleURL(newStyle.first);
|
||||
view.setWindowTitle(newStyle.second);
|
||||
|
||||
view.run();
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
'../platform/darwin/reachability.m',
|
||||
'../platform/default/glfw_view.hpp',
|
||||
'../platform/default/glfw_view.cpp',
|
||||
'../platform/default/default_styles.hpp',
|
||||
'../platform/default/default_styles.cpp',
|
||||
],
|
||||
|
||||
'variables' : {
|
||||
|
||||
18
platform/default/default_styles.cpp
Normal file
18
platform/default/default_styles.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "default_styles.hpp"
|
||||
|
||||
namespace mbgl {
|
||||
namespace util {
|
||||
|
||||
const std::vector<std::pair<std::string, std::string>> defaultStyles = {
|
||||
{ "asset://styles/mapbox-streets-v7.json", "Mapbox Streets" },
|
||||
{ "asset://styles/emerald-v7.json", "Emerald" },
|
||||
{ "asset://styles/light-v7.json", "Light" },
|
||||
{ "asset://styles/dark-v7.json", "Dark" },
|
||||
{ "asset://styles/bright-v7.json", "Bright" },
|
||||
{ "asset://styles/basic-v7.json", "Basic" },
|
||||
{ "asset://styles/outdoors-v7.json", "Outdoors" },
|
||||
{ "asset://styles/satellite-v7.json", "Satellite" },
|
||||
};
|
||||
|
||||
} // end namespace util
|
||||
} // end namespace mbgl
|
||||
17
platform/default/default_styles.hpp
Normal file
17
platform/default/default_styles.hpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef MBGL_PLATFORM_DEFAULT_STYLES
|
||||
#define MBGL_PLATFORM_DEFAULT_STYLES
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
namespace mbgl {
|
||||
namespace util {
|
||||
|
||||
// A list of default styles, with the first string being the URL
|
||||
// and the second being the user-visible name.
|
||||
extern const std::vector<std::pair<std::string, std::string>> defaultStyles;
|
||||
|
||||
} // end namespace util
|
||||
} // end namespace mbgl
|
||||
|
||||
#endif
|
||||
@@ -179,6 +179,10 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action,
|
||||
if (!mods)
|
||||
view->map->resetPosition();
|
||||
break;
|
||||
case GLFW_KEY_S:
|
||||
if (view->changeStyleCallback)
|
||||
view->changeStyleCallback();
|
||||
break;
|
||||
case GLFW_KEY_R:
|
||||
if (!mods) {
|
||||
view->map->setDefaultTransitionDuration(std::chrono::milliseconds(300));
|
||||
@@ -306,6 +310,19 @@ void GLFWView::fps() {
|
||||
}
|
||||
}
|
||||
|
||||
void GLFWView::setChangeStyleCallback(std::function<void()> callback) {
|
||||
changeStyleCallback = callback;
|
||||
}
|
||||
|
||||
void GLFWView::setShouldClose() {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
glfwPostEmptyEvent();
|
||||
}
|
||||
|
||||
void GLFWView::setWindowTitle(const std::string& title) {
|
||||
glfwSetWindowTitle(window, (std::string { "Mapbox GL: " } + title).c_str());
|
||||
}
|
||||
|
||||
namespace mbgl {
|
||||
namespace platform {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user