From 23b26c4197b926c848ae360347f54277a0bd5e66 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Fri, 21 Apr 2023 17:37:50 +0200 Subject: [PATCH] Refactoring: fix background layer switch and hotkeys --- Logic/Actors/BackgroundLayerResetter.ts | 8 +++- Logic/MetaTagging.ts | 2 +- Models/RasterLayers.ts | 56 +++++++++++-------------- Models/ThemeViewState.ts | 40 ++++++------------ UI/Map/MapLibreAdaptor.ts | 2 +- 5 files changed, 44 insertions(+), 64 deletions(-) diff --git a/Logic/Actors/BackgroundLayerResetter.ts b/Logic/Actors/BackgroundLayerResetter.ts index f2bf154d3b..30a0fbc16a 100644 --- a/Logic/Actors/BackgroundLayerResetter.ts +++ b/Logic/Actors/BackgroundLayerResetter.ts @@ -23,20 +23,24 @@ export default class BackgroundLayerResetter { availableLayers.addCallbackAndRunD((availableLayers) => { // We only check on move/on change of the availableLayers const currentBgPolygon: RasterLayerPolygon | undefined = currentBackgroundLayer.data + if (currentBackgroundLayer === undefined) { + return + } if (availableLayers.findIndex((available) => currentBgPolygon == available) >= 0) { // Still available! return } + console.log("Current layer properties:", currentBgPolygon) // Oops, we panned out of range for this layer! // What is the 'best' map of the same category which is available? const availableInSameCat = RasterLayerUtils.SelectBestLayerAccordingTo( availableLayers, - currentBgPolygon?.properties?.category ?? "osmbasedmap" + currentBgPolygon?.properties?.category ) console.log("Selecting a different layer:", availableInSameCat.properties.id) - currentBackgroundLayer.setData(availableInSameCat ?? AvailableRasterLayers.osmCarto) + currentBackgroundLayer.setData(availableInSameCat) }) } } diff --git a/Logic/MetaTagging.ts b/Logic/MetaTagging.ts index b8ff65993a..15f10e4ab1 100644 --- a/Logic/MetaTagging.ts +++ b/Logic/MetaTagging.ts @@ -71,7 +71,7 @@ export default class MetaTagging { return } - console.trace("Recalculating metatags...") + console.debug("Recalculating metatags...") const metatagsToApply: SimpleMetaTagger[] = [] for (const metatag of SimpleMetaTaggers.metatags) { if (metatag.includesDates) { diff --git a/Models/RasterLayers.ts b/Models/RasterLayers.ts index 0602015f7b..57e5274978 100644 --- a/Models/RasterLayers.ts +++ b/Models/RasterLayers.ts @@ -45,6 +45,7 @@ export class AvailableRasterLayers { }, geometry: BBox.global.asGeometry(), } + public static layersAvailableAt( location: Store<{ lon: number; lat: number }> ): Store { @@ -77,44 +78,35 @@ export class AvailableRasterLayers { } export class RasterLayerUtils { + /** + * Selects, from the given list of available rasterLayerPolygons, a rasterLayer. + * This rasterlayer will be of type 'preferredCategory' and will be of the 'best'-layer (if available). + * Returns 'undefined' if no such layer is available + * @param available + * @param preferredCategory + * @param ignoreLayer + */ public static SelectBestLayerAccordingTo( available: RasterLayerPolygon[], - preferredCategory: string | string[] + preferredCategory: string, + ignoreLayer?: RasterLayerPolygon ): RasterLayerPolygon { - available = [...available] - - if (preferredCategory === undefined) { - return available[0] - } - - let prefered: string[] - if (typeof preferredCategory === "string") { - prefered = [preferredCategory] - } else { - prefered = preferredCategory - } - - for (let i = prefered.length - 1; i >= 0; i--) { - const category = prefered[i] - //Then sort all layers of the preferred type to the top. Stability of the sorting will force a 'best' photo layer on top - available.sort((ap, bp) => { - const a = ap.properties - const b = bp.properties - if (a.category === category && b.category === category) { - return 0 + let secondBest: RasterLayerPolygon = undefined + for (const rasterLayer of available) { + if (rasterLayer === ignoreLayer) { + continue + } + const p = rasterLayer.properties + if (p.category === preferredCategory) { + if (p.best) { + return rasterLayer } - if (a.category !== category) { - return 1 + if (!secondBest) { + secondBest = rasterLayer } - - return -1 - }) + } } - const best = available.find((l) => l.properties.best) - if (best) { - return best - } - return available[0] + return secondBest } } diff --git a/Models/ThemeViewState.ts b/Models/ThemeViewState.ts index 53afde28d9..53b8099aeb 100644 --- a/Models/ThemeViewState.ts +++ b/Models/ThemeViewState.ts @@ -21,7 +21,7 @@ import { QueryParameters } from "../Logic/Web/QueryParameters" import UserRelatedState from "../Logic/State/UserRelatedState" import LayerConfig from "./ThemeConfig/LayerConfig" import GeoLocationHandler from "../Logic/Actors/GeoLocationHandler" -import { AvailableRasterLayers, RasterLayerPolygon } from "./RasterLayers" +import { AvailableRasterLayers, RasterLayerPolygon, RasterLayerUtils } from "./RasterLayers" import LayoutSource from "../Logic/FeatureSource/Sources/LayoutSource" import StaticFeatureSource from "../Logic/FeatureSource/Sources/StaticFeatureSource" import FeaturePropertiesStore from "../Logic/FeatureSource/Actors/FeaturePropertiesStore" @@ -46,6 +46,7 @@ import OsmObjectDownloader from "../Logic/Osm/OsmObjectDownloader" import ShowOverlayRasterLayer from "../UI/Map/ShowOverlayRasterLayer" import { Utils } from "../Utils" import { EliCategory } from "./RasterLayerProperties" +import BackgroundLayerResetter from "../Logic/Actors/BackgroundLayerResetter" /** * @@ -300,34 +301,16 @@ export default class ThemeViewState implements SpecialVisualizationState { this.mapProperties.rasterLayer.setData(AvailableRasterLayers.osmCarto) } ) - const self = this - - function setLayerCategory(category: EliCategory) { - const available = self.availableLayers.data - const matchingCategoryLayers = available.filter( - (l) => l.properties.category === category + const setLayerCategory = (category: EliCategory) => { + const available = this.availableLayers.data + const current = this.mapProperties.rasterLayer + const best = RasterLayerUtils.SelectBestLayerAccordingTo( + available, + category, + current.data ) - const best = - matchingCategoryLayers.find((l) => l.properties.best) ?? matchingCategoryLayers[0] - const rasterLayer = self.mapProperties.rasterLayer - if (rasterLayer.data !== best) { - rasterLayer.setData(best) - return - } - - // The current layer is already selected... - // We switch the layers again - - if (category === "osmbasedmap") { - rasterLayer.setData(undefined) - } else { - // search the _second_ best layer - const secondbest = matchingCategoryLayers.find( - (l) => l.properties.best && l !== best - ) - const secondNotBest = matchingCategoryLayers.find((l) => l !== best) - rasterLayer.setData(secondbest ?? secondNotBest) - } + console.log("Best layer for category", category, "is", best.properties.id) + current.setData(best) } Hotkeys.RegisterHotkey( @@ -454,5 +437,6 @@ export default class ThemeViewState implements SpecialVisualizationState { new ChangeToElementsActor(this.changes, this.featureProperties) new PendingChangesUploader(this.changes, this.selectedElement) new SelectedElementTagsUpdater(this) + new BackgroundLayerResetter(this.mapProperties.rasterLayer, this.availableLayers) } } diff --git a/UI/Map/MapLibreAdaptor.ts b/UI/Map/MapLibreAdaptor.ts index dd618d912a..f989f164a1 100644 --- a/UI/Map/MapLibreAdaptor.ts +++ b/UI/Map/MapLibreAdaptor.ts @@ -343,7 +343,7 @@ export class MapLibreAdaptor implements MapProperties, ExportableMap { // already the correct background layer, nothing to do return } - await this.awaitStyleIsLoaded() + // await this.awaitStyleIsLoaded() if (background !== this.rasterLayer?.data?.properties) { // User selected another background in the meantime... abort