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.
This commit is contained in:
Thiago Marcos P. Santos
2015-05-06 15:22:43 +03:00
committed by Konstantin Käfer
parent 0d16110733
commit 168d7c4f17
7 changed files with 73 additions and 3 deletions

View File

@@ -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<void()> 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<void()> changeStyleCallback;
};
#endif

View File

@@ -3,6 +3,7 @@
#include <cmath>
#include <string>
#include <vector>
namespace mbgl {

View File

@@ -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,9 +14,12 @@
#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...");
@@ -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);

View File

@@ -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' : {

View 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

View 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

View File

@@ -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<void()> callback) {
changeStyleCallback = callback;
}
namespace mbgl {
namespace platform {