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

Reload tiles on adding sprite image #6735

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.content.ContextCompat;
Expand All @@ -15,6 +17,8 @@
import android.view.MenuItem;
import android.view.View;

import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.geometry.LatLng;
Expand Down Expand Up @@ -75,10 +79,13 @@ public void onMapReady(@NonNull final MapboxMap map) {
fab.setColorFilter(ContextCompat.getColor(CustomSpriteActivity.this, R.color.primary));
fab.setOnClickListener(new View.OnClickListener() {
private Point point;
private int click;

@Override
public void onClick(View view) {
if (point == null) {
click++;

if (click == 1) {
Log.i(TAG, "First click -> Car");
// Add an icon to reference later
mapboxMap.addImage(CUSTOM_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.ic_car_top));
Expand All @@ -99,13 +106,17 @@ public void onClick(View view) {

fab.setImageResource(R.drawable.ic_directions_car_black_24dp);
} else {
//Update point
point = Point.fromCoordinates(Position.fromCoordinates(point.getCoordinates().getLongitude() + 0.001, point.getCoordinates().getLatitude() + 0.001));
GeoJsonSource source = mapboxMap.getSourceAs("point");
source.setGeoJson(FeatureCollection.fromFeatures(new Feature[]{Feature.fromGeometry(point)}));

//Move the camera as well
mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(point.getCoordinates().getLatitude(), point.getCoordinates().getLongitude())));
//Update icon
mapboxMap.removeImage(CUSTOM_ICON);
mapboxMap.addImage(CUSTOM_ICON, BitmapFactory.decodeResource(getResources(), click % 2 == 0 ? R.drawable.ic_taxi_top : R.drawable.ic_car_top));
//
// //Update point
// point = Point.fromCoordinates(Position.fromCoordinates(point.getCoordinates().getLongitude() + 0.001, point.getCoordinates().getLatitude() + 0.001));
// GeoJsonSource source = mapboxMap.getSourceAs("point");
// source.setGeoJson(FeatureCollection.fromFeatures(new Feature[]{Feature.fromGeometry(point)}));
//
// //Move the camera as well
//// mapboxMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(point.getCoordinates().getLatitude(), point.getCoordinates().getLongitude())));
}
}
});
Expand Down
4 changes: 0 additions & 4 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,8 +830,6 @@ void Map::addImage(const std::string& name, std::unique_ptr<const SpriteImage> i
impl->styleMutated = true;
impl->style->spriteAtlas->setSprite(name, std::move(image));
impl->style->spriteAtlas->updateDirty();

update(Update::Repaint);
}

void Map::removeImage(const std::string& name) {
Expand All @@ -842,8 +840,6 @@ void Map::removeImage(const std::string& name) {
impl->styleMutated = true;
impl->style->spriteAtlas->removeSprite(name);
impl->style->spriteAtlas->updateDirty();

update(Update::Repaint);
}

#pragma mark - Defaults
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/sprite/sprite_atlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ void SpriteAtlas::setSprites(const Sprites& newSprites) {
void SpriteAtlas::setSprite(const std::string& name, std::shared_ptr<const SpriteImage> sprite) {
std::lock_guard<std::mutex> lock(mutex);
_setSprite(name, sprite);
observer->onSpriteAdded();
}

void SpriteAtlas::removeSprite(const std::string& name) {
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/sprite/sprite_atlas_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class SpriteAtlasObserver {
virtual ~SpriteAtlasObserver() = default;

virtual void onSpriteLoaded() {}
virtual void onSpriteAdded() {}
virtual void onSpriteError(std::exception_ptr) {}
};

Expand Down
29 changes: 22 additions & 7 deletions src/mbgl/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,13 +509,6 @@ void Style::onSpriteLoaded() {
observer->onUpdate(Update::Repaint);
}

void Style::onSpriteError(std::exception_ptr error) {
lastError = error;
Log::Error(Event::Style, "Failed to load sprite: %s", util::toString(error).c_str());
observer->onSpriteError(error);
observer->onResourceError(error);
}

struct QueueSourceReloadVisitor {
UpdateBatch& updateBatch;

Expand All @@ -525,12 +518,34 @@ struct QueueSourceReloadVisitor {
void operator()(RasterLayer&) {}
void operator()(BackgroundLayer&) {}

void operator()(SymbolLayer& layer) {
updateBatch.sourceIDs.insert(layer.getSourceID());
}

template <class VectorLayer>
void operator()(VectorLayer& layer) {
updateBatch.sourceIDs.insert(layer.getSourceID());
}
};

void Style::onSpriteAdded() {
observer->onSpriteAdded();

//Add all sources that might be affected
for (const auto& layer : layers) {
layer->accept(QueueSourceReloadVisitor { updateBatch });
}

observer->onUpdate(Update::Layout | Update::Repaint);
}

void Style::onSpriteError(std::exception_ptr error) {
lastError = error;
Log::Error(Event::Style, "Failed to load sprite: %s", util::toString(error).c_str());
observer->onSpriteError(error);
observer->onResourceError(error);
}

void Style::onLayerFilterChanged(Layer& layer) {
layer.accept(QueueSourceReloadVisitor { updateBatch });
observer->onUpdate(Update::Layout);
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/style/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Style : public GlyphAtlasObserver,

// SpriteStoreObserver implementation.
void onSpriteLoaded() override;
void onSpriteAdded() override;
void onSpriteError(std::exception_ptr) override;

// SourceObserver implementation.
Expand Down
Binary file added test/fixtures/map/update_icon/expected.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions test/map/map.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,23 @@ TEST(Map, RemoveImage) {
test::checkImage("test/fixtures/map/remove_icon", test::render(map));
}

TEST(Map, UpdateImage) {
MapTest test;

Map map(test.view, test.fileSource, MapMode::Still);
auto decoded1 = decodeImage(util::read_file("test/fixtures/sprites/default_marker.png"));
auto decoded2 = decodeImage(util::read_file("test/fixtures/sprites/flipped_marker.png"));
auto image1 = std::make_unique<SpriteImage>(std::move(decoded1), 1.0);
auto image2 = std::make_unique<SpriteImage>(std::move(decoded2), 1.0);

map.setStyleJSON(util::read_file("test/fixtures/api/icon_style.json"));

//Add initial
map.addImage("test-icon", std::move(image1));

//Update the image
map.removeImage("test-icon");
map.addImage("test-icon", std::move(image2));
test::checkImage("test/fixtures/map/update_icon", test::render(map));
}