diff options
Diffstat (limited to 'src/mbgl/style')
-rw-r--r-- | src/mbgl/style/conversion/coordinate.cpp | 17 | ||||
-rw-r--r-- | src/mbgl/style/sources/image_source.cpp | 36 |
2 files changed, 45 insertions, 8 deletions
diff --git a/src/mbgl/style/conversion/coordinate.cpp b/src/mbgl/style/conversion/coordinate.cpp index ee03bffb30..9435403659 100644 --- a/src/mbgl/style/conversion/coordinate.cpp +++ b/src/mbgl/style/conversion/coordinate.cpp @@ -25,6 +25,23 @@ optional<LatLng> Converter<LatLng>::operator() (const Convertible& value, Error& return LatLng(*latitude, *longitude); } +optional<std::array<LatLng, 4>> Converter<std::array<LatLng, 4>>::operator()(const Convertible& value, + Error& error) const { + if (!isArray(value) || arrayLength(value) != 4u) { + error.message = "Image coordinates must be an array of four longitude latitude pairs"; + return nullopt; + } + std::array<LatLng, 4> coordinates; + for (std::size_t i = 0; i < 4u; ++i) { + auto latLng = convert<LatLng>(arrayMember(value, i), error); + if (!latLng) { + return nullopt; + } + coordinates[i] = *latLng; + } + return coordinates; +} + } // namespace conversion } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/sources/image_source.cpp b/src/mbgl/style/sources/image_source.cpp index 7b62c6fb9d..2e2f8c0148 100644 --- a/src/mbgl/style/sources/image_source.cpp +++ b/src/mbgl/style/sources/image_source.cpp @@ -1,4 +1,7 @@ #include <mbgl/storage/file_source.hpp> +#include <mbgl/style/conversion/constant.hpp> +#include <mbgl/style/conversion/coordinate.hpp> +#include <mbgl/style/conversion_impl.hpp> #include <mbgl/style/layer.hpp> #include <mbgl/style/source_observer.hpp> #include <mbgl/style/sources/image_source.hpp> @@ -105,22 +108,39 @@ Mutable<Source::Impl> ImageSource::createMutable() const noexcept { } Value ImageSource::serialize() const { + using namespace conversion; auto value = Source::serialize(); assert(value.getObject()); auto object = value.getObject(); - if (url) object->insert({"url", url.value()}); + object->insert({"coordinates", makeValue(getCoordinates())}); - const auto source_coordinates = getCoordinates(); - std::vector<mapbox::base::Value> coordinates; - coordinates.reserve(source_coordinates.size()); + return value; +} - for (const auto& c : source_coordinates) { - coordinates.emplace_back(std::vector<mapbox::base::Value>{c.longitude(), c.latitude()}); +optional<conversion::Error> ImageSource::setPropertyInternal(const std::string& name, + const conversion::Convertible& value) { + using namespace conversion; + optional<Error> error; + if (name == "url") { + if (auto url_ = convert<std::string>(value, *error)) { + setURL(*url_); + return nullopt; + } + } else if (name == "coordinates") { + if (auto coordinates = convert<std::array<LatLng, 4>>(value, *error)) { + setCoordinates(*coordinates); + return nullopt; + } } - object->insert({"coordinates", coordinates}); + return error ? error : Source::setPropertyInternal(name, value); +} - return value; +Value ImageSource::getPropertyInternal(const std::string& name) const { + using namespace conversion; + if (name == "url" && url) return *url; + if (name == "coordinates") return makeValue(getCoordinates()); + return Value(); } } // namespace style |