Factor out icon image loading code

This commit is contained in:
Éric Lemoine
2014-02-03 14:46:45 +01:00
parent f9e04ad7d5
commit 63aadc5c0f
3 changed files with 52 additions and 54 deletions

View File

@@ -10,6 +10,7 @@ goog.require('ol.geom.MultiPolygon');
goog.require('ol.geom.Point'); goog.require('ol.geom.Point');
goog.require('ol.geom.Polygon'); goog.require('ol.geom.Polygon');
goog.require('ol.render.IReplayGroup'); goog.require('ol.render.IReplayGroup');
goog.require('ol.style.ImageState');
goog.require('ol.style.Style'); goog.require('ol.style.Style');
@@ -47,8 +48,51 @@ ol.renderer.vector.renderCircleGeometry_ =
* @param {ol.style.Style} style Style. * @param {ol.style.Style} style Style.
* @param {number} squaredTolerance Squared tolerance. * @param {number} squaredTolerance Squared tolerance.
* @param {Object} data Opaque data object. * @param {Object} data Opaque data object.
* @param {function(this: T, goog.events.Event)} listener Listener function.
* @param {T} thisArg Value to use as `this` when executing `listener`.
* @return {boolean} `true` if style is loading.
* @template T
*/ */
ol.renderer.vector.renderFeature = function( ol.renderer.vector.renderFeature = function(
replayGroup, feature, style, squaredTolerance, data, listener, thisArg) {
var loading = false;
var imageStyle, imageState;
imageStyle = style.getImage();
if (goog.isNull(imageStyle)) {
ol.renderer.vector.renderFeature_(
replayGroup, feature, style, squaredTolerance, data);
} else {
imageState = imageStyle.getImageState();
if (imageState == ol.style.ImageState.LOADED ||
imageState == ol.style.ImageState.ERROR) {
imageStyle.unlistenImageChange(listener, thisArg);
if (imageState == ol.style.ImageState.LOADED) {
ol.renderer.vector.renderFeature_(
replayGroup, feature, style, squaredTolerance, data);
}
} else {
if (imageState == ol.style.ImageState.IDLE) {
imageStyle.load();
}
imageState = imageStyle.getImageState();
goog.asserts.assert(imageState == ol.style.ImageState.LOADING);
imageStyle.listenImageChange(listener, thisArg);
loading = true;
}
}
return loading;
};
/**
* @param {ol.render.IReplayGroup} replayGroup Replay group.
* @param {ol.Feature} feature Feature.
* @param {ol.style.Style} style Style.
* @param {number} squaredTolerance Squared tolerance.
* @param {Object} data Opaque data object.
* @private
*/
ol.renderer.vector.renderFeature_ = function(
replayGroup, feature, style, squaredTolerance, data) { replayGroup, feature, style, squaredTolerance, data) {
var geometry = feature.getGeometry(); var geometry = feature.getGeometry();
if (goog.isNull(geometry)) { if (goog.isNull(geometry)) {

View File

@@ -11,7 +11,6 @@ goog.require('ol.render.canvas.ReplayGroup');
goog.require('ol.renderer.canvas.Layer'); goog.require('ol.renderer.canvas.Layer');
goog.require('ol.renderer.vector'); goog.require('ol.renderer.vector');
goog.require('ol.source.Vector'); goog.require('ol.source.Vector');
goog.require('ol.style.ImageState');
@@ -245,7 +244,6 @@ ol.renderer.canvas.VectorLayer.prototype.prepareFrame =
*/ */
ol.renderer.canvas.VectorLayer.prototype.renderFeature = ol.renderer.canvas.VectorLayer.prototype.renderFeature =
function(feature, resolution, pixelRatio, styleFunction, replayGroup) { function(feature, resolution, pixelRatio, styleFunction, replayGroup) {
var loading = false;
var styles = styleFunction(feature, resolution); var styles = styleFunction(feature, resolution);
// FIXME if styles is null, should we use the default style? // FIXME if styles is null, should we use the default style?
if (!goog.isDefAndNotNull(styles)) { if (!goog.isDefAndNotNull(styles)) {
@@ -254,32 +252,11 @@ ol.renderer.canvas.VectorLayer.prototype.renderFeature =
// simplify to a tolerance of half a device pixel // simplify to a tolerance of half a device pixel
var squaredTolerance = var squaredTolerance =
resolution * resolution / (4 * pixelRatio * pixelRatio); resolution * resolution / (4 * pixelRatio * pixelRatio);
var i, ii, style, imageStyle, imageState; var i, ii, loading = false;
for (i = 0, ii = styles.length; i < ii; ++i) { for (i = 0, ii = styles.length; i < ii; ++i) {
style = styles[i]; loading = ol.renderer.vector.renderFeature(
imageStyle = style.getImage(); replayGroup, feature, styles[i], squaredTolerance, feature,
if (goog.isNull(imageStyle)) { this.handleImageChange_, this) || loading;
ol.renderer.vector.renderFeature(
replayGroup, feature, style, squaredTolerance, feature);
} else {
imageState = imageStyle.getImageState();
if (imageState == ol.style.ImageState.LOADED ||
imageState == ol.style.ImageState.ERROR) {
imageStyle.unlistenImageChange(this.handleImageChange_, this);
if (imageState == ol.style.ImageState.LOADED) {
ol.renderer.vector.renderFeature(
replayGroup, feature, style, squaredTolerance, feature);
}
} else {
if (imageState == ol.style.ImageState.IDLE) {
imageStyle.load();
}
imageState = imageStyle.getImageState();
goog.asserts.assert(imageState == ol.style.ImageState.LOADING);
imageStyle.listenImageChange(this.handleImageChange_, this);
loading = true;
}
}
} }
return loading; return loading;
}; };

View File

@@ -12,7 +12,6 @@ goog.require('ol.render.canvas.ReplayGroup');
goog.require('ol.renderer.vector'); goog.require('ol.renderer.vector');
goog.require('ol.source.ImageCanvas'); goog.require('ol.source.ImageCanvas');
goog.require('ol.source.Vector'); goog.require('ol.source.Vector');
goog.require('ol.style.ImageState');
goog.require('ol.vec.Mat4'); goog.require('ol.vec.Mat4');
@@ -220,7 +219,6 @@ ol.source.ImageVector.prototype.handleSourceChange_ = function() {
*/ */
ol.source.ImageVector.prototype.renderFeature_ = ol.source.ImageVector.prototype.renderFeature_ =
function(feature, resolution, pixelRatio, replayGroup) { function(feature, resolution, pixelRatio, replayGroup) {
var loading = false;
var styles = this.styleFunction_(feature, resolution); var styles = this.styleFunction_(feature, resolution);
if (!goog.isDefAndNotNull(styles)) { if (!goog.isDefAndNotNull(styles)) {
return false; return false;
@@ -228,32 +226,11 @@ ol.source.ImageVector.prototype.renderFeature_ =
// simplify to a tolerance of half a device pixel // simplify to a tolerance of half a device pixel
var squaredTolerance = var squaredTolerance =
resolution * resolution / (4 * pixelRatio * pixelRatio); resolution * resolution / (4 * pixelRatio * pixelRatio);
var i, ii, style, imageStyle, imageState; var i, ii, loading = false;
for (i = 0, ii = styles.length; i < ii; ++i) { for (i = 0, ii = styles.length; i < ii; ++i) {
style = styles[i]; loading = ol.renderer.vector.renderFeature(
imageStyle = style.getImage(); replayGroup, feature, styles[i], squaredTolerance, feature,
if (goog.isNull(imageStyle)) { this.handleImageChange_, this) || loading;
ol.renderer.vector.renderFeature(
replayGroup, feature, style, squaredTolerance, feature);
} else {
imageState = imageStyle.getImageState();
if (imageState == ol.style.ImageState.LOADED ||
imageState == ol.style.ImageState.ERROR) {
imageStyle.unlistenImageChange(this.handleImageChange_, this);
if (imageState == ol.style.ImageState.LOADED) {
ol.renderer.vector.renderFeature(
replayGroup, feature, style, squaredTolerance, feature);
}
} else {
if (imageState == ol.style.ImageState.IDLE) {
imageStyle.load();
}
imageState = imageStyle.getImageState();
goog.asserts.assert(imageState == ol.style.ImageState.LOADING);
imageStyle.listenImageChange(this.handleImageChange_, this);
loading = true;
}
}
} }
return loading; return loading;
}; };