Make code prettier
This updates ESLint and our shared eslint-config-openlayers to use Prettier. Most formatting changes were automatically applied with this:
npm run lint -- --fix
A few manual changes were required:
* In `examples/offscreen-canvas.js`, the `//eslint-disable-line` comment needed to be moved to the appropriate line to disable the error about the `'worker-loader!./offscreen-canvas.worker.js'` import.
* In `examples/webpack/exapmle-builder.js`, spaces could not be added after a couple `function`s for some reason. While editing this, I reworked `ExampleBuilder` to be a class.
* In `src/ol/format/WMSGetFeatureInfo.js`, the `// @ts-ignore` comment needed to be moved down one line so it applied to the `parsersNS` argument.
This commit is contained in:
@@ -4,7 +4,6 @@
|
||||
|
||||
import RegularShape from './RegularShape.js';
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Object} Options
|
||||
* @property {import("./Fill.js").default} [fill] Fill style.
|
||||
@@ -13,7 +12,6 @@ import RegularShape from './RegularShape.js';
|
||||
* @property {Array<number>} [displacement=[0,0]] displacement
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
* Set circle style for vector features.
|
||||
@@ -24,7 +22,6 @@ class CircleStyle extends RegularShape {
|
||||
* @param {Options=} opt_options Options.
|
||||
*/
|
||||
constructor(opt_options) {
|
||||
|
||||
const options = opt_options ? opt_options : {};
|
||||
|
||||
super({
|
||||
@@ -32,9 +29,9 @@ class CircleStyle extends RegularShape {
|
||||
fill: options.fill,
|
||||
radius: options.radius,
|
||||
stroke: options.stroke,
|
||||
displacement: options.displacement !== undefined ? options.displacement : [0, 0]
|
||||
displacement:
|
||||
options.displacement !== undefined ? options.displacement : [0, 0],
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,7 +44,7 @@ class CircleStyle extends RegularShape {
|
||||
fill: this.getFill() ? this.getFill().clone() : undefined,
|
||||
stroke: this.getStroke() ? this.getStroke().clone() : undefined,
|
||||
radius: this.getRadius(),
|
||||
displacement: this.getDisplacement().slice()
|
||||
displacement: this.getDisplacement().slice(),
|
||||
});
|
||||
style.setOpacity(this.getOpacity());
|
||||
style.setScale(this.getScale());
|
||||
@@ -66,5 +63,4 @@ class CircleStyle extends RegularShape {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default CircleStyle;
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
* @module ol/style/Fill
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Object} Options
|
||||
* @property {import("../color.js").Color|import("../colorlike.js").ColorLike} [color=null] A color, gradient or pattern.
|
||||
@@ -10,7 +9,6 @@
|
||||
* Default null; if null, the Canvas/renderer default black will be used.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
* Set fill style for vector features.
|
||||
@@ -21,7 +19,6 @@ class Fill {
|
||||
* @param {Options=} opt_options Options.
|
||||
*/
|
||||
constructor(opt_options) {
|
||||
|
||||
const options = opt_options || {};
|
||||
|
||||
/**
|
||||
@@ -39,7 +36,7 @@ class Fill {
|
||||
clone() {
|
||||
const color = this.getColor();
|
||||
return new Fill({
|
||||
color: Array.isArray(color) ? color.slice() : color || undefined
|
||||
color: Array.isArray(color) ? color.slice() : color || undefined,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -61,7 +58,6 @@ class Fill {
|
||||
setColor(color) {
|
||||
this.color_ = color;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Fill;
|
||||
|
||||
+64
-43
@@ -1,16 +1,15 @@
|
||||
/**
|
||||
* @module ol/style/Icon
|
||||
*/
|
||||
import {getUid} from '../util.js';
|
||||
import ImageState from '../ImageState.js';
|
||||
import {assert} from '../asserts.js';
|
||||
import {asArray} from '../color.js';
|
||||
import EventType from '../events/EventType.js';
|
||||
import IconAnchorUnits from './IconAnchorUnits.js';
|
||||
import {get as getIconImage} from './IconImage.js';
|
||||
import IconOrigin from './IconOrigin.js';
|
||||
import ImageState from '../ImageState.js';
|
||||
import ImageStyle from './Image.js';
|
||||
|
||||
import {asArray} from '../color.js';
|
||||
import {assert} from '../asserts.js';
|
||||
import {get as getIconImage} from './IconImage.js';
|
||||
import {getUid} from '../util.js';
|
||||
|
||||
/**
|
||||
* @typedef {Object} Options
|
||||
@@ -47,7 +46,6 @@ import ImageStyle from './Image.js';
|
||||
* @property {string} [src] Image source URI.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
* Set icon style for vector features.
|
||||
@@ -78,15 +76,16 @@ class Icon extends ImageStyle {
|
||||
/**
|
||||
* @type {boolean}
|
||||
*/
|
||||
const rotateWithView = options.rotateWithView !== undefined ?
|
||||
options.rotateWithView : false;
|
||||
const rotateWithView =
|
||||
options.rotateWithView !== undefined ? options.rotateWithView : false;
|
||||
|
||||
super({
|
||||
opacity: opacity,
|
||||
rotation: rotation,
|
||||
scale: scale,
|
||||
displacement: options.displacement !== undefined ? options.displacement : [0, 0],
|
||||
rotateWithView: rotateWithView
|
||||
displacement:
|
||||
options.displacement !== undefined ? options.displacement : [0, 0],
|
||||
rotateWithView: rotateWithView,
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -105,29 +104,35 @@ class Icon extends ImageStyle {
|
||||
* @private
|
||||
* @type {import("./IconOrigin.js").default}
|
||||
*/
|
||||
this.anchorOrigin_ = options.anchorOrigin !== undefined ?
|
||||
options.anchorOrigin : IconOrigin.TOP_LEFT;
|
||||
this.anchorOrigin_ =
|
||||
options.anchorOrigin !== undefined
|
||||
? options.anchorOrigin
|
||||
: IconOrigin.TOP_LEFT;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {import("./IconAnchorUnits.js").default}
|
||||
*/
|
||||
this.anchorXUnits_ = options.anchorXUnits !== undefined ?
|
||||
options.anchorXUnits : IconAnchorUnits.FRACTION;
|
||||
this.anchorXUnits_ =
|
||||
options.anchorXUnits !== undefined
|
||||
? options.anchorXUnits
|
||||
: IconAnchorUnits.FRACTION;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {import("./IconAnchorUnits.js").default}
|
||||
*/
|
||||
this.anchorYUnits_ = options.anchorYUnits !== undefined ?
|
||||
options.anchorYUnits : IconAnchorUnits.FRACTION;
|
||||
this.anchorYUnits_ =
|
||||
options.anchorYUnits !== undefined
|
||||
? options.anchorYUnits
|
||||
: IconAnchorUnits.FRACTION;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {?string}
|
||||
*/
|
||||
this.crossOrigin_ =
|
||||
options.crossOrigin !== undefined ? options.crossOrigin : null;
|
||||
options.crossOrigin !== undefined ? options.crossOrigin : null;
|
||||
|
||||
/**
|
||||
* @type {HTMLImageElement|HTMLCanvasElement}
|
||||
@@ -144,22 +149,19 @@ class Icon extends ImageStyle {
|
||||
*/
|
||||
let src = options.src;
|
||||
|
||||
assert(!(src !== undefined && image),
|
||||
4); // `image` and `src` cannot be provided at the same time
|
||||
assert(!image || (image && imgSize),
|
||||
5); // `imgSize` must be set when `image` is provided
|
||||
assert(!(src !== undefined && image), 4); // `image` and `src` cannot be provided at the same time
|
||||
assert(!image || (image && imgSize), 5); // `imgSize` must be set when `image` is provided
|
||||
|
||||
if ((src === undefined || src.length === 0) && image) {
|
||||
src = /** @type {HTMLImageElement} */ (image).src || getUid(image);
|
||||
}
|
||||
assert(src !== undefined && src.length > 0,
|
||||
6); // A defined and non-empty `src` or `image` must be provided
|
||||
assert(src !== undefined && src.length > 0, 6); // A defined and non-empty `src` or `image` must be provided
|
||||
|
||||
/**
|
||||
* @type {import("../ImageState.js").default}
|
||||
*/
|
||||
const imageState = options.src !== undefined ?
|
||||
ImageState.IDLE : ImageState.LOADED;
|
||||
const imageState =
|
||||
options.src !== undefined ? ImageState.IDLE : ImageState.LOADED;
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -172,7 +174,13 @@ class Icon extends ImageStyle {
|
||||
* @type {import("./IconImage.js").default}
|
||||
*/
|
||||
this.iconImage_ = getIconImage(
|
||||
image, /** @type {string} */ (src), imgSize, this.crossOrigin_, imageState, this.color_);
|
||||
image,
|
||||
/** @type {string} */ (src),
|
||||
imgSize,
|
||||
this.crossOrigin_,
|
||||
imageState,
|
||||
this.color_
|
||||
);
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -183,8 +191,10 @@ class Icon extends ImageStyle {
|
||||
* @private
|
||||
* @type {import("./IconOrigin.js").default}
|
||||
*/
|
||||
this.offsetOrigin_ = options.offsetOrigin !== undefined ?
|
||||
options.offsetOrigin : IconOrigin.TOP_LEFT;
|
||||
this.offsetOrigin_ =
|
||||
options.offsetOrigin !== undefined
|
||||
? options.offsetOrigin
|
||||
: IconOrigin.TOP_LEFT;
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -197,7 +207,6 @@ class Icon extends ImageStyle {
|
||||
* @type {import("../size.js").Size}
|
||||
*/
|
||||
this.size_ = options.size !== undefined ? options.size : null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,7 +221,10 @@ class Icon extends ImageStyle {
|
||||
anchorXUnits: this.anchorXUnits_,
|
||||
anchorYUnits: this.anchorYUnits_,
|
||||
crossOrigin: this.crossOrigin_,
|
||||
color: (this.color_ && this.color_.slice) ? this.color_.slice() : this.color_ || undefined,
|
||||
color:
|
||||
this.color_ && this.color_.slice
|
||||
? this.color_.slice()
|
||||
: this.color_ || undefined,
|
||||
src: this.getSrc(),
|
||||
offset: this.offset_.slice(),
|
||||
offsetOrigin: this.offsetOrigin_,
|
||||
@@ -220,7 +232,7 @@ class Icon extends ImageStyle {
|
||||
opacity: this.getOpacity(),
|
||||
scale: this.getScale(),
|
||||
rotation: this.getRotation(),
|
||||
rotateWithView: this.getRotateWithView()
|
||||
rotateWithView: this.getRotateWithView(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -236,8 +248,10 @@ class Icon extends ImageStyle {
|
||||
}
|
||||
let anchor = this.anchor_;
|
||||
const size = this.getSize();
|
||||
if (this.anchorXUnits_ == IconAnchorUnits.FRACTION ||
|
||||
this.anchorYUnits_ == IconAnchorUnits.FRACTION) {
|
||||
if (
|
||||
this.anchorXUnits_ == IconAnchorUnits.FRACTION ||
|
||||
this.anchorYUnits_ == IconAnchorUnits.FRACTION
|
||||
) {
|
||||
if (!size) {
|
||||
return null;
|
||||
}
|
||||
@@ -257,12 +271,16 @@ class Icon extends ImageStyle {
|
||||
if (anchor === this.anchor_) {
|
||||
anchor = this.anchor_.slice();
|
||||
}
|
||||
if (this.anchorOrigin_ == IconOrigin.TOP_RIGHT ||
|
||||
this.anchorOrigin_ == IconOrigin.BOTTOM_RIGHT) {
|
||||
if (
|
||||
this.anchorOrigin_ == IconOrigin.TOP_RIGHT ||
|
||||
this.anchorOrigin_ == IconOrigin.BOTTOM_RIGHT
|
||||
) {
|
||||
anchor[0] = -anchor[0] + size[0];
|
||||
}
|
||||
if (this.anchorOrigin_ == IconOrigin.BOTTOM_LEFT ||
|
||||
this.anchorOrigin_ == IconOrigin.BOTTOM_RIGHT) {
|
||||
if (
|
||||
this.anchorOrigin_ == IconOrigin.BOTTOM_LEFT ||
|
||||
this.anchorOrigin_ == IconOrigin.BOTTOM_RIGHT
|
||||
) {
|
||||
anchor[1] = -anchor[1] + size[1];
|
||||
}
|
||||
}
|
||||
@@ -349,12 +367,16 @@ class Icon extends ImageStyle {
|
||||
return null;
|
||||
}
|
||||
offset = offset.slice();
|
||||
if (this.offsetOrigin_ == IconOrigin.TOP_RIGHT ||
|
||||
this.offsetOrigin_ == IconOrigin.BOTTOM_RIGHT) {
|
||||
if (
|
||||
this.offsetOrigin_ == IconOrigin.TOP_RIGHT ||
|
||||
this.offsetOrigin_ == IconOrigin.BOTTOM_RIGHT
|
||||
) {
|
||||
offset[0] = iconImageSize[0] - size[0] - offset[0];
|
||||
}
|
||||
if (this.offsetOrigin_ == IconOrigin.BOTTOM_LEFT ||
|
||||
this.offsetOrigin_ == IconOrigin.BOTTOM_RIGHT) {
|
||||
if (
|
||||
this.offsetOrigin_ == IconOrigin.BOTTOM_LEFT ||
|
||||
this.offsetOrigin_ == IconOrigin.BOTTOM_RIGHT
|
||||
) {
|
||||
offset[1] = iconImageSize[1] - size[1] - offset[1];
|
||||
}
|
||||
}
|
||||
@@ -408,5 +430,4 @@ class Icon extends ImageStyle {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default Icon;
|
||||
|
||||
@@ -16,5 +16,5 @@ export default {
|
||||
* Anchor is in pixels
|
||||
* @api
|
||||
*/
|
||||
PIXELS: 'pixels'
|
||||
PIXELS: 'pixels',
|
||||
};
|
||||
|
||||
@@ -2,14 +2,13 @@
|
||||
* @module ol/style/IconImage
|
||||
*/
|
||||
|
||||
import {createCanvasContext2D} from '../dom.js';
|
||||
import EventTarget from '../events/Target.js';
|
||||
import EventType from '../events/EventType.js';
|
||||
import ImageState from '../ImageState.js';
|
||||
import {createCanvasContext2D} from '../dom.js';
|
||||
import {shared as iconImageCache} from './IconImageCache.js';
|
||||
import {listenImage} from '../Image.js';
|
||||
|
||||
|
||||
class IconImage extends EventTarget {
|
||||
/**
|
||||
* @param {HTMLImageElement|HTMLCanvasElement} image Image.
|
||||
@@ -20,7 +19,6 @@ class IconImage extends EventTarget {
|
||||
* @param {import("../color.js").Color} color Color.
|
||||
*/
|
||||
constructor(image, src, size, crossOrigin, imageState, color) {
|
||||
|
||||
super();
|
||||
|
||||
/**
|
||||
@@ -80,7 +78,6 @@ class IconImage extends EventTarget {
|
||||
* @type {boolean|undefined}
|
||||
*/
|
||||
this.tainted_;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -236,7 +233,12 @@ class IconImage extends EventTarget {
|
||||
return;
|
||||
}
|
||||
|
||||
const imgData = ctx.getImageData(0, 0, this.image_.width, this.image_.height);
|
||||
const imgData = ctx.getImageData(
|
||||
0,
|
||||
0,
|
||||
this.image_.width,
|
||||
this.image_.height
|
||||
);
|
||||
const data = imgData.data;
|
||||
const r = this.color_[0] / 255.0;
|
||||
const g = this.color_[1] / 255.0;
|
||||
@@ -263,7 +265,6 @@ class IconImage extends EventTarget {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {HTMLImageElement|HTMLCanvasElement} image Image.
|
||||
* @param {string} src Src.
|
||||
@@ -282,5 +283,4 @@ export function get(image, src, size, crossOrigin, imageState, color) {
|
||||
return iconImage;
|
||||
}
|
||||
|
||||
|
||||
export default IconImage;
|
||||
|
||||
@@ -9,29 +9,28 @@ import {asString} from '../color.js';
|
||||
*/
|
||||
class IconImageCache {
|
||||
constructor() {
|
||||
|
||||
/**
|
||||
* @type {!Object<string, import("./IconImage.js").default>}
|
||||
* @private
|
||||
*/
|
||||
* @type {!Object<string, import("./IconImage.js").default>}
|
||||
* @private
|
||||
*/
|
||||
this.cache_ = {};
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
* @private
|
||||
*/
|
||||
* @type {number}
|
||||
* @private
|
||||
*/
|
||||
this.cacheSize_ = 0;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
* @private
|
||||
*/
|
||||
* @type {number}
|
||||
* @private
|
||||
*/
|
||||
this.maxCacheSize_ = 32;
|
||||
}
|
||||
|
||||
/**
|
||||
* FIXME empty description for jsdoc
|
||||
*/
|
||||
* FIXME empty description for jsdoc
|
||||
*/
|
||||
clear() {
|
||||
this.cache_ = {};
|
||||
this.cacheSize_ = 0;
|
||||
@@ -45,8 +44,8 @@ class IconImageCache {
|
||||
}
|
||||
|
||||
/**
|
||||
* FIXME empty description for jsdoc
|
||||
*/
|
||||
* FIXME empty description for jsdoc
|
||||
*/
|
||||
expire() {
|
||||
if (this.canExpireCache()) {
|
||||
let i = 0;
|
||||
@@ -61,22 +60,22 @@ class IconImageCache {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} src Src.
|
||||
* @param {?string} crossOrigin Cross origin.
|
||||
* @param {import("../color.js").Color} color Color.
|
||||
* @return {import("./IconImage.js").default} Icon image.
|
||||
*/
|
||||
* @param {string} src Src.
|
||||
* @param {?string} crossOrigin Cross origin.
|
||||
* @param {import("../color.js").Color} color Color.
|
||||
* @return {import("./IconImage.js").default} Icon image.
|
||||
*/
|
||||
get(src, crossOrigin, color) {
|
||||
const key = getKey(src, crossOrigin, color);
|
||||
return key in this.cache_ ? this.cache_[key] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} src Src.
|
||||
* @param {?string} crossOrigin Cross origin.
|
||||
* @param {import("../color.js").Color} color Color.
|
||||
* @param {import("./IconImage.js").default} iconImage Icon image.
|
||||
*/
|
||||
* @param {string} src Src.
|
||||
* @param {?string} crossOrigin Cross origin.
|
||||
* @param {import("../color.js").Color} color Color.
|
||||
* @param {import("./IconImage.js").default} iconImage Icon image.
|
||||
*/
|
||||
set(src, crossOrigin, color, iconImage) {
|
||||
const key = getKey(src, crossOrigin, color);
|
||||
this.cache_[key] = iconImage;
|
||||
@@ -84,19 +83,18 @@ class IconImageCache {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the cache size of the icon cache. Default is `32`. Change this value when
|
||||
* your map uses more than 32 different icon images and you are not caching icon
|
||||
* styles on the application level.
|
||||
* @param {number} maxCacheSize Cache max size.
|
||||
* @api
|
||||
*/
|
||||
* Set the cache size of the icon cache. Default is `32`. Change this value when
|
||||
* your map uses more than 32 different icon images and you are not caching icon
|
||||
* styles on the application level.
|
||||
* @param {number} maxCacheSize Cache max size.
|
||||
* @api
|
||||
*/
|
||||
setSize(maxCacheSize) {
|
||||
this.maxCacheSize_ = maxCacheSize;
|
||||
this.expire();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} src Src.
|
||||
* @param {?string} crossOrigin Cross origin.
|
||||
@@ -108,10 +106,8 @@ function getKey(src, crossOrigin, color) {
|
||||
return crossOrigin + ':' + src + ':' + colorString;
|
||||
}
|
||||
|
||||
|
||||
export default IconImageCache;
|
||||
|
||||
|
||||
/**
|
||||
* The {@link module:ol/style/IconImageCache~IconImageCache} for
|
||||
* {@link module:ol/style/Icon~Icon} images.
|
||||
|
||||
@@ -26,5 +26,5 @@ export default {
|
||||
* Origin is at top right
|
||||
* @api
|
||||
*/
|
||||
TOP_RIGHT: 'top-right'
|
||||
TOP_RIGHT: 'top-right',
|
||||
};
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
*/
|
||||
import {abstract} from '../util.js';
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Object} Options
|
||||
* @property {number} opacity
|
||||
@@ -13,7 +12,6 @@ import {abstract} from '../util.js';
|
||||
* @property {Array<number>} displacement
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
* A base class used for creating subclasses and not instantiated in
|
||||
@@ -27,7 +25,6 @@ class ImageStyle {
|
||||
* @param {Options} options Options.
|
||||
*/
|
||||
constructor(options) {
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {number}
|
||||
@@ -57,7 +54,6 @@ class ImageStyle {
|
||||
* @type {Array<number>}
|
||||
*/
|
||||
this.displacement_ = options.displacement;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,7 +67,7 @@ class ImageStyle {
|
||||
scale: this.getScale(),
|
||||
rotation: this.getRotation(),
|
||||
rotateWithView: this.getRotateWithView(),
|
||||
displacement: this.getDisplacement().slice()
|
||||
displacement: this.getDisplacement().slice(),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -24,10 +24,9 @@ export const SymbolType = {
|
||||
CIRCLE: 'circle',
|
||||
SQUARE: 'square',
|
||||
TRIANGLE: 'triangle',
|
||||
IMAGE: 'image'
|
||||
IMAGE: 'image',
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Object} LiteralSymbolStyle
|
||||
* @property {ExpressionValue|Array<ExpressionValue, ExpressionValue>} size Size, mandatory.
|
||||
|
||||
@@ -2,13 +2,19 @@
|
||||
* @module ol/style/RegularShape
|
||||
*/
|
||||
|
||||
import ImageState from '../ImageState.js';
|
||||
import ImageStyle from './Image.js';
|
||||
import {asArray} from '../color.js';
|
||||
import {asColorLike} from '../colorlike.js';
|
||||
import {createCanvasContext2D} from '../dom.js';
|
||||
import ImageState from '../ImageState.js';
|
||||
import {defaultStrokeStyle, defaultFillStyle, defaultLineCap, defaultLineWidth, defaultLineJoin, defaultMiterLimit} from '../render/canvas.js';
|
||||
import ImageStyle from './Image.js';
|
||||
|
||||
import {
|
||||
defaultFillStyle,
|
||||
defaultLineCap,
|
||||
defaultLineJoin,
|
||||
defaultLineWidth,
|
||||
defaultMiterLimit,
|
||||
defaultStrokeStyle,
|
||||
} from '../render/canvas.js';
|
||||
|
||||
/**
|
||||
* Specify radius for regular polygons, or radius1 and radius2 for stars.
|
||||
@@ -26,7 +32,6 @@ import ImageStyle from './Image.js';
|
||||
* @property {boolean} [rotateWithView=false] Whether to rotate the shape with the view.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Object} RenderOptions
|
||||
* @property {import("../colorlike.js").ColorLike} [strokeStyle]
|
||||
@@ -39,7 +44,6 @@ import ImageStyle from './Image.js';
|
||||
* @property {number} miterLimit
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
* Set regular shape style for vector features. The resulting shape will be
|
||||
@@ -55,15 +59,16 @@ class RegularShape extends ImageStyle {
|
||||
/**
|
||||
* @type {boolean}
|
||||
*/
|
||||
const rotateWithView = options.rotateWithView !== undefined ?
|
||||
options.rotateWithView : false;
|
||||
const rotateWithView =
|
||||
options.rotateWithView !== undefined ? options.rotateWithView : false;
|
||||
|
||||
super({
|
||||
opacity: 1,
|
||||
rotateWithView: rotateWithView,
|
||||
rotation: options.rotation !== undefined ? options.rotation : 0,
|
||||
scale: 1,
|
||||
displacement: options.displacement !== undefined ? options.displacement : [0, 0]
|
||||
displacement:
|
||||
options.displacement !== undefined ? options.displacement : [0, 0],
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -100,7 +105,8 @@ class RegularShape extends ImageStyle {
|
||||
* @protected
|
||||
* @type {number}
|
||||
*/
|
||||
this.radius_ = options.radius !== undefined ? options.radius : options.radius1;
|
||||
this.radius_ =
|
||||
options.radius !== undefined ? options.radius : options.radius1;
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -145,7 +151,6 @@ class RegularShape extends ImageStyle {
|
||||
this.hitDetectionImageSize_ = null;
|
||||
|
||||
this.render();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -163,7 +168,7 @@ class RegularShape extends ImageStyle {
|
||||
stroke: this.getStroke() ? this.getStroke().clone() : undefined,
|
||||
rotation: this.getRotation(),
|
||||
rotateWithView: this.getRotateWithView(),
|
||||
displacement: this.getDisplacement().slice()
|
||||
displacement: this.getDisplacement().slice(),
|
||||
});
|
||||
style.setOpacity(this.getOpacity());
|
||||
style.setScale(this.getScale());
|
||||
@@ -354,7 +359,7 @@ class RegularShape extends ImageStyle {
|
||||
lineDash: lineDash,
|
||||
lineDashOffset: lineDashOffset,
|
||||
lineJoin: lineJoin,
|
||||
miterLimit: miterLimit
|
||||
miterLimit: miterLimit,
|
||||
};
|
||||
|
||||
const context = createCanvasContext2D(size, size);
|
||||
@@ -394,23 +399,29 @@ class RegularShape extends ImageStyle {
|
||||
let points = this.points_;
|
||||
if (points === Infinity) {
|
||||
context.arc(
|
||||
renderOptions.size / 2, renderOptions.size / 2,
|
||||
this.radius_, 0, 2 * Math.PI, true);
|
||||
renderOptions.size / 2,
|
||||
renderOptions.size / 2,
|
||||
this.radius_,
|
||||
0,
|
||||
2 * Math.PI,
|
||||
true
|
||||
);
|
||||
} else {
|
||||
const radius2 = (this.radius2_ !== undefined) ? this.radius2_
|
||||
: this.radius_;
|
||||
const radius2 =
|
||||
this.radius2_ !== undefined ? this.radius2_ : this.radius_;
|
||||
if (radius2 !== this.radius_) {
|
||||
points = 2 * points;
|
||||
}
|
||||
for (i = 0; i <= points; i++) {
|
||||
angle0 = i * 2 * Math.PI / points - Math.PI / 2 + this.angle_;
|
||||
angle0 = (i * 2 * Math.PI) / points - Math.PI / 2 + this.angle_;
|
||||
radiusC = i % 2 === 0 ? this.radius_ : radius2;
|
||||
context.lineTo(renderOptions.size / 2 + radiusC * Math.cos(angle0),
|
||||
renderOptions.size / 2 + radiusC * Math.sin(angle0));
|
||||
context.lineTo(
|
||||
renderOptions.size / 2 + radiusC * Math.cos(angle0),
|
||||
renderOptions.size / 2 + radiusC * Math.sin(angle0)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (this.fill_) {
|
||||
let color = this.fill_.getColor();
|
||||
if (color === null) {
|
||||
@@ -455,16 +466,17 @@ class RegularShape extends ImageStyle {
|
||||
opacity = color.length === 4 ? color[3] : 1;
|
||||
}
|
||||
if (opacity === 0) {
|
||||
|
||||
// if a transparent fill style is set, create an extra hit-detection image
|
||||
// with a default fill style
|
||||
const context = createCanvasContext2D(renderOptions.size, renderOptions.size);
|
||||
const context = createCanvasContext2D(
|
||||
renderOptions.size,
|
||||
renderOptions.size
|
||||
);
|
||||
this.hitDetectionCanvas_ = context.canvas;
|
||||
|
||||
this.drawHitDetectionCanvas_(renderOptions, context, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -486,20 +498,27 @@ class RegularShape extends ImageStyle {
|
||||
let points = this.points_;
|
||||
if (points === Infinity) {
|
||||
context.arc(
|
||||
renderOptions.size / 2, renderOptions.size / 2,
|
||||
this.radius_, 0, 2 * Math.PI, true);
|
||||
renderOptions.size / 2,
|
||||
renderOptions.size / 2,
|
||||
this.radius_,
|
||||
0,
|
||||
2 * Math.PI,
|
||||
true
|
||||
);
|
||||
} else {
|
||||
const radius2 = (this.radius2_ !== undefined) ? this.radius2_
|
||||
: this.radius_;
|
||||
const radius2 =
|
||||
this.radius2_ !== undefined ? this.radius2_ : this.radius_;
|
||||
if (radius2 !== this.radius_) {
|
||||
points = 2 * points;
|
||||
}
|
||||
let i, radiusC, angle0;
|
||||
for (i = 0; i <= points; i++) {
|
||||
angle0 = i * 2 * Math.PI / points - Math.PI / 2 + this.angle_;
|
||||
angle0 = (i * 2 * Math.PI) / points - Math.PI / 2 + this.angle_;
|
||||
radiusC = i % 2 === 0 ? this.radius_ : radius2;
|
||||
context.lineTo(renderOptions.size / 2 + radiusC * Math.cos(angle0),
|
||||
renderOptions.size / 2 + radiusC * Math.sin(angle0));
|
||||
context.lineTo(
|
||||
renderOptions.size / 2 + radiusC * Math.cos(angle0),
|
||||
renderOptions.size / 2 + radiusC * Math.sin(angle0)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -516,8 +535,6 @@ class RegularShape extends ImageStyle {
|
||||
}
|
||||
context.closePath();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export default RegularShape;
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
* @module ol/style/Stroke
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Object} Options
|
||||
* @property {import("../color.js").Color|import("../colorlike.js").ColorLike} [color] A color, gradient or pattern.
|
||||
@@ -18,7 +17,6 @@
|
||||
* @property {number} [width] Width.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
* Set stroke style for vector features.
|
||||
@@ -32,7 +30,6 @@ class Stroke {
|
||||
* @param {Options=} opt_options Options.
|
||||
*/
|
||||
constructor(opt_options) {
|
||||
|
||||
const options = opt_options || {};
|
||||
|
||||
/**
|
||||
@@ -92,7 +89,7 @@ class Stroke {
|
||||
lineDashOffset: this.getLineDashOffset(),
|
||||
lineJoin: this.getLineJoin(),
|
||||
miterLimit: this.getMiterLimit(),
|
||||
width: this.getWidth()
|
||||
width: this.getWidth(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -234,7 +231,6 @@ class Stroke {
|
||||
setWidth(width) {
|
||||
this.width_ = width;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Stroke;
|
||||
|
||||
+41
-61
@@ -2,12 +2,11 @@
|
||||
* @module ol/style/Style
|
||||
*/
|
||||
|
||||
import {assert} from '../asserts.js';
|
||||
import GeometryType from '../geom/GeometryType.js';
|
||||
import CircleStyle from './Circle.js';
|
||||
import Fill from './Fill.js';
|
||||
import GeometryType from '../geom/GeometryType.js';
|
||||
import Stroke from './Stroke.js';
|
||||
|
||||
import {assert} from '../asserts.js';
|
||||
|
||||
/**
|
||||
* A function that takes an {@link module:ol/Feature} and a `{number}`
|
||||
@@ -32,7 +31,6 @@ import Stroke from './Stroke.js';
|
||||
* (import("../geom/Geometry.js").default|import("../render/Feature.js").default|undefined)} GeometryFunction
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Custom renderer function. Takes two arguments:
|
||||
*
|
||||
@@ -43,7 +41,6 @@ import Stroke from './Stroke.js';
|
||||
* RenderFunction
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Object} Options
|
||||
* @property {string|import("../geom/Geometry.js").default|GeometryFunction} [geometry] Feature property or geometry
|
||||
@@ -153,7 +150,6 @@ class Style {
|
||||
* @param {Options=} opt_options Style options.
|
||||
*/
|
||||
constructor(opt_options) {
|
||||
|
||||
const options = opt_options || {};
|
||||
|
||||
/**
|
||||
@@ -179,9 +175,9 @@ class Style {
|
||||
this.fill_ = options.fill !== undefined ? options.fill : null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {import("./Image.js").default}
|
||||
*/
|
||||
* @private
|
||||
* @type {import("./Image.js").default}
|
||||
*/
|
||||
this.image_ = options.image !== undefined ? options.image : null;
|
||||
|
||||
/**
|
||||
@@ -207,7 +203,6 @@ class Style {
|
||||
* @type {number|undefined}
|
||||
*/
|
||||
this.zIndex_ = options.zIndex;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -226,7 +221,7 @@ class Style {
|
||||
image: this.getImage() ? this.getImage().clone() : undefined,
|
||||
stroke: this.getStroke() ? this.getStroke().clone() : undefined,
|
||||
text: this.getText() ? this.getText().clone() : undefined,
|
||||
zIndex: this.getZIndex()
|
||||
zIndex: this.getZIndex(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -364,18 +359,16 @@ class Style {
|
||||
if (typeof geometry === 'function') {
|
||||
this.geometryFunction_ = geometry;
|
||||
} else if (typeof geometry === 'string') {
|
||||
this.geometryFunction_ = function(feature) {
|
||||
return (
|
||||
/** @type {import("../geom/Geometry.js").default} */ (feature.get(geometry))
|
||||
);
|
||||
this.geometryFunction_ = function (feature) {
|
||||
return /** @type {import("../geom/Geometry.js").default} */ (feature.get(
|
||||
geometry
|
||||
));
|
||||
};
|
||||
} else if (!geometry) {
|
||||
this.geometryFunction_ = defaultGeometryFunction;
|
||||
} else if (geometry !== undefined) {
|
||||
this.geometryFunction_ = function() {
|
||||
return (
|
||||
/** @type {import("../geom/Geometry.js").default} */ (geometry)
|
||||
);
|
||||
this.geometryFunction_ = function () {
|
||||
return /** @type {import("../geom/Geometry.js").default} */ (geometry);
|
||||
};
|
||||
}
|
||||
this.geometry_ = geometry;
|
||||
@@ -392,7 +385,6 @@ class Style {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert the provided object into a style function. Functions passed through
|
||||
* unchanged. Arrays of Style or single style objects wrapped in a
|
||||
@@ -414,25 +406,22 @@ export function toFunction(obj) {
|
||||
if (Array.isArray(obj)) {
|
||||
styles = obj;
|
||||
} else {
|
||||
assert(typeof /** @type {?} */ (obj).getZIndex === 'function',
|
||||
41); // Expected an `Style` or an array of `Style`
|
||||
assert(typeof (/** @type {?} */ (obj).getZIndex) === 'function', 41); // Expected an `Style` or an array of `Style`
|
||||
const style = /** @type {Style} */ (obj);
|
||||
styles = [style];
|
||||
}
|
||||
styleFunction = function() {
|
||||
styleFunction = function () {
|
||||
return styles;
|
||||
};
|
||||
}
|
||||
return styleFunction;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @type {Array<Style>}
|
||||
*/
|
||||
let defaultStyles = null;
|
||||
|
||||
|
||||
/**
|
||||
* @param {import("../Feature.js").FeatureLike} feature Feature.
|
||||
* @param {number} resolution Resolution.
|
||||
@@ -446,28 +435,27 @@ export function createDefaultStyle(feature, resolution) {
|
||||
// in such browsers.)
|
||||
if (!defaultStyles) {
|
||||
const fill = new Fill({
|
||||
color: 'rgba(255,255,255,0.4)'
|
||||
color: 'rgba(255,255,255,0.4)',
|
||||
});
|
||||
const stroke = new Stroke({
|
||||
color: '#3399CC',
|
||||
width: 1.25
|
||||
width: 1.25,
|
||||
});
|
||||
defaultStyles = [
|
||||
new Style({
|
||||
image: new CircleStyle({
|
||||
fill: fill,
|
||||
stroke: stroke,
|
||||
radius: 5
|
||||
radius: 5,
|
||||
}),
|
||||
fill: fill,
|
||||
stroke: stroke
|
||||
})
|
||||
stroke: stroke,
|
||||
}),
|
||||
];
|
||||
}
|
||||
return defaultStyles;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Default styles for editing features.
|
||||
* @return {Object<import("../geom/GeometryType.js").default, Array<Style>>} Styles
|
||||
@@ -481,64 +469,56 @@ export function createEditingStyle() {
|
||||
styles[GeometryType.POLYGON] = [
|
||||
new Style({
|
||||
fill: new Fill({
|
||||
color: [255, 255, 255, 0.5]
|
||||
})
|
||||
})
|
||||
color: [255, 255, 255, 0.5],
|
||||
}),
|
||||
}),
|
||||
];
|
||||
styles[GeometryType.MULTI_POLYGON] =
|
||||
styles[GeometryType.POLYGON];
|
||||
styles[GeometryType.MULTI_POLYGON] = styles[GeometryType.POLYGON];
|
||||
|
||||
styles[GeometryType.LINE_STRING] = [
|
||||
new Style({
|
||||
stroke: new Stroke({
|
||||
color: white,
|
||||
width: width + 2
|
||||
})
|
||||
width: width + 2,
|
||||
}),
|
||||
}),
|
||||
new Style({
|
||||
stroke: new Stroke({
|
||||
color: blue,
|
||||
width: width
|
||||
})
|
||||
})
|
||||
width: width,
|
||||
}),
|
||||
}),
|
||||
];
|
||||
styles[GeometryType.MULTI_LINE_STRING] =
|
||||
styles[GeometryType.LINE_STRING];
|
||||
|
||||
styles[GeometryType.CIRCLE] =
|
||||
styles[GeometryType.POLYGON].concat(
|
||||
styles[GeometryType.LINE_STRING]
|
||||
);
|
||||
styles[GeometryType.MULTI_LINE_STRING] = styles[GeometryType.LINE_STRING];
|
||||
|
||||
styles[GeometryType.CIRCLE] = styles[GeometryType.POLYGON].concat(
|
||||
styles[GeometryType.LINE_STRING]
|
||||
);
|
||||
|
||||
styles[GeometryType.POINT] = [
|
||||
new Style({
|
||||
image: new CircleStyle({
|
||||
radius: width * 2,
|
||||
fill: new Fill({
|
||||
color: blue
|
||||
color: blue,
|
||||
}),
|
||||
stroke: new Stroke({
|
||||
color: white,
|
||||
width: width / 2
|
||||
})
|
||||
width: width / 2,
|
||||
}),
|
||||
}),
|
||||
zIndex: Infinity
|
||||
})
|
||||
zIndex: Infinity,
|
||||
}),
|
||||
];
|
||||
styles[GeometryType.MULTI_POINT] =
|
||||
styles[GeometryType.POINT];
|
||||
styles[GeometryType.MULTI_POINT] = styles[GeometryType.POINT];
|
||||
|
||||
styles[GeometryType.GEOMETRY_COLLECTION] =
|
||||
styles[GeometryType.POLYGON].concat(
|
||||
styles[GeometryType.LINE_STRING],
|
||||
styles[GeometryType.POINT]
|
||||
);
|
||||
styles[GeometryType.GEOMETRY_COLLECTION] = styles[
|
||||
GeometryType.POLYGON
|
||||
].concat(styles[GeometryType.LINE_STRING], styles[GeometryType.POINT]);
|
||||
|
||||
return styles;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Function that is called with a feature and returns its default geometry.
|
||||
* @param {import("../Feature.js").FeatureLike} feature Feature to get the geometry for.
|
||||
|
||||
+21
-13
@@ -4,7 +4,6 @@
|
||||
import Fill from './Fill.js';
|
||||
import TextPlacement from './TextPlacement.js';
|
||||
|
||||
|
||||
/**
|
||||
* The default fill color to use if no fill was set at construction time; a
|
||||
* blackish `#333`.
|
||||
@@ -13,7 +12,6 @@ import TextPlacement from './TextPlacement.js';
|
||||
*/
|
||||
const DEFAULT_FILL_COLOR = '#333';
|
||||
|
||||
|
||||
/**
|
||||
* @typedef {Object} Options
|
||||
* @property {string} [font] Font style as CSS 'font' value, see:
|
||||
@@ -44,7 +42,6 @@ const DEFAULT_FILL_COLOR = '#333';
|
||||
* values in the array is `[top, right, bottom, left]`.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @classdesc
|
||||
* Set text style for vector features.
|
||||
@@ -55,7 +52,6 @@ class Text {
|
||||
* @param {Options=} opt_options Options.
|
||||
*/
|
||||
constructor(opt_options) {
|
||||
|
||||
const options = opt_options || {};
|
||||
|
||||
/**
|
||||
@@ -104,20 +100,24 @@ class Text {
|
||||
* @private
|
||||
* @type {import("./Fill.js").default}
|
||||
*/
|
||||
this.fill_ = options.fill !== undefined ? options.fill :
|
||||
new Fill({color: DEFAULT_FILL_COLOR});
|
||||
this.fill_ =
|
||||
options.fill !== undefined
|
||||
? options.fill
|
||||
: new Fill({color: DEFAULT_FILL_COLOR});
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {number}
|
||||
*/
|
||||
this.maxAngle_ = options.maxAngle !== undefined ? options.maxAngle : Math.PI / 4;
|
||||
this.maxAngle_ =
|
||||
options.maxAngle !== undefined ? options.maxAngle : Math.PI / 4;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {import("./TextPlacement.js").default|string}
|
||||
*/
|
||||
this.placement_ = options.placement !== undefined ? options.placement : TextPlacement.POINT;
|
||||
this.placement_ =
|
||||
options.placement !== undefined ? options.placement : TextPlacement.POINT;
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -147,13 +147,17 @@ class Text {
|
||||
* @private
|
||||
* @type {import("./Fill.js").default}
|
||||
*/
|
||||
this.backgroundFill_ = options.backgroundFill ? options.backgroundFill : null;
|
||||
this.backgroundFill_ = options.backgroundFill
|
||||
? options.backgroundFill
|
||||
: null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {import("./Stroke.js").default}
|
||||
*/
|
||||
this.backgroundStroke_ = options.backgroundStroke ? options.backgroundStroke : null;
|
||||
this.backgroundStroke_ = options.backgroundStroke
|
||||
? options.backgroundStroke
|
||||
: null;
|
||||
|
||||
/**
|
||||
* @private
|
||||
@@ -183,9 +187,13 @@ class Text {
|
||||
stroke: this.getStroke() ? this.getStroke().clone() : undefined,
|
||||
offsetX: this.getOffsetX(),
|
||||
offsetY: this.getOffsetY(),
|
||||
backgroundFill: this.getBackgroundFill() ? this.getBackgroundFill().clone() : undefined,
|
||||
backgroundStroke: this.getBackgroundStroke() ? this.getBackgroundStroke().clone() : undefined,
|
||||
padding: this.getPadding()
|
||||
backgroundFill: this.getBackgroundFill()
|
||||
? this.getBackgroundFill().clone()
|
||||
: undefined,
|
||||
backgroundStroke: this.getBackgroundStroke()
|
||||
? this.getBackgroundStroke().clone()
|
||||
: undefined,
|
||||
padding: this.getPadding(),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -11,5 +11,5 @@
|
||||
*/
|
||||
export default {
|
||||
POINT: 'point',
|
||||
LINE: 'line'
|
||||
LINE: 'line',
|
||||
};
|
||||
|
||||
+212
-118
@@ -85,7 +85,7 @@ export const ValueTypes = {
|
||||
BOOLEAN: 0b01000,
|
||||
NUMBER_ARRAY: 0b10000,
|
||||
ANY: 0b11111,
|
||||
NONE: 0
|
||||
NONE: 0,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -126,8 +126,8 @@ export function getValueType(value) {
|
||||
if (!Array.isArray(value)) {
|
||||
throw new Error(`Unhandled value type: ${JSON.stringify(value)}`);
|
||||
}
|
||||
const valueArr = /** @type {Array<*>} */(value);
|
||||
const onlyNumbers = valueArr.every(function(v) {
|
||||
const valueArr = /** @type {Array<*>} */ (value);
|
||||
const onlyNumbers = valueArr.every(function (v) {
|
||||
return typeof v === 'number';
|
||||
});
|
||||
if (onlyNumbers) {
|
||||
@@ -137,11 +137,17 @@ export function getValueType(value) {
|
||||
return ValueTypes.NUMBER_ARRAY;
|
||||
}
|
||||
if (typeof valueArr[0] !== 'string') {
|
||||
throw new Error(`Expected an expression operator but received: ${JSON.stringify(valueArr)}`);
|
||||
throw new Error(
|
||||
`Expected an expression operator but received: ${JSON.stringify(
|
||||
valueArr
|
||||
)}`
|
||||
);
|
||||
}
|
||||
const operator = Operators[valueArr[0]];
|
||||
if (operator === undefined) {
|
||||
throw new Error(`Unrecognized expression operator: ${JSON.stringify(valueArr)}`);
|
||||
throw new Error(
|
||||
`Unrecognized expression operator: ${JSON.stringify(valueArr)}`
|
||||
);
|
||||
}
|
||||
return operator.getReturnType(valueArr.slice(1));
|
||||
}
|
||||
@@ -181,7 +187,9 @@ export function numberToGlsl(v) {
|
||||
*/
|
||||
export function arrayToGlsl(array) {
|
||||
if (array.length < 2 || array.length > 4) {
|
||||
throw new Error('`formatArray` can only output `vec2`, `vec3` or `vec4` arrays.');
|
||||
throw new Error(
|
||||
'`formatArray` can only output `vec2`, `vec3` or `vec4` arrays.'
|
||||
);
|
||||
}
|
||||
return `vec${array.length}(${array.map(numberToGlsl).join(', ')})`;
|
||||
}
|
||||
@@ -199,7 +207,7 @@ export function colorToGlsl(color) {
|
||||
array.push(1);
|
||||
}
|
||||
return arrayToGlsl(
|
||||
array.map(function(c, i) {
|
||||
array.map(function (c, i) {
|
||||
return i < 3 ? c / 255 : c;
|
||||
})
|
||||
);
|
||||
@@ -213,7 +221,9 @@ export function colorToGlsl(color) {
|
||||
*/
|
||||
export function getStringNumberEquivalent(context, string) {
|
||||
if (context.stringLiteralsMap[string] === undefined) {
|
||||
context.stringLiteralsMap[string] = Object.keys(context.stringLiteralsMap).length;
|
||||
context.stringLiteralsMap[string] = Object.keys(
|
||||
context.stringLiteralsMap
|
||||
).length;
|
||||
}
|
||||
return context.stringLiteralsMap[string];
|
||||
}
|
||||
@@ -242,31 +252,35 @@ export function expressionToGlsl(context, value, typeHint) {
|
||||
if (Array.isArray(value) && typeof value[0] === 'string') {
|
||||
const operator = Operators[value[0]];
|
||||
if (operator === undefined) {
|
||||
throw new Error(`Unrecognized expression operator: ${JSON.stringify(value)}`);
|
||||
throw new Error(
|
||||
`Unrecognized expression operator: ${JSON.stringify(value)}`
|
||||
);
|
||||
}
|
||||
return operator.toGlsl(context, value.slice(1), typeHint);
|
||||
} else if ((getValueType(value) & ValueTypes.NUMBER) > 0) {
|
||||
return numberToGlsl(/** @type {number} */(value));
|
||||
return numberToGlsl(/** @type {number} */ (value));
|
||||
} else if ((getValueType(value) & ValueTypes.BOOLEAN) > 0) {
|
||||
return value.toString();
|
||||
} else if (
|
||||
((getValueType(value) & ValueTypes.STRING) > 0) &&
|
||||
(getValueType(value) & ValueTypes.STRING) > 0 &&
|
||||
(typeHint === undefined || typeHint == ValueTypes.STRING)
|
||||
) {
|
||||
return stringToGlsl(context, value.toString());
|
||||
} else if (
|
||||
((getValueType(value) & ValueTypes.COLOR) > 0) &&
|
||||
(getValueType(value) & ValueTypes.COLOR) > 0 &&
|
||||
(typeHint === undefined || typeHint == ValueTypes.COLOR)
|
||||
) {
|
||||
return colorToGlsl(/** @type {number[]|string} */(value));
|
||||
return colorToGlsl(/** @type {number[]|string} */ (value));
|
||||
} else if ((getValueType(value) & ValueTypes.NUMBER_ARRAY) > 0) {
|
||||
return arrayToGlsl(/** @type {number[]} */(value));
|
||||
return arrayToGlsl(/** @type {number[]} */ (value));
|
||||
}
|
||||
}
|
||||
|
||||
function assertNumber(value) {
|
||||
if (!(getValueType(value) & ValueTypes.NUMBER)) {
|
||||
throw new Error(`A numeric value was expected, got ${JSON.stringify(value)} instead`);
|
||||
throw new Error(
|
||||
`A numeric value was expected, got ${JSON.stringify(value)} instead`
|
||||
);
|
||||
}
|
||||
}
|
||||
function assertNumbers(values) {
|
||||
@@ -276,50 +290,68 @@ function assertNumbers(values) {
|
||||
}
|
||||
function assertString(value) {
|
||||
if (!(getValueType(value) & ValueTypes.STRING)) {
|
||||
throw new Error(`A string value was expected, got ${JSON.stringify(value)} instead`);
|
||||
throw new Error(
|
||||
`A string value was expected, got ${JSON.stringify(value)} instead`
|
||||
);
|
||||
}
|
||||
}
|
||||
function assertBoolean(value) {
|
||||
if (!(getValueType(value) & ValueTypes.BOOLEAN)) {
|
||||
throw new Error(`A boolean value was expected, got ${JSON.stringify(value)} instead`);
|
||||
throw new Error(
|
||||
`A boolean value was expected, got ${JSON.stringify(value)} instead`
|
||||
);
|
||||
}
|
||||
}
|
||||
function assertArgsCount(args, count) {
|
||||
if (args.length !== count) {
|
||||
throw new Error(`Exactly ${count} arguments were expected, got ${args.length} instead`);
|
||||
throw new Error(
|
||||
`Exactly ${count} arguments were expected, got ${args.length} instead`
|
||||
);
|
||||
}
|
||||
}
|
||||
function assertArgsMinCount(args, count) {
|
||||
if (args.length < count) {
|
||||
throw new Error(`At least ${count} arguments were expected, got ${args.length} instead`);
|
||||
throw new Error(
|
||||
`At least ${count} arguments were expected, got ${args.length} instead`
|
||||
);
|
||||
}
|
||||
}
|
||||
function assertArgsMaxCount(args, count) {
|
||||
if (args.length > count) {
|
||||
throw new Error(`At most ${count} arguments were expected, got ${args.length} instead`);
|
||||
throw new Error(
|
||||
`At most ${count} arguments were expected, got ${args.length} instead`
|
||||
);
|
||||
}
|
||||
}
|
||||
function assertArgsEven(args) {
|
||||
if (args.length % 2 !== 0) {
|
||||
throw new Error(`An even amount of arguments was expected, got ${args} instead`);
|
||||
throw new Error(
|
||||
`An even amount of arguments was expected, got ${args} instead`
|
||||
);
|
||||
}
|
||||
}
|
||||
function assertArgsOdd(args) {
|
||||
if (args.length % 2 === 0) {
|
||||
throw new Error(`An even amount of arguments was expected, got ${args} instead`);
|
||||
throw new Error(
|
||||
`An even amount of arguments was expected, got ${args} instead`
|
||||
);
|
||||
}
|
||||
}
|
||||
function assertUniqueInferredType(args, types) {
|
||||
if (!isTypeUnique(types)) {
|
||||
throw new Error(`Could not infer only one type from the following expression: ${JSON.stringify(args)}`);
|
||||
throw new Error(
|
||||
`Could not infer only one type from the following expression: ${JSON.stringify(
|
||||
args
|
||||
)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Operators['get'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.ANY;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 1);
|
||||
assertString(args[0]);
|
||||
const value = args[0].toString();
|
||||
@@ -328,13 +360,13 @@ Operators['get'] = {
|
||||
}
|
||||
const prefix = context.inFragmentShader ? 'v_' : 'a_';
|
||||
return prefix + value;
|
||||
}
|
||||
},
|
||||
};
|
||||
Operators['var'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.ANY;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 1);
|
||||
assertString(args[0]);
|
||||
const value = args[0].toString();
|
||||
@@ -342,156 +374,186 @@ Operators['var'] = {
|
||||
context.variables.push(value);
|
||||
}
|
||||
return `u_${value}`;
|
||||
}
|
||||
},
|
||||
};
|
||||
Operators['time'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.NUMBER;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 0);
|
||||
return 'u_time';
|
||||
}
|
||||
},
|
||||
};
|
||||
Operators['zoom'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.NUMBER;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 0);
|
||||
return 'u_zoom';
|
||||
}
|
||||
},
|
||||
};
|
||||
Operators['resolution'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.NUMBER;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 0);
|
||||
return 'u_resolution';
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Operators['*'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.NUMBER;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 2);
|
||||
assertNumbers(args);
|
||||
return `(${expressionToGlsl(context, args[0])} * ${expressionToGlsl(context, args[1])})`;
|
||||
}
|
||||
return `(${expressionToGlsl(context, args[0])} * ${expressionToGlsl(
|
||||
context,
|
||||
args[1]
|
||||
)})`;
|
||||
},
|
||||
};
|
||||
Operators['/'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.NUMBER;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 2);
|
||||
assertNumbers(args);
|
||||
return `(${expressionToGlsl(context, args[0])} / ${expressionToGlsl(context, args[1])})`;
|
||||
}
|
||||
return `(${expressionToGlsl(context, args[0])} / ${expressionToGlsl(
|
||||
context,
|
||||
args[1]
|
||||
)})`;
|
||||
},
|
||||
};
|
||||
Operators['+'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.NUMBER;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 2);
|
||||
assertNumbers(args);
|
||||
return `(${expressionToGlsl(context, args[0])} + ${expressionToGlsl(context, args[1])})`;
|
||||
}
|
||||
return `(${expressionToGlsl(context, args[0])} + ${expressionToGlsl(
|
||||
context,
|
||||
args[1]
|
||||
)})`;
|
||||
},
|
||||
};
|
||||
Operators['-'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.NUMBER;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 2);
|
||||
assertNumbers(args);
|
||||
return `(${expressionToGlsl(context, args[0])} - ${expressionToGlsl(context, args[1])})`;
|
||||
}
|
||||
return `(${expressionToGlsl(context, args[0])} - ${expressionToGlsl(
|
||||
context,
|
||||
args[1]
|
||||
)})`;
|
||||
},
|
||||
};
|
||||
Operators['clamp'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.NUMBER;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 3);
|
||||
assertNumbers(args);
|
||||
const min = expressionToGlsl(context, args[1]);
|
||||
const max = expressionToGlsl(context, args[2]);
|
||||
return `clamp(${expressionToGlsl(context, args[0])}, ${min}, ${max})`;
|
||||
}
|
||||
},
|
||||
};
|
||||
Operators['%'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.NUMBER;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 2);
|
||||
assertNumbers(args);
|
||||
return `mod(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(context, args[1])})`;
|
||||
}
|
||||
return `mod(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(
|
||||
context,
|
||||
args[1]
|
||||
)})`;
|
||||
},
|
||||
};
|
||||
Operators['^'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.NUMBER;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 2);
|
||||
assertNumbers(args);
|
||||
return `pow(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(context, args[1])})`;
|
||||
}
|
||||
return `pow(${expressionToGlsl(context, args[0])}, ${expressionToGlsl(
|
||||
context,
|
||||
args[1]
|
||||
)})`;
|
||||
},
|
||||
};
|
||||
|
||||
Operators['>'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.BOOLEAN;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 2);
|
||||
assertNumbers(args);
|
||||
return `(${expressionToGlsl(context, args[0])} > ${expressionToGlsl(context, args[1])})`;
|
||||
}
|
||||
return `(${expressionToGlsl(context, args[0])} > ${expressionToGlsl(
|
||||
context,
|
||||
args[1]
|
||||
)})`;
|
||||
},
|
||||
};
|
||||
Operators['>='] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.BOOLEAN;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 2);
|
||||
assertNumbers(args);
|
||||
return `(${expressionToGlsl(context, args[0])} >= ${expressionToGlsl(context, args[1])})`;
|
||||
}
|
||||
return `(${expressionToGlsl(context, args[0])} >= ${expressionToGlsl(
|
||||
context,
|
||||
args[1]
|
||||
)})`;
|
||||
},
|
||||
};
|
||||
Operators['<'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.BOOLEAN;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 2);
|
||||
assertNumbers(args);
|
||||
return `(${expressionToGlsl(context, args[0])} < ${expressionToGlsl(context, args[1])})`;
|
||||
}
|
||||
return `(${expressionToGlsl(context, args[0])} < ${expressionToGlsl(
|
||||
context,
|
||||
args[1]
|
||||
)})`;
|
||||
},
|
||||
};
|
||||
Operators['<='] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.BOOLEAN;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 2);
|
||||
assertNumbers(args);
|
||||
return `(${expressionToGlsl(context, args[0])} <= ${expressionToGlsl(context, args[1])})`;
|
||||
}
|
||||
return `(${expressionToGlsl(context, args[0])} <= ${expressionToGlsl(
|
||||
context,
|
||||
args[1]
|
||||
)})`;
|
||||
},
|
||||
};
|
||||
|
||||
function getEqualOperator(operator) {
|
||||
return {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.BOOLEAN;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 2);
|
||||
|
||||
// find common type
|
||||
@@ -500,82 +562,93 @@ function getEqualOperator(operator) {
|
||||
type = type & getValueType(args[i]);
|
||||
}
|
||||
if (type === 0) {
|
||||
throw new Error(`All arguments should be of compatible type, got ${JSON.stringify(args)} instead`);
|
||||
throw new Error(
|
||||
`All arguments should be of compatible type, got ${JSON.stringify(
|
||||
args
|
||||
)} instead`
|
||||
);
|
||||
}
|
||||
|
||||
return `(${expressionToGlsl(context, args[0], type)} ${operator} ${expressionToGlsl(context, args[1], type)})`;
|
||||
}
|
||||
return `(${expressionToGlsl(
|
||||
context,
|
||||
args[0],
|
||||
type
|
||||
)} ${operator} ${expressionToGlsl(context, args[1], type)})`;
|
||||
},
|
||||
};
|
||||
}
|
||||
Operators['=='] = getEqualOperator('==');
|
||||
Operators['!='] = getEqualOperator('!=');
|
||||
|
||||
Operators['!'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.BOOLEAN;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 1);
|
||||
assertBoolean(args[0]);
|
||||
return `(!${expressionToGlsl(context, args[0])})`;
|
||||
}
|
||||
},
|
||||
};
|
||||
Operators['between'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.BOOLEAN;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsCount(args, 3);
|
||||
assertNumbers(args);
|
||||
const min = expressionToGlsl(context, args[1]);
|
||||
const max = expressionToGlsl(context, args[2]);
|
||||
const value = expressionToGlsl(context, args[0]);
|
||||
return `(${value} >= ${min} && ${value} <= ${max})`;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Operators['array'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.NUMBER_ARRAY;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsMinCount(args, 2);
|
||||
assertArgsMaxCount(args, 4);
|
||||
assertNumbers(args);
|
||||
const parsedArgs = args.map(function(val) {
|
||||
const parsedArgs = args.map(function (val) {
|
||||
return expressionToGlsl(context, val, ValueTypes.NUMBER);
|
||||
});
|
||||
return `vec${args.length}(${parsedArgs.join(', ')})`;
|
||||
}
|
||||
},
|
||||
};
|
||||
Operators['color'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
return ValueTypes.COLOR;
|
||||
},
|
||||
toGlsl: function(context, args) {
|
||||
toGlsl: function (context, args) {
|
||||
assertArgsMinCount(args, 3);
|
||||
assertArgsMaxCount(args, 4);
|
||||
assertNumbers(args);
|
||||
const array = /** @type {number[]} */(args);
|
||||
const array = /** @type {number[]} */ (args);
|
||||
if (args.length === 3) {
|
||||
array.push(1);
|
||||
}
|
||||
const parsedArgs = args.map(function(val, i) {
|
||||
return expressionToGlsl(context, val, ValueTypes.NUMBER) + (i < 3 ? ' / 255.0' : '');
|
||||
const parsedArgs = args.map(function (val, i) {
|
||||
return (
|
||||
expressionToGlsl(context, val, ValueTypes.NUMBER) +
|
||||
(i < 3 ? ' / 255.0' : '')
|
||||
);
|
||||
});
|
||||
return `vec${args.length}(${parsedArgs.join(', ')})`;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Operators['interpolate'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
let type = ValueTypes.COLOR | ValueTypes.NUMBER;
|
||||
for (let i = 3; i < args.length; i += 2) {
|
||||
type = type & getValueType(args[i]);
|
||||
}
|
||||
return type;
|
||||
},
|
||||
toGlsl: function(context, args, opt_typeHint) {
|
||||
toGlsl: function (context, args, opt_typeHint) {
|
||||
assertArgsEven(args);
|
||||
assertArgsMinCount(args, 6);
|
||||
|
||||
@@ -583,12 +656,21 @@ Operators['interpolate'] = {
|
||||
const type = args[0];
|
||||
let interpolation;
|
||||
switch (type[0]) {
|
||||
case 'linear': interpolation = 1; break;
|
||||
case 'exponential': interpolation = type[1]; break;
|
||||
default: interpolation = null;
|
||||
case 'linear':
|
||||
interpolation = 1;
|
||||
break;
|
||||
case 'exponential':
|
||||
interpolation = type[1];
|
||||
break;
|
||||
default:
|
||||
interpolation = null;
|
||||
}
|
||||
if (!interpolation) {
|
||||
throw new Error(`Invalid interpolation type for "interpolate" operator, received: ${JSON.stringify(type)}`);
|
||||
throw new Error(
|
||||
`Invalid interpolation type for "interpolate" operator, received: ${JSON.stringify(
|
||||
type
|
||||
)}`
|
||||
);
|
||||
}
|
||||
|
||||
// compute input/output types
|
||||
@@ -603,13 +685,17 @@ Operators['interpolate'] = {
|
||||
const output1 = expressionToGlsl(context, args[i + 1], outputType);
|
||||
const stop2 = expressionToGlsl(context, args[i + 2]);
|
||||
const output2 = expressionToGlsl(context, args[i + 3], outputType);
|
||||
result = `mix(${result || output1}, ${output2}, pow(clamp((${input} - ${stop1}) / (${stop2} - ${stop1}), 0.0, 1.0), ${numberToGlsl(interpolation)}))`;
|
||||
result = `mix(${
|
||||
result || output1
|
||||
}, ${output2}, pow(clamp((${input} - ${stop1}) / (${stop2} - ${stop1}), 0.0, 1.0), ${numberToGlsl(
|
||||
interpolation
|
||||
)}))`;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
},
|
||||
};
|
||||
Operators['match'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
let type = ValueTypes.ANY;
|
||||
for (let i = 2; i < args.length; i += 2) {
|
||||
type = type & getValueType(args[i]);
|
||||
@@ -617,7 +703,7 @@ Operators['match'] = {
|
||||
type = type & getValueType(args[args.length - 1]);
|
||||
return type;
|
||||
},
|
||||
toGlsl: function(context, args, opt_typeHint) {
|
||||
toGlsl: function (context, args, opt_typeHint) {
|
||||
assertArgsEven(args);
|
||||
assertArgsMinCount(args, 4);
|
||||
|
||||
@@ -626,7 +712,11 @@ Operators['match'] = {
|
||||
assertUniqueInferredType(args, outputType);
|
||||
|
||||
const input = expressionToGlsl(context, args[0]);
|
||||
const fallback = expressionToGlsl(context, args[args.length - 1], outputType);
|
||||
const fallback = expressionToGlsl(
|
||||
context,
|
||||
args[args.length - 1],
|
||||
outputType
|
||||
);
|
||||
let result = null;
|
||||
for (let i = args.length - 3; i >= 1; i -= 2) {
|
||||
const match = expressionToGlsl(context, args[i]);
|
||||
@@ -634,10 +724,10 @@ Operators['match'] = {
|
||||
result = `(${input} == ${match} ? ${output} : ${result || fallback})`;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
},
|
||||
};
|
||||
Operators['case'] = {
|
||||
getReturnType: function(args) {
|
||||
getReturnType: function (args) {
|
||||
let type = ValueTypes.ANY;
|
||||
for (let i = 1; i < args.length; i += 2) {
|
||||
type = type & getValueType(args[i]);
|
||||
@@ -645,7 +735,7 @@ Operators['case'] = {
|
||||
type = type & getValueType(args[args.length - 1]);
|
||||
return type;
|
||||
},
|
||||
toGlsl: function(context, args, opt_typeHint) {
|
||||
toGlsl: function (context, args, opt_typeHint) {
|
||||
assertArgsOdd(args);
|
||||
assertArgsMinCount(args, 3);
|
||||
|
||||
@@ -656,7 +746,11 @@ Operators['case'] = {
|
||||
assertBoolean(args[i]);
|
||||
}
|
||||
|
||||
const fallback = expressionToGlsl(context, args[args.length - 1], outputType);
|
||||
const fallback = expressionToGlsl(
|
||||
context,
|
||||
args[args.length - 1],
|
||||
outputType
|
||||
);
|
||||
let result = null;
|
||||
for (let i = args.length - 3; i >= 0; i -= 2) {
|
||||
const condition = expressionToGlsl(context, args[i]);
|
||||
@@ -664,5 +758,5 @@ Operators['case'] = {
|
||||
result = `(${condition} ? ${output} : ${result || fallback})`;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user