diff --git a/include/mbgl/platform/default/glfw_view.hpp b/include/mbgl/platform/default/glfw_view.hpp index 46baa7002..74dc6fe43 100644 --- a/include/mbgl/platform/default/glfw_view.hpp +++ b/include/mbgl/platform/default/glfw_view.hpp @@ -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 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 changeStyleCallback; + GLFWwindow *window = nullptr; }; diff --git a/include/mbgl/util/constants.hpp b/include/mbgl/util/constants.hpp index e598806c2..bb5aad2af 100644 --- a/include/mbgl/util/constants.hpp +++ b/include/mbgl/util/constants.hpp @@ -3,6 +3,7 @@ #include #include +#include namespace mbgl { diff --git a/linux/main.cpp b/linux/main.cpp index 738bbfb0a..d279e9d8f 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -1,4 +1,5 @@ #include +#include "../platform/default/default_styles.hpp" #include #include #include @@ -13,14 +14,16 @@ #include #include +namespace { std::unique_ptr 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); diff --git a/linux/mapboxgl-app.gypi b/linux/mapboxgl-app.gypi index ec3c5d5fe..35ca322e2 100644 --- a/linux/mapboxgl-app.gypi +++ b/linux/mapboxgl-app.gypi @@ -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' : { diff --git a/macosx/main.mm b/macosx/main.mm index 28d78e442..e5a711753 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -1,4 +1,5 @@ #include +#include "../platform/default/default_styles.hpp" #include #include #include @@ -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(); diff --git a/macosx/mapboxgl-app.gypi b/macosx/mapboxgl-app.gypi index 381795404..c56bca659 100644 --- a/macosx/mapboxgl-app.gypi +++ b/macosx/mapboxgl-app.gypi @@ -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' : { diff --git a/platform/default/default_styles.cpp b/platform/default/default_styles.cpp new file mode 100644 index 000000000..727636b3c --- /dev/null +++ b/platform/default/default_styles.cpp @@ -0,0 +1,18 @@ +#include "default_styles.hpp" + +namespace mbgl { +namespace util { + +const std::vector> 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 diff --git a/platform/default/default_styles.hpp b/platform/default/default_styles.hpp new file mode 100644 index 000000000..dc1000930 --- /dev/null +++ b/platform/default/default_styles.hpp @@ -0,0 +1,17 @@ +#ifndef MBGL_PLATFORM_DEFAULT_STYLES +#define MBGL_PLATFORM_DEFAULT_STYLES + +#include +#include + +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> defaultStyles; + +} // end namespace util +} // end namespace mbgl + +#endif diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 56aeaf30a..789d2d4b6 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -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 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 {