Added new 'hitDEtectionRenderer' property to ol.style.Style and used it in custom drawing

This commit is contained in:
Anna Shchurova
2021-08-20 15:10:59 -04:00
parent 44ec78749f
commit aa58a358ea
4 changed files with 526 additions and 449 deletions

View File

@@ -15,8 +15,9 @@ class VectorContext {
* @param {import("../geom/SimpleGeometry.js").default} geometry Geometry. * @param {import("../geom/SimpleGeometry.js").default} geometry Geometry.
* @param {import("../Feature.js").FeatureLike} feature Feature. * @param {import("../Feature.js").FeatureLike} feature Feature.
* @param {Function} renderer Renderer. * @param {Function} renderer Renderer.
* @param {Function} hitDetectionRenderer Renderer.
*/ */
drawCustom(geometry, feature, renderer) {} drawCustom(geometry, feature, renderer, hitDetectionRenderer) {}
/** /**
* Render a geometry. * Render a geometry.

View File

@@ -247,15 +247,20 @@ class CanvasBuilder extends VectorContext {
* @param {import("../../geom/SimpleGeometry.js").default} geometry Geometry. * @param {import("../../geom/SimpleGeometry.js").default} geometry Geometry.
* @param {import("../../Feature.js").FeatureLike} feature Feature. * @param {import("../../Feature.js").FeatureLike} feature Feature.
* @param {Function} renderer Renderer. * @param {Function} renderer Renderer.
* @param {Function} hitDetectionRenderer Renderer.
*/ */
drawCustom(geometry, feature, renderer) { drawCustom(geometry, feature, renderer, hitDetectionRenderer) {
this.beginGeometry(geometry, feature); this.beginGeometry(geometry, feature);
const type = geometry.getType(); const type = geometry.getType();
const stride = geometry.getStride(); const stride = geometry.getStride();
const builderBegin = this.coordinates.length; const builderBegin = this.coordinates.length;
let flatCoordinates, builderEnd, builderEnds, builderEndss; let flatCoordinates, builderEnd, builderEnds, builderEndss;
let offset; let offset;
if (type == GeometryType.MULTI_POLYGON) {
switch(type) {
case GeometryType.MULTI_POLYGON:
flatCoordinates = flatCoordinates =
/** @type {import("../../geom/MultiPolygon.js").default} */ ( /** @type {import("../../geom/MultiPolygon.js").default} */ (
geometry geometry
@@ -285,10 +290,16 @@ class CanvasBuilder extends VectorContext {
renderer, renderer,
inflateMultiCoordinatesArray, inflateMultiCoordinatesArray,
]); ]);
} else if ( this.hitDetectionInstructions.push([
type == GeometryType.POLYGON || CanvasInstruction.CUSTOM,
type == GeometryType.MULTI_LINE_STRING builderBegin,
) { builderEndss,
geometry,
hitDetectionRenderer || renderer,
inflateMultiCoordinatesArray]);
break;
case GeometryType.POLYGON:
case GeometryType.MULTI_LINE_STRING:
builderEnds = []; builderEnds = [];
flatCoordinates = flatCoordinates =
type == GeometryType.POLYGON type == GeometryType.POLYGON
@@ -313,10 +324,17 @@ class CanvasBuilder extends VectorContext {
renderer, renderer,
inflateCoordinatesArray, inflateCoordinatesArray,
]); ]);
} else if ( this.hitDetectionInstructions.push([
type == GeometryType.LINE_STRING || CanvasInstruction.CUSTOM,
type == GeometryType.CIRCLE builderBegin,
) { builderEnds,
geometry,
hitDetectionRenderer || renderer,
inflateCoordinatesArray
]);
break;
case GeometryType.LINE_STRING:
case GeometryType.CIRCLE:
flatCoordinates = geometry.getFlatCoordinates(); flatCoordinates = geometry.getFlatCoordinates();
builderEnd = this.appendFlatLineCoordinates( builderEnd = this.appendFlatLineCoordinates(
flatCoordinates, flatCoordinates,
@@ -334,9 +352,19 @@ class CanvasBuilder extends VectorContext {
renderer, renderer,
inflateCoordinates, inflateCoordinates,
]); ]);
} else if (type == GeometryType.MULTI_POINT) { this.hitDetectionInstructions.push([
CanvasInstruction.CUSTOM,
builderBegin,
builderEnd,
geometry,
hitDetectionRenderer || renderer,
inflateCoordinates,
]);
break;
case GeometryType.MULTI_POINT:
flatCoordinates = geometry.getFlatCoordinates(); flatCoordinates = geometry.getFlatCoordinates();
builderEnd = this.appendFlatPointCoordinates(flatCoordinates, stride); builderEnd = this.appendFlatPointCoordinates(flatCoordinates, stride);
if (builderEnd > builderBegin) { if (builderEnd > builderBegin) {
this.instructions.push([ this.instructions.push([
CanvasInstruction.CUSTOM, CanvasInstruction.CUSTOM,
@@ -346,11 +374,21 @@ class CanvasBuilder extends VectorContext {
renderer, renderer,
inflateCoordinates, inflateCoordinates,
]); ]);
this.hitDetectionInstructions.push([
CanvasInstruction.CUSTOM,
builderBegin,
builderEnd,
geometry,
hitDetectionRenderer || renderer,
inflateCoordinates,
]);
} }
} else if (type == GeometryType.POINT) { break;
case type == GeometryType.POINT:
flatCoordinates = geometry.getFlatCoordinates(); flatCoordinates = geometry.getFlatCoordinates();
this.coordinates.push(flatCoordinates[0], flatCoordinates[1]); this.coordinates.push(flatCoordinates[0], flatCoordinates[1]);
builderEnd = this.coordinates.length; builderEnd = this.coordinates.length;
this.instructions.push([ this.instructions.push([
CanvasInstruction.CUSTOM, CanvasInstruction.CUSTOM,
builderBegin, builderBegin,
@@ -358,6 +396,14 @@ class CanvasBuilder extends VectorContext {
geometry, geometry,
renderer, renderer,
]); ]);
this.hitDetectionInstructions.push([
CanvasInstruction.CUSTOM,
builderBegin,
builderEnd,
geometry,
hitDetectionRenderer || renderer,
]);
break;
} }
this.endGeometry(feature); this.endGeometry(feature);
} }

View File

@@ -208,7 +208,8 @@ function renderGeometry(replayGroup, geometry, style, feature) {
replay.drawCustom( replay.drawCustom(
/** @type {import("../geom/SimpleGeometry.js").default} */ (geometry), /** @type {import("../geom/SimpleGeometry.js").default} */ (geometry),
feature, feature,
style.getRenderer() style.getRenderer(),
style.getHitDetectionRenderer()
); );
} }

View File

@@ -9,142 +9,144 @@ import Stroke from './Stroke.js';
import {assert} from '../asserts.js'; import {assert} from '../asserts.js';
/** /**
* A function that takes an {@link module:ol/Feature} and a `{number}` * A function that takes an {@link module:ol/Feature} and a `{number}`
* representing the view's resolution. The function should return a * representing the view's resolution. The function should return a
* {@link module:ol/style/Style} or an array of them. This way e.g. a * {@link module:ol/style/Style} or an array of them. This way e.g. a
* vector layer can be styled. If the function returns `undefined`, the * vector layer can be styled. If the function returns `undefined`, the
* feature will not be rendered. * feature will not be rendered.
* *
* @typedef {function(import("../Feature.js").FeatureLike, number):(Style|Array<Style>|void)} StyleFunction * @typedef {function(import("../Feature.js").FeatureLike, number):(Style|Array<Style>|void)} StyleFunction
*/ */
/** /**
* A {@link Style}, an array of {@link Style}, or a {@link StyleFunction}. * A {@link Style}, an array of {@link Style}, or a {@link StyleFunction}.
* @typedef {Style|Array<Style>|StyleFunction} StyleLike * @typedef {Style|Array<Style>|StyleFunction} StyleLike
*/ */
/** /**
* A function that takes an {@link module:ol/Feature} as argument and returns an * A function that takes an {@link module:ol/Feature} as argument and returns an
* {@link module:ol/geom/Geometry} that will be rendered and styled for the feature. * {@link module:ol/geom/Geometry} that will be rendered and styled for the feature.
* *
* @typedef {function(import("../Feature.js").FeatureLike): * @typedef {function(import("../Feature.js").FeatureLike):
* (import("../geom/Geometry.js").default|import("../render/Feature.js").default|undefined)} GeometryFunction * (import("../geom/Geometry.js").default|import("../render/Feature.js").default|undefined)} GeometryFunction
*/ */
/** /**
* Custom renderer function. Takes two arguments: * Custom renderer function. Takes two arguments:
* *
* 1. The pixel coordinates of the geometry in GeoJSON notation. * 1. The pixel coordinates of the geometry in GeoJSON notation.
* 2. The {@link module:ol/render~State} of the layer renderer. * 2. The {@link module:ol/render~State} of the layer renderer.
* *
* @typedef {function((import("../coordinate.js").Coordinate|Array<import("../coordinate.js").Coordinate>|Array<Array<import("../coordinate.js").Coordinate>>),import("../render.js").State): void} * @typedef {function((import("../coordinate.js").Coordinate|Array<import("../coordinate.js").Coordinate>|Array<Array<import("../coordinate.js").Coordinate>>),import("../render.js").State): void}
* RenderFunction * RenderFunction
*/ */
/** /**
* @typedef {Object} Options * @typedef {Object} Options
* @property {string|import("../geom/Geometry.js").default|GeometryFunction} [geometry] Feature property or geometry * @property {string|import("../geom/Geometry.js").default|GeometryFunction} [geometry] Feature property or geometry
* or function returning a geometry to render for this style. * or function returning a geometry to render for this style.
* @property {import("./Fill.js").default} [fill] Fill style. * @property {import("./Fill.js").default} [fill] Fill style.
* @property {import("./Image.js").default} [image] Image style. * @property {import("./Image.js").default} [image] Image style.
* @property {RenderFunction} [renderer] Custom renderer. When configured, `fill`, `stroke` and `image` will be * @property {RenderFunction} [renderer] Custom renderer. When configured, `fill`, `stroke` and `image` will be
* ignored, and the provided function will be called with each render frame for each geometry. * ignored, and the provided function will be called with each render frame for each geometry.
* @property {import("./Stroke.js").default} [stroke] Stroke style. * @property {RenderFunction} [hitDetectionRenderer] Custom renderer for hit detection. If provided will be used
* @property {import("./Text.js").default} [text] Text style. * in hit detection rendering.
* @property {number} [zIndex] Z index. * @property {import("./Stroke.js").default} [stroke] Stroke style.
*/ * @property {import("./Text.js").default} [text] Text style.
* @property {number} [zIndex] Z index.
*/
/** /**
* @classdesc * @classdesc
* Container for vector feature rendering styles. Any changes made to the style * Container for vector feature rendering styles. Any changes made to the style
* or its children through `set*()` methods will not take effect until the * or its children through `set*()` methods will not take effect until the
* feature or layer that uses the style is re-rendered. * feature or layer that uses the style is re-rendered.
* *
* ## Feature styles * ## Feature styles
* *
* If no style is defined, the following default style is used: * If no style is defined, the following default style is used:
* ```js * ```js
* import {Fill, Stroke, Circle, Style} from 'ol/style'; * import {Fill, Stroke, Circle, Style} from 'ol/style';
* *
* var fill = new Fill({ * var fill = new Fill({
* color: 'rgba(255,255,255,0.4)' * color: 'rgba(255,255,255,0.4)'
* }); * });
* var stroke = new Stroke({ * var stroke = new Stroke({
* color: '#3399CC', * color: '#3399CC',
* width: 1.25 * width: 1.25
* }); * });
* var styles = [ * var styles = [
* new Style({ * new Style({
* image: new Circle({ * image: new Circle({
* fill: fill, * fill: fill,
* stroke: stroke, * stroke: stroke,
* radius: 5 * radius: 5
* }), * }),
* fill: fill, * fill: fill,
* stroke: stroke * stroke: stroke
* }) * })
* ]; * ];
* ``` * ```
* *
* A separate editing style has the following defaults: * A separate editing style has the following defaults:
* ```js * ```js
* import {Fill, Stroke, Circle, Style} from 'ol/style'; * import {Fill, Stroke, Circle, Style} from 'ol/style';
* import GeometryType from 'ol/geom/GeometryType'; * import GeometryType from 'ol/geom/GeometryType';
* *
* var white = [255, 255, 255, 1]; * var white = [255, 255, 255, 1];
* var blue = [0, 153, 255, 1]; * var blue = [0, 153, 255, 1];
* var width = 3; * var width = 3;
* styles[GeometryType.POLYGON] = [ * styles[GeometryType.POLYGON] = [
* new Style({ * new Style({
* fill: new Fill({ * fill: new Fill({
* color: [255, 255, 255, 0.5] * color: [255, 255, 255, 0.5]
* }) * })
* }) * })
* ]; * ];
* styles[GeometryType.MULTI_POLYGON] = * styles[GeometryType.MULTI_POLYGON] =
* styles[GeometryType.POLYGON]; * styles[GeometryType.POLYGON];
* styles[GeometryType.LINE_STRING] = [ * styles[GeometryType.LINE_STRING] = [
* new Style({ * new Style({
* stroke: new Stroke({ * stroke: new Stroke({
* color: white, * color: white,
* width: width + 2 * width: width + 2
* }) * })
* }), * }),
* new Style({ * new Style({
* stroke: new Stroke({ * stroke: new Stroke({
* color: blue, * color: blue,
* width: width * width: width
* }) * })
* }) * })
* ]; * ];
* styles[GeometryType.MULTI_LINE_STRING] = * styles[GeometryType.MULTI_LINE_STRING] =
* styles[GeometryType.LINE_STRING]; * styles[GeometryType.LINE_STRING];
* styles[GeometryType.POINT] = [ * styles[GeometryType.POINT] = [
* new Style({ * new Style({
* image: new Circle({ * image: new Circle({
* radius: width * 2, * radius: width * 2,
* fill: new Fill({ * fill: new Fill({
* color: blue * color: blue
* }), * }),
* stroke: new Stroke({ * stroke: new Stroke({
* color: white, * color: white,
* width: width / 2 * width: width / 2
* }) * })
* }), * }),
* zIndex: Infinity * zIndex: Infinity
* }) * })
* ]; * ];
* styles[GeometryType.MULTI_POINT] = * styles[GeometryType.MULTI_POINT] =
* styles[GeometryType.POINT]; * styles[GeometryType.POINT];
* styles[GeometryType.GEOMETRY_COLLECTION] = * styles[GeometryType.GEOMETRY_COLLECTION] =
* styles[GeometryType.POLYGON].concat( * styles[GeometryType.POLYGON].concat(
* styles[GeometryType.LINE_STRING], * styles[GeometryType.LINE_STRING],
* styles[GeometryType.POINT] * styles[GeometryType.POINT]
* ); * );
* ``` * ```
* *
* @api * @api
*/ */
class Style { class Style {
/** /**
* @param {Options} [opt_options] Style options. * @param {Options} [opt_options] Style options.
@@ -186,6 +188,12 @@ class Style {
*/ */
this.renderer_ = options.renderer !== undefined ? options.renderer : null; this.renderer_ = options.renderer !== undefined ? options.renderer : null;
/**
* @private
* @type {RenderFunction|null}
*/
this.hitDetectionRenderer_ = options.hitDetectionRenderer !== undefined ? options.hitDetectionRenderer : null;
/** /**
* @private * @private
* @type {import("./Stroke.js").default} * @type {import("./Stroke.js").default}
@@ -248,6 +256,27 @@ class Style {
this.renderer_ = renderer; this.renderer_ = renderer;
} }
/**
* Sets a custom renderer function for this style used
* in hit detection.
* @param {RenderFunction|null} renderer Custom renderer function.
* @api
*/
setHitDetectionRenderer(renderer) {
this.hitDetectionRenderer_ = renderer;
}
/**
* Get the custom renderer function that was configured with
* {@link #setHitDetectionRenderer} or the `hitDetectionRenderer` constructor option.
* @return {RenderFunction|null} Custom renderer function.
* @api
*/
getHitDetectionRenderer() {
return this.hitDetectionRenderer_;
}
/** /**
* Get the geometry to be rendered. * Get the geometry to be rendered.
* @return {string|import("../geom/Geometry.js").default|GeometryFunction} * @return {string|import("../geom/Geometry.js").default|GeometryFunction}
@@ -389,13 +418,13 @@ class Style {
} }
/** /**
* Convert the provided object into a style function. Functions passed through * Convert the provided object into a style function. Functions passed through
* unchanged. Arrays of Style or single style objects wrapped in a * unchanged. Arrays of Style or single style objects wrapped in a
* new style function. * new style function.
* @param {StyleFunction|Array<Style>|Style} obj * @param {StyleFunction|Array<Style>|Style} obj
* A style function, a single style, or an array of styles. * A style function, a single style, or an array of styles.
* @return {StyleFunction} A style function. * @return {StyleFunction} A style function.
*/ */
export function toFunction(obj) { export function toFunction(obj) {
let styleFunction; let styleFunction;
@@ -421,15 +450,15 @@ export function toFunction(obj) {
} }
/** /**
* @type {Array<Style>} * @type {Array<Style>}
*/ */
let defaultStyles = null; let defaultStyles = null;
/** /**
* @param {import("../Feature.js").FeatureLike} feature Feature. * @param {import("../Feature.js").FeatureLike} feature Feature.
* @param {number} resolution Resolution. * @param {number} resolution Resolution.
* @return {Array<Style>} Style. * @return {Array<Style>} Style.
*/ */
export function createDefaultStyle(feature, resolution) { export function createDefaultStyle(feature, resolution) {
// We don't use an immediately-invoked function // We don't use an immediately-invoked function
// and a closure so we don't get an error at script evaluation time in // and a closure so we don't get an error at script evaluation time in
@@ -460,9 +489,9 @@ export function createDefaultStyle(feature, resolution) {
} }
/** /**
* Default styles for editing features. * Default styles for editing features.
* @return {Object<import("../geom/GeometryType.js").default, Array<Style>>} Styles * @return {Object<import("../geom/GeometryType.js").default, Array<Style>>} Styles
*/ */
export function createEditingStyle() { export function createEditingStyle() {
/** @type {Object<import("../geom/GeometryType.js").default, Array<Style>>} */ /** @type {Object<import("../geom/GeometryType.js").default, Array<Style>>} */
const styles = {}; const styles = {};
@@ -523,10 +552,10 @@ export function createEditingStyle() {
} }
/** /**
* Function that is called with a feature and returns its default geometry. * Function that is called with a feature and returns its default geometry.
* @param {import("../Feature.js").FeatureLike} feature Feature to get the geometry for. * @param {import("../Feature.js").FeatureLike} feature Feature to get the geometry for.
* @return {import("../geom/Geometry.js").default|import("../render/Feature.js").default|undefined} Geometry to render. * @return {import("../geom/Geometry.js").default|import("../render/Feature.js").default|undefined} Geometry to render.
*/ */
function defaultGeometryFunction(feature) { function defaultGeometryFunction(feature) {
return feature.getGeometry(); return feature.getGeometry();
} }