diff --git a/Source/Scene/Cesium3DTileStyle.js b/Source/Scene/Cesium3DTileStyle.js index ebe58488f23a..c4987a1fff86 100644 --- a/Source/Scene/Cesium3DTileStyle.js +++ b/Source/Scene/Cesium3DTileStyle.js @@ -40,11 +40,11 @@ define([ * @example * tileset.style = new Cesium.Cesium3DTileStyle({ * color : { - * conditions : { - * '${Height} >= 100' : 'color("purple", 0.5)', - * '${Height} >= 50' : 'color("red")', - * 'true' : 'color("blue")' - * } + * conditions : [ + * ['${Height} >= 100', 'color("purple", 0.5)'], + * ['${Height} >= 50', 'color("red")'], + * ['true', 'color("blue")'] + * ] * }, * show : '${Height} > 0', * meta : { diff --git a/Source/Scene/Cesium3DTileset.js b/Source/Scene/Cesium3DTileset.js index e80c8f1d8a8a..fcc16a681918 100644 --- a/Source/Scene/Cesium3DTileset.js +++ b/Source/Scene/Cesium3DTileset.js @@ -500,11 +500,11 @@ define([ * @example * tileset.style = new Cesium.Cesium3DTileStyle({ * color : { - * conditions : { - * '${Height} >= 100' : 'color("purple", 0.5)', - * '${Height} >= 50' : 'color("red")', - * 'true' : 'color("blue")' - * } + * conditions : [ + * ['${Height} >= 100', 'color("purple", 0.5)'], + * ['${Height} >= 50', 'color("red")'], + * ['true' : 'color("blue")'] + * ] * }, * show : '${Height} > 0', * meta : { diff --git a/Source/Scene/ConditionsExpression.js b/Source/Scene/ConditionsExpression.js index 986467e1e23d..c8c6138dfb90 100644 --- a/Source/Scene/ConditionsExpression.js +++ b/Source/Scene/ConditionsExpression.js @@ -32,11 +32,11 @@ define([ * @example * var expression = new Cesium.Expression({ * expression : 'regExp("^1(\\d)").exec(${id})', - * conditions : { - * '${expression} === "1"' : 'color("#FF0000")', - * '${expression} === "2"' : 'color("#00FF00")', - * 'true' : 'color("#FFFFFF")' - * } + * conditions : [ + * ['${expression} === "1"', 'color("#FF0000")'], + * ['${expression} === "2"', 'color("#00FF00")'], + * ['true', 'color("#FFFFFF")'] + * ] * }); * expression.evaluateColor(feature, result); // returns a Cesium.Color object * @@ -78,23 +78,25 @@ define([ function setRuntime(expression) { var runtimeConditions = []; var conditions = expression._conditions; - var exp = expression._expression; - var length = conditions.length; - for (var i = 0; i < length; ++i) { - var statement = conditions[i]; - var cond = String(statement[0]); - var condExpression = String(statement[1]); - if (defined(exp)) { - cond = cond.replace(expressionPlaceholder, exp); - condExpression = condExpression.replace(expressionPlaceholder, exp); - } else { - cond = cond.replace(expressionPlaceholder, 'undefined'); - condExpression = condExpression.replace(expressionPlaceholder, 'undefined'); + if (defined(conditions)) { + var exp = expression._expression; + var length = conditions.length; + for (var i = 0; i < length; ++i) { + var statement = conditions[i]; + var cond = String(statement[0]); + var condExpression = String(statement[1]); + if (defined(exp)) { + cond = cond.replace(expressionPlaceholder, exp); + condExpression = condExpression.replace(expressionPlaceholder, exp); + } else { + cond = cond.replace(expressionPlaceholder, 'undefined'); + condExpression = condExpression.replace(expressionPlaceholder, 'undefined'); + } + runtimeConditions.push(new Statement( + new Expression(cond), + new Expression(condExpression) + )); } - runtimeConditions.push(new Statement( - new Expression(cond), - new Expression(condExpression) - )); } expression._runtimeConditions = runtimeConditions; diff --git a/Source/Scene/Expression.js b/Source/Scene/Expression.js index 1867181bce2c..9ba8dc1607c4 100644 --- a/Source/Scene/Expression.js +++ b/Source/Scene/Expression.js @@ -118,7 +118,11 @@ define([ */ Expression.prototype.evaluate = function(feature) { scratchColorIndex = 0; - return this._runtimeAst.evaluate(feature); + var result = this._runtimeAst.evaluate(feature); + if (result instanceof Color) { + return Color.clone(result); + } + return result; }; /** diff --git a/Specs/Scene/Cesium3DTileStyleSpec.js b/Specs/Scene/Cesium3DTileStyleSpec.js index 3509c71f332c..34c479572ac5 100644 --- a/Specs/Scene/Cesium3DTileStyleSpec.js +++ b/Specs/Scene/Cesium3DTileStyleSpec.js @@ -148,10 +148,10 @@ defineSuite([ it ('sets color value to conditional', function() { var jsonExp = { - conditions : { - '${height} > 2' : 'color("cyan")', - 'true' : 'color("blue")' - } + conditions : [ + ['${height} > 2', 'color("cyan")'], + ['true', 'color("blue")'] + ] }; var style = new Cesium3DTileStyle({ @@ -295,11 +295,11 @@ defineSuite([ var style = new Cesium3DTileStyle({ "color" : { "expression" : "regExp('^1(\\d)').exec(${id})", - "conditions" : { - "${expression} === '1'" : "color('#FF0000')", - "${expression} === '2'" : "color('#00FF00')", - "true" : "color('#FFFFFF')" - } + "conditions" : [ + ["${expression} === '1'", "color('#FF0000')"], + ["${expression} === '2'", "color('#00FF00')"], + ["true", "color('#FFFFFF')"] + ] } }); expect(style.show.evaluate(feature1)).toEqual(true); @@ -311,14 +311,14 @@ defineSuite([ var style = new Cesium3DTileStyle({ "color" : { "expression" : "${Height}", - "conditions" : { - "(${expression} >= 1.0) && (${expression} < 10.0)" : "color('#FF00FF')", - "(${expression} >= 10.0) && (${expression} < 30.0)" : "color('#FF0000')", - "(${expression} >= 30.0) && (${expression} < 50.0)" : "color('#FFFF00')", - "(${expression} >= 50.0) && (${expression} < 70.0)" : "color('#00FF00')", - "(${expression} >= 70.0) && (${expression} < 100.0)" : "color('#00FFFF')", - "(${expression} >= 100.0)" : "color('#0000FF')" - } + "conditions" : [ + ["(${expression} >= 1.0) && (${expression} < 10.0)", "color('#FF00FF')"], + ["(${expression} >= 10.0) && (${expression} < 30.0)", "color('#FF0000')"], + ["(${expression} >= 30.0) && (${expression} < 50.0)", "color('#FFFF00')"], + ["(${expression} >= 50.0) && (${expression} < 70.0)", "color('#00FF00')"], + ["(${expression} >= 70.0) && (${expression} < 100.0)", "color('#00FFFF')"], + ["(${expression} >= 100.0)", "color('#0000FF')"] + ] } }); expect(style.show.evaluate(feature1)).toEqual(true); @@ -329,14 +329,14 @@ defineSuite([ it ('applies color style with conditional', function() { var style = new Cesium3DTileStyle({ "color" : { - "conditions" : { - "(${Height} >= 100.0)" : "color('#0000FF')", - "(${Height} >= 70.0)" : "color('#00FFFF')", - "(${Height} >= 50.0)" : "color('#00FF00')", - "(${Height} >= 30.0)" : "color('#FFFF00')", - "(${Height} >= 10.0)" : "color('#FF0000')", - "(${Height} >= 1.0)" : "color('#FF00FF')" - } + "conditions" : [ + ["(${Height} >= 100.0)", "color('#0000FF')"], + ["(${Height} >= 70.0)", "color('#00FFFF')"], + ["(${Height} >= 50.0)", "color('#00FF00')"], + ["(${Height} >= 30.0)", "color('#FFFF00')"], + ["(${Height} >= 10.0)", "color('#FF0000')"], + ["(${Height} >= 1.0)", "color('#FF00FF')"] + ] } }); expect(style.show.evaluate(feature1)).toEqual(true); diff --git a/Specs/Scene/Cesium3DTilesetSpec.js b/Specs/Scene/Cesium3DTilesetSpec.js index 64986b4f83c4..974047751629 100644 --- a/Specs/Scene/Cesium3DTilesetSpec.js +++ b/Specs/Scene/Cesium3DTilesetSpec.js @@ -1177,10 +1177,10 @@ defineSuite([ // ${id} < 10 will always evaluate to true tileset.style = new Cesium3DTileStyle({ color : { - conditions : { - '${id} < 10' : 'color("red")', - 'true' : 'color("blue")' - } + conditions : [ + ['${id} < 10', 'color("red")'], + ['true', 'color("blue")'] + ] } }); var color = scene.renderForSpecs(); @@ -1192,10 +1192,10 @@ defineSuite([ // ${id}>= 10 will always evaluate to false tileset.style = new Cesium3DTileStyle({ color : { - conditions : { - '${id} >= 10' : 'color("red")', - 'true' : 'color("blue")' - } + conditions : [ + ['${id} >= 10', 'color("red")'], + ['true', 'color("blue")'] + ] } }); color = scene.renderForSpecs(); diff --git a/Specs/Scene/ConditionsExpressionSpec.js b/Specs/Scene/ConditionsExpressionSpec.js index aa18b8ecb1d7..e7100876dccd 100644 --- a/Specs/Scene/ConditionsExpressionSpec.js +++ b/Specs/Scene/ConditionsExpressionSpec.js @@ -18,37 +18,37 @@ defineSuite([ }; var jsonExp = { - conditions : { - '${Height} > 100' : 'color("blue")', - '${Height} > 50' : 'color("red")', - 'true' : 'color("green")' - } + conditions : [ + ['${Height} > 100', 'color("blue")'], + ['${Height} > 50', 'color("red")'], + ['true', 'color("green")'] + ] }; var jsonExpWithExpression = { expression : '${Height}/2', - conditions : { - '${expression} > 50' : 'color("blue")', - '${expression} > 25' : 'color("red")', - 'true' : 'color("green")' - } + conditions : [ + ['${expression} > 50', 'color("blue")'], + ['${expression} > 25', 'color("red")'], + ['true', 'color("green")'] + ] }; var jsonExpWithMultipleExpression = { expression : '${Height}/2', - conditions : { - '${expression} > 50 && ${expression} < 100' : 'color("blue")', - '${expression} > 25 && ${expression} < 26' : 'color("red")', - 'true' : 'color("green")' - } + conditions : [ + ['${expression} > 50 && ${expression} < 100', 'color("blue")'], + ['${expression} > 25 && ${expression} < 26', 'color("red")'], + ['true', 'color("green")'] + ] }; var jsonExpWithUndefinedExpression = { - conditions : { - '${expression} === undefined' : 'color("blue")', - 'true' : 'color("green")' - } + conditions : [ + ['${expression} === undefined', 'color("blue")'], + ['true', 'color("green")'] + ] }; @@ -60,60 +60,60 @@ defineSuite([ it('constructs with expression', function() { var expression = new ConditionsExpression(jsonExpWithExpression); expect(expression._expression).toEqual('${Height}/2'); - expect(expression._conditions).toEqual({ - '${expression} > 50' : 'color("blue")', - '${expression} > 25' : 'color("red")', - 'true' : 'color("green")' - }); + expect(expression._conditions).toEqual([ + ['${expression} > 50', 'color("blue")'], + ['${expression} > 25', 'color("red")'], + ['true', 'color("green")'] + ]); }); it('evaluates undefined expression', function() { var expression = new ConditionsExpression(jsonExpWithExpression); expect(expression._expression).toEqual('${Height}/2'); - expect(expression._conditions).toEqual({ - '${expression} > 50' : 'color("blue")', - '${expression} > 25' : 'color("red")', - 'true' : 'color("green")' - }); + expect(expression._conditions).toEqual([ + ['${expression} > 50', 'color("blue")'], + ['${expression} > 25', 'color("red")'], + ['true', 'color("green")'] + ]); }); it('evaluates conditional', function() { var expression = new ConditionsExpression(jsonExp); - expect(expression.evaluate(new MockFeature('101'))).toEqual(Color.BLUE); - expect(expression.evaluate(new MockFeature('52'))).toEqual(Color.RED); - expect(expression.evaluate(new MockFeature('3'))).toEqual(Color.GREEN); + expect(expression.evaluate(new MockFeature(101))).toEqual(Color.BLUE); + expect(expression.evaluate(new MockFeature(52))).toEqual(Color.RED); + expect(expression.evaluate(new MockFeature(3))).toEqual(Color.GREEN); }); it('evaluates conditional with multiple expressions', function() { var expression = new ConditionsExpression(jsonExpWithMultipleExpression); - expect(expression.evaluate(new MockFeature('101'))).toEqual(Color.BLUE); - expect(expression.evaluate(new MockFeature('52'))).toEqual(Color.GREEN); - expect(expression.evaluate(new MockFeature('3'))).toEqual(Color.GREEN); + expect(expression.evaluate(new MockFeature(101))).toEqual(Color.BLUE); + expect(expression.evaluate(new MockFeature(52))).toEqual(Color.GREEN); + expect(expression.evaluate(new MockFeature(3))).toEqual(Color.GREEN); }); it('constructs and evaluates empty conditional', function() { var expression = new ConditionsExpression({ - "conditions" : {} + "conditions" : [] }); - expect(expression._conditions).toEqual({}); - expect(expression.evaluate(new MockFeature('101'))).toEqual(undefined); - expect(expression.evaluate(new MockFeature('52'))).toEqual(undefined); - expect(expression.evaluate(new MockFeature('3'))).toEqual(undefined); + expect(expression._conditions).toEqual([]); + expect(expression.evaluate(new MockFeature(101))).toEqual(undefined); + expect(expression.evaluate(new MockFeature(52))).toEqual(undefined); + expect(expression.evaluate(new MockFeature(3))).toEqual(undefined); }); it('constructs and evaluates empty', function() { - var expression = new ConditionsExpression({}); + var expression = new ConditionsExpression([]); expect(expression._conditions).toEqual(undefined); - expect(expression.evaluate(new MockFeature('101'))).toEqual(undefined); - expect(expression.evaluate(new MockFeature('52'))).toEqual(undefined); - expect(expression.evaluate(new MockFeature('3'))).toEqual(undefined); + expect(expression.evaluate(new MockFeature(101))).toEqual(undefined); + expect(expression.evaluate(new MockFeature(52))).toEqual(undefined); + expect(expression.evaluate(new MockFeature(3))).toEqual(undefined); }); it('evaluates conditional with expression', function() { var expression = new ConditionsExpression(jsonExpWithExpression); - expect(expression.evaluate(new MockFeature('101'))).toEqual(Color.BLUE); - expect(expression.evaluate(new MockFeature('52'))).toEqual(Color.RED); - expect(expression.evaluate(new MockFeature('3'))).toEqual(Color.GREEN); + expect(expression.evaluate(new MockFeature(101))).toEqual(Color.BLUE); + expect(expression.evaluate(new MockFeature(52))).toEqual(Color.RED); + expect(expression.evaluate(new MockFeature(3))).toEqual(Color.GREEN); }); it('evaluates undefined conditional expression', function() {