Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
style::Style::getImage() returns optional<style::Image>
Browse files Browse the repository at this point in the history
  • Loading branch information
pozdnyakov committed Feb 11, 2020
1 parent 43cc398 commit 4db780d
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 18 deletions.
1 change: 1 addition & 0 deletions include/mbgl/style/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Image {

class Impl;
Immutable<Impl> baseImpl;
explicit Image(Immutable<Impl> baseImpl_) : baseImpl(std::move(baseImpl_)) {}
};

} // namespace style
Expand Down
5 changes: 2 additions & 3 deletions include/mbgl/style/style.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include <mbgl/map/camera.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/immutable.hpp>

#include <string>
Expand All @@ -17,7 +17,6 @@ class FileSource;
namespace style {

class Light;
class Image;
class Source;
class Layer;

Expand Down Expand Up @@ -47,7 +46,7 @@ class Style {
void setLight(std::unique_ptr<Light>);

// Images
const PremultipliedImage* getImage(const std::string&) const;
optional<Image> getImage(const std::string&) const;
void addImage(std::unique_ptr<Image>);
void removeImage(const std::string&);

Expand Down
4 changes: 2 additions & 2 deletions platform/android/src/native_map_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1099,8 +1099,8 @@ void NativeMapView::removeImage(JNIEnv& env, const jni::String& name) {
}

jni::Local<jni::Object<Bitmap>> NativeMapView::getImage(JNIEnv& env, const jni::String& name) {
if (auto* image = map->getStyle().getImage(jni::Make<std::string>(env, name))) {
return Bitmap::CreateBitmap(env, *image);
if (auto image = map->getStyle().getImage(jni::Make<std::string>(env, name))) {
return Bitmap::CreateBitmap(env, image->getImage());
}
return jni::Local<jni::Object<Bitmap>>();
}
Expand Down
2 changes: 1 addition & 1 deletion platform/darwin/src/MGLStyle.mm
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ - (MGLImage *)imageForName:(NSString *)name
}

auto styleImage = self.rawStyle->getImage([name UTF8String]);
return styleImage ? [[MGLImage alloc] initWithMGLStyleImage:styleImage] : nil;
return styleImage ? [[MGLImage alloc] initWithMGLStyleImage:&(*styleImage)] : nil;
}

#pragma mark Style transitions
Expand Down
4 changes: 1 addition & 3 deletions src/mbgl/map/map_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,7 @@ void Map::Impl::jumpTo(const CameraOptions& camera) {
}

void Map::Impl::onStyleImageMissing(const std::string& id, std::function<void()> done) {
if (style->getImage(id) == nullptr) {
observer.onStyleImageMissing(id);
}
if (!style->getImage(id)) observer.onStyleImageMissing(id);

done();
onUpdate();
Expand Down
9 changes: 4 additions & 5 deletions src/mbgl/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,10 @@ const Light* Style::getLight() const {
return impl->getLight();
}

const PremultipliedImage* Style::getImage(const std::string& name) const {
if (auto* image = impl->getImage(name)) {
return &(image->image);
}
return nullptr;
optional<Image> Style::getImage(const std::string& name) const {
auto image = impl->getImage(name);
if (!image) return nullopt;
return style::Image(std::move(*image));
}

void Style::addImage(std::unique_ptr<Image> image) {
Expand Down
6 changes: 3 additions & 3 deletions src/mbgl/style/style_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,10 @@ void Style::Impl::removeImage(const std::string& id) {
images = std::move(newImages);
}

const style::Image::Impl* Style::Impl::getImage(const std::string& id) const {
optional<Immutable<style::Image::Impl>> Style::Impl::getImage(const std::string& id) const {
auto found = std::find_if(images->begin(), images->end(), [&id](const auto& image) { return image->id == id; });
if (found == images->end()) return nullptr;
return found->get();
if (found == images->end()) return nullopt;
return *found;
}

void Style::Impl::setObserver(style::Observer* observer_) {
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/style/style_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Style::Impl : public SpriteLoaderObserver,
void setLight(std::unique_ptr<Light>);
Light* getLight() const;

const style::Image::Impl* getImage(const std::string&) const;
optional<Immutable<style::Image::Impl>> getImage(const std::string&) const;
void addImage(std::unique_ptr<style::Image>);
void removeImage(const std::string&);

Expand Down

0 comments on commit 4db780d

Please sign in to comment.