Allow forEachFeatureAtPixel callback to break out of loop
This commit is contained in:
@@ -457,10 +457,13 @@ ol.Map.prototype.disposeInternal = function() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {ol.Pixel} pixel Pixel.
|
* @param {ol.Pixel} pixel Pixel.
|
||||||
* @param {function(ol.Feature)} callback Feature callback.
|
* @param {function(this: S, ol.Feature): T} callback Feature callback.
|
||||||
|
* @param {S=} opt_obj Scope.
|
||||||
|
* @return {T|undefined} Callback result.
|
||||||
|
* @template S,T
|
||||||
*/
|
*/
|
||||||
ol.Map.prototype.forEachFeatureAtPixel = function(pixel, callback) {
|
ol.Map.prototype.forEachFeatureAtPixel = function(pixel, callback, opt_obj) {
|
||||||
this.renderer_.forEachFeatureAtPixel(pixel, callback);
|
return this.renderer_.forEachFeatureAtPixel(pixel, callback, opt_obj);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -120,7 +120,10 @@ ol.render.canvas.Replay.prototype.beginGeometry = function(geometry) {
|
|||||||
* @param {goog.vec.Mat4.AnyType} transform Transform.
|
* @param {goog.vec.Mat4.AnyType} transform Transform.
|
||||||
* @param {function(ol.geom.Geometry): boolean} renderGeometryFunction Render
|
* @param {function(ol.geom.Geometry): boolean} renderGeometryFunction Render
|
||||||
* geometry function.
|
* geometry function.
|
||||||
* @param {function(ol.geom.Geometry, Object)=} opt_callback Geometry callback.
|
* @param {function(ol.geom.Geometry, Object): T=} opt_callback
|
||||||
|
* Geometry callback.
|
||||||
|
* @return {T|undefined} Callback result.
|
||||||
|
* @template T
|
||||||
*/
|
*/
|
||||||
ol.render.canvas.Replay.prototype.replay =
|
ol.render.canvas.Replay.prototype.replay =
|
||||||
function(context, transform, renderGeometryFunction, opt_callback) {
|
function(context, transform, renderGeometryFunction, opt_callback) {
|
||||||
@@ -189,7 +192,10 @@ ol.render.canvas.Replay.prototype.replay =
|
|||||||
goog.asserts.assert(goog.isDef(opt_callback));
|
goog.asserts.assert(goog.isDef(opt_callback));
|
||||||
geometry = /** @type {ol.geom.Geometry} */ (instruction[1]);
|
geometry = /** @type {ol.geom.Geometry} */ (instruction[1]);
|
||||||
var data = /** @type {Object} */ (instruction[2]);
|
var data = /** @type {Object} */ (instruction[2]);
|
||||||
opt_callback(geometry, data);
|
var result = opt_callback(geometry, data);
|
||||||
|
if (result) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
++i;
|
++i;
|
||||||
} else if (type == ol.render.canvas.Instruction.FILL) {
|
} else if (type == ol.render.canvas.Instruction.FILL) {
|
||||||
@@ -240,6 +246,7 @@ ol.render.canvas.Replay.prototype.replay =
|
|||||||
goog.asserts.assert(d == pixelCoordinates.length);
|
goog.asserts.assert(d == pixelCoordinates.length);
|
||||||
// assert that all instructions were consumed
|
// assert that all instructions were consumed
|
||||||
goog.asserts.assert(i == instructions.length);
|
goog.asserts.assert(i == instructions.length);
|
||||||
|
return undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -958,15 +965,18 @@ ol.render.canvas.ReplayGroup = function() {
|
|||||||
* @param {goog.vec.Mat4.AnyType} transform Transform.
|
* @param {goog.vec.Mat4.AnyType} transform Transform.
|
||||||
* @param {function(ol.geom.Geometry): boolean} renderGeometryFunction Render
|
* @param {function(ol.geom.Geometry): boolean} renderGeometryFunction Render
|
||||||
* geometry function.
|
* geometry function.
|
||||||
* @param {function(ol.geom.Geometry, Object)=} opt_callback Geometry callback.
|
* @param {function(ol.geom.Geometry, Object): T=} opt_callback Geometry
|
||||||
|
* callback.
|
||||||
|
* @return {T|undefined} Callback result.
|
||||||
|
* @template T
|
||||||
*/
|
*/
|
||||||
ol.render.canvas.ReplayGroup.prototype.replay =
|
ol.render.canvas.ReplayGroup.prototype.replay =
|
||||||
function(context, extent, transform, renderGeometryFunction, opt_callback) {
|
function(context, extent, transform, renderGeometryFunction, opt_callback) {
|
||||||
/** @type {Array.<number>} */
|
/** @type {Array.<number>} */
|
||||||
var zs = goog.array.map(goog.object.getKeys(this.replayesByZIndex_), Number);
|
var zs = goog.array.map(goog.object.getKeys(this.replayesByZIndex_), Number);
|
||||||
goog.array.sort(zs);
|
goog.array.sort(zs);
|
||||||
this.replay_(zs, context, extent, transform, renderGeometryFunction,
|
return this.replay_(
|
||||||
opt_callback);
|
zs, context, extent, transform, renderGeometryFunction, opt_callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -978,11 +988,13 @@ ol.render.canvas.ReplayGroup.prototype.replay =
|
|||||||
* @param {goog.vec.Mat4.AnyType} transform Transform.
|
* @param {goog.vec.Mat4.AnyType} transform Transform.
|
||||||
* @param {function(ol.geom.Geometry): boolean} renderGeometryFunction Render
|
* @param {function(ol.geom.Geometry): boolean} renderGeometryFunction Render
|
||||||
* geometry function.
|
* geometry function.
|
||||||
* @param {function(ol.geom.Geometry, Object)=} opt_callback Geometry callback.
|
* @param {function(ol.geom.Geometry, Object): T=} opt_callback Geometry
|
||||||
|
* callback.
|
||||||
|
* @return {T|undefined} Callback result.
|
||||||
|
* @template T
|
||||||
*/
|
*/
|
||||||
ol.render.canvas.ReplayGroup.prototype.replay_ =
|
ol.render.canvas.ReplayGroup.prototype.replay_ = function(zs, context, extent,
|
||||||
function(zs, context, extent, transform, renderGeometryFunction,
|
transform, renderGeometryFunction, opt_callback) {
|
||||||
opt_callback) {
|
|
||||||
var i, ii;
|
var i, ii;
|
||||||
for (i = 0, ii = zs.length; i < ii; ++i) {
|
for (i = 0, ii = zs.length; i < ii; ++i) {
|
||||||
var replayes = this.replayesByZIndex_[zs[i].toString()];
|
var replayes = this.replayesByZIndex_[zs[i].toString()];
|
||||||
@@ -990,10 +1002,15 @@ ol.render.canvas.ReplayGroup.prototype.replay_ =
|
|||||||
for (replayType in replayes) {
|
for (replayType in replayes) {
|
||||||
var replay = replayes[replayType];
|
var replay = replayes[replayType];
|
||||||
if (ol.extent.intersects(extent, replay.getExtent())) {
|
if (ol.extent.intersects(extent, replay.getExtent())) {
|
||||||
replay.replay(context, transform, renderGeometryFunction, opt_callback);
|
var result = replay.replay(
|
||||||
|
context, transform, renderGeometryFunction, opt_callback);
|
||||||
|
if (result) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@@ -1003,10 +1020,12 @@ ol.render.canvas.ReplayGroup.prototype.replay_ =
|
|||||||
* @param {ol.Coordinate} coordinate Coordinate.
|
* @param {ol.Coordinate} coordinate Coordinate.
|
||||||
* @param {function(ol.geom.Geometry): boolean} renderGeometryFunction Render
|
* @param {function(ol.geom.Geometry): boolean} renderGeometryFunction Render
|
||||||
* geometry function.
|
* geometry function.
|
||||||
* @param {function(ol.geom.Geometry, Object)} callback Geometry callback.
|
* @param {function(ol.geom.Geometry, Object): T} callback Geometry callback.
|
||||||
|
* @return {T|undefined} Callback result.
|
||||||
|
* @template T
|
||||||
*/
|
*/
|
||||||
ol.render.canvas.ReplayGroup.prototype.forEachGeometryAtCoordinate =
|
ol.render.canvas.ReplayGroup.prototype.forEachGeometryAtCoordinate = function(
|
||||||
function(extent, resolution, coordinate, renderGeometryFunction, callback) {
|
extent, resolution, coordinate, renderGeometryFunction, callback) {
|
||||||
|
|
||||||
var transform = this.hitDetectionTransform_;
|
var transform = this.hitDetectionTransform_;
|
||||||
ol.vec.Mat4.makeTransform2D(transform, 0.5, 0.5,
|
ol.vec.Mat4.makeTransform2D(transform, 0.5, 0.5,
|
||||||
@@ -1017,16 +1036,21 @@ ol.render.canvas.ReplayGroup.prototype.forEachGeometryAtCoordinate =
|
|||||||
goog.array.sort(zs, function(a, b) { return b - a; });
|
goog.array.sort(zs, function(a, b) { return b - a; });
|
||||||
|
|
||||||
var context = this.hitDetectionContext_;
|
var context = this.hitDetectionContext_;
|
||||||
|
context.clearRect(0, 0, 1, 1);
|
||||||
|
|
||||||
this.replay_(zs, context, extent, transform, renderGeometryFunction,
|
return this.replay_(zs, context, extent, transform, renderGeometryFunction,
|
||||||
/**
|
/**
|
||||||
* @param {ol.geom.Geometry} geometry Geometry.
|
* @param {ol.geom.Geometry} geometry Geometry.
|
||||||
* @param {Object} data Opaque data object.
|
* @param {Object} data Opaque data object.
|
||||||
|
* @return {?} Callback result.
|
||||||
*/
|
*/
|
||||||
function(geometry, data) {
|
function(geometry, data) {
|
||||||
var imageData = context.getImageData(0, 0, 1, 1).data;
|
var imageData = context.getImageData(0, 0, 1, 1).data;
|
||||||
if (imageData[3] > 0) {
|
if (imageData[3] > 0) {
|
||||||
callback(geometry, data);
|
var result = callback(geometry, data);
|
||||||
|
if (result) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
context.clearRect(0, 0, 1, 1);
|
context.clearRect(0, 0, 1, 1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -85,14 +85,16 @@ ol.renderer.canvas.VectorLayer.prototype.composeFrame =
|
|||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
ol.renderer.canvas.VectorLayer.prototype.forEachFeatureAtPixel =
|
ol.renderer.canvas.VectorLayer.prototype.forEachFeatureAtPixel =
|
||||||
function(pixel, callback) {
|
function(pixel, callback, opt_obj) {
|
||||||
if (!goog.isNull(this.replayGroup_)) {
|
if (goog.isNull(this.replayGroup_)) {
|
||||||
|
return undefined;
|
||||||
|
} else {
|
||||||
goog.asserts.assert(!ol.extent.isEmpty(this.renderedExtent_));
|
goog.asserts.assert(!ol.extent.isEmpty(this.renderedExtent_));
|
||||||
goog.asserts.assert(!isNaN(this.renderedResolution_));
|
goog.asserts.assert(!isNaN(this.renderedResolution_));
|
||||||
var coordinate = this.getMap().getCoordinateFromPixel(pixel);
|
var coordinate = this.getMap().getCoordinateFromPixel(pixel);
|
||||||
var renderGeometryFunction = this.getRenderGeometryFunction_();
|
var renderGeometryFunction = this.getRenderGeometryFunction_();
|
||||||
goog.asserts.assert(goog.isFunction(renderGeometryFunction));
|
goog.asserts.assert(goog.isFunction(renderGeometryFunction));
|
||||||
this.replayGroup_.forEachGeometryAtCoordinate(this.renderedExtent_,
|
return this.replayGroup_.forEachGeometryAtCoordinate(this.renderedExtent_,
|
||||||
this.renderedResolution_, coordinate, renderGeometryFunction,
|
this.renderedResolution_, coordinate, renderGeometryFunction,
|
||||||
/**
|
/**
|
||||||
* @param {ol.geom.Geometry} geometry Geometry.
|
* @param {ol.geom.Geometry} geometry Geometry.
|
||||||
@@ -101,7 +103,7 @@ ol.renderer.canvas.VectorLayer.prototype.forEachFeatureAtPixel =
|
|||||||
function(geometry, data) {
|
function(geometry, data) {
|
||||||
var feature = /** @type {ol.Feature} */ (data);
|
var feature = /** @type {ol.Feature} */ (data);
|
||||||
goog.asserts.assert(goog.isDef(feature));
|
goog.asserts.assert(goog.isDef(feature));
|
||||||
callback(feature);
|
return callback.call(opt_obj, feature);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -44,7 +44,10 @@ goog.inherits(ol.renderer.Layer, goog.Disposable);
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {ol.Pixel} pixel Pixel.
|
* @param {ol.Pixel} pixel Pixel.
|
||||||
* @param {function(ol.Feature)} callback Feature callback.
|
* @param {function(this: S, ol.Feature): T} callback Feature callback.
|
||||||
|
* @param {S=} opt_obj Scope.
|
||||||
|
* @return {T|undefined} Callback result.
|
||||||
|
* @template S,T
|
||||||
*/
|
*/
|
||||||
ol.renderer.Layer.prototype.forEachFeatureAtPixel = goog.nullFunction;
|
ol.renderer.Layer.prototype.forEachFeatureAtPixel = goog.nullFunction;
|
||||||
|
|
||||||
|
|||||||
@@ -80,13 +80,30 @@ ol.renderer.Map.prototype.disposeInternal = function() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {ol.Pixel} pixel Pixel.
|
* @param {ol.Pixel} pixel Pixel.
|
||||||
* @param {function(ol.Feature)} callback Feature callback.
|
* @param {function(this: S, ol.Feature): T} callback Feature callback.
|
||||||
|
* @param {S=} opt_obj Scope.
|
||||||
|
* @return {T|undefined} Callback result.
|
||||||
|
* @template S,T
|
||||||
*/
|
*/
|
||||||
ol.renderer.Map.prototype.forEachFeatureAtPixel =
|
ol.renderer.Map.prototype.forEachFeatureAtPixel =
|
||||||
function(pixel, callback) {
|
function(pixel, callback, opt_obj) {
|
||||||
goog.object.forEach(this.layerRenderers_, function(layerRenderer) {
|
var layers = this.map_.getLayers();
|
||||||
layerRenderer.forEachFeatureAtPixel(pixel, callback);
|
if (goog.isDef(layers)) {
|
||||||
});
|
var layersArray = layers.getArray();
|
||||||
|
var i;
|
||||||
|
for (i = layersArray.length - 1; i >= 0; --i) {
|
||||||
|
var layer = layersArray[i];
|
||||||
|
if (layer.getVisible()) {
|
||||||
|
var layerRenderer = this.getLayerRenderer(layer);
|
||||||
|
var result =
|
||||||
|
layerRenderer.forEachFeatureAtPixel(pixel, callback, opt_obj);
|
||||||
|
if (result) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user