MapComplete/src/Logic/Actors/BackgroundLayerResetter.ts

71 lines
2.6 KiB
TypeScript
Raw Normal View History

import { Store, UIEventSource } from "../UIEventSource"
import { Utils } from "../../Utils"
2024-08-14 13:53:56 +02:00
import {
AvailableRasterLayers,
RasterLayerPolygon,
RasterLayerUtils,
} from "../../Models/RasterLayers"
2021-01-02 21:03:40 +01:00
/**
* When a user pans around on the map, they might pan out of the range of the current background raster layer.
* This actor will then quickly select a (best) raster layer of the same category which is available
2021-01-02 21:03:40 +01:00
*/
export default class BackgroundLayerResetter {
2022-09-08 21:40:48 +02:00
constructor(
currentBackgroundLayer: UIEventSource<RasterLayerPolygon | undefined>,
2024-08-14 13:53:56 +02:00
availableLayers: { store: Store<RasterLayerPolygon[]> }
2022-09-08 21:40:48 +02:00
) {
2021-11-07 16:34:51 +01:00
if (Utils.runningFromConsole) {
return
}
2021-11-07 16:34:51 +01:00
currentBackgroundLayer.addCallbackAndRunD(async (l) => {
2024-08-14 13:53:56 +02:00
if (
l.geometry !== undefined &&
AvailableRasterLayers.globalLayers.find(
(global) => global.properties.id !== l.properties.id
)
) {
await AvailableRasterLayers.editorLayerIndex()
2024-08-14 13:53:56 +02:00
BackgroundLayerResetter.installHandler(
currentBackgroundLayer,
availableLayers.store
)
return true // unregister
}
})
}
2024-08-14 13:53:56 +02:00
private static installHandler(
currentBackgroundLayer: UIEventSource<RasterLayerPolygon | undefined>,
availableLayers: Store<RasterLayerPolygon[]>
) {
// Change the baseLayer back to OSM if we go out of the current range of the layer
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
2021-01-02 21:03:40 +01:00
}
console.log("Current layer properties:", currentBgPolygon)
2021-01-02 21:03:40 +01:00
// 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
2022-09-08 21:40:48 +02:00
)
if (!availableInSameCat) {
return
}
console.log("Selecting a different layer:", availableInSameCat.properties.id)
currentBackgroundLayer.setData(availableInSameCat)
2022-09-08 21:40:48 +02:00
})
2021-01-02 21:03:40 +01:00
}
2022-09-08 21:40:48 +02:00
}