Merge pull request #293 from mapbox/livestyle-squash

merge programmatic styling API
This commit is contained in:
Justin R. Miller
2014-05-31 11:54:00 -07:00
15 changed files with 91 additions and 94 deletions

7
.gitignore vendored
View File

@@ -6,4 +6,9 @@
/ios/build
/config.gypi
/config.mk
/build
/build
/bin/node_modules
/include/llmr/shader/shaders.hpp
/src/shader/shaders_gl.cpp
/src/shader/shaders_gles2.cpp
/bin/style.bin.js

View File

@@ -7,36 +7,6 @@ var mkdirp = require('./mkdirp');
var name = 'style';
var data = JSON.stringify(require(path.join(process.cwd(), process.argv[2])));
var length = data.length;
var header = '// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n\n';
header += '#ifndef LLMR_STYLE_RESOURCES\n';
header += '#define LLMR_STYLE_RESOURCES\n';
header += '\n';
header += 'namespace llmr {\n';
header += 'namespace resources {\n';
header += '\n';
header += 'extern const unsigned char ' + name + '[];\n';
header += 'extern const unsigned long ' + name + '_size;\n';
header += '\n';
header += '}\n';
header += '}\n';
header += '\n';
header += '#endif\n';
var header_path = path.join(process.argv[3], 'include/llmr/style/resources.hpp');
mkdirp.sync(path.dirname(header_path));
fs.writeFileSync(header_path, header);
var code = '// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.\n';
code += '#include <llmr/style/resources.hpp>\n';
code += '\n';
code += 'using namespace llmr;\n';
code += '\n';
code += 'const unsigned char resources::' + name + '[] = R"JSON(' + data + ')JSON";\n';
code += 'const unsigned long resources::' + name + '_size = sizeof(resources::' + name + ');\n';
var file_path = path.join(process.argv[3], 'src/style/resources.cpp');
mkdirp.sync(path.dirname(file_path));
fs.writeFileSync(file_path, code);
var out_path = path.join(process.argv[3], 'bin/style.min.js');
mkdirp.sync(path.dirname(out_path));
fs.writeFileSync(out_path, data);

View File

@@ -2628,7 +2628,7 @@ module.exports = {
}
},
{
"name": "satellite",
"name": "night",
"layers": {
"background": {
"color": "land_night"

View File

@@ -74,7 +74,7 @@ void GLFWView::key(GLFWwindow *window, int key, int /*scancode*/, int action, in
break;
case GLFW_KEY_R:
if (!mods)
view->map->toggleRaster();
view->map->toggleStyle();
break;
case GLFW_KEY_N:
if (!mods)

View File

@@ -52,6 +52,14 @@ public:
void resize(uint16_t width, uint16_t height, float ratio = 1);
void resize(uint16_t width, uint16_t height, float ratio, uint16_t fb_width, uint16_t fb_height);
// Styling
void toggleStyle();
void setAppliedClasses(std::set<std::string> appliedClasses);
std::set<std::string> getAppliedClasses() const;
void setDefaultTransitionDuration(uint64_t duration_milliseconds = 0);
void setStyleJSON(std::string newStyleJSON);
std::string getStyleJSON() const;
// Transition
void cancelTransitions();
@@ -92,9 +100,6 @@ public:
void toggleDebug();
bool getDebug() const;
// TEMPORARY DEBUG API
void toggleRaster();
public:
inline const TransformState &getState() const { return state; }
inline std::shared_ptr<const Style> getStyle() const { return style; }
@@ -115,7 +120,6 @@ private:
// Setup
void setup();
void loadStyle(const uint8_t *const data, uint32_t bytes);
void updateTiles();
void updateRenderState();
@@ -161,6 +165,8 @@ private:
Sources sources;
std::string styleJSON = "";
bool debug = false;
time animationTime = 0;

View File

@@ -30,7 +30,7 @@ public:
void reset();
void loadJSON(const uint8_t *const data, size_t bytes);
void loadJSON(const uint8_t *const data);
size_t layerCount() const;
void cascade(float z);
@@ -39,7 +39,7 @@ public:
void updateTransitions(time now);
void cancelTransitions();
void setDefaultTransitionDuration(uint64_t duration = 0);
void setDefaultTransitionDuration(uint64_t duration_milliseconds = 0);
public:
std::shared_ptr<Sprite> sprite;

View File

@@ -85,7 +85,7 @@ llmr::Settings_NSUserDefaults *settings = nullptr;
singleTap.numberOfTapsRequired = 1;
[self.mapView addGestureRecognizer:singleTap];
NSArray *selectorNames = @[ @"unrotate", @"resetPosition", @"toggleDebug", @"toggleRaster", @"locateUser" ];
NSArray *selectorNames = @[ @"unrotate", @"resetPosition", @"toggleDebug", @"toggleStyle", @"locateUser" ];
CGFloat buttonSize = 40;
CGFloat bufferSize = 20;
CGFloat alpha = 0.75;
@@ -166,9 +166,9 @@ llmr::Settings_NSUserDefaults *settings = nullptr;
self.debug = ! self.debug;
}
- (void)toggleRaster
- (void)toggleStyle
{
[self.mapView toggleRaster];
[self.mapView toggleStyle];
}
- (void)locateUser

View File

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View File

@@ -14,8 +14,10 @@
"./MBXAppDelegate.m",
"./MBXViewController.h",
"./MBXViewController.mm",
"./MVKMapKit/MVKMapKit/MVKMapView.h",
"./MVKMapKit/MVKMapKit/MVKMapView.mm",
'<!@(find MVKMapKit/MVKMapKit -type f -name "MVKMapView.*")',
'<!@(find MVKMapKit/MVKMapKit -type f -name "MVKStyleFunctionValue.*")',
'<!@(find MVKMapKit/MVKMapKit -type f -name "MVKTypes.*")',
'<!@(find MVKMapKit/MVKMapKit -type f -name "*+MVKAdditions.*")',
"../common/settings_nsuserdefaults.hpp",
"../common/settings_nsuserdefaults.mm",
"../common/foundation_request.h",
@@ -25,7 +27,8 @@
'mac_bundle': 1,
'mac_bundle_resources': [
'<!@(find img -type f)',
'<!@(find MVKMapKit/MVKMapKit/Resources -type f)'
'<!@(find MVKMapKit/MVKMapKit/Resources -type f)',
'<(SHARED_INTERMEDIATE_DIR)/bin/style.min.js'
],
'link_settings': {
'libraries': [

View File

@@ -44,20 +44,15 @@
'bin/style.js',
],
'outputs': [
'<(SHARED_INTERMEDIATE_DIR)/include/llmr/style/resources.hpp',
'<(SHARED_INTERMEDIATE_DIR)/src/style/resources.cpp'
'<(SHARED_INTERMEDIATE_DIR)/bin/style.min.js'
],
'action': ['<@(node)', 'bin/build-style.js', '<@(_inputs)', '<(SHARED_INTERMEDIATE_DIR)']
}
],
'direct_dependent_settings': {
'sources': [
'<(SHARED_INTERMEDIATE_DIR)/include/llmr/style/resources.hpp',
'<(SHARED_INTERMEDIATE_DIR)/src/style/resources.cpp'
'<(SHARED_INTERMEDIATE_DIR)/bin/style.min.js'
],
'include_dirs':[
'<(SHARED_INTERMEDIATE_DIR)/include/',
]
}
},
{
@@ -135,7 +130,7 @@
'shaders',
],
'sources': [
'<!@(find src/ \( -name "*.cpp" ! -name resources.cpp ! -name shaders.hpp ! -name resources.hpp ! -name shaders_gles2.cpp ! -name shaders_gl.cpp \))',
'<!@(find src/ \( -name "*.cpp" ! -name shaders.hpp ! -name shaders_gles2.cpp ! -name shaders_gl.cpp \))',
'<!@(find src -name "*.c")',
'<!@(find src -name "*.h")',
'<!@(find include -name "*.hpp")',

View File

@@ -21,6 +21,7 @@
'mac_bundle': 1,
'mac_bundle_resources': [
'Icon.icns',
'<(SHARED_INTERMEDIATE_DIR)/bin/style.min.js'
],
'xcode_settings': {
'SDKROOT': 'macosx',

View File

@@ -1,6 +1,8 @@
#include "../common/settings_nsuserdefaults.hpp"
#include "../common/glfw_view.hpp"
#import <Foundation/Foundation.h>
int main() {
GLFWView view;
llmr::Map map(view);
@@ -10,7 +12,13 @@ int main() {
map.setLonLatZoom(settings.longitude, settings.latitude, settings.zoom);
map.setAngle(settings.angle);
map.setDebug(settings.debug);
map.toggleRaster();
// Load style
NSString *path = [[NSBundle mainBundle] pathForResource:@"style.min" ofType:@"js"];
NSString *json = [NSString stringWithContentsOfFile:path
encoding:[NSString defaultCStringEncoding]
error:nil];
map.setStyleJSON((std::string)[json cStringUsingEncoding:[NSString defaultCStringEncoding]]);
// fprintf(stderr, "lon: %f, lat: %f, zoom: %f, angle: %f, debug: %d\n", settings.l)

View File

@@ -174,25 +174,20 @@ void Map::setup() {
14,
true)));
sources.emplace("satellite",
std::unique_ptr<Source>(new Source(*this,
painter,
"https://a.tiles.mapbox.com/v3/justin.hh0gkdfm/%d/%d/%d%s.png256",
Source::Type::raster,
{{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 }},
256,
0,
21,
false)));
loadStyle(resources::style, resources::style_size);
setStyleJSON(styleJSON);
}
void Map::loadStyle(const uint8_t *const data, uint32_t bytes) {
style->loadJSON(data, bytes);
void Map::setStyleJSON(std::string newStyleJSON) {
styleJSON.swap(newStyleJSON);
style->cancelTransitions();
style->loadJSON((const uint8_t *)styleJSON.c_str());
update();
}
std::string Map::getStyleJSON() const {
return styleJSON;
}
#pragma mark - Size
// Note: This function is called from another thread. Make sure you only call threadsafe functions!
@@ -388,25 +383,45 @@ bool Map::getDebug() const {
return debug;
}
void Map::toggleRaster() {
style->setDefaultTransitionDuration(300);
void Map::toggleStyle() {
setDefaultTransitionDuration(300);
style->cancelTransitions();
auto it = sources.find("satellite");
if (it != sources.end()) {
Source &satellite_source = *it->second;
if (satellite_source.enabled) {
satellite_source.enabled = false;
style->appliedClasses.erase("satellite");
} else {
satellite_source.enabled = true;
style->appliedClasses.insert("satellite");
if (style->appliedClasses.size() == 1) {
for (auto source_it = style->classes.begin(); source_it != style->classes.end(); source_it++) {
if (source_it->first != "default") {
style->appliedClasses.insert(source_it->first);
break;
}
}
} else {
for (auto source_it = style->classes.begin(); source_it != style->classes.end(); source_it++) {
if (source_it->first != "default") {
style->appliedClasses.erase(source_it->first);
break;
}
}
}
update();
}
void Map::setAppliedClasses(std::set<std::string> appliedClasses) {
style->cancelTransitions();
style->appliedClasses.swap(appliedClasses);
update();
}
std::set<std::string> Map::getAppliedClasses() const {
return style->appliedClasses;
}
void Map::setDefaultTransitionDuration(uint64_t duration_milliseconds) {
style->setDefaultTransitionDuration(duration_milliseconds);
}
void Map::updateTiles() {
for (auto &pair : sources) {
const std::unique_ptr<Source> &source = pair.second;

View File

@@ -965,21 +965,15 @@ size_t Style::layerCount() const {
return count;
}
void Style::setDefaultTransitionDuration(uint64_t duration) {
default_transition_duration = duration;
void Style::setDefaultTransitionDuration(uint64_t duration_milliseconds) {
default_transition_duration = duration_milliseconds;
}
void Style::loadJSON(const uint8_t *const data, size_t bytes) {
void Style::loadJSON(const uint8_t *const data) {
uv::writelock lock(mtx);
rapidjson::Document doc;
if (bytes <= 0) {
return;
}
if (data[bytes - 1] != 0) {
throw exception("style JSON string is not 0-terminated");
}
doc.Parse<0>((const char *const)data);
const rapidjson::Document& document = doc;