Added all and any operator

This commit is contained in:
michael ryding
2020-06-17 20:04:41 +01:00
parent a396ca62d1
commit b25d7bc58a
2 changed files with 72 additions and 1 deletions

View File

@@ -53,6 +53,8 @@ import {asArray, isStringColor} from '../color.js';
* * `['==', value1, value2]` returns `true` if `value1` equals `value2`, or `false` otherwise. * * `['==', value1, value2]` returns `true` if `value1` equals `value2`, or `false` otherwise.
* * `['!=', value1, value2]` returns `true` if `value1` does not equal `value2`, or `false` otherwise. * * `['!=', value1, value2]` returns `true` if `value1` does not equal `value2`, or `false` otherwise.
* * `['!', value1]` returns `false` if `value1` is `true` or greater than `0`, or `true` otherwise. * * `['!', value1]` returns `false` if `value1` is `true` or greater than `0`, or `true` otherwise.
* * `['all', value1, value2, ...]` returns `true` if all the inputs are `true`, `false` otherwise.
* * `['any', value1, value2, ...]` returns `true` if any of the inputs are `true`, `false` otherwise.
* * `['between', value1, value2, value3]` returns `true` if `value1` is contained between `value2` and `value3` * * `['between', value1, value2, value3]` returns `true` if `value1` is contained between `value2` and `value3`
* (inclusively), or `false` otherwise. * (inclusively), or `false` otherwise.
* *
@@ -494,7 +496,6 @@ Operators['^'] = {
)})`; )})`;
}, },
}; };
Operators['>'] = { Operators['>'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.BOOLEAN; return ValueTypes.BOOLEAN;
@@ -590,6 +591,29 @@ Operators['!'] = {
return `(!${expressionToGlsl(context, args[0])})`; return `(!${expressionToGlsl(context, args[0])})`;
}, },
}; };
function getDecisionOperator(operator) {
return {
getReturnType: function (args) {
return ValueTypes.BOOLEAN;
},
toGlsl: function (context, args) {
assertArgsMinCount(args, 2);
for (let i = 0; i < args.length; i++) {
assertBoolean(args[i]);
}
let result = '';
result = args
.map((arg) => expressionToGlsl(context, arg))
.join(` ${operator} `);
result = `(${result})`;
return result;
},
};
}
Operators['all'] = getDecisionOperator('&&');
Operators['any'] = getDecisionOperator('||');
Operators['between'] = { Operators['between'] = {
getReturnType: function (args) { getReturnType: function (args) {
return ValueTypes.BOOLEAN; return ValueTypes.BOOLEAN;

View File

@@ -179,6 +179,12 @@ describe('ol.style.expressions', function () {
expect(getValueType(['!=', 10, ['get', 'attr4']])).to.eql( expect(getValueType(['!=', 10, ['get', 'attr4']])).to.eql(
ValueTypes.BOOLEAN ValueTypes.BOOLEAN
); );
expect(getValueType(['all', true, ['get', 'attr4']])).to.eql(
ValueTypes.BOOLEAN
);
expect(getValueType(['any', true, ['get', 'attr4']])).to.eql(
ValueTypes.BOOLEAN
);
expect(getValueType(['between', ['get', 'attr4'], -4.0, 5.0])).to.eql( expect(getValueType(['between', ['get', 'attr4'], -4.0, 5.0])).to.eql(
ValueTypes.BOOLEAN ValueTypes.BOOLEAN
); );
@@ -244,6 +250,15 @@ describe('ol.style.expressions', function () {
expect(expressionToGlsl(context, ['!=', 10, ['get', 'attr4']])).to.eql( expect(expressionToGlsl(context, ['!=', 10, ['get', 'attr4']])).to.eql(
'(10.0 != a_attr4)' '(10.0 != a_attr4)'
); );
expect(expressionToGlsl(context, ['all', true, ['get', 'attr4']])).to.eql(
'(true && a_attr4)'
);
expect(expressionToGlsl(context, ['any', true, ['get', 'attr4']])).to.eql(
'(true || a_attr4)'
);
expect(
expressionToGlsl(context, ['any', true, ['get', 'attr4'], true])
).to.eql('(true || a_attr4 || true)');
expect( expect(
expressionToGlsl(context, ['between', ['get', 'attr4'], -4.0, 5.0]) expressionToGlsl(context, ['between', ['get', 'attr4'], -4.0, 5.0])
).to.eql('(a_attr4 >= -4.0 && a_attr4 <= 5.0)'); ).to.eql('(a_attr4 >= -4.0 && a_attr4 <= 5.0)');
@@ -294,6 +309,22 @@ describe('ol.style.expressions', function () {
} }
expect(thrown).to.be(true); expect(thrown).to.be(true);
thrown = false;
try {
expressionToGlsl(context, ['any', ['var', 'aa'], 10]);
} catch (e) {
thrown = true;
}
expect(thrown).to.be(true);
thrown = false;
try {
expressionToGlsl(context, ['all', ['var', 'aa'], 10]);
} catch (e) {
thrown = true;
}
expect(thrown).to.be(true);
thrown = false; thrown = false;
try { try {
expressionToGlsl(context, ['<', 0, 'aa']); expressionToGlsl(context, ['<', 0, 'aa']);
@@ -336,6 +367,22 @@ describe('ol.style.expressions', function () {
} }
expect(thrown).to.be(true); expect(thrown).to.be(true);
thrown = false;
try {
expressionToGlsl(context, ['all', ['var', true], ['get', true], true]);
} catch (e) {
thrown = true;
}
expect(thrown).to.be(true);
thrown = false;
try {
expressionToGlsl(context, ['any', ['var', true]]);
} catch (e) {
thrown = true;
}
expect(thrown).to.be(true);
thrown = false; thrown = false;
try { try {
expressionToGlsl(context, ['<', 4]); expressionToGlsl(context, ['<', 4]);