diff --git a/shared/public/Tiled2dMapSource.h b/shared/public/Tiled2dMapSource.h index b5ef4f085..4e1456540 100644 --- a/shared/public/Tiled2dMapSource.h +++ b/shared/public/Tiled2dMapSource.h @@ -164,8 +164,9 @@ class Tiled2dMapSource : void onVisibleTilesChanged(const std::vector &pyramid, int keepZoomLevelOffset = 0); private: - void performLoadingTask(Tiled2dMapTileInfo tile, size_t loaderIndex); + void onStableTriggerNewTileLoading(uint64_t updateId); + void performLoadingTask(Tiled2dMapTileInfo tile, size_t loaderIndex); void updateTileMasks(); @@ -181,11 +182,14 @@ class Tiled2dMapSource : std::unordered_map> errorTiles; std::optional nextDelayTaskExecution; + std::vector pendingTilesToLoad; std::unordered_set notFoundTiles; std::string layerName; + uint64_t currentNewTilesUpdateId = 0; + const static int64_t PENDING_NEW_TILE_DEBOUNCE_MS = 200; }; #include "Tiled2dMapSourceImpl.h" diff --git a/shared/public/Tiled2dMapSourceImpl.h b/shared/public/Tiled2dMapSourceImpl.h index 83ff8fdcc..692c2365f 100644 --- a/shared/public/Tiled2dMapSourceImpl.h +++ b/shared/public/Tiled2dMapSourceImpl.h @@ -361,17 +361,42 @@ void Tiled2dMapSource::onVisibleTilesChanged(const std::vector lock(mailbox->receivingMutex); + std::weak_ptr weakSelfPtr = std::dynamic_pointer_cast( + shared_from_this()); + std::sort(toAdd.begin(), toAdd.end()); + if (toAdd != pendingTilesToLoad) { + long newId = ++currentNewTilesUpdateId; + pendingTilesToLoad = toAdd; + strongScheduler->addTask(std::make_shared( + TaskConfig("DebounceNewTilesLoad_" + std::to_string(newId), PENDING_NEW_TILE_DEBOUNCE_MS, TaskPriority::NORMAL, + ExecutionEnvironment::COMPUTATION), + [weakSelfPtr, newId] { + if (auto strongSelf = weakSelfPtr.lock()) { + strongSelf->onStableTriggerNewTileLoading(newId); + } + })); + } } + //if we removed tiles, we potentially need to update the tilemasks - also if no new tile is loaded updateTileMasks(); notifyTilesUpdates(); } +template +void Tiled2dMapSource::onStableTriggerNewTileLoading(uint64_t updateId) { + for (const auto &tileToLoad : pendingTilesToLoad) { + std::lock_guard lock(mailbox->receivingMutex); + if (updateId != currentNewTilesUpdateId) { + break; + } + performLoadingTask(tileToLoad.tileInfo, 0); + } +} + template void Tiled2dMapSource::performLoadingTask(Tiled2dMapTileInfo tile, size_t loaderIndex) { if (currentlyLoading.count(tile) != 0) return;