[core] Added RendererState::has{Image,Layer,Source}

This commit is contained in:
Bruno de Oliveira Abinader
2018-10-24 11:23:05 +03:00
parent 8e2214129e
commit 55710cbb67
5 changed files with 107 additions and 1 deletions

View File

@@ -8,7 +8,7 @@ namespace mbgl {
class UpdateParameters;
/**
* RendererState acts as a Memento-like object that is able to extract data out of UpdateParameters (an opaque object
* RendererState acts as a Memento object that is able to extract data out of UpdateParameters (an opaque object
* from the public API perspective).
*/
class RendererState {
@@ -17,6 +17,14 @@ public:
// Obtains a CameraOptions out of a given UpdateParameters object.
static CameraOptions getCameraOptions(UpdateParameters&, const EdgeInsets& = {});
// Returns whether an image with the given ID exists from a given UpdateParamters object.
static bool hasImage(UpdateParameters&, const std::string& id);
// Returns whether a layer with the given ID exists from a given UpdateParamters object.
static bool hasLayer(UpdateParameters&, const std::string& id);
// Returns whether a source with the given ID exists from a given UpdateParamters object.
static bool hasSource(UpdateParameters&, const std::string& id);
};
} // namespace mbgl

View File

@@ -64,6 +64,30 @@ CameraOptions HeadlessFrontend::getCameraOptions() {
return nullCamera;
}
bool HeadlessFrontend::hasImage(const std::string& id) {
if (updateParameters) {
return RendererState::hasImage(*updateParameters, id);
}
return false;
}
bool HeadlessFrontend::hasLayer(const std::string& id) {
if (updateParameters) {
return RendererState::hasLayer(*updateParameters, id);
}
return false;
}
bool HeadlessFrontend::hasSource(const std::string& id) {
if (updateParameters) {
return RendererState::hasSource(*updateParameters, id);
}
return false;
}
void HeadlessFrontend::setSize(Size size_) {
if (size != size_) {
size = size_;

View File

@@ -35,6 +35,10 @@ public:
RendererBackend* getBackend();
CameraOptions getCameraOptions();
bool hasImage(const std::string&);
bool hasLayer(const std::string&);
bool hasSource(const std::string&);
PremultipliedImage readStillImage();
PremultipliedImage render(Map&);

View File

@@ -1,10 +1,42 @@
#include <mbgl/renderer/renderer_state.hpp>
#include <mbgl/renderer/update_parameters.hpp>
#include <mbgl/style/image_impl.hpp>
#include <mbgl/style/layer_impl.hpp>
#include <mbgl/style/source_impl.hpp>
namespace mbgl {
CameraOptions RendererState::getCameraOptions(UpdateParameters& updateParameters, const EdgeInsets& padding) {
return updateParameters.transformState.getCameraOptions(padding);
}
bool RendererState::hasImage(UpdateParameters& updateParameters, const std::string& id) {
for (const auto& image : *updateParameters.images) {
if (image.get()->id == id) {
return true;
}
}
return false;
}
bool RendererState::hasLayer(UpdateParameters& updateParameters, const std::string& id) {
for (const auto& layer : *updateParameters.layers) {
if (layer.get()->id == id) {
return true;
}
}
return false;
}
bool RendererState::hasSource(UpdateParameters& updateParameters, const std::string& id) {
for (const auto& source : *updateParameters.sources) {
if (source.get()->id == id) {
return true;
}
}
return false;
}
} // namespace mbgl

View File

@@ -18,6 +18,8 @@
#include <mbgl/style/style.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/style/layers/background_layer.hpp>
#include <mbgl/style/layers/symbol_layer.hpp>
#include <mbgl/style/sources/geojson_source.hpp>
#include <mbgl/util/color.hpp>
using namespace mbgl;
@@ -69,12 +71,48 @@ TEST(Map, RendererState) {
test.runLoop.runOnce();
test.frontend.render(test.map);
// RendererState::getCameraOptions
const CameraOptions& options = test.frontend.getCameraOptions();
EXPECT_NEAR(options.center->latitude(), coordinate.latitude(), 1e-7);
EXPECT_NEAR(options.center->longitude(), coordinate.longitude(), 1e-7);
ASSERT_DOUBLE_EQ(*options.zoom, zoom);
ASSERT_DOUBLE_EQ(*options.pitch, pitchInDegrees);
EXPECT_NEAR(*options.angle, bearingInDegrees, 1e-7);
// RendererState::hasImage
test.map.getStyle().addImage(std::make_unique<style::Image>("default_marker", decodeImage(util::read_file("test/fixtures/sprites/default_marker.png")), 1.0));
// The frontend has not yet been notified about the newly-added image.
EXPECT_FALSE(test.frontend.hasImage("default_marker"));
test.runLoop.runOnce();
test.frontend.render(test.map);
EXPECT_TRUE(test.frontend.hasImage("default_marker"));
// RendererState::hasSource
auto source = std::make_unique<GeoJSONSource>("GeoJSONSource");
source->setGeoJSON( Geometry<double>{ Point<double>{ 0, 0 } } );
test.map.getStyle().addSource(std::move(source));
// The frontend has not yet been notified about the newly-added source.
EXPECT_FALSE(test.frontend.hasSource("GeoJSONSource"));
test.runLoop.runOnce();
test.frontend.render(test.map);
EXPECT_TRUE(test.frontend.hasSource("GeoJSONSource"));
// RendererState::hasLayer
test.map.getStyle().addLayer(std::make_unique<SymbolLayer>("SymbolLayer", "GeoJSONSource"));
// The frontend has not yet been notified about the newly-added source.
EXPECT_FALSE(test.frontend.hasLayer("SymbolLayer"));
test.runLoop.runOnce();
test.frontend.render(test.map);
EXPECT_TRUE(test.frontend.hasLayer("SymbolLayer"));
}
TEST(Map, LatLngBehavior) {