From 168d7c4f171b44b0e3cd4ef78bc4e6b5e8181953 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Wed, 6 May 2015 15:22:43 +0300 Subject: [PATCH 1/4] Add style change functionality to the Linux test app Makes the life of people using Linux as development environment way easier. Just press 's' to cycle through 3 different styles. --- include/mbgl/platform/default/glfw_view.hpp | 10 ++++++++-- include/mbgl/util/constants.hpp | 1 + linux/main.cpp | 19 ++++++++++++++++++- linux/mapboxgl-app.gypi | 3 +++ platform/default/default_styles.cpp | 18 ++++++++++++++++++ platform/default/default_styles.hpp | 17 +++++++++++++++++ platform/default/glfw_view.cpp | 8 ++++++++ 7 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 platform/default/default_styles.cpp create mode 100644 platform/default/default_styles.hpp diff --git a/include/mbgl/platform/default/glfw_view.hpp b/include/mbgl/platform/default/glfw_view.hpp index 46baa7002..87710ff43 100644 --- a/include/mbgl/platform/default/glfw_view.hpp +++ b/include/mbgl/platform/default/glfw_view.hpp @@ -25,10 +25,16 @@ 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 run(); void fps(); -public: + GLFWwindow *window = nullptr; + +private: bool fullscreen = false; double lastX = 0, lastY = 0; @@ -38,7 +44,7 @@ public: double lastClick = -1; - GLFWwindow *window = nullptr; + std::function changeStyleCallback; }; #endif 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..2361c996d 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -1,4 +1,5 @@ #include +#include "../platform/default/default_styles.hpp" #include #include #include @@ -13,9 +14,12 @@ #include #include +namespace { std::unique_ptr view; +} + void quit_handler(int) { if (view) { mbgl::Log::Info(mbgl::Event::Setup, "waiting for quit..."); @@ -75,6 +79,19 @@ 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); + + 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,7 @@ int main(int argc, char *argv[]) { // Load style if (style.empty()) { - style = std::string("asset://") + std::string("styles/bright-v7.json"); + style = mbgl::util::defaultStyles.front().first; } 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/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..bfe188260 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,10 @@ void GLFWView::fps() { } } +void GLFWView::setChangeStyleCallback(std::function callback) { + changeStyleCallback = callback; +} + namespace mbgl { namespace platform { From 769364c117dfa618801fa4d90b74043bb46b8092 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Wed, 6 May 2015 16:04:13 +0300 Subject: [PATCH 2/4] Make all GLFWView attributes private --- include/mbgl/platform/default/glfw_view.hpp | 11 ++++++----- linux/main.cpp | 3 +-- platform/default/glfw_view.cpp | 5 +++++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/mbgl/platform/default/glfw_view.hpp b/include/mbgl/platform/default/glfw_view.hpp index 87710ff43..1afb25d14 100644 --- a/include/mbgl/platform/default/glfw_view.hpp +++ b/include/mbgl/platform/default/glfw_view.hpp @@ -29,22 +29,23 @@ public: // The expected action is to set a new style, different to the current one. void setChangeStyleCallback(std::function callback); + void setShouldClose(); + void run(); void fps(); - GLFWwindow *window = nullptr; - private: bool fullscreen = false; + bool tracking = false; + bool rotating = false; double lastX = 0, lastY = 0; - bool tracking = false; - - bool rotating = false; double lastClick = -1; std::function changeStyleCallback; + + GLFWwindow *window = nullptr; }; #endif diff --git a/linux/main.cpp b/linux/main.cpp index 2361c996d..c295eb18e 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -23,8 +23,7 @@ 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); } diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index bfe188260..286be5d8a 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -314,6 +314,11 @@ void GLFWView::setChangeStyleCallback(std::function callback) { changeStyleCallback = callback; } +void GLFWView::setShouldClose() { + glfwSetWindowShouldClose(window, true); + glfwPostEmptyEvent(); +} + namespace mbgl { namespace platform { From b7fa1d719677e7782d9a53f8fce92c09b15eba12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Thu, 7 May 2015 10:16:04 +0200 Subject: [PATCH 3/4] add style cycling to Mac OS X app as well --- macosx/main.mm | 17 ++++++++++++++++- macosx/mapboxgl-app.gypi | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/macosx/main.mm b/macosx/main.mm index 28d78e442..7922ee7c0 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,27 @@ int main() { 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); + + 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.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' : { From 7dbe768ffc91859ace1ea1947698be4db6be9701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Thu, 7 May 2015 10:57:37 +0200 Subject: [PATCH 4/4] change the window title to indicate the current style --- include/mbgl/platform/default/glfw_view.hpp | 2 ++ linux/main.cpp | 5 ++++- macosx/main.mm | 4 +++- platform/default/glfw_view.cpp | 4 ++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/mbgl/platform/default/glfw_view.hpp b/include/mbgl/platform/default/glfw_view.hpp index 1afb25d14..74dc6fe43 100644 --- a/include/mbgl/platform/default/glfw_view.hpp +++ b/include/mbgl/platform/default/glfw_view.hpp @@ -31,6 +31,8 @@ public: void setShouldClose(); + void setWindowTitle(const std::string&); + void run(); void fps(); diff --git a/linux/main.cpp b/linux/main.cpp index c295eb18e..d279e9d8f 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -87,6 +87,7 @@ int main(int argc, char *argv[]) { 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); }); @@ -101,7 +102,9 @@ int main(int argc, char *argv[]) { // Load style if (style.empty()) { - style = mbgl::util::defaultStyles.front().first; + const auto& newStyle = mbgl::util::defaultStyles.front(); + style = newStyle.first; + view->setWindowTitle(newStyle.second); } map.setStyleURL(style); diff --git a/macosx/main.mm b/macosx/main.mm index 7922ee7c0..e5a711753 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -126,7 +126,7 @@ int main() { map.setBearing(settings.bearing); map.setDebug(settings.debug); - view.setChangeStyleCallback([&map] () { + view.setChangeStyleCallback([&map, &view] () { static uint8_t currentStyleIndex; if (++currentStyleIndex == mbgl::util::defaultStyles.size()) { @@ -135,6 +135,7 @@ int main() { 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); }); @@ -147,6 +148,7 @@ int main() { // Load style const auto& newStyle = mbgl::util::defaultStyles.front(); map.setStyleURL(newStyle.first); + view.setWindowTitle(newStyle.second); view.run(); diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 286be5d8a..789d2d4b6 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -319,6 +319,10 @@ void GLFWView::setShouldClose() { glfwPostEmptyEvent(); } +void GLFWView::setWindowTitle(const std::string& title) { + glfwSetWindowTitle(window, (std::string { "Mapbox GL: " } + title).c_str()); +} + namespace mbgl { namespace platform {