Continued refactoring of the maps library.
This commit is contained in:
+104
-60
@@ -1,10 +1,8 @@
|
||||
import { GeoMap } from './map.js'
|
||||
import MapView from './mapview.js'
|
||||
import { EventHandler } from './utils.js'
|
||||
|
||||
import { PIXILayer } from '../../../../src/layers/js/layer.js'
|
||||
import Logging from '../../logging.js'
|
||||
import { GeoGraphics } from './geographics.js'
|
||||
import { MapList } from './maplist.js'
|
||||
//import { GeoGraphics } from "../pixi/geographics.js"
|
||||
|
||||
/**
|
||||
@@ -13,7 +11,7 @@ import { GeoGraphics } from './geographics.js'
|
||||
* of the Adaption.
|
||||
*/
|
||||
export class GeoLayer {
|
||||
constructor(displayObject) {
|
||||
constructor(displayObject, opts = {}) {
|
||||
if (displayObject == null || !(displayObject instanceof PIXI.DisplayObject)) {
|
||||
console.error(
|
||||
`You need to provide a displayObject to make a ${this.constructor.name} out of it.`,
|
||||
@@ -22,15 +20,14 @@ export class GeoLayer {
|
||||
return null
|
||||
} else {
|
||||
this.geographics = []
|
||||
displayObject.map = this
|
||||
// displayObject.map = this
|
||||
this.displayObject = displayObject
|
||||
|
||||
this.layers = []
|
||||
|
||||
this.pixiAddChild = displayObject.addChild.bind(displayObject)
|
||||
displayObject.addChild = (...elements) => {
|
||||
elements.forEach(element => {
|
||||
if (element instanceof GeoGraphics) {
|
||||
element.setLayer(this)
|
||||
this.geographics.push(element)
|
||||
this.pixiAddChild(element.graphics)
|
||||
} else {
|
||||
@@ -39,6 +36,62 @@ export class GeoLayer {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
this.name = opts['name'] != null ? opts.name : 'Unnamed Layer'
|
||||
this.parent = null
|
||||
this.parentMapLayerTransformedHandler = new EventHandler('onParentMapLayerTransformed')
|
||||
this.layers = []
|
||||
this._visibility = { min: 0, max: Number.MAX_VALUE }
|
||||
}
|
||||
|
||||
parentMapLayerTransformed(mapLayer) {
|
||||
this.layers.forEach(layer => {
|
||||
if (!(layer instanceof MapList)) {
|
||||
layer.parentMapLayerTransformed()
|
||||
}
|
||||
})
|
||||
|
||||
this.parentMapLayerTransformedHandler.call(null, mapLayer)
|
||||
|
||||
this.rescaleChildren()
|
||||
}
|
||||
|
||||
rescaleChildren() {
|
||||
let map = this.map
|
||||
|
||||
if (this.rescale) {
|
||||
if (map != null) {
|
||||
let scale = map.image.scatter.scale
|
||||
|
||||
this.displayObject.children.forEach(graphics => {
|
||||
graphics.scale.set(1 / scale, 1 / scale)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let mapLayer = this.mapLayer
|
||||
if (this.visibility && mapLayer != null) {
|
||||
const zoom = mapLayer.mapview.zoom
|
||||
|
||||
// TODO
|
||||
// Currently I dont know what elemnts was.
|
||||
// We just log an error and resolve this on a later point.
|
||||
if (zoom > this.visibility.min && zoom < this.visibility.max) {
|
||||
this.displayObject.children.forEach(it => (it.visible = true))
|
||||
} else {
|
||||
this.displayObject.children.forEach(it => (it.visible = false))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
set visibility(value) {
|
||||
let { min = 0, max = Infinity } = value
|
||||
console.log(min)
|
||||
this._visibility = { min, max }
|
||||
}
|
||||
|
||||
get visibility() {
|
||||
return this._visibility
|
||||
}
|
||||
|
||||
addChild(element) {
|
||||
@@ -60,6 +113,8 @@ export class GeoLayer {
|
||||
this.layers.forEach(layer => {
|
||||
if (layer.adapt) layer.adapt(map)
|
||||
})
|
||||
|
||||
this.rescaleChildren()
|
||||
} else console.error('There was no map specified.', this)
|
||||
}
|
||||
|
||||
@@ -72,19 +127,54 @@ export class GeoLayer {
|
||||
// } else super.place(geographic)
|
||||
// }
|
||||
|
||||
removeFromParent() {
|
||||
if (this.parent) {
|
||||
this.parent.removeLayer(this)
|
||||
}
|
||||
}
|
||||
|
||||
removeLayer(layer) {
|
||||
let idx = this.layers.indexOf(layer)
|
||||
if (idx != -1) {
|
||||
layer.parent = null
|
||||
this.layers.splice(idx, 1)
|
||||
|
||||
if (layer.displayObject.parent) {
|
||||
layer.displayObject.parent.removeChild(layer.displayObject)
|
||||
}
|
||||
} else console.warn('Tried to remove layer that was not set.', this, layer)
|
||||
}
|
||||
|
||||
set parent(parent) {
|
||||
this._parent = parent
|
||||
}
|
||||
|
||||
get parent() {
|
||||
return this._parent
|
||||
}
|
||||
|
||||
addLayer(layer) {
|
||||
if (layer instanceof GeoLayer || layer instanceof MapLayer) {
|
||||
layer.removeFromParent()
|
||||
|
||||
this.layers.push(layer)
|
||||
layer.parent = this
|
||||
layer.parentChanged()
|
||||
|
||||
this.displayObject.addChild(layer.displayObject)
|
||||
if (this.map) layer.geographics.forEach(geographics => geographics.adaptTo(this.map))
|
||||
} else
|
||||
console.error('Could not place layer. Only MapLayer and GeoLayers can be child layers of GeoLayers.', layer)
|
||||
}
|
||||
|
||||
parentChanged() {
|
||||
this.rescaleChildren()
|
||||
}
|
||||
|
||||
//GeoLayers have to be children of a map layer,
|
||||
// therefore we can recursively get the map.
|
||||
get map() {
|
||||
return this._mapLayer.map
|
||||
return this.mapLayer ? this.mapLayer.map : null
|
||||
}
|
||||
|
||||
get mapLayer() {
|
||||
@@ -120,18 +210,12 @@ export class MapLayer extends GeoLayer {
|
||||
constructor(
|
||||
scatterContainer,
|
||||
displayObject,
|
||||
{ onTransform = null, onChange = null, focus = null, zoom = null, viewport = null } = {}
|
||||
{ onTransform = null, onChange = null, focus = null, zoom = null, viewport = null, mapList = null } = {}
|
||||
) {
|
||||
super(displayObject)
|
||||
|
||||
let onTransformListeners = [
|
||||
() => {
|
||||
this.labelVisibility()
|
||||
}
|
||||
]
|
||||
if (onTransform) onTransformListeners.push(onTransform)
|
||||
this.transformHandler = new EventHandler('onTransform', {
|
||||
listeners: onTransformListeners
|
||||
listeners: onTransform
|
||||
})
|
||||
|
||||
this.scatterContainer = scatterContainer
|
||||
@@ -146,6 +230,7 @@ export class MapLayer extends GeoLayer {
|
||||
viewport
|
||||
})
|
||||
|
||||
this.mapList = mapList
|
||||
this._map = null
|
||||
|
||||
// //TODO Implement error handling here.
|
||||
@@ -154,46 +239,6 @@ export class MapLayer extends GeoLayer {
|
||||
this.dynamicElements = new Map()
|
||||
}
|
||||
|
||||
labelVisibility() {
|
||||
const visibility = this.childrenVisibility
|
||||
|
||||
if (visibility) {
|
||||
const zoom = this.mapview.zoom
|
||||
|
||||
const min = visibility.min || 0
|
||||
const max = visibility.max || Number.MAX_VALUE
|
||||
|
||||
// TODO
|
||||
// Currently I dont know what elemnts was.
|
||||
// We just log an error and resolve this on a later point.
|
||||
if (this.elements) {
|
||||
if (zoom > min && zoom < max) {
|
||||
this.elements.forEach(it => (it.visible = true))
|
||||
|
||||
this.elements.forEach(it => {
|
||||
const scale = 1 / it.parent.scale.x
|
||||
|
||||
// it.children are poi groups
|
||||
// it.children[0] is the poi group of the tübingen poi
|
||||
// it.children[0].children are the text containers (not PIXI.Text), about 20 pieces
|
||||
|
||||
if (it.children.length > 0) {
|
||||
it.children[0].children.forEach(poi => {
|
||||
if (poi.children.length === 1) {
|
||||
poi.scale.set(scale, scale)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
} else {
|
||||
this.elements.forEach(it => (it.visible = false))
|
||||
}
|
||||
}else{
|
||||
console.error("DEBUG: Elements was not defined.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
adapt() {
|
||||
this.layers.forEach(layer => {
|
||||
if (layer.adapt) layer.adapt(this.map)
|
||||
@@ -209,6 +254,7 @@ export class MapLayer extends GeoLayer {
|
||||
|
||||
transformed(e) {
|
||||
this.mapview.transformed(this.map)
|
||||
this.layers.forEach(layer => layer.parentMapLayerTransformed(this))
|
||||
this.transformHandler.call(this)
|
||||
}
|
||||
|
||||
@@ -243,8 +289,7 @@ export class MapLayer extends GeoLayer {
|
||||
useScatterAsContainer = true // If set to false, the normal container is used. This is necessary when using submaps and the container need to be a RigidContainer.*/
|
||||
) {
|
||||
if (map instanceof GeoMap) {
|
||||
|
||||
console.log("Change map to: " , map)
|
||||
console.log('Change map to: ', map)
|
||||
let oldMap = this.map
|
||||
if (oldMap) oldMap.unload()
|
||||
|
||||
@@ -252,10 +297,9 @@ export class MapLayer extends GeoLayer {
|
||||
|
||||
this.map.load()
|
||||
this.scatterContainer.addChild(this.map.image)
|
||||
console.log(this.map.image.parent)
|
||||
|
||||
this.mapview.apply(this.map)
|
||||
map.image.addChild(this.displayObject)
|
||||
this.mapview.update(this.map)
|
||||
|
||||
// A geolayer's displayObject is on the parent layer.
|
||||
// A maplayer's displayobject is always the child of the map.
|
||||
|
||||
Reference in New Issue
Block a user