diff --git a/.gitignore b/.gitignore index 0b4742764..5295ff95f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,9 @@ /ios/build /config.gypi /config.mk -/build \ No newline at end of file +/build +/bin/node_modules +/include/llmr/shader/shaders.hpp +/src/shader/shaders_gl.cpp +/src/shader/shaders_gles2.cpp +/bin/style.bin.js diff --git a/bin/build-style.js b/bin/build-style.js index 932287b89..c69fcb8b4 100755 --- a/bin/build-style.js +++ b/bin/build-style.js @@ -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 \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); diff --git a/bin/style.js b/bin/style.js index ada7625cb..ffa326525 100644 --- a/bin/style.js +++ b/bin/style.js @@ -2628,7 +2628,7 @@ module.exports = { } }, { - "name": "satellite", + "name": "night", "layers": { "background": { "color": "land_night" diff --git a/common/glfw_view.cpp b/common/glfw_view.cpp index 3cc50b591..5c623d725 100644 --- a/common/glfw_view.cpp +++ b/common/glfw_view.cpp @@ -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) diff --git a/include/llmr/map/map.hpp b/include/llmr/map/map.hpp index 8874172c3..d5d11dde8 100644 --- a/include/llmr/map/map.hpp +++ b/include/llmr/map/map.hpp @@ -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 appliedClasses); + std::set 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 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; diff --git a/include/llmr/style/style.hpp b/include/llmr/style/style.hpp index 97abf586e..58202393d 100644 --- a/include/llmr/style/style.hpp +++ b/include/llmr/style/style.hpp @@ -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; diff --git a/ios/MBXViewController.mm b/ios/MBXViewController.mm index 25fca0599..213fdceeb 100644 --- a/ios/MBXViewController.mm +++ b/ios/MBXViewController.mm @@ -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 diff --git a/ios/MVKMapKit b/ios/MVKMapKit index 7c7bb2310..113cec55d 160000 --- a/ios/MVKMapKit +++ b/ios/MVKMapKit @@ -1 +1 @@ -Subproject commit 7c7bb2310216a8d18108db40b09db08732df1f88 +Subproject commit 113cec55d080fc407d0c3d37f4aa36323749c1ec diff --git a/ios/img/toggleRaster.png b/ios/img/toggleStyle.png similarity index 100% rename from ios/img/toggleRaster.png rename to ios/img/toggleStyle.png diff --git a/ios/llmr-app.gyp b/ios/llmr-app.gyp index 5d7e1f8e3..89acc2d74 100644 --- a/ios/llmr-app.gyp +++ b/ios/llmr-app.gyp @@ -14,8 +14,10 @@ "./MBXAppDelegate.m", "./MBXViewController.h", "./MBXViewController.mm", - "./MVKMapKit/MVKMapKit/MVKMapView.h", - "./MVKMapKit/MVKMapKit/MVKMapView.mm", + ' + 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) diff --git a/src/map/map.cpp b/src/map/map.cpp index c4c736d1a..305b523c1 100644 --- a/src/map/map.cpp +++ b/src/map/map.cpp @@ -174,25 +174,20 @@ void Map::setup() { 14, true))); - sources.emplace("satellite", - std::unique_ptr(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 appliedClasses) { + style->cancelTransitions(); + + style->appliedClasses.swap(appliedClasses); + + update(); +} + +std::set 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 = pair.second; diff --git a/src/style/style.cpp b/src/style/style.cpp index f1694694c..9ae0b273f 100644 --- a/src/style/style.cpp +++ b/src/style/style.cpp @@ -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;