From ac27d538af33ca8d67d3d88729f49c05793afda7 Mon Sep 17 00:00:00 2001 From: Nivesh Rajbhandari Date: Mon, 9 Jul 2012 11:43:36 -0700 Subject: PI, drawing and editing fixes for shapes and materials. IKNinja-1841 - Cannot change webgl shape with LinearGradient and RadialGradient to solid color. IKNINJA-1851 - Cannot draw webgl shapes with Linear/RadialGradient material. IKNINJA-1864 - PI doesn't update the color of shape if WebGL material switches to Flat. IKNINJA-1886 - Gradient edits not applied to WebGL Stage object. Signed-off-by: Nivesh Rajbhandari --- .../ui/property-control.reel/property-control.js | 8 ++- js/controllers/elements/shapes-controller.js | 15 ++--- js/lib/geom/circle.js | 20 +++++- js/lib/geom/geom-obj.js | 25 ++----- js/lib/geom/line.js | 13 +++- js/lib/geom/rectangle.js | 17 ++++- js/lib/rdge/materials/linear-gradient-material.js | 50 ++++++++++++++ js/lib/rdge/materials/material.js | 69 ++++++++++++-------- js/lib/rdge/materials/radial-gradient-material.js | 43 +++++++++++- .../materials-popup.reel/materials-popup.css | 10 +++ .../materials-popup.reel/materials-popup.js | 76 ++++++++++++++++++---- js/tools/LineTool.js | 6 +- js/tools/OvalTool.js | 12 +++- js/tools/RectTool.js | 12 +++- 14 files changed, 293 insertions(+), 83 deletions(-) diff --git a/js/components/ui/property-control.reel/property-control.js b/js/components/ui/property-control.reel/property-control.js index 20ec173e..cd684168 100755 --- a/js/components/ui/property-control.reel/property-control.js +++ b/js/components/ui/property-control.reel/property-control.js @@ -15,7 +15,8 @@ var Montage = require("montage/core/core").Montage, TextField = require("js/components/TextField.reel").TextField, ColorChip = require("js/components/ui/color-chip.reel").ColorChip, FileInput = require("js/components/ui/file-input.reel").FileInput, - InputGroup = require("js/components/ui/input-group.reel").InputGroup; + InputGroup = require("js/components/ui/input-group.reel").InputGroup, + GradientPicker = require("js/components/gradientpicker.reel").GradientPicker; var PropertyControl = exports.PropertyControl = Montage.create(Component, { @@ -237,6 +238,11 @@ var PropertyControl = exports.PropertyControl = Montage.create(Component, { this._control.addEventListener("changing", this, false); this._prop = "value"; break; + case "GradientPicker": + this._control = GradientPicker.create(); + this._control.addEventListener("change", this, false); + this._prop = "value"; + break; default: break; } diff --git a/js/controllers/elements/shapes-controller.js b/js/controllers/elements/shapes-controller.js index c3099459..636482f5 100755 --- a/js/controllers/elements/shapes-controller.js +++ b/js/controllers/elements/shapes-controller.js @@ -858,15 +858,12 @@ exports.ShapesController = Montage.create(CanvasController, { value: function(m) { var css, - colorObj; - if(m === "Linear Gradient") - { - css = "-webkit-gradient(linear, left top, right top, from(rgb(255, 0, 0)), color-stop(0.3, rgb(0, 255, 0)), color-stop(0.6, rgb(0, 0, 255)), to(rgb(0, 255, 255)))"; - } - else if(m === "Radial Gradient") - { - css = "-webkit-radial-gradient(50% 50%, ellipse cover, rgb(255, 0, 0) 0%, rgb(0, 255, 0) 30%, rgb(0, 0, 255) 60%, rgb(0, 255, 255) 100%)"; - } + colorObj, + material; + + material = MaterialsModel.getMaterial(m); + + css = material.getGradientData(); if(css) { diff --git a/js/lib/geom/circle.js b/js/lib/geom/circle.js index 086c1058..848ae10d 100755 --- a/js/lib/geom/circle.js +++ b/js/lib/geom/circle.js @@ -61,14 +61,28 @@ exports.Circle = Object.create(GeomObj, { } else { this._strokeMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); } - if (strokeColor && this._strokeMaterial.hasProperty( "color" )) this._strokeMaterial.setProperty( "color", this._strokeColor ); + + if(strokeColor) { + if(this._strokeMaterial.hasProperty("color")) { + this._strokeMaterial.setProperty( "color", this._strokeColor ); + } else if (this._strokeMaterial && (this._strokeMaterial.gradientType === this._strokeColor.gradientMode)) { + this._strokeMaterial.setGradientData(this._strokeColor.color); + } + } if(fillMaterial) { this._fillMaterial = fillMaterial.dup(); } else { - this._fillMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); + this._fillMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); + } + + if(fillColor) { + if(this._fillMaterial.hasProperty("color")) { + this._fillMaterial.setProperty( "color", this._fillColor ); + } else if (this._fillMaterial && (this._fillMaterial.gradientType === this._fillColor.gradientMode)) { + this._fillMaterial.setGradientData(this._fillColor.color); + } } - if (fillColor && this._fillMaterial.hasProperty( "color" )) this._fillMaterial.setProperty( "color", this._fillColor ); } }, diff --git a/js/lib/geom/geom-obj.js b/js/lib/geom/geom-obj.js index 3a7a5619..66f557dd 100755 --- a/js/lib/geom/geom-obj.js +++ b/js/lib/geom/geom-obj.js @@ -191,27 +191,10 @@ exports.GeomObj = Object.create(Object.prototype, { nMats = this._materialArray.length; } - var stops = [], - colors = c.color; - - var len = colors.length; - // TODO - Current shaders only support 4 color stops - if (len > 4) { - len = 4; - } - - for (var n = 0; n < len; n++) { - var position = colors[n].position / 100; - var cs = colors[n].value; - var stop = [cs.r / 255, cs.g / 255, cs.b / 255, cs.a]; - stops.push(stop); - - if (nMats === this._materialTypeArray.length) { - for (i = 0; i < nMats; i++) { - if (this._materialTypeArray[i] == type) { - this._materialArray[i].setProperty("color" + (n + 1), stop.slice(0)); - this._materialArray[i].setProperty("colorStop" + (n + 1), position); - } + if (nMats === this._materialTypeArray.length) { + for (i = 0; i < nMats; i++) { + if (this._materialTypeArray[i] == type) { + this._materialArray[i].setGradientData(c.color); } } } diff --git a/js/lib/geom/line.js b/js/lib/geom/line.js index a016d7a3..f782f2a8 100755 --- a/js/lib/geom/line.js +++ b/js/lib/geom/line.js @@ -63,8 +63,17 @@ exports.Line = Object.create(GeomObj, { this._materialSpecular = [0.4, 0.4, 0.4, 1.0]; if(strokeMaterial) { - this._strokeMaterial = strokeMaterial; - if (strokeColor && this._strokeMaterial.hasProperty( "color" )) this._strokeMaterial.setProperty( "color", this._strokeColor ); + this._strokeMaterial = strokeMaterial.dup(); + } else { + this._strokeMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); + } + + if(strokeColor) { + if(this._strokeMaterial.hasProperty("color")) { + this._strokeMaterial.setProperty( "color", this._strokeColor ); + } else if (this._strokeMaterial && (this._strokeMaterial.gradientType === this._strokeColor.gradientMode)) { + this._strokeMaterial.setGradientData(this._strokeColor.color); + } } } }, diff --git a/js/lib/geom/rectangle.js b/js/lib/geom/rectangle.js index 81a8556d..3e4c469a 100755 --- a/js/lib/geom/rectangle.js +++ b/js/lib/geom/rectangle.js @@ -77,15 +77,28 @@ exports.Rectangle = Object.create(GeomObj, { } else { this._strokeMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); } - if (strokeColor && this._strokeMaterial.hasProperty( "color" )) this._strokeMaterial.setProperty( "color", this._strokeColor ); + if(strokeColor) { + if(this._strokeMaterial.hasProperty("color")) { + this._strokeMaterial.setProperty( "color", this._strokeColor ); + } else if (this._strokeMaterial && (this._strokeMaterial.gradientType === this._strokeColor.gradientMode)) { + this._strokeMaterial.setGradientData(this._strokeColor.color); + } + } if(fillMaterial) { this._fillMaterial = fillMaterial.dup(); } else { this._fillMaterial = MaterialsModel.getMaterial( MaterialsModel.getDefaultMaterialName() ).dup(); } - if (fillColor && this._fillMaterial.hasProperty( "color" )) this._fillMaterial.setProperty( "color", this._fillColor ); + + if(fillColor) { + if(this._fillMaterial.hasProperty("color")) { + this._fillMaterial.setProperty( "color", this._fillColor ); + } else if (this._fillMaterial && (this._fillMaterial.gradientType === this._fillColor.gradientMode)) { + this._fillMaterial.setGradientData(this._fillColor.color); + } + } } }, diff --git a/js/lib/rdge/materials/linear-gradient-material.js b/js/lib/rdge/materials/linear-gradient-material.js index 7e53db84..d26143de 100755 --- a/js/lib/rdge/materials/linear-gradient-material.js +++ b/js/lib/rdge/materials/linear-gradient-material.js @@ -79,6 +79,56 @@ var LinearGradientMaterial = function LinearGradientMaterial() { this.setShaderValues(); this.update( 0 ); }; + + // Only Linear Gradient and Radial Gradients support gradients; + this.gradientType = "linear"; + + this.getGradientData = function() { + var angle = Math.round(this._angle*180/Math.PI), + color, + colorStr, + css = "-webkit-gradient(linear, " + angle + "deg"; + + // TODO - Angle is not supported in -webkit-gradient syntax, so just default to across: + css = '-webkit-gradient(linear, left top, right top'; + + // TODO - Also, Color Model requires from and to in the gradient string + color = this.getProperty('u_color1'); + colorStr = Math.round(color[0] * 255) + ', ' + Math.round(color[1] * 255) + ', ' + Math.round(color[2] * 255) + ', ' + Math.round(color[3] * 100); + css += ', from(rgba(' + colorStr + '))'; + + for (var i=2; i < 4; i++) { + color = this.getProperty('u_color'+i); + colorStr = Math.round(color[0] * 255) + ', ' + Math.round(color[1] * 255) + ', ' + Math.round(color[2] * 255) + ', ' + Math.round(color[3] * 100); + css += ', color-stop(' + this.getProperty('u_colorStop'+i) + ', rgba(' + colorStr + '))'; + } + + color = this.getProperty('u_color4'); + colorStr = Math.round(color[0] * 255) + ', ' + Math.round(color[1] * 255) + ', ' + Math.round(color[2] * 255) + ', ' + Math.round(color[3] * 100); + css += ', to(rgba(' + colorStr + '))'; + + css += ')'; + + return css; + }; + + // Only Linear Gradient and Radial Gradient have gradient data. + this.setGradientData = function(colors) { + var len = colors.length; + // TODO - Current shaders only support 4 color stops + if (len > 4) { + len = 4; + } + + for (var n = 0; n < len; n++) { + var position = colors[n].position/100; + var cs = colors[n].value; + var stop = [cs.r/255, cs.g/255, cs.b/255, cs.a]; + + this.setProperty("u_color" + (n + 1), stop.slice(0)); + this.setProperty("u_colorStop" + (n + 1), position); + } + }; }; /////////////////////////////////////////////////////////////////////////////////////// diff --git a/js/lib/rdge/materials/material.js b/js/lib/rdge/materials/material.js index 65448c0c..1864b84e 100755 --- a/js/lib/rdge/materials/material.js +++ b/js/lib/rdge/materials/material.js @@ -177,6 +177,8 @@ var Material = function GLMaterial( world ) { }; this.validateProperty = function( prop, value ) { + if(prop === "gradient") return true; + var rtnVal = false; try { @@ -240,33 +242,37 @@ var Material = function GLMaterial( world ) { var material = this._materialNode; if (material) technique = material.shaderProgram[this.getTechniqueName()]; - switch (this.getPropertyType(prop)) - { - case "angle": - case "float": - this._propValues[prop] = value; - if (technique) technique[prop].set( [value] ); - break; - - case "file": - this._propValues[prop] = value.slice(); - if (technique) - { - var glTex = new Texture( this.getWorld(), value ); - this._glTextures[prop] = glTex; - glTex.render(); - var tex = glTex.getTexture(); - if (tex) technique[prop].set( tex ); - } - break; - - case "color": - case "vector2d": - case "vector3d": - this._propValues[prop] = value.slice(); - if (technique) technique[prop].set( value ); - break; - } + if(prop === "gradient") { + this.setGradientData(value); + } else { + switch (this.getPropertyType(prop)) + { + case "angle": + case "float": + this._propValues[prop] = value; + if (technique) technique[prop].set( [value] ); + break; + + case "file": + this._propValues[prop] = value.slice(); + if (technique) + { + var glTex = new Texture( this.getWorld(), value ); + this._glTextures[prop] = glTex; + glTex.render(); + var tex = glTex.getTexture(); + if (tex) technique[prop].set( tex ); + } + break; + + case "color": + case "vector2d": + case "vector3d": + this._propValues[prop] = value.slice(); + if (technique) technique[prop].set( value ); + break; + } + } }; this.setShaderValues = function() @@ -398,6 +404,15 @@ var Material = function GLMaterial( world ) { return tex; }; + this.gradientType = null; + + this.getGradientData = function() { + return null; + }; + + this.setGradientData = function() { + // override in linear-gradient-material and radial-gradient-material + }; }; if (typeof exports === "object") { diff --git a/js/lib/rdge/materials/radial-gradient-material.js b/js/lib/rdge/materials/radial-gradient-material.js index c9c2536f..c68cad43 100755 --- a/js/lib/rdge/materials/radial-gradient-material.js +++ b/js/lib/rdge/materials/radial-gradient-material.js @@ -113,7 +113,48 @@ var RadialGradientMaterial = function RadialGradientMaterial() { { jObj.u_texTransform = this._textureTransform.slice(); return jObj; - } + }; + + // Only Linear Gradient and Radial Gradient have gradient data. + this.gradientType = "radial"; + + this.getGradientData = function() { + var angle = Math.round(this._angle*180/Math.PI), + color, + colorStr, + css = "-webkit-gradient(linear, " + angle + "deg"; + + // TODO - Angle is not supported in -webkit-gradient syntax, so just default to across: + css = '-webkit-radial-gradient(50% 50%, ellipse cover'; + + // TODO - Also, Color Model requires from and to in the gradient string + for (var i=1; i < 5; i++) { + color = this.getProperty('u_color'+i); + colorStr = Math.round(color[0] * 255) + ', ' + Math.round(color[1] * 255) + ', ' + Math.round(color[2] * 255) + ', ' + Math.round(color[3] * 100); + css += ', rgba(' + colorStr + ') ' + Math.round(this.getProperty('u_colorStop'+i)*100) + '%'; + } + + css += ')'; + + return css; + }; + + this.setGradientData = function(colors) { + var len = colors.length; + // TODO - Current shaders only support 4 color stops + if (len > 4) { + len = 4; + } + + for (var n = 0; n < len; n++) { + var position = colors[n].position/100; + var cs = colors[n].value; + var stop = [cs.r/255, cs.g/255, cs.b/255, cs.a]; + + this.setProperty("u_color" + (n + 1), stop.slice(0)); + this.setProperty("u_colorStop" + (n + 1), position); + } + }; }; /////////////////////////////////////////////////////////////////////////////////////// diff --git a/js/panels/Materials/materials-popup.reel/materials-popup.css b/js/panels/Materials/materials-popup.reel/materials-popup.css index 2de03e91..b57d4a03 100755 --- a/js/panels/Materials/materials-popup.reel/materials-popup.css +++ b/js/panels/Materials/materials-popup.reel/materials-popup.css @@ -88,6 +88,16 @@ position: relative; } +.mp_property .gradientpicker +{ + top: 0; +} + +.mp_property .gradientpicker .type +{ + display: none; +} + .mp_properties_list { padding-bottom: 8px; diff --git a/js/panels/Materials/materials-popup.reel/materials-popup.js b/js/panels/Materials/materials-popup.reel/materials-popup.js index 9b7b031b..a1415343 100755 --- a/js/panels/Materials/materials-popup.reel/materials-popup.js +++ b/js/panels/Materials/materials-popup.reel/materials-popup.js @@ -9,7 +9,8 @@ var Montage = require("montage/core/core").Montage, MaterialsModel = require("js/models/materials-model").MaterialsModel, NJUtils = require("js/lib/NJUtils").NJUtils, World = require("js/lib/drawing/world").World, - Rectangle = require("js/lib/geom/rectangle").Rectangle; + Rectangle = require("js/lib/geom/rectangle").Rectangle, + ShapesController = require("js/controllers/elements/shapes-controller").ShapesController; //////////////////////////////////////////////////////////////////////// //Exporting as MaterialsPopup @@ -341,6 +342,10 @@ exports.MaterialsPopup = Montage.create(Component, { rtnValue = value; break; + case "gradient": + rtnValue = value; + break; + default: console.log( "unrecognized material control type: " + type ); break; @@ -432,7 +437,11 @@ exports.MaterialsPopup = Montage.create(Component, { { this._material = material; this._originalValues = material.exportJSON(); - this.materialsData = this.getMaterialData( material ); + if((materialID === "Linear Gradient") || (materialID === "Radial Gradient")) { + this.materialsData = this.getEditableProperties( material ); + } else { + this.materialsData = this.getMaterialData( material ); + } } else { @@ -442,6 +451,26 @@ exports.MaterialsPopup = Montage.create(Component, { } }, + getEditableProperties: { + value: function(material) { + // declare the array to hold the results + var rtnArray = [], + obj, + colorObj = ShapesController.getMaterialColor(material.getName()); + + this._propNames = ["gradient"]; + this._propValues = ["gradient"]; + this._propTypes = ["gradient"]; + this._propLabels = ["gradient"]; + + obj = this.createGradientData("gradient", colorObj); + + rtnArray.push(obj); + + return rtnArray; + } + }, + getMaterialData: { value: function( material ) @@ -489,17 +518,21 @@ exports.MaterialsPopup = Montage.create(Component, { obj = this.createCheckboxData( propLabels[i], propValues[i] ); break; - default: - console.log( "unrecognized material control type: " + propType[i] ); - break; - } + case "gradient": + obj = this.createGradientData( propLabels[i], propValues[i] ); + break; - if (obj) - { - rtnArray.push( obj ); - obj = null; - } - } + default: + console.log( "unrecognized material control type: " + propType[i] ); + break; + } + + if (obj) + { + rtnArray.push( obj ); + obj = null; + } + } return rtnArray; } @@ -650,6 +683,25 @@ exports.MaterialsPopup = Montage.create(Component, { } }, + createGradientData: + { + value: function( label, colorObj ) + { + var obj = { + "label": label, + "description": "a gradient", + "controlType": "GradientPicker", + "defaults": + { + "_mode": colorObj.gradientMode, + "value": colorObj.color + } + }; + + return obj; + } + }, + materialsProperties: { serializable: true, value: null diff --git a/js/tools/LineTool.js b/js/tools/LineTool.js index 413c0302..f9f2f817 100755 --- a/js/tools/LineTool.js +++ b/js/tools/LineTool.js @@ -244,7 +244,11 @@ exports.LineTool = Montage.create(ShapeTool, { { strokeMaterial = Object.create(MaterialsModel.getMaterial(strokeM)); } - strokeColor = ShapesController.getMaterialColor(strokeM) || strokeColor; + if (strokeMaterial && this.options.stroke.color && (strokeMaterial.gradientType === this.options.stroke.color.gradientMode)) { + strokeColor = {gradientMode:strokeMaterial.gradientType, color:this.options.stroke.color.stops}; + } else { + strokeColor = ShapesController.getMaterialColor(strokeM) || strokeColor; + } } var world = this.getGLWorld(canvas, this.options.use3D); diff --git a/js/tools/OvalTool.js b/js/tools/OvalTool.js index e798d1a7..f1e33205 100755 --- a/js/tools/OvalTool.js +++ b/js/tools/OvalTool.js @@ -54,14 +54,22 @@ exports.OvalTool = Montage.create(ShapeTool, { { strokeMaterial = Object.create(MaterialsModel.getMaterial(strokeM)); } - strokeColor = ShapesController.getMaterialColor(strokeM) || strokeColor; + if (strokeMaterial && this.options.stroke.color && (strokeMaterial.gradientType === this.options.stroke.color.gradientMode)) { + strokeColor = {gradientMode:strokeMaterial.gradientType, color:this.options.stroke.color.stops}; + } else { + strokeColor = ShapesController.getMaterialColor(strokeM) || strokeColor; + } fillM = this.options.fillMaterial; if(fillM) { fillMaterial = Object.create(MaterialsModel.getMaterial(fillM)); } - fillColor = ShapesController.getMaterialColor(fillM) || fillColor; + if (fillMaterial && this.options.fill.color && (fillMaterial.gradientType === this.options.fill.color.gradientMode)) { + fillColor = {gradientMode:fillMaterial.gradientType, color:this.options.fill.color.stops}; + } else { + fillColor = ShapesController.getMaterialColor(fillM) || fillColor; + } } var world = this.getGLWorld(canvas, this.options.use3D); diff --git a/js/tools/RectTool.js b/js/tools/RectTool.js index 6f0e65c7..0dfafe57 100755 --- a/js/tools/RectTool.js +++ b/js/tools/RectTool.js @@ -74,14 +74,22 @@ exports.RectTool = Montage.create(ShapeTool, { { strokeMaterial = Object.create(MaterialsModel.getMaterial(strokeM)); } - strokeColor = ShapesController.getMaterialColor(strokeM) || strokeColor; + if (strokeMaterial && this.options.stroke.color && (strokeMaterial.gradientType === this.options.stroke.color.gradientMode)) { + strokeColor = {gradientMode:strokeMaterial.gradientType, color:this.options.stroke.color.stops}; + } else { + strokeColor = ShapesController.getMaterialColor(strokeM) || strokeColor; + } fillM = this.options.fillMaterial; if(fillM) { fillMaterial = Object.create(MaterialsModel.getMaterial(fillM)); } - fillColor = ShapesController.getMaterialColor(fillM) || fillColor; + if (fillMaterial && this.options.fill.color && (fillMaterial.gradientType === this.options.fill.color.gradientMode)) { + fillColor = {gradientMode:fillMaterial.gradientType, color:this.options.fill.color.stops}; + } else { + fillColor = ShapesController.getMaterialColor(fillM) || fillColor; + } } var world = this.getGLWorld(canvas, this.options.use3D); -- cgit v1.2.3