From f540dd86893de2365f0e4528925ff5b05869df7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 31 Jan 2013 13:37:18 +0100 Subject: [PATCH 1/2] Image layer renderers add attributions to frame state --- src/ol/framestate.js | 2 ++ src/ol/image.js | 18 +++++++++++++++++- src/ol/map.js | 1 + .../canvas/canvasimagelayerrenderer.js | 3 +++ src/ol/renderer/dom/domimagelayerrenderer.js | 2 ++ src/ol/renderer/layerrenderer.js | 18 ++++++++++++++++++ .../renderer/webgl/webglimagelayerrenderer.js | 2 ++ src/ol/source/imagesource.js | 3 ++- 8 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/ol/framestate.js b/src/ol/framestate.js index 9e45ce99b6..a4c97039a0 100644 --- a/src/ol/framestate.js +++ b/src/ol/framestate.js @@ -6,6 +6,7 @@ goog.provide('ol.PostRenderFunction'); goog.provide('ol.PreRenderFunction'); goog.require('goog.vec.Mat4'); +goog.require('ol.Attribution'); goog.require('ol.Color'); goog.require('ol.Extent'); goog.require('ol.Size'); @@ -18,6 +19,7 @@ goog.require('ol.layer.LayerState'); /** * @typedef {{animate: boolean, + * attributions: Object., * backgroundColor: ol.Color, * coordinateToPixelMatrix: goog.vec.Mat4.Number, * extent: (null|ol.Extent), diff --git a/src/ol/image.js b/src/ol/image.js index 9f7cbe0c47..41d5e6fc88 100644 --- a/src/ol/image.js +++ b/src/ol/image.js @@ -5,6 +5,7 @@ goog.require('goog.array'); goog.require('goog.events'); goog.require('goog.events.EventTarget'); goog.require('goog.events.EventType'); +goog.require('ol.Attribution'); goog.require('ol.Extent'); @@ -27,8 +28,15 @@ ol.ImageState = { * @param {number} resolution Resolution. * @param {string} src Image source URI. * @param {?string} crossOrigin Cross origin. + * @param {Array.} attributions Attributions. */ -ol.Image = function(extent, resolution, src, crossOrigin) { +ol.Image = function(extent, resolution, src, crossOrigin, attributions) { + + /** + * @private + * @type {Array.} + */ + this.attributions_ = attributions; /** * @private @@ -86,6 +94,14 @@ ol.Image.prototype.dispatchChangeEvent = function() { }; +/** + * @return {Array.} Attributions. + */ +ol.Image.prototype.getAttributions = function() { + return this.attributions_; +}; + + /** * @return {ol.Extent} Extent. */ diff --git a/src/ol/map.js b/src/ol/map.js index 9d391c90d5..4034fc9e5a 100644 --- a/src/ol/map.js +++ b/src/ol/map.js @@ -664,6 +664,7 @@ ol.Map.prototype.renderFrame_ = function(time) { var view2DState = view2D.getView2DState(); frameState = { animate: false, + attributions: {}, backgroundColor: goog.isDef(backgroundColor) ? backgroundColor : new ol.Color(255, 255, 255, 1), coordinateToPixelMatrix: this.coordinateToPixelMatrix_, diff --git a/src/ol/renderer/canvas/canvasimagelayerrenderer.js b/src/ol/renderer/canvas/canvasimagelayerrenderer.js index 273f22d49f..61eb2b898d 100644 --- a/src/ol/renderer/canvas/canvasimagelayerrenderer.js +++ b/src/ol/renderer/canvas/canvasimagelayerrenderer.js @@ -94,6 +94,7 @@ ol.renderer.canvas.ImageLayer.prototype.renderFrame = if (!goog.isNull(this.image_)) { image = this.image_; + var imageExtent = image.getExtent(); var imageResolution = image.getResolution(); var transform = this.transform_; @@ -111,5 +112,7 @@ ol.renderer.canvas.ImageLayer.prototype.renderFrame = (imageExtent.minX - viewCenter.x) / imageResolution, (viewCenter.y - imageExtent.maxY) / imageResolution, 0); + + this.updateAttributions(frameState.attributions, image.getAttributions()); } }; diff --git a/src/ol/renderer/dom/domimagelayerrenderer.js b/src/ol/renderer/dom/domimagelayerrenderer.js index b3f5f9f224..7190d8c268 100644 --- a/src/ol/renderer/dom/domimagelayerrenderer.js +++ b/src/ol/renderer/dom/domimagelayerrenderer.js @@ -106,6 +106,8 @@ ol.renderer.dom.ImageLayer.prototype.renderFrame = this.image_ = image; } this.setTransform(transform); + + this.updateAttributions(frameState.attributions, image.getAttributions()); } }; diff --git a/src/ol/renderer/layerrenderer.js b/src/ol/renderer/layerrenderer.js index bfe98600c4..3379e0da5e 100644 --- a/src/ol/renderer/layerrenderer.js +++ b/src/ol/renderer/layerrenderer.js @@ -2,6 +2,7 @@ goog.provide('ol.renderer.Layer'); goog.require('goog.events'); goog.require('goog.events.EventType'); +goog.require('ol.Attribution'); goog.require('ol.FrameState'); goog.require('ol.Image'); goog.require('ol.ImageState'); @@ -201,6 +202,23 @@ ol.renderer.Layer.prototype.scheduleExpireCache = }; +/** + * @protected + * @param {Object.} attributionsSet Attributions + * set (target). + * @param {Array.} attributions Attributions (source). + */ +ol.renderer.Layer.prototype.updateAttributions = + function(attributionsSet, attributions) { + var i; + var attribution; + for (i = 0; i < attributions.length; ++i) { + attribution = attributions[i]; + attributionsSet[goog.getUid(attribution).toString()] = attribution; + } +}; + + /** * @protected * @param {Object.>} usedTiles Used tiles. diff --git a/src/ol/renderer/webgl/webglimagelayerrenderer.js b/src/ol/renderer/webgl/webglimagelayerrenderer.js index 6449a7c178..5d0341f3c1 100644 --- a/src/ol/renderer/webgl/webglimagelayerrenderer.js +++ b/src/ol/renderer/webgl/webglimagelayerrenderer.js @@ -198,6 +198,8 @@ ol.renderer.webgl.ImageLayer.prototype.renderFrame = this.image_ = image; this.texture_ = texture; + + this.updateAttributions(frameState.attributions, image.getAttributions()); } }; diff --git a/src/ol/source/imagesource.js b/src/ol/source/imagesource.js index caad04b5e9..879a007d5c 100644 --- a/src/ol/source/imagesource.js +++ b/src/ol/source/imagesource.js @@ -84,7 +84,8 @@ ol.source.ImageSource.prototype.createImage = var imageUrl = this.imageUrlFunction(extent, size); if (goog.isDef(imageUrl)) { image = new ol.Image( - extent, resolution, imageUrl, this.crossOrigin_); + extent, resolution, imageUrl, this.crossOrigin_, + this.getAttributions()); } return image; }; From 82aa44fdd1403774de16e68fb5c46441bdec35aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Thu, 31 Jan 2013 13:38:36 +0100 Subject: [PATCH 2/2] Attribution control reads attribs from frame state --- src/ol/control/attributioncontrol.js | 89 +++++++++++++++------------- 1 file changed, 49 insertions(+), 40 deletions(-) diff --git a/src/ol/control/attributioncontrol.js b/src/ol/control/attributioncontrol.js index 731e2b9435..c6f79717aa 100644 --- a/src/ol/control/attributioncontrol.js +++ b/src/ol/control/attributioncontrol.js @@ -1,5 +1,4 @@ // FIXME handle date line wrap -// FIXME does not handle image sources goog.provide('ol.control.Attribution'); @@ -10,11 +9,12 @@ goog.require('goog.events'); goog.require('goog.object'); goog.require('goog.style'); goog.require('ol.Attribution'); +goog.require('ol.FrameState'); goog.require('ol.MapEvent'); goog.require('ol.MapEventType'); goog.require('ol.TileRange'); goog.require('ol.control.Control'); -goog.require('ol.source.TileSource'); +goog.require('ol.source.Source'); @@ -66,16 +66,46 @@ ol.control.Attribution = function(attributionOptions) { goog.inherits(ol.control.Attribution, ol.control.Control); +/** + * @param {?Object.>} usedTiles Used + * tiles. + * @param {Object.} sources Sources. + * @return {Object.} Attributions. + */ +ol.control.Attribution.prototype.getTileSourceAttributions = + function(usedTiles, sources) { + /** @type {Object.} */ + var attributions = {}; + var i, tileRanges, tileSource, tileSourceAttribution, + tileSourceAttributionKey, tileSourceAttributions, tileSourceKey, z; + for (tileSourceKey in usedTiles) { + goog.asserts.assert(tileSourceKey in sources); + tileSource = sources[tileSourceKey]; + tileSourceAttributions = tileSource.getAttributions(); + if (goog.isNull(tileSourceAttributions)) { + continue; + } + tileRanges = usedTiles[tileSourceKey]; + for (i = 0; i < tileSourceAttributions.length; ++i) { + tileSourceAttribution = tileSourceAttributions[i]; + tileSourceAttributionKey = goog.getUid(tileSourceAttribution).toString(); + if (tileSourceAttributionKey in attributions) { + continue; + } + if (tileSourceAttribution.intersectsAnyTileRange(tileRanges)) { + attributions[tileSourceAttributionKey] = tileSourceAttribution; + } + } + } + return attributions; +}; + + /** * @param {ol.MapEvent} mapEvent Map event. */ ol.control.Attribution.prototype.handleMapPostrender = function(mapEvent) { - var frameState = mapEvent.frameState; - if (goog.isNull(frameState)) { - this.updateElement_(null); - } else { - this.updateElement_(frameState.usedTiles); - } + this.updateElement_(mapEvent.frameState); }; @@ -99,12 +129,11 @@ ol.control.Attribution.prototype.setMap = function(map) { /** * @private - * @param {?Object.>} usedTiles Used - * tiles. + * @param {?ol.FrameState} frameState Frame state. */ -ol.control.Attribution.prototype.updateElement_ = function(usedTiles) { +ol.control.Attribution.prototype.updateElement_ = function(frameState) { - if (goog.isNull(usedTiles)) { + if (goog.isNull(frameState)) { if (this.renderedVisible_) { goog.style.showElement(this.element, false); this.renderedVisible_ = false; @@ -116,15 +145,13 @@ ol.control.Attribution.prototype.updateElement_ = function(usedTiles) { /** @type {Object.} */ var attributionsToRemove = {}; - /** @type {Object.} */ - var tileSources = {}; + /** @type {Object.} */ + var sources = {}; var layers = map.getLayers(); if (goog.isDef(layers)) { layers.forEach(function(layer) { var source = layer.getSource(); - if (source instanceof ol.source.TileSource) { - tileSources[goog.getUid(source).toString()] = source; - } + sources[goog.getUid(source).toString()] = source; var attributions = source.getAttributions(); if (!goog.isNull(attributions)) { var attribution, i; @@ -138,34 +165,16 @@ ol.control.Attribution.prototype.updateElement_ = function(usedTiles) { } /** @type {Object.} */ - var attributions = {}; - var i, tileRanges, tileSource, tileSourceAttribution, - tileSourceAttributionKey, tileSourceAttributions, tileSourceKey, z; - for (tileSourceKey in usedTiles) { - goog.asserts.assert(tileSourceKey in tileSources); - tileSource = tileSources[tileSourceKey]; - tileSourceAttributions = tileSource.getAttributions(); - if (goog.isNull(tileSourceAttributions)) { - continue; - } - tileRanges = usedTiles[tileSourceKey]; - for (i = 0; i < tileSourceAttributions.length; ++i) { - tileSourceAttribution = tileSourceAttributions[i]; - tileSourceAttributionKey = goog.getUid(tileSourceAttribution).toString(); - if (tileSourceAttributionKey in attributions) { - continue; - } - if (tileSourceAttribution.intersectsAnyTileRange(tileRanges)) { - attributions[tileSourceAttributionKey] = tileSourceAttribution; - } - } - } + var attributions = goog.object.clone(frameState.attributions); + var tileSourceAttributions = this.getTileSourceAttributions( + frameState.usedTiles, sources); + goog.object.extend(attributions, tileSourceAttributions); /** @type {Array.} */ var attributionKeys = goog.array.map(goog.object.getKeys(attributions), Number); goog.array.sort(attributionKeys); - var attributionElement, attributionKey; + var i, attributionElement, attributionKey; for (i = 0; i < attributionKeys.length; ++i) { attributionKey = attributionKeys[i].toString(); if (attributionKey in this.attributionElements_) {