From d511f74ce45b8d18c376e7c3d806819cbe438627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 24 Jan 2018 12:46:49 -0800 Subject: [PATCH] [node] add setLight support --- platform/node/src/node_map.cpp | 51 +++++++++++++++++++--- platform/node/src/node_map.hpp | 1 + platform/node/test/js/map.test.js | 1 + platform/node/test/suite_implementation.js | 3 ++ 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index 5dd5e6b49..3e66aaa78 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -21,6 +22,7 @@ #include #include +#include #include #include @@ -31,6 +33,7 @@ namespace node_mbgl { struct NodeMap::RenderOptions { double zoom = 0; double bearing = 0; + mbgl::style::Light light; double pitch = 0; double latitude = 0; double longitude = 0; @@ -77,6 +80,7 @@ void NodeMap::Init(v8::Local target) { Nan::SetPrototypeMethod(tpl, "setZoom", SetZoom); Nan::SetPrototypeMethod(tpl, "setBearing", SetBearing); Nan::SetPrototypeMethod(tpl, "setPitch", SetPitch); + Nan::SetPrototypeMethod(tpl, "setLight", SetLight); Nan::SetPrototypeMethod(tpl, "setAxonometric", SetAxonometric); Nan::SetPrototypeMethod(tpl, "setXSkew", SetXSkew); Nan::SetPrototypeMethod(tpl, "setYSkew", SetYSkew); @@ -266,6 +270,16 @@ NodeMap::RenderOptions NodeMap::ParseOptions(v8::Local obj) { options.pitch = Nan::Get(obj, Nan::New("pitch").ToLocalChecked()).ToLocalChecked()->NumberValue(); } + if (Nan::Has(obj, Nan::New("light").ToLocalChecked()).FromJust()) { + auto lightObj = Nan::Get(obj, Nan::New("light").ToLocalChecked()).ToLocalChecked(); + mbgl::style::conversion::Error conversionError; + if (auto light = mbgl::style::conversion::convert(lightObj, conversionError)) { + options.light = *light; + } else { + throw conversionError; + } + } + if (Nan::Has(obj, Nan::New("axonometric").ToLocalChecked()).FromJust()) { options.axonometric = Nan::Get(obj, Nan::New("axonometric").ToLocalChecked()).ToLocalChecked()->BooleanValue(); } @@ -373,14 +387,14 @@ void NodeMap::Render(const Nan::FunctionCallbackInfo& info) { return Nan::ThrowError("Map is currently rendering an image"); } - auto options = ParseOptions(Nan::To(info[0]).ToLocalChecked()); - - assert(!nodeMap->callback); - assert(!nodeMap->image.data); - nodeMap->callback = std::make_unique(info[1].As()); - try { + auto options = ParseOptions(Nan::To(info[0]).ToLocalChecked()); + assert(!nodeMap->callback); + assert(!nodeMap->image.data); + nodeMap->callback = std::make_unique(info[1].As()); nodeMap->startRender(std::move(options)); + } catch (mbgl::style::conversion::Error& err) { + return Nan::ThrowTypeError(err.message.c_str()); } catch (mbgl::util::Exception &ex) { return Nan::ThrowError(ex.what()); } @@ -924,6 +938,31 @@ void NodeMap::SetPitch(const Nan::FunctionCallbackInfo& info) { info.GetReturnValue().SetUndefined(); } +void NodeMap::SetLight(const Nan::FunctionCallbackInfo& info) { + using namespace mbgl::style; + using namespace mbgl::style::conversion; + + auto nodeMap = Nan::ObjectWrap::Unwrap(info.Holder()); + if (!nodeMap->map) return Nan::ThrowError(releasedMessage()); + + if (info.Length() <= 0 || !info[0]->IsObject()) { + return Nan::ThrowTypeError("First argument must be an object"); + } + + try { + Error conversionError; + if (auto light = convert(info[0], conversionError)) { + nodeMap->map->getStyle().setLight(std::make_unique(*light)); + } else { + return Nan::ThrowTypeError(conversionError.message.c_str()); + } + } catch (const std::exception &ex) { + return Nan::ThrowError(ex.what()); + } + + info.GetReturnValue().SetUndefined(); +} + void NodeMap::SetAxonometric(const Nan::FunctionCallbackInfo& info) { auto nodeMap = Nan::ObjectWrap::Unwrap(info.Holder()); if (!nodeMap->map) return Nan::ThrowError(releasedMessage()); diff --git a/platform/node/src/node_map.hpp b/platform/node/src/node_map.hpp index b84f4d576..efa6f6cee 100644 --- a/platform/node/src/node_map.hpp +++ b/platform/node/src/node_map.hpp @@ -57,6 +57,7 @@ public: static void SetZoom(const Nan::FunctionCallbackInfo&); static void SetBearing(const Nan::FunctionCallbackInfo&); static void SetPitch(const Nan::FunctionCallbackInfo&); + static void SetLight(const Nan::FunctionCallbackInfo&); static void SetAxonometric(const Nan::FunctionCallbackInfo&); static void SetXSkew(const Nan::FunctionCallbackInfo&); static void SetYSkew(const Nan::FunctionCallbackInfo&); diff --git a/platform/node/test/js/map.test.js b/platform/node/test/js/map.test.js index e3c803190..81c15f9c1 100644 --- a/platform/node/test/js/map.test.js +++ b/platform/node/test/js/map.test.js @@ -121,6 +121,7 @@ test('Map', function(t) { 'setZoom', 'setBearing', 'setPitch', + 'setLight', 'setAxonometric', 'setXSkew', 'setYSkew', diff --git a/platform/node/test/suite_implementation.js b/platform/node/test/suite_implementation.js index b0be2fa0e..c09e8f50b 100644 --- a/platform/node/test/suite_implementation.js +++ b/platform/node/test/suite_implementation.js @@ -53,6 +53,7 @@ module.exports = function (style, options, callback) { options.zoom = style.zoom || 0; options.bearing = style.bearing || 0; options.pitch = style.pitch || 0; + options.light = style.light || {}; map.load(style, { defaultStyleCamera: true }); @@ -129,6 +130,8 @@ module.exports = function (style, options, callback) { options.bearing = operation[1]; } else if (operation[0] === 'setPitch') { options.pitch = operation[1]; + } else if (operation[0] === 'setLight') { + options.light = operation[1]; } map[operation[0]].apply(map, operation.slice(1));