From: Richard Huang Date: Mon, 12 Oct 2015 10:58:35 +0000 (+0100) Subject: JavaScript binding for new mesh APIs X-Git-Tag: dali_1.1.8~1 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=commitdiff_plain;h=8c31a5ca493d17693e53f9909a4453b1fa058ab3 JavaScript binding for new mesh APIs Change-Id: Iec5fc37065d17b52aa3878b464df5681b9b6d775 --- diff --git a/node-addon/binding.gyp b/node-addon/binding.gyp index d8f07f5..0098678 100644 --- a/node-addon/binding.gyp +++ b/node-addon/binding.gyp @@ -41,12 +41,24 @@ '<(DALI_JS_DIR)/image/resource-image-api.cpp', '<(DALI_JS_DIR)/image/nine-patch-image-api.cpp', '<(DALI_JS_DIR)/object/handle-wrapper.cpp', + '<(DALI_JS_DIR)/object/property-buffer-api.cpp', + '<(DALI_JS_DIR)/object/property-buffer-wrapper.cpp', '<(DALI_JS_DIR)/object/property-value-wrapper.cpp', '<(DALI_JS_DIR)/signals/signal-manager.cpp', '<(DALI_JS_DIR)/render-tasks/render-task-list-api.cpp', '<(DALI_JS_DIR)/render-tasks/render-task-list-wrapper.cpp', '<(DALI_JS_DIR)/render-tasks/render-task-api.cpp', '<(DALI_JS_DIR)/render-tasks/render-task-wrapper.cpp', + '<(DALI_JS_DIR)/rendering/geometry-api.cpp', + '<(DALI_JS_DIR)/rendering/geometry-wrapper.cpp', + '<(DALI_JS_DIR)/rendering/material-api.cpp', + '<(DALI_JS_DIR)/rendering/material-wrapper.cpp', + '<(DALI_JS_DIR)/rendering/renderer-api.cpp', + '<(DALI_JS_DIR)/rendering/renderer-wrapper.cpp', + '<(DALI_JS_DIR)/rendering/sampler-api.cpp', + '<(DALI_JS_DIR)/rendering/sampler-wrapper.cpp', + '<(DALI_JS_DIR)/rendering/shader-api.cpp', + '<(DALI_JS_DIR)/rendering/shader-wrapper.cpp', '<(DALI_JS_DIR)/toolkit/builder/builder-api.cpp', '<(DALI_JS_DIR)/toolkit/builder/builder-wrapper.cpp', '<(DALI_JS_DIR)/toolkit/focus-manager/keyboard-focus-manager-api.cpp', diff --git a/node-addon/image-1.jpg b/node-addon/image-1.jpg new file mode 100644 index 0000000..155ab30 Binary files /dev/null and b/node-addon/image-1.jpg differ diff --git a/node-addon/image-2.jpg b/node-addon/image-2.jpg new file mode 100644 index 0000000..e855ecf Binary files /dev/null and b/node-addon/image-2.jpg differ diff --git a/node-addon/line-mesh.js b/node-addon/line-mesh.js new file mode 100644 index 0000000..9dd4053 --- /dev/null +++ b/node-addon/line-mesh.js @@ -0,0 +1,138 @@ + var window= { + x:0, + y:0, + width:1920, + height: 1080, + transparent: false, + name:'line-mesh-app' + }; + + var viewMode={ + 'stereoscopic-mode':'mono', // stereo-horizontal, stereo-vertical, stereo-interlaced, + 'stereo-base': 65 // Distance in millimeters between left/right cameras typically between (50-70mm) + }; + + var options= { + 'window': window, + 'view-mode': viewMode, + } + +var dali = require('./build/Release/dali')( options ); + +var daliApp = {}; + +daliApp.createMeshActor = function() { + + // Create shader + var vertShader = + "attribute mediump vec2 aPosition1;\ + attribute mediump vec2 aPosition2;\ + uniform mediump mat4 uMvpMatrix;\ + uniform mediump vec3 uSize;\ + uniform mediump float uMorphAmount;\ + void main()\ + {\ + mediump vec2 morphPosition = mix(aPosition1, aPosition2, uMorphAmount);\ + mediump vec4 vertexPosition = vec4(morphPosition, 0.0, 1.0);\ + vertexPosition.xyz *= uSize;\ + vertexPosition = uMvpMatrix * vertexPosition;\ + gl_Position = vertexPosition;\ + }"; + + var fragShader = + "uniform lowp vec4 uColor;\ + uniform sampler2D sTexture;\ + void main()\ + {\ + gl_FragColor = uColor;\ + }"; + + var shaderOptions = { + vertexShader : vertShader, + fragmentShader: fragShader + }; + + var shader = new dali.Shader(shaderOptions); + + var material = new dali.Material( shader ); + + // Create vertex buffer + var pentagonVertexFormat ={ "aPosition1" : dali.PROPERTY_VECTOR2 }; + + var pentagonVertexData = [ 0, 1, + -0.95, 0.31, + -0.59, -0.81, + 0.59, -0.81, + 0.95, 0.31]; + + var pentagonVertexDataArray = new Float32Array(pentagonVertexData.length); + pentagonVertexDataArray.set(pentagonVertexData, 0); + var pentagonVertices = new dali.PropertyBuffer(pentagonVertexFormat, 5); + pentagonVertices.setData(pentagonVertexDataArray); + + var pentacleVertexFormat ={ "aPosition2" : dali.PROPERTY_VECTOR2}; + + var pentacleVertexData = [ 0, -1, + 0.59, 0.81, + -0.95, -0.31, + 0.95, -0.31, + -0.59, 0.81]; + + var pentacleVertexDataArray = new Float32Array(pentacleVertexData.length); + pentacleVertexDataArray.set(pentacleVertexData, 0); + var pentacleVertices = new dali.PropertyBuffer(pentacleVertexFormat, 5); + pentacleVertices.setData(pentacleVertexDataArray); + + var indexFormat ={ "indices" : dali.PROPERTY_INTEGER }; + + var indexData = [0, 1, 1, 2, 2, 3, 3, 4, 4, 0]; + + var indexDataArray = new Uint32Array(indexData.length); + indexDataArray.set(indexData, 0); + var indices = new dali.PropertyBuffer(indexFormat, 10); + indices.setData(indexDataArray); + + // Create geometry + var geometry = new dali.Geometry(); + geometry.addVertexBuffer( pentagonVertices ); + geometry.addVertexBuffer( pentacleVertices ); + geometry.setIndexBuffer( indices ); + geometry.setGeometryType( dali.GEOMETRY_LINES ); + + var renderer = new dali.Renderer(geometry, material); + renderer.depthIndex = 0; + + // Create mesh actor + var meshActor = new dali.Actor(); + meshActor.addRenderer( renderer ); + meshActor.size = [300, 300, 0]; + meshActor.parentOrigin = dali.CENTER; + meshActor.anchorPoint = dali.CENTER; + meshActor.registerAnimatableProperty("uMorphAmount", 0.0); + + dali.stage.add( meshActor ); + + // Animate the mesh actor + var animation = new dali.Animation(5); + + var keyFramesIndex = [{progress:0.0, value: 0.0}, {progress:1.0, value:1.0}]; + var animOptions = { alpha:"sin", delay:0, duration:5}; + + animation.animateBetween( meshActor, "uMorphAmount", keyFramesIndex, animOptions ); + animation.setLooping(true); + animation.play(); +} + +function startup() +{ + daliApp.init(); +} + +daliApp.init = function() +{ + daliApp.createMeshActor(); +} + + +startup(); + diff --git a/node-addon/mesh-morph.js b/node-addon/mesh-morph.js new file mode 100644 index 0000000..1fa1fb6 --- /dev/null +++ b/node-addon/mesh-morph.js @@ -0,0 +1,258 @@ + var window= { + x:0, + y:0, + width:1920, + height: 1080, + transparent: false, + name:'mesh-morph-app' + }; + + var viewMode={ + 'stereoscopic-mode':'mono', // stereo-horizontal, stereo-vertical, stereo-interlaced, + 'stereo-base': 65 // Distance in millimeters between left/right cameras typically between (50-70mm) + }; + + var options= { + 'window': window, + 'view-mode': viewMode, + } + +var dali = require('./build/Release/dali')( options ); + +var daliApp = {}; + +daliApp.createMeshActor = function() { + + // Create shader + var vertShader = + "attribute mediump vec2 aInitPos;\ + attribute mediump vec2 aFinalPos;\ + attribute mediump vec3 aColor;\ + uniform mediump mat4 uMvpMatrix;\ + uniform mediump vec3 uSize;\ + uniform mediump float uDelta;\ + uniform lowp vec4 uColor;\ + varying lowp vec4 vColor;\ + void main()\ + {\ + mediump vec4 vertexPosition = vec4(mix(aInitPos, aFinalPos, uDelta), 0.0, 1.0);\ + vertexPosition.xyz *= uSize;\ + vertexPosition = uMvpMatrix * vertexPosition;\ + gl_Position = vertexPosition;\ + vColor = vec4(aColor, 0.) * uColor;\ + }"; + + var fragShader = + "varying lowp vec4 vColor;\ + void main()\ + {\ + gl_FragColor = vColor;\ + }"; + + var shaderOptions = { + vertexShader : vertShader, + fragmentShader : fragShader + }; + + var shader = new dali.Shader(shaderOptions); + + var material = new dali.Material( shader ); + + // Create vertex buffer for initial positions + var initialPositionVertexFormat = { "aInitPos" : dali.PROPERTY_VECTOR2 }; + + var initialPositionVertexData = [ + // yellow + -0.50, -0.50, + 0.0, 0.0, + -0.50, 0.50, + + // green + -0.50, -0.50, + 0.50, -0.50, + 0.0, 0.0, + + // blue + 0.50, -0.50, + 0.50, 0.0, + 0.25, -0.25, + + // red + 0.25, -0.25, + 0.50, 0.0, + 0.25, 0.25, + 0.25, 0.25, + 0.0, 0.0, + 0.25, -0.25, + + // cyan + 0.0, 0.0, + 0.25, 0.25, + -0.25, 0.25, + + // magenta + -0.25, 0.25, + 0.25, 0.25, + 0.0, 0.50, + 0.0, 0.50, + -0.50, 0.50, + -0.25, 0.25, + + // orange + 0.50, 0.0, + 0.50, 0.50, + 0.0, 0.50 + ]; + + var initialPositionVertexDataArray = new Float32Array(initialPositionVertexData.length); + initialPositionVertexDataArray.set(initialPositionVertexData, 0); + var initialPositionVertices = new dali.PropertyBuffer(initialPositionVertexFormat, 27); + initialPositionVertices.setData(initialPositionVertexDataArray); + + // Create vertex buffer for final positions + var finalPositionVertexFormat = { "aFinalPos" : dali.PROPERTY_VECTOR2 }; + + var bigSide = 0.707106781; + var side = bigSide * 0.5; + + var finalPositionVertexData = [ + // yellow + side, 0.25, // pA + side, 0.25 + bigSide, // pB + side - bigSide, 0.25 + bigSide, // pC + + // green + side - 0.50, -0.25, // pD + side, 0.25, // pA + side - 0.50, 0.75, // pE + + // blue + side - 0.50, -0.75, // pJ + side - 0.50, -0.25, // pD + side - 0.75, -0.50, // pI + + // red + side - 0.75, -0.50, // pI + side - 0.50, -0.25, // pD + side - 0.75, 0.0, // pG + side - 0.75, 0.0, // pG + side - 1.0, -0.25, // pH + side - 0.75, -0.50, // pI + + // cyan + side - 0.75, -0.50, // pI + side - 1.0, -0.25, // pH + side - 1.0, -0.75, // pK + + // magenta + side, 0.25 + bigSide - side, // pL + side * 2.0, 0.25 + bigSide - side * 2.0, // pM + side * 2.0, 0.25 + bigSide - side, // pN + side * 2.0, 0.25 + bigSide - side, // pN + side, 0.25 + bigSide, // pB + side, 0.25 + bigSide - side, // pL + + // orange + side - 0.50, -0.25, // pD + side - 0.50, -0.25 + bigSide, // pF2 + - 0.50, -0.25 + side // pF + ]; + + var finalPositionVertexDataArray = new Float32Array(finalPositionVertexData.length); + finalPositionVertexDataArray.set(finalPositionVertexData, 0); + var finalPositionVertices = new dali.PropertyBuffer(finalPositionVertexFormat, 27); + finalPositionVertices.setData(finalPositionVertexDataArray); + + // Create vertex buffer for color + var colorVertexFormat = { "aColor" : dali.PROPERTY_VECTOR3 }; + + var colorVertexData = [ + // yellow + 1.0, 1.0, 0.0, + 1.0, 1.0, 0.0, + 1.0, 1.0, 0.0, + + // green + 0.0, 1.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, 1.0, 0.0, + + // blue + 0.0, 0.0, 1.0, + 0.0, 0.0, 1.0, + 0.0, 0.0, 1.0, + + // red + 1.0, 0.0, 0.0, + 1.0, 0.0, 0.0, + 1.0, 0.0, 0.0, + 1.0, 0.0, 0.0, + 1.0, 0.0, 0.0, + 1.0, 0.0, 0.0, + + // cyan + 0.0, 1.0, 1.0, + 0.0, 1.0, 1.0, + 0.0, 1.0, 1.0, + + // magenta + 1.0, 0.0, 1.0, + 1.0, 0.0, 1.0, + 1.0, 0.0, 1.0, + 1.0, 0.0, 1.0, + 1.0, 0.0, 1.0, + 1.0, 0.0, 1.0, + + // orange + 1.0, 0.5, 0.0, + 1.0, 0.5, 0.0, + 1.0, 0.5, 0.0 + ]; + + var colorVertexDataArray = new Float32Array(colorVertexData.length); + colorVertexDataArray.set(colorVertexData, 0); + var colorVertices = new dali.PropertyBuffer(colorVertexFormat, 27); + colorVertices.setData(colorVertexDataArray); + + // Create geometry + var geometry = new dali.Geometry(); + geometry.addVertexBuffer( initialPositionVertices ); + geometry.addVertexBuffer( finalPositionVertices ); + geometry.addVertexBuffer( colorVertices ); + + var renderer = new dali.Renderer(geometry, material); + renderer.depthIndex = 0; + + // Create mesh actor + var meshActor = new dali.Actor(); + meshActor.addRenderer( renderer ); + meshActor.size = [400, 400, 0]; + meshActor.parentOrigin = dali.CENTER; + meshActor.anchorPoint = dali.CENTER; + meshActor.registerAnimatableProperty("uDelta", 0.0); + + dali.stage.add( meshActor ); + + // Animate the mesh actor + var animation = new dali.Animation(5); + + var animOptions = { alpha:"sin", delay:0, duration:5}; + + animation.animateTo( meshActor, "uDelta", 1.0, animOptions ); + animation.setLooping(true); + animation.play(); +} + +function startup() +{ + daliApp.init(); +} + +daliApp.init = function() +{ + daliApp.createMeshActor(); +} + + +startup(); + diff --git a/node-addon/point-mesh.js b/node-addon/point-mesh.js new file mode 100644 index 0000000..afe6e3a --- /dev/null +++ b/node-addon/point-mesh.js @@ -0,0 +1,149 @@ + var window= { + x:0, + y:0, + width:1920, + height: 1080, + transparent: false, + name:'line-mesh-app' + }; + + var viewMode={ + 'stereoscopic-mode':'mono', // stereo-horizontal, stereo-vertical, stereo-interlaced, + 'stereo-base': 65 // Distance in millimeters between left/right cameras typically between (50-70mm) + }; + + var options= { + 'window': window, + 'view-mode': viewMode, + } + +var imageDir = "./"; + +var dali = require('./build/Release/dali')( options ); + +var daliApp = {}; + +daliApp.createMeshActor = function() { + + // Create shader + var vertShader = + "attribute mediump vec2 aPosition;\ + attribute highp float aHue;\ + varying mediump vec2 vTexCoord;\ + uniform mediump mat4 uMvpMatrix;\ + uniform mediump vec3 uSize;\ + uniform mediump float uPointSize;\ + uniform lowp vec4 uFadeColor;\ + varying mediump vec3 vVertexColor;\ + varying mediump float vHue;\ + vec3 hsv2rgb(vec3 c)\ + {\ + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\ + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\ + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\ + }\ + void main()\ + {\ + mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\ + vertexPosition.xyz *= (uSize-uPointSize);\ + vertexPosition = uMvpMatrix * vertexPosition;\ + vVertexColor = hsv2rgb( vec3( aHue, 0.7, 1.0 ) );\ + vHue = aHue;\ + gl_PointSize = uPointSize;\ + gl_Position = vertexPosition;\ + }"; + + var fragShader = + "varying mediump vec3 vVertexColor;\ + varying mediump float vHue;\ + uniform lowp vec4 uColor;\ + uniform sampler2D sTexture1;\ + uniform sampler2D sTexture2;\ + uniform lowp vec4 uFadeColor;\ + void main()\ + {\ + mediump vec4 texCol1 = texture2D(sTexture1, gl_PointCoord);\ + mediump vec4 texCol2 = texture2D(sTexture2, gl_PointCoord);\ + gl_FragColor = vec4(vVertexColor, 1.0) * ((texCol1*vHue) + (texCol2*(1.0-vHue)));\ + }"; + + var shaderOptions = { + vertexShader : vertShader, + fragmentShader: fragShader + }; + + var shader = new dali.Shader(shaderOptions); + + var material = new dali.Material( shader ); + var image = new dali.ResourceImage( {url: imageDir + "image-1.jpg"} ); + material.addTexture(image, "sTexture"); + + // Create vertex buffer + var polyhedraVertexFormat ={ "aPosition" : dali.PROPERTY_VECTOR2, + "aHue" : dali.PROPERTY_FLOAT }; + + var numSides = 20; + var angle = 0; + var sectorAngle = 2.0 * Math.PI / numSides; + + var polyhedraVertexData = []; + for(i=0; i #include #include +#include +#include namespace Dali { @@ -697,6 +699,130 @@ void ActorApi::ScaleBy( const v8::FunctionCallbackInfo& args ) actor.ScaleBy( vector ); } +/** + * Add a renderer to this actor. + * @example + * + * var renderer = new dali.Renderer( geometry, material ); + * actor.addRenderer( renderer ); + * + * @for Actor + * @method addRenderer + * @param {object} renderer Renderer to add to the actor + * @return {integer} The index of the Renderer that was added + */ +void ActorApi::AddRenderer( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + Actor actor = GetActor( isolate, args ); + + unsigned int index = 0; + + bool found( false ); + Renderer renderer = RendererApi::GetRendererFromParams( 0, found, isolate, args ); + if( found ) + { + index = actor.AddRenderer(renderer); + } + else + { + DALI_SCRIPT_EXCEPTION( isolate, "Renderer parameter missing" ); + return; + } + + args.GetReturnValue().Set( v8::Integer::New( isolate, index ) ); +} + +/** + * Get the number of renderers on this actor. + * @example + * + * var count = actor.getRendererCount(); + * + * @for Actor + * @method getRendererCount + * @return {integer} the number of renderers on this actor + */ +void ActorApi::GetRendererCount( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Actor actor = GetActor( isolate, args ); + args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetRendererCount() ) ); +} + +/** + * Get a Renderer by index. + * @example + * + * var renderer = actor.getRendererAt( 0 ); + * + * @for Actor + * @method getRendererAt + * @param {integer} index The index of the renderer to fetch, which must be between 0 and getRendererCount()-1 + * @return {object} The renderer at the specified index + */ +void ActorApi::GetRendererAt( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + Actor actor = GetActor( isolate, args ); + + Renderer renderer; + + bool found( false ); + int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" ); + return; + } + else + { + renderer = actor.GetRendererAt(static_cast(index)); + if( !renderer ) + { + DALI_SCRIPT_EXCEPTION( isolate, "renderer not found" ); + return; + } + } + + // Wrap the renderer + v8::Local localObject = RendererWrapper::WrapRenderer( isolate, renderer ); + args.GetReturnValue().Set( localObject ); +} + +/** + * Remove an renderer from the actor by index. + * @example + * + * actor.removeRenderer( 0 ); + * + * @for Actor + * @method removeRenderer + * @param {integer} index Index of the renderer to be removed, which must be between 0 and getRendererCount()-1 + */ +void ActorApi::RemoveRenderer( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + Actor actor = GetActor( isolate, args ); + + bool found( false ); + int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" ); + } + else + { + actor.RemoveRenderer(static_cast(index)); + } +} + + } // namespace V8Plugin } // namespace Dali diff --git a/plugins/dali-script-v8/src/actors/actor-api.h b/plugins/dali-script-v8/src/actors/actor-api.h index 82f41d3..26f8991 100644 --- a/plugins/dali-script-v8/src/actors/actor-api.h +++ b/plugins/dali-script-v8/src/actors/actor-api.h @@ -70,6 +70,10 @@ namespace ActorApi void TranslateBy( const v8::FunctionCallbackInfo< v8::Value >& args ); void RotateBy( const v8::FunctionCallbackInfo< v8::Value >& args ); void ScaleBy( const v8::FunctionCallbackInfo< v8::Value >& args ); + void AddRenderer( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetRendererCount( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetRendererAt( const v8::FunctionCallbackInfo< v8::Value >& args ); + void RemoveRenderer( const v8::FunctionCallbackInfo< v8::Value >& args ); // new function just for JavaScript API, to help developers know what type of actor // they're dealing with, returns actor name as a string diff --git a/plugins/dali-script-v8/src/actors/actor-wrapper.cpp b/plugins/dali-script-v8/src/actors/actor-wrapper.cpp index 9b81f41..a462723 100644 --- a/plugins/dali-script-v8/src/actors/actor-wrapper.cpp +++ b/plugins/dali-script-v8/src/actors/actor-wrapper.cpp @@ -227,6 +227,11 @@ const ActorFunctions ActorFunctionTable[]= { "SetKeyboardFocusable", ActorApi::SetKeyboardFocusable, ACTOR_API }, //-- should this be a property??? { "IsKeyboardFocusable" , ActorApi::IsKeyboardFocusable, ACTOR_API }, //-- should this be a property??? + { "AddRenderer", ActorApi::AddRenderer, ACTOR_API }, + { "GetRendererCount", ActorApi::GetRendererCount, ACTOR_API }, + { "GetRendererAt" , ActorApi::GetRendererAt, ACTOR_API }, + { "RemoveRenderer" , ActorApi::RemoveRenderer, ACTOR_API }, + /************************************** * Layer API (in order of layer.h) **************************************/ diff --git a/plugins/dali-script-v8/src/constants/constants-wrapper.cpp b/plugins/dali-script-v8/src/constants/constants-wrapper.cpp index 1571273..162db9e 100644 --- a/plugins/dali-script-v8/src/constants/constants-wrapper.cpp +++ b/plugins/dali-script-v8/src/constants/constants-wrapper.cpp @@ -30,9 +30,11 @@ #include #include #include +#include #include #include - +#include +#include namespace Dali { @@ -211,8 +213,46 @@ const IntegerPair EnumTable[] = {"IMAGE_ACTOR_STYLE_QUAD", ImageActor::STYLE_QUAD}, {"IMAGE_ACTOR_STYLE_NINE_PATCH", ImageActor::STYLE_NINE_PATCH}, - {"IMAGE_ACTOR_STYLE_NINE_PATCH_NO_CENTER",ImageActor::STYLE_NINE_PATCH_NO_CENTER } - + {"IMAGE_ACTOR_STYLE_NINE_PATCH_NO_CENTER",ImageActor::STYLE_NINE_PATCH_NO_CENTER }, + + { "FILTER_MODE_NONE", FilterMode::NONE }, + { "FILTER_MODE_DEFAULT", FilterMode::DEFAULT }, + { "FILTER_MODE_NEAREST", FilterMode::NEAREST }, + { "FILTER_MODE_LINEAR", FilterMode::LINEAR }, + + { "WRAP_MODE_DEFAULT", WrapMode::DEFAULT }, + { "WRAP_MODE_CLAMP_TO_EDGE", WrapMode::CLAMP_TO_EDGE }, + { "WRAP_MODE_REPEAT", WrapMode::REPEAT }, + { "WRAP_MODE_MIRRORED_REPEAT", WrapMode::MIRRORED_REPEAT }, + + { "MATERIAL_NONE", Material::NONE }, + { "MATERIAL_CULL_BACK", Material::CULL_BACK }, + { "MATERIAL_CULL_FRONT", Material::CULL_FRONT }, + { "MATERIAL_CULL_BACK_AND_FRONT", Material::CULL_BACK_AND_FRONT }, + + { "GEOMETRY_POINTS", Geometry::POINTS }, + { "GEOMETRY_LINES", Geometry::LINES }, + { "GEOMETRY_LINE_LOOP", Geometry::LINE_LOOP }, + { "GEOMETRY_LINE_STRIP", Geometry::LINE_STRIP }, + { "GEOMETRY_TRIANGLES", Geometry::TRIANGLES }, + { "GEOMETRY_TRIANGLE_FAN", Geometry::TRIANGLE_FAN }, + { "GEOMETRY_TRIANGLE_STRIP", Geometry::TRIANGLE_STRIP }, + + { "PROPERTY_NONE", Property::NONE }, + { "PROPERTY_BOOLEAN", Property::BOOLEAN }, + { "PROPERTY_FLOAT", Property::FLOAT }, + { "PROPERTY_INTEGER", Property::INTEGER }, + { "PROPERTY_VECTOR2", Property::VECTOR2 }, + { "PROPERTY_VECTOR3", Property::VECTOR3 }, + { "PROPERTY_VECTOR4", Property::VECTOR4 }, + { "PROPERTY_MATRIX3", Property::MATRIX3 }, + { "PROPERTY_MATRIX", Property::MATRIX }, + { "PROPERTY_RECTANGLE", Property::RECTANGLE }, + { "PROPERTY_ROTATION", Property::ROTATION }, + { "PROPERTY_STRING", Property::STRING }, + { "PROPERTY_ARRAY", Property::ARRAY }, + { "PROPERTY_MAP", Property::MAP }, + { "PROPERTY_INVALID_INDEX", Property::INVALID_INDEX }, }; const unsigned int EnumTableCount = sizeof(EnumTable)/sizeof(EnumTable[0]); diff --git a/plugins/dali-script-v8/src/dali-wrapper.cpp b/plugins/dali-script-v8/src/dali-wrapper.cpp index 257b5d4..cc905a3 100644 --- a/plugins/dali-script-v8/src/dali-wrapper.cpp +++ b/plugins/dali-script-v8/src/dali-wrapper.cpp @@ -31,6 +31,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include #include #include #include @@ -73,6 +79,12 @@ const ApiFunction ConstructorFunctionTable[]= { "FrameBufferImage", ImageWrapper::NewImage }, { "Animation", AnimationWrapper::NewAnimation}, { "ShaderEffect", ShaderEffectWrapper::NewShaderEffect}, + { "Shader", ShaderWrapper::NewShader}, + { "Sampler", SamplerWrapper::NewSampler}, + { "Material", MaterialWrapper::NewMaterial}, + { "Geometry", GeometryWrapper::NewGeometry}, + { "Renderer", RendererWrapper::NewRenderer}, + { "PropertyBuffer", PropertyBufferWrapper::NewPropertyBuffer}, { "Builder", BuilderWrapper::NewBuilder}, { "PanGestureDetector", PanGestureDetectorWrapper::NewPanGestureDetector}, diff --git a/plugins/dali-script-v8/src/object/handle-wrapper.cpp b/plugins/dali-script-v8/src/object/handle-wrapper.cpp index 88a6e3e..2705eb0 100644 --- a/plugins/dali-script-v8/src/object/handle-wrapper.cpp +++ b/plugins/dali-script-v8/src/object/handle-wrapper.cpp @@ -33,6 +33,24 @@ namespace Dali namespace V8Plugin { +namespace // un-named name space +{ + +/** + * Contains a list of all functions that can be called + */ +const ApiFunction HandleFunctionTable[]= +{ + { "RegisterAnimatableProperty", HandleWrapper::RegisterAnimatableProperty }, +}; + +const unsigned int HandleFunctionTableCount = sizeof(HandleFunctionTable)/sizeof(HandleFunctionTable[0]); +} //un-named space + +/** + * @class Handle + */ + HandleWrapper::HandleWrapper( BaseWrappedObject::Type type, Handle handle, GarbageCollectorInterface& gc ) : @@ -175,10 +193,67 @@ void HandleWrapper::AddInterceptsToTemplate( v8::Isolate* isolate, v8::LocalSetNamedPropertyHandler( HandleWrapper::PropertyGet, HandleWrapper::PropertySet); + // add function properties + ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, HandleFunctionTable, HandleFunctionTableCount ); + ObjectTemplateHelper::AddSignalConnectAndDisconnect( isolate, objTemplate ); } +/** + * Register a new animatable property. + * + * The object should support dynamic properties. + * Property names are expected to be unique, but this is not enforced. + * Property indices are unique to each registered custom property in a given object. + * returns dali.PROPERTY_INVALID_INDEX if registration failed. This can happen if you try + * to register animatable property on an object that does not have scene graph object. + * + * @method registerAnimatableProperty + * @for Handle + * @param {string} name The name of the property. + * @param {Object} propertyValue The new value of the property. + * @return {integer} The index of the property or dali.PROPERTY_INVALID_INDEX if registration failed + * @example + * + * var morphPropertyIdex = actor.registerAnimatableProperty("uMorphAmount", 0.0f); + * var fadeColorPropertyIdex = handle.registerAnimatableProperty("uFadeColor", [1.0, 0.0, 0.0, 1.0]); + * + */ +void HandleWrapper::RegisterAnimatableProperty( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + // unwrap the object + HandleWrapper* handleWrapper = Unwrap( isolate, args.This() ); + if( !handleWrapper ) + { + return; + } + + Handle handle = handleWrapper->mHandle; + + bool found( false ); + std::string propertyName = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "bad property name parameter" ); + return; + } + + found = false; + Dali::Property::Value daliPropertyValue = V8Utils::GetPropertyValueParameter(PARAMETER_1, found, isolate, args ); + if( !found || Dali::Property::NONE == daliPropertyValue.GetType() ) + { + DALI_SCRIPT_EXCEPTION( isolate, "bad property value parameter" ); + return; + } + else + { + args.GetReturnValue().Set( v8::Integer::New( isolate, handle.RegisterProperty(propertyName, daliPropertyValue) ) ); + } +} } // namespace V8Plugin diff --git a/plugins/dali-script-v8/src/object/handle-wrapper.h b/plugins/dali-script-v8/src/object/handle-wrapper.h index e0b1859..2e612c7 100644 --- a/plugins/dali-script-v8/src/object/handle-wrapper.h +++ b/plugins/dali-script-v8/src/object/handle-wrapper.h @@ -74,6 +74,12 @@ public: */ static void AddInterceptsToTemplate( v8::Isolate* isolate, v8::Local& objTemplate ); + /** + * @brief Register an animatable property for a JavaScript object that + * contains a Dali Handle. + * @param[in] args v8 function call arguments interpreted + */ + static void RegisterAnimatableProperty( const v8::FunctionCallbackInfo< v8::Value >& args ); Handle GetHandle() { return mHandle; } Handle mHandle; diff --git a/plugins/dali-script-v8/src/object/property-buffer-api.cpp b/plugins/dali-script-v8/src/object/property-buffer-api.cpp new file mode 100644 index 0000000..dabaaf6 --- /dev/null +++ b/plugins/dali-script-v8/src/object/property-buffer-api.cpp @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// CLASS HEADER +#include "property-buffer-api.h" + +// INTERNAL INCLUDES +#include +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +namespace // unnamed namespace +{ + +struct PropertyBufferParameters +{ + PropertyBufferParameters() + : mSize( 0 ) + { + } + + PropertyBuffer NewPropertyBuffer() + { + return PropertyBuffer::New( mBufferFormat, + mSize); + } + + Property::Map mBufferFormat; + std::size_t mSize; +}; + +} // unnamed space + +/** + * ## PropertyBuffer API + * + * PropertyBuffer is a handle to an object that contains a buffer of structured properties. + * It can be used to provide data to Geometry objects. + * + * ### Simple example + * + *``` + * var vertexFormat ={ "aPosition" : dali.PROPERTY_VECTOR2 }; + * var vertexData = [ 0, 1, + * -0.95, 0.31, + * -0.59, -0.81, + * 0.59, -0.81, + * 0.95, 0.31]; + * + * var vertexDataArray = new Float32Array(vertexData.length); + * vertexDataArray.set(vertexData, 0); + * var vertices = new dali.PropertyBuffer(vertexFormat, 5); + * vertices.setData(vertexDataArray); + * + * var geometry = new dali.Geometry(); + * geometry.addVertexBuffer( vertices ); + *``` + * @class PropertyBuffer + */ + +PropertyBuffer PropertyBufferApi::GetPropertyBuffer( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::HandleScope handleScope( isolate ); + + v8::Local object = args.This(); + v8::Local field = v8::Local::Cast( object->GetInternalField(0) ); + void* ptr = field->Value(); + + PropertyBufferWrapper* wrapper = static_cast< PropertyBufferWrapper *>(ptr); + return wrapper->GetPropertyBuffer(); +} + +PropertyBuffer PropertyBufferApi::GetPropertyBufferFromParams( int paramIndex, + bool& found, + v8::Isolate* isolate, + const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + found = false; + + v8::HandleScope handleScope( isolate ); + BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::PROPERTY_BUFFER, isolate, args ); + if( wrappedObject ) + { + found = true; + PropertyBufferWrapper* wrapper = static_cast< PropertyBufferWrapper *>(wrappedObject); + return wrapper->GetPropertyBuffer(); + } + else + { + return PropertyBuffer(); + } +} + +/** + * Create a new PropertyBuffer + * + * PropertyBuffers contains a buffer of structured properties and can be + * used to provide data to Geometry objects. + * + * @constructor + * @for PropertyBuffer + * @method PropertyBuffer + * @param {Object} bufferFormat + * @param {integer} size + * @return {Object} PropertyBuffer + * @example + *``` + * var bufferFormat = { + * "aPosition" : dali.PROPERTY_VECTOR2, + * "aTexCoord" : dali.PROPERTY_VECTOR2, + * "aHue" : dali.PROPERTY_FLOAT, + * }; + * + * var buffer = new dali.PropertyBuffer(bufferFormat, 5); + *``` + */ +PropertyBuffer PropertyBufferApi::New( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::HandleScope handleScope( isolate ); + + bool found( false ); + Dali::Property::Map bufferFormat = V8Utils::GetPropertyMapParameter( PARAMETER_0, found, isolate, args); + if( !found || bufferFormat.Empty() ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid property map parameter" ); + return PropertyBuffer(); + } + + found = false; + int size = V8Utils::GetIntegerParameter( PARAMETER_1, found, isolate, args, 0); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing buffer size from param 1" ); + return PropertyBuffer(); + } + + return PropertyBuffer::New(bufferFormat, static_cast(size)); +} + +/** + * Update the whole buffer information + * + * This function expects an array of data with the same format that was given + * in the construction, and the number of elements to be the same as the size + * of the buffer. + * + * @method setData + * @for PropertyBuffer + * @param {Object} data The data that will be copied to the buffer. + * @example + *``` + * var vertexData = [ 0, 1, + * -0.95, 0.31, + * -0.59, -0.81, + * 0.59, -0.81, + * 0.95, 0.31]; + * + * var vertexDataArray = new Float32Array(vertexData.length); + * vertexDataArray.set(vertexData, 0); + * + * propertyBuffer.setData( vertexDataArray ); + *``` + */ +void PropertyBufferApi::SetData( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + PropertyBuffer buffer = GetPropertyBuffer( isolate, args ); + + bool found( false ); + void* data = V8Utils::GetArrayBufferViewParameter( PARAMETER_0, found, isolate, args); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid data parameter" ); + } + else + { + buffer.SetData( data ); + } +} + +} // namespace V8Plugin + +} // namespace Dali diff --git a/plugins/dali-script-v8/src/object/property-buffer-api.h b/plugins/dali-script-v8/src/object/property-buffer-api.h new file mode 100644 index 0000000..39b1707 --- /dev/null +++ b/plugins/dali-script-v8/src/object/property-buffer-api.h @@ -0,0 +1,70 @@ +#ifndef __DALI_V8PLUGIN_PROPERTY_BUFFER_API_H__ +#define __DALI_V8PLUGIN_PROPERTY_BUFFER_API_H__ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + + +namespace Dali +{ + +namespace V8Plugin +{ + +namespace PropertyBufferApi +{ + + /** + * @brief Helper to get property buffer from args.This() + * @param[in] isolate v8 isolated instance + * @param[in] args v8 function call arguments interpreted + */ + PropertyBuffer GetPropertyBuffer( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * @brief Helper to get property buffer from the JavaScript object held in the given function argument + * @param[in] paramIndex Argument index the object is held in + * @param[in] found Whether property buffer is found in the given function parameter + * @param[in] isolate v8 isolated instance + * @param[in] args v8 function call arguments interpreted + */ + PropertyBuffer GetPropertyBufferFromParams( int paramIndex, + bool& found, + v8::Isolate* isolate, + const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * Constructor + */ + PropertyBuffer New( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * PropertyBuffer API see property-buffer.h for a description + */ + void SetData( const v8::FunctionCallbackInfo< v8::Value >& args ); + +}; // namespace PropertyBufferApi + +} // namespace V8Plugin + +} // namespace Dali + +#endif // header __DALI_V8PLUGIN_PROPERTY_BUFFER_API_H__ diff --git a/plugins/dali-script-v8/src/object/property-buffer-wrapper.cpp b/plugins/dali-script-v8/src/object/property-buffer-wrapper.cpp new file mode 100644 index 0000000..b3a1894 --- /dev/null +++ b/plugins/dali-script-v8/src/object/property-buffer-wrapper.cpp @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// CLASS HEADER +#include "property-buffer-wrapper.h" + +// INTERNAL INCLUDES +#include +#include +#include +#include +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +v8::Persistent PropertyBufferWrapper::mPropertyBufferTemplate; + +namespace // un-named name space +{ + +/** + * Contains a list of all functions that can be called + */ +const ApiFunction PropertyBufferFunctionTable[]= +{ + /************************************** + * PropertyBuffer API (in order of property-buffer.h) + **************************************/ + + { "SetData" , PropertyBufferApi::SetData }, +}; + +const unsigned int PropertyBufferFunctionTableCount = sizeof(PropertyBufferFunctionTable)/sizeof(PropertyBufferFunctionTable[0]); +} //un-named space + + +PropertyBufferWrapper::PropertyBufferWrapper( const Dali::PropertyBuffer& buffer, GarbageCollectorInterface& gc ) +: BaseWrappedObject( BaseWrappedObject::PROPERTY_BUFFER , gc ) +{ + mPropertyBuffer = buffer; +} + +v8::Handle PropertyBufferWrapper::WrapPropertyBuffer(v8::Isolate* isolate, const Dali::PropertyBuffer& buffer ) +{ + v8::EscapableHandleScope handleScope( isolate ); + v8::Local objectTemplate; + + objectTemplate = GetPropertyBufferTemplate( isolate); + + // create an instance of the template + v8::Local localObject = objectTemplate->NewInstance(); + + // create the PropertyBuffer wrapper + PropertyBufferWrapper* pointer = new PropertyBufferWrapper( buffer, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() ); + + // assign the JavaScript object to the wrapper. + pointer->SetJavascriptObject( isolate, localObject ); + + return handleScope.Escape( localObject ); +} + +v8::Local PropertyBufferWrapper::GetPropertyBufferTemplate( v8::Isolate* isolate) +{ + v8::EscapableHandleScope handleScope( isolate ); + v8::Local objectTemplate; + + if( mPropertyBufferTemplate.IsEmpty() ) + { + objectTemplate = MakePropertyBufferTemplate( isolate ); + mPropertyBufferTemplate.Reset( isolate, objectTemplate ); + } + else + { + // get the object template + objectTemplate = v8::Local::New( isolate, mPropertyBufferTemplate ); + } + return handleScope.Escape( objectTemplate ); +} + +v8::Handle PropertyBufferWrapper::MakePropertyBufferTemplate( v8::Isolate* isolate ) +{ + v8::EscapableHandleScope handleScope( isolate ); + + v8::Local objTemplate = v8::ObjectTemplate::New(); + + objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT ); + + // add our function properties + ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, PropertyBufferFunctionTable, PropertyBufferFunctionTableCount ); + + return handleScope.Escape( objTemplate ); +} + +void PropertyBufferWrapper::NewPropertyBuffer( const v8::FunctionCallbackInfo< v8::Value >& args) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate); + + if(!args.IsConstructCall()) + { + DALI_SCRIPT_EXCEPTION( isolate, "PropertyBuffer constructor called without 'new'"); + return; + } + Dali::PropertyBuffer buffer = PropertyBufferApi::New( isolate, args ); + + if(buffer) + { + v8::Local localObject = WrapPropertyBuffer( isolate, buffer ); + args.GetReturnValue().Set( localObject ); + } +} + + +PropertyBuffer PropertyBufferWrapper::GetPropertyBuffer() +{ + return mPropertyBuffer; +} + + +} // namespace V8Plugin + +} // namespace Dali diff --git a/plugins/dali-script-v8/src/object/property-buffer-wrapper.h b/plugins/dali-script-v8/src/object/property-buffer-wrapper.h new file mode 100644 index 0000000..7cb8b84 --- /dev/null +++ b/plugins/dali-script-v8/src/object/property-buffer-wrapper.h @@ -0,0 +1,95 @@ +#ifndef __DALI_V8PLUGIN_PROPERTY_BUFFER_WRAPPER_H__ +#define __DALI_V8PLUGIN_PROPERTY_BUFFER_WRAPPER_H__ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + +// INTERNAL INCLUDES +#include + +namespace Dali +{ + +namespace V8Plugin +{ + + +/** + * A PropertyBuffer wrapper. + * Provides access to PropertyBuffer specific functionality and V8 memory handling. + */ +class PropertyBufferWrapper : public BaseWrappedObject +{ + +public: + + /** + * @brief Constructor + * @param[in] propertyBuffer DALi property buffer + * @param[in] gc garbage collection interface + */ + PropertyBufferWrapper( const PropertyBuffer& propertyBuffer, + GarbageCollectorInterface& gc ); + + /** + * @brief Destructor + */ + virtual ~PropertyBufferWrapper() + { + }; + + /** + * @brief Creates a new PropertyBuffer wrapped inside a Javascript Object. + * @param[in] args v8 function call arguments interpreted + */ + static void NewPropertyBuffer( const v8::FunctionCallbackInfo< v8::Value >& args); + + /** + * Wraps a property buffer + */ + static v8::Handle WrapPropertyBuffer(v8::Isolate* isolate, const Dali::PropertyBuffer& ); + + + // The PropertyBufferAttribute ObjectTemplate, we cache templates so we don't have + // keep generating them everytime we want to create a PropertyBuffer + static v8::Persistent mPropertyBufferTemplate; + + /** + * @brief Gets the handle of property buffer + */ + PropertyBuffer GetPropertyBuffer(); + + +private: + + // PropertyBuffer + PropertyBuffer mPropertyBuffer; + + static v8::Handle MakePropertyBufferTemplate( v8::Isolate* isolate ); + static v8::Local GetPropertyBufferTemplate( v8::Isolate* isolate ); + +}; + +} // namespace V8Plugin + +} // namespace Dali + +#endif // __DALI_V8PLUGIN_PROPERTY_BUFFER_WRAPPER_H__ diff --git a/plugins/dali-script-v8/src/object/property-value-wrapper.cpp b/plugins/dali-script-v8/src/object/property-value-wrapper.cpp index 0b70084..f518848 100644 --- a/plugins/dali-script-v8/src/object/property-value-wrapper.cpp +++ b/plugins/dali-script-v8/src/object/property-value-wrapper.cpp @@ -754,6 +754,16 @@ Dali::Property::Value PropertyValueWrapper::ExtractPropertyValue( v8::Isolate* i daliPropertyValue = ArrayFromV8Array( isolate, v8Value ); break; } + case Dali::Property::MAP: + { + if( v8Value->IsObject() ) + { + v8::Local object = v8::Handle::Cast(v8Value); + Dali::Property::Map propertyMap = V8Utils::GetPropertyMapFromObject(isolate, object); + daliPropertyValue = Dali::Property::Value( propertyMap ); + } + break; + } case Dali::Property::NONE: default: { diff --git a/plugins/dali-script-v8/src/rendering/geometry-api.cpp b/plugins/dali-script-v8/src/rendering/geometry-api.cpp new file mode 100644 index 0000000..01aec00 --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/geometry-api.cpp @@ -0,0 +1,278 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +// CLASS HEADER +#include "geometry-api.h" + +// EXTERNAL INCLUDES +#include + +// INTERNAL INCLUDES +#include +#include +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +/** + * ## Geometry API + * + * Geometry is handle to an object that can be used to define a geometric elements. + * + * @class Geometry + * @extends Handle + */ + +Geometry GeometryApi::GetGeometry( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::HandleScope handleScope( isolate ); + + v8::Local object = args.This(); + v8::Local field = v8::Local::Cast( object->GetInternalField(0) ); + void* ptr = field->Value(); + + GeometryWrapper* wrapper = static_cast< GeometryWrapper *>(ptr); + return wrapper->GetGeometry(); +} + +Geometry GeometryApi::GetGeometryFromParams( int paramIndex, + bool& found, + v8::Isolate* isolate, + const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + found = false; + + v8::HandleScope handleScope( isolate ); + BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::GEOMETRY, isolate, args ); + if( wrappedObject ) + { + found = true; + GeometryWrapper* wrapper = static_cast< GeometryWrapper *>(wrappedObject); + return wrapper->GetGeometry(); + } + else + { + return Geometry(); + } +} + +/** + * Create a new geometry object. + * + * @constructor + * @method Geometry + * @for Geometry + * @return {Object} Geometry + */ +Geometry GeometryApi::New( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + return Geometry::New(); +} + +/** + * Add a PropertyBuffer to be used as source of geometry vertices + * + * @method addVertexBuffer + * @for Geometry + * @param {Object} vertexBuffer PropertyBuffer to be used as source of geometry vertices + * @return {interger} Index of the newly added buffer, can be used with RemoveVertexBuffer + * to remove this buffer if no longer required + * @example + *``` + * var vertexFormat ={ "aPosition" : dali.PROPERTY_VECTOR2 }; + * var vertexData = [ 0, 1, + * -0.95, 0.31, + * -0.59, -0.81, + * 0.59, -0.81, + * 0.95, 0.31]; + * + * var vertexDataArray = new Float32Array(vertexData.length); + * vertexDataArray.set(vertexData, 0); + * var vertices = new dali.PropertyBuffer(vertexFormat, 5); + * vertices.setData(vertexDataArray); + * + * var geometry = new dali.Geometry(); + * geometry.addVertexBuffer( vertices ); + *``` + */ +void GeometryApi::AddVertexBuffer( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Geometry geometry = GetGeometry( isolate, args ); + + bool found( false ); + PropertyBuffer vertexBuffer = PropertyBufferApi::GetPropertyBufferFromParams( 0, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid property buffer parameter" ); + } + else + { + args.GetReturnValue().Set( v8::Integer::New( isolate, geometry.AddVertexBuffer(vertexBuffer) ) ); + } +} + +/** + * Retrieve the number of vertex buffers that have been added to this geometry + * + * @method getNumberOfVertexBuffers + * @for Geometry + * @return {integer} Number of vertex buffers that have been added to this geometry + */ +void GeometryApi::GetNumberOfVertexBuffers( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Geometry geometry = GetGeometry( isolate, args ); + + args.GetReturnValue().Set( v8::Integer::New( isolate, geometry.GetNumberOfVertexBuffers() ) ); +} + +/** + * Remove a vertex buffer + * + * @method removeVertexBuffer + * @for Geometry + * @param {integer} index Index to the vertex buffer to remove, + * which must be between 0 and getNumberOfVertexBuffers() + */ +void GeometryApi::RemoveVertexBuffer( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Geometry geometry = GetGeometry( isolate, args ); + + bool found( false ); + int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing index from param 0" ); + } + else + { + geometry.RemoveVertexBuffer(static_cast(index)); + } +} + +/** + * Set a PropertyBuffer to be used as a source of indices for the geometry + * + * This buffer is required to have exactly one component and it must be of the + * type dali.PROPERTY_INTEGER + * + * By setting this buffer it will cause the geometry to be rendered using indices. + * To unset call SetIndexBuffer with an empty handle. + * + * @method setIndexBuffer + * @for Geometry + * @param {Object} indexBuffer PropertyBuffer to be used as a source of indices + * for the geometry + */ +void GeometryApi::SetIndexBuffer( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Geometry geometry = GetGeometry( isolate, args ); + + bool found( false ); + PropertyBuffer indexBuffer = PropertyBufferApi::GetPropertyBufferFromParams( 0, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid property buffer parameter" ); + } + else + { + geometry.SetIndexBuffer(indexBuffer); + } +} + +/** + * Set the type of primitives this geometry contains + * + * @method setGeometryType + * @for Geometry + * @param {integer} geometryType Type of primitives this geometry contains + * @example + * // geometry type is one of the following + * dali.GEOMETRY_POINTS + * dali.GEOMETRY_LINES + * dali.GEOMETRY_LINE_LOOP + * dali.GEOMETRY_LINE_STRIP + * dali.GEOMETRY_TRIANGLES + * dali.GEOMETRY_TRIANGLE_FAN + * dali.GEOMETRY_TRIANGLE_STRIP + * + * geometry.SetGeometryType( dali.GEOMETRY_LINES ); + */ +void GeometryApi::SetGeometryType( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Geometry geometry = GetGeometry( isolate, args ); + + bool found( false ); + int geometryType = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing geometryType from param 0" ); + } + else + { + geometry.SetGeometryType(static_cast(geometryType)); + } +} + +/** + * Get the type of primitives this geometry contains + * + * @method getGeometryType + * @for Geometry + * @return {integer} Type of primitives this geometry contains + * @example + * // returns one of the following + * dali.GEOMETRY_POINTS + * dali.GEOMETRY_LINES + * dali.GEOMETRY_LINE_LOOP + * dali.GEOMETRY_LINE_STRIP + * dali.GEOMETRY_TRIANGLES + * dali.GEOMETRY_TRIANGLE_FAN + * dali.GEOMETRY_TRIANGLE_STRIP + */ +void GeometryApi::GetGeometryType( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Geometry geometry = GetGeometry( isolate, args ); + + args.GetReturnValue().Set( v8::Integer::New( isolate, geometry.GetGeometryType() ) ); +} + +} // namespace V8Plugin + +} // namespace Dali diff --git a/plugins/dali-script-v8/src/rendering/geometry-api.h b/plugins/dali-script-v8/src/rendering/geometry-api.h new file mode 100644 index 0000000..2a8778f --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/geometry-api.h @@ -0,0 +1,73 @@ +#ifndef __DALI_V8PLUGIN_GEOMETRY_API_H__ +#define __DALI_V8PLUGIN_GEOMETRY_API_H__ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + + +namespace Dali +{ + +namespace V8Plugin +{ + +namespace GeometryApi +{ + + /** + * Helper to get geometry from args.This() + */ + Geometry GetGeometry( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * @brief Helper to get geometry from the JavaScript object held in the given function argument + * @param[in] paramIndex Argument index the object is held in + * @param[in] found Whether geometry is found in the given function parameter + * @param[in] isolate v8 isolated instance + * @param[in] args v8 function call arguments interpreted + */ + Geometry GetGeometryFromParams( int paramIndex, + bool& found, + v8::Isolate* isolate, + const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * Constructor + */ + Geometry New( const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * Geometry API see geometry.h for a description + */ + void AddVertexBuffer( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetNumberOfVertexBuffers( const v8::FunctionCallbackInfo< v8::Value >& args ); + void RemoveVertexBuffer( const v8::FunctionCallbackInfo< v8::Value >& args ); + void SetIndexBuffer( const v8::FunctionCallbackInfo< v8::Value >& args ); + void SetGeometryType( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetGeometryType( const v8::FunctionCallbackInfo< v8::Value >& args ); + +}; // namespace GeometryApi + +} // namespace V8Plugin + +} // namespace Dali + +#endif // header __DALI_V8PLUGIN_GEOMETRY_API_H__ diff --git a/plugins/dali-script-v8/src/rendering/geometry-wrapper.cpp b/plugins/dali-script-v8/src/rendering/geometry-wrapper.cpp new file mode 100644 index 0000000..d9ea511 --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/geometry-wrapper.cpp @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// CLASS HEADER +#include "geometry-wrapper.h" + +// INTERNAL INCLUDES +#include +#include +#include +#include +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +v8::Persistent GeometryWrapper::mGeometryTemplate; + +namespace // un-named name space +{ + +/** + * Contains a list of all functions that can be called + */ +const ApiFunction GeometryFunctionTable[]= +{ + /************************************** + * Geometry API (in order of Geometry.h) + **************************************/ + + { "AddVertexBuffer" , GeometryApi::AddVertexBuffer }, + { "GetNumberOfVertexBuffers" , GeometryApi::GetNumberOfVertexBuffers }, + { "RemoveVertexBuffer" , GeometryApi::RemoveVertexBuffer }, + { "SetIndexBuffer" , GeometryApi::SetIndexBuffer }, + { "SetGeometryType" , GeometryApi::SetGeometryType }, + { "GetGeometryType" , GeometryApi::GetGeometryType }, +}; + +const unsigned int GeometryFunctionTableCount = sizeof(GeometryFunctionTable)/sizeof(GeometryFunctionTable[0]); +} //un-named space + + +GeometryWrapper::GeometryWrapper( const Dali::Geometry& geometry, GarbageCollectorInterface& gc ) +: HandleWrapper( BaseWrappedObject::GEOMETRY , geometry, gc ) +{ + mGeometry = geometry; +} + +v8::Handle GeometryWrapper::WrapGeometry(v8::Isolate* isolate, const Dali::Geometry& geometry ) +{ + v8::EscapableHandleScope handleScope( isolate ); + v8::Local objectTemplate; + + objectTemplate = GetGeometryTemplate( isolate); + + // create an instance of the template + v8::Local localObject = objectTemplate->NewInstance(); + + // create the Geometry wrapper + GeometryWrapper* pointer = new GeometryWrapper( geometry, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() ); + + // assign the JavaScript object to the wrapper. + pointer->SetJavascriptObject( isolate, localObject ); + + return handleScope.Escape( localObject ); +} + +v8::Local GeometryWrapper::GetGeometryTemplate( v8::Isolate* isolate) +{ + v8::EscapableHandleScope handleScope( isolate ); + v8::Local objectTemplate; + + if( mGeometryTemplate.IsEmpty() ) + { + objectTemplate = MakeGeometryTemplate( isolate ); + mGeometryTemplate.Reset( isolate, objectTemplate ); + } + else + { + // get the object template + objectTemplate = v8::Local::New( isolate, mGeometryTemplate ); + } + return handleScope.Escape( objectTemplate ); +} + +v8::Handle GeometryWrapper::MakeGeometryTemplate( v8::Isolate* isolate ) +{ + v8::EscapableHandleScope handleScope( isolate ); + + v8::Local objTemplate = v8::ObjectTemplate::New(); + + // property handle intercepts property getters and setters and signals + HandleWrapper::AddInterceptsToTemplate( isolate, objTemplate ); + + objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT ); + + // add our function properties + ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, GeometryFunctionTable, GeometryFunctionTableCount ); + + return handleScope.Escape( objTemplate ); +} + +void GeometryWrapper::NewGeometry( const v8::FunctionCallbackInfo< v8::Value >& args) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate); + + if(!args.IsConstructCall()) + { + DALI_SCRIPT_EXCEPTION( isolate, "Geometry constructor called without 'new'"); + return; + } + Dali::Geometry geometry = GeometryApi::New( args ); + + if(geometry) + { + v8::Local localObject = WrapGeometry( isolate, geometry ); + args.GetReturnValue().Set( localObject ); + } +} + + +Geometry GeometryWrapper::GetGeometry() +{ + return mGeometry; +} + + +} // namespace V8Plugin + +} // namespace Dali diff --git a/plugins/dali-script-v8/src/rendering/geometry-wrapper.h b/plugins/dali-script-v8/src/rendering/geometry-wrapper.h new file mode 100644 index 0000000..b108e6c --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/geometry-wrapper.h @@ -0,0 +1,107 @@ +#ifndef __DALI_V8PLUGIN_GEOMETRY_WRAPPER_H__ +#define __DALI_V8PLUGIN_GEOMETRY_WRAPPER_H__ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + +// INTERNAL INCLUDES +#include + +namespace Dali +{ + +namespace V8Plugin +{ + + +/** + * A Geometry wrapper. + * Provides access to Geometry specific functionality and V8 memory handling. + */ +class GeometryWrapper : public HandleWrapper +{ + +public: + + /** + * @brief Constructor + * @param[in] propertyBuffer DALi geometry + * @param[in] gc garbage collection interface + */ + GeometryWrapper( const Geometry& geometry, + GarbageCollectorInterface& gc ); + + /** + * @brief Destructor + */ + virtual ~GeometryWrapper() + { + }; + + /** + * @brief Creates a new Geometry wrapped inside a Javascript Object. + * @param[in] args v8 function call arguments interpreted + */ + static void NewGeometry( const v8::FunctionCallbackInfo< v8::Value >& args); + + /** + * Wraps a geometry + */ + static v8::Handle WrapGeometry(v8::Isolate* isolate, const Dali::Geometry& ); + + + // The GeometryAttribute ObjectTemplate, we cache templates so we don't have + // keep generating them everytime we want to create a Geometry + static v8::Persistent mGeometryTemplate; + + /** + * @brief Gets the handle of geometry + * @return the handle of geometry + */ + Geometry GetGeometry(); + + +private: + + // Geometry + Geometry mGeometry; + + /** + * @brief Creates the object template that used to create geometry at runtime + * @param[in] isolate v8 isolated instance + * @return the object template + */ + static v8::Handle MakeGeometryTemplate( v8::Isolate* isolate ); + + /** + * @brief Gets the object template that used to create geometry at runtime + * @param[in] isolate v8 isolated instance + * @return the object template + */ + static v8::Local GetGeometryTemplate( v8::Isolate* isolate ); + +}; + +} // namespace V8Plugin + +} // namespace Dali + +#endif // __DALI_V8PLUGIN_GEOMETRY_WRAPPER_H__ diff --git a/plugins/dali-script-v8/src/rendering/material-api.cpp b/plugins/dali-script-v8/src/rendering/material-api.cpp new file mode 100644 index 0000000..8b2d0f9 --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/material-api.cpp @@ -0,0 +1,654 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +// CLASS HEADER +#include "material-api.h" + +// EXTERNAL INCLUDES +#include + +// INTERNAL INCLUDES +#include +#include +#include +#include +#include +#include +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +/** + * ## Material API + * + * Material is a handle to an object that specifies the visual properties of the renderer. + * + * @class Material + * @extends Handle + */ + +Material MaterialApi::GetMaterial( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::HandleScope handleScope( isolate ); + + v8::Local object = args.This(); + v8::Local field = v8::Local::Cast( object->GetInternalField(0) ); + void* ptr = field->Value(); + + MaterialWrapper* wrapper = static_cast< MaterialWrapper *>(ptr); + return wrapper->GetMaterial(); +} + +Material MaterialApi::GetMaterialFromParams( int paramIndex, + bool& found, + v8::Isolate* isolate, + const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + found = false; + + v8::HandleScope handleScope( isolate ); + BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::MATERIAL, isolate, args ); + if( wrappedObject ) + { + found = true; + MaterialWrapper* wrapper = static_cast< MaterialWrapper *>(wrappedObject); + return wrapper->GetMaterial(); + } + else + { + return Material(); + } +} + +/** + * Create a new material object. + * + * @constructor + * @method Material + * @for Material + * @param {Object} shader The shader used by the material + * @return {Object} Material + */ +Material MaterialApi::New( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + bool found( false ); + Shader shader = ShaderApi::GetShaderFromParams( 0, found, isolate, args ); + if( found ) + { + return Material::New( shader ); + } + else + { + DALI_SCRIPT_EXCEPTION( isolate, "missing shader from param 0" ); + return Material(); + } +} + +/** + * Set the Shader used by this material + * + * @method setShader + * @for Material + * @param {Object} shader Handle to the shader + */ +void MaterialApi::SetShader( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + bool found( false ); + Shader shader = ShaderApi::GetShaderFromParams( 0, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid shader parameter" ); + } + else + { + return material.SetShader( shader ); + } +} + +/** + * Get the Shader used by this material + * + * @method getShader + * @for Material + * @return {Object} Shader + */ +void MaterialApi::GetShader( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + Shader shader = material.GetShader(); + + // Wrap the shader + v8::Local localObject = ShaderWrapper::WrapShader( isolate, shader ); + args.GetReturnValue().Set( localObject ); +} + +/** + * Add a new texture to be used by the material + * + * @method addTexture + * @for Material + * @param {Object} image The image used by this sampler + * @param {String} uniformName The string with the name of the uniform + * @param {Object} sampler The sampler to add to this material + * @return {integer} The index of the texture in the array of textures or -1 if texture can not be added + */ +void MaterialApi::AddTexture( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + bool found( false ); + Image image = V8Utils::GetImageParameter( PARAMETER_0, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing image from param 0" ); + return; + } + + found = false; + std::string uniformName = V8Utils::GetStringParameter( PARAMETER_1, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing uniform name from param 1" ); + return; + } + + found = false; + Sampler sampler = SamplerApi::GetSamplerFromParams( PARAMETER_2, found, isolate, args ); + if( !found ) + { + args.GetReturnValue().Set( v8::Integer::New( isolate, material.AddTexture( image, uniformName ) ) ); + } + else + { + args.GetReturnValue().Set( v8::Integer::New( isolate, material.AddTexture( image, uniformName, sampler ) ) ); + } +} + +/** + * Removes a texture from the material + * + * @method removeTexture + * @for Material + * @param {integer} index The index of the texture in the array of textures + */ +void MaterialApi::RemoveTexture( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + bool found( false ); + int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" ); + } + else + { + material.RemoveTexture( index ); + } +} + +/** + * Sets the image to be used by a given texture + * @method setTextureImage + * @for Material + * @param {integer} index The index of the texture in the array of textures + * @param {Object} image The image used by this sampler + */ +void MaterialApi::SetTextureImage( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + bool found( false ); + int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" ); + return; + } + + found = false; + Image image = V8Utils::GetImageParameter( PARAMETER_1, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing image from param 1" ); + } + else + { + material.SetTextureImage(index, image); + } +} + +/** + * Set the sampler used by a given texture + * @method setTextureSampler + * @for Material + * @param {integer} index The index of the texture in the array of textures + * @param {Object} sampler The new sampler + */ +void MaterialApi::SetTextureSampler( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + bool found( false ); + int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" ); + return; + } + + found = false; + Sampler sampler = SamplerApi::GetSamplerFromParams( PARAMETER_1, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing sampler from param 1" ); + } + else + { + material.SetTextureSampler(index, sampler); + } +} + +/** + * Set the uniform name of a given texture + * @method setTextureUniformName + * @for Material + * @param {integer} index The index of the texture in the array of textures + * @param {string} uniformName The new uniform name + */ +void MaterialApi::SetTextureUniformName( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + bool found( false ); + int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" ); + return; + } + + found = false; + std::string uniformName = V8Utils::GetStringParameter( PARAMETER_1, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid uniform name parameter" ); + } + else + { + material.SetTextureUniformName(index, uniformName); + } +} + +/** + * Establish if a given texture will affect the transparency of the material ( true by default ) + * @method setTextureAffectsTransparency + * @for Material + * @param {integer} index The index of the texture in the array of textures + * @param {string} affectsTransparency True if the texture affects transparency, false otherwise + */ +void MaterialApi::SetTextureAffectsTransparency( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + bool found( false ); + int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" ); + return; + } + + found = false; + bool affectsTransparency = V8Utils::GetBooleanParameter( PARAMETER_1, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid affectsTransparency parameter" ); + } + else + { + material.SetTextureAffectsTransparency(index, affectsTransparency); + } +} + +/** + * Retrive the index of a texture given its uniform name + * @method getTextureIndex + * @for Material + * @param {string} uniformName The uniform name + * @return {integer} The index in the array of textures or -1 if the texture is not found + */ +void MaterialApi::GetTextureIndex( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + bool found( false ); + std::string uniformName = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid uniform name parameter" ); + } + else + { + args.GetReturnValue().Set( v8::Integer::New( isolate, material.GetTextureIndex(uniformName) ) ); + } +} + +/** + * Retrieve the number of textures used by the material + * + * @method getNumberOfTextures + * @for Material + * @return {integer} The number of textures + */ +void MaterialApi::GetNumberOfTextures( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + args.GetReturnValue().Set( v8::Integer::New( isolate, material.GetNumberOfTextures() ) ); +} + +/** + * Set the culling mode for this material + * + * @method setFaceCullingMode + * @for Material + * @param {integer} cullingMode The culling mode for this material + * @example + * // face culling mode is one of the following + * dali.MATERIAL_NONE // None of the faces should be culled + * dali.MATERIAL_CULL_BACK // Cull back face, back face should never be shown + * dali.MATERIAL_CULL_FRONT // Cull front face, back face should never be shown + * dali.MATERIAL_CULL_BACK_AND_FRONT // Cull back and front faces, if the geometry is composed of triangles none of the faces will be shown + * + * material.setFaceCullingMode( dali.MATERIAL_NONE ); + */ +void MaterialApi::SetFaceCullingMode( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + bool found( false ); + int mode = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid cullingMode parameter" ); + } + else + { + material.SetFaceCullingMode( static_cast(mode) ); + } +} + +/** + * Set the blending mode. + * + * If blending is disabled (BLENDING_OFF) fade in and fade out animations do not work. + * + * @for Material + * @method setBlendMode + * @param { integer } mode The blending mode. + * @example + * // blend mode is one of the following + * dali.BLENDING_OFF // Blending is disabled. + * dali.BLENDING_AUTO // Blending is enabled if there is alpha channel. + * dali.BLENDING_ON // Blending is enabled. + * + * material.setBlendMode( dali.BLENDING_AUTO ); + */ +void MaterialApi::SetBlendMode( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + bool found( false ); + int mode = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid blendMode parameter" ); + } + else + { + material.SetBlendMode( static_cast( mode ) ); + } +} + +/** + * Retrieves the blending mode. + * + * @for Material + * @method getBlendMode + * @return { integer } blendMode + * @example returns one of the following: + * + * dali.BLENDING_OFF // Blending is disabled. + * dali.BLENDING_AUTO // Blending is enabled if there is alpha channel. + * dali.BLENDING_ON // Blending is enabled. + */ +void MaterialApi::GetBlendMode( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + args.GetReturnValue().Set( v8::Integer::New( isolate, material.GetBlendMode() ) ); +} + +/** + * Specify the pixel arithmetic used when the actor is blended. + * + * @for Material + * @method setBlendFunc + * @param {integer} srcFactorRgb Source Blending RGB + * @param {integer} destFactorRgb Destination Blending RGB + * @param {integer} srcFactorAlpha Source Blending Alpha + * @param {integer} destFactorAlpha Destinatino Blending Alpha + * @example + * //blending constants + * dali.BLEND_FACTOR_ZERO + * dali.BLEND_FACTOR_ONE + * dali.BLEND_FACTOR_SRC_COLOR + * dali.BLEND_FACTOR_ONE_MINUS_SRC_COLOR + * dali.BLEND_FACTOR_SRC_ALPHA + * dali.BLEND_FACTOR_ONE_MINUS_SRC_ALPHA + * dali.BLEND_FACTOR_DST_ALPHA + * dali.BLEND_FACTOR_ONE_MINUS_DST_ALPHA + * dali.BLEND_FACTOR_DST_COLOR + * dali.BLEND_FACTOR_ONE_MINUS_DST_COLOR + * dali.BLEND_FACTOR_SRC_ALPHA_SATURATE + * dali.BLEND_FACTOR_CONSTANT_COLOR + * dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR + * dali.BLEND_FACTOR_CONSTANT_ALPHA + * dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA + * + * material.setBlendFunc( dali.BLEND_FACTOR_CONSTANT_COLOR, dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR, + * dali.BLEND_FACTOR_CONSTANT_ALPHA, dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA ); + */ +void MaterialApi::SetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + int params[4]; + bool foundAllParams(false); + V8Utils::ReadIntegerArguments( foundAllParams, ¶ms[0], 4, args, 0 ); + if( foundAllParams ) + { + material.SetBlendFunc( static_cast< Dali::BlendingFactor::Type>(params[0]), + static_cast< Dali::BlendingFactor::Type>(params[1]), + static_cast< Dali::BlendingFactor::Type>(params[2]), + static_cast< Dali::BlendingFactor::Type>(params[3]) ); + } + else + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid blendFunc parameter"); + } +} + +/** + * Query the pixel arithmetic used when the actor is blended. + * + * @for Material + * @method getBlendFunc + * @return {Object} Blend properties + * @example Blend properties object has 4 fields + * + * blendProperties.sourceRgb // source rgb enum + * blendProperties.destinationRgb // destination rgb enum + * blendProperties.sourceAlpha source // alpha enum + * blendProperties.destinationAlpha // destination alpha enum + */ +void MaterialApi::GetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + // Pass by reference doesn't work in Javascript + // For now just return a vector 4... + + BlendingFactor::Type srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha; + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + material.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha ); + + v8::Local blendProperties = v8::Object::New( isolate ); + + blendProperties->Set( v8::String::NewFromUtf8( isolate, "sourceRgb" ), v8::Integer::New( isolate, srcFactorRgb) ); + blendProperties->Set( v8::String::NewFromUtf8( isolate, "destinationRgb" ), v8::Integer::New( isolate, destFactorRgb ) ); + blendProperties->Set( v8::String::NewFromUtf8( isolate, "sourceAlpha" ), v8::Integer::New( isolate, srcFactorAlpha ) ); + blendProperties->Set( v8::String::NewFromUtf8( isolate, "destinationAlpha" ), v8::Integer::New( isolate, destFactorAlpha ) ); + + args.GetReturnValue().Set( blendProperties ); +} + +/** + * Specify the equation used when the actor is blended. + * + * @for Material + * @method setBlendEquation + * @param { integer } equationRgb The equation used for combining red, green, and blue components. + * @param { integer } equationAlpha The equation used for combining the alpha component. + * @example + * // blend equation is one of the following + * dali.BLEND_EQUATION_ADD + * dali.BLEND_EQUATION_SUBTRACT + * dali.BLEND_EQUATION_REVERSE_SUBTRACT + * + * material.setBlendEquation( dali.BLEND_EQUATION_ADD, dali.BLEND_EQUATION_REVERSE_SUBTRACT ); + */ +void MaterialApi::SetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + int params[2]; + bool foundAllParams(false); + V8Utils::ReadIntegerArguments( foundAllParams, ¶ms[0], 2, args, 0 ); + if( foundAllParams ) + { + material.SetBlendEquation( static_cast< BlendingEquation::Type>(params[0]), static_cast< BlendingEquation::Type>(params[1]) ); + } + else + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid BlendEquation parameter"); + } +} + +/** + * Query the equation used when the actor is blended. + * + * @for Material + * @method getBlendEquation + * @return {Object} Blend equations + * @example Blend equations object has 2 fields + * + * blendEquations.equationRgb // equation used for combining rgb components + * blendEquations.equationAlpha // equation used for combining alpha components + */ +void MaterialApi::GetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + // Pass by reference doesn't work in Javascript + // For now just return a vector 2... + + BlendingEquation::Type equationRgb, equationAlpha; + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Material material = GetMaterial( isolate, args ); + + material.GetBlendEquation( equationRgb, equationAlpha ); + + v8::Local blendEquations = v8::Object::New( isolate ); + + blendEquations->Set( v8::String::NewFromUtf8( isolate, "equationRgb" ), v8::Integer::New( isolate, equationRgb) ); + blendEquations->Set( v8::String::NewFromUtf8( isolate, "equationAlpha" ), v8::Integer::New( isolate, equationAlpha ) ); + + args.GetReturnValue().Set( blendEquations ); +} + +} // namespace V8Plugin + +} // namespace Dali diff --git a/plugins/dali-script-v8/src/rendering/material-api.h b/plugins/dali-script-v8/src/rendering/material-api.h new file mode 100644 index 0000000..326b22c --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/material-api.h @@ -0,0 +1,84 @@ +#ifndef __DALI_V8PLUGIN_MATERIAL_API_H__ +#define __DALI_V8PLUGIN_MATERIAL_API_H__ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + + +namespace Dali +{ + +namespace V8Plugin +{ + +namespace MaterialApi +{ + + /** + * Helper to get material from args.This() + */ + Material GetMaterial( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * @brief Helper to get material from the JavaScript object held in the given function argument + * @param[in] paramIndex Argument index the object is held in + * @param[in] found Whether material is found in the given function parameter + * @param[in] isolate v8 isolated instance + * @param[in] args v8 function call arguments interpreted + */ + Material GetMaterialFromParams( int paramIndex, + bool& found, + v8::Isolate* isolate, + const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * Constructor + */ + Material New( const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * Material API see material.h for a description + */ + void SetShader( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetShader( const v8::FunctionCallbackInfo< v8::Value >& args ); + void AddTexture( const v8::FunctionCallbackInfo< v8::Value >& args ); + void RemoveTexture( const v8::FunctionCallbackInfo< v8::Value >& args ); + void SetTextureImage( const v8::FunctionCallbackInfo< v8::Value >& args ); + void SetTextureSampler( const v8::FunctionCallbackInfo< v8::Value >& args ); + void SetTextureUniformName( const v8::FunctionCallbackInfo< v8::Value >& args ); + void SetTextureAffectsTransparency( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetTextureIndex( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetNumberOfTextures( const v8::FunctionCallbackInfo< v8::Value >& args ); + void SetFaceCullingMode( const v8::FunctionCallbackInfo< v8::Value >& args ); + void SetBlendMode( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetBlendMode( const v8::FunctionCallbackInfo< v8::Value >& args ); + void SetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args ); + void SetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetBlendEquation( const v8::FunctionCallbackInfo< v8::Value >& args ); + +}; // namespace MaterialApi + +} // namespace V8Plugin + +} // namespace Dali + +#endif // header __DALI_V8PLUGIN_MATERIAL_API_H__ diff --git a/plugins/dali-script-v8/src/rendering/material-wrapper.cpp b/plugins/dali-script-v8/src/rendering/material-wrapper.cpp new file mode 100644 index 0000000..4fc5a77 --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/material-wrapper.cpp @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// CLASS HEADER +#include "material-wrapper.h" + +// INTERNAL INCLUDES +#include +#include +#include +#include +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +v8::Persistent MaterialWrapper::mMaterialTemplate; + +namespace // un-named name space +{ + +/** + * Contains a list of all functions that can be called + */ +const ApiFunction MaterialFunctionTable[]= +{ + /************************************** + * Material API (in order of Material.h) + **************************************/ + + { "SetShader" , MaterialApi::SetShader }, + { "GetShader" , MaterialApi::GetShader }, + { "AddTexture" , MaterialApi::AddTexture }, + { "RemoveTexture" , MaterialApi::RemoveTexture }, + { "SetTextureImage" , MaterialApi::SetTextureImage }, + { "SetTextureSampler" , MaterialApi::SetTextureSampler }, + { "SetTextureUniformName" , MaterialApi::SetTextureUniformName }, + { "SetTextureAffectsTransparency" , MaterialApi::SetTextureAffectsTransparency }, + { "GetTextureIndex" , MaterialApi::GetTextureIndex }, + { "GetNumberOfTextures" , MaterialApi::GetNumberOfTextures }, + { "SetFaceCullingMode" , MaterialApi::SetFaceCullingMode }, + { "SetBlendMode" , MaterialApi::SetBlendMode }, + { "GetBlendMode" , MaterialApi::GetBlendMode }, + { "SetBlendFunc" , MaterialApi::SetBlendFunc }, + { "GetBlendFunc" , MaterialApi::GetBlendFunc }, + { "SetBlendEquation" , MaterialApi::SetBlendEquation }, + { "GetBlendEquation" , MaterialApi::GetBlendEquation }, +}; + +const unsigned int MaterialFunctionTableCount = sizeof(MaterialFunctionTable)/sizeof(MaterialFunctionTable[0]); +} //un-named space + + +MaterialWrapper::MaterialWrapper( const Dali::Material& material, GarbageCollectorInterface& gc ) +: HandleWrapper( BaseWrappedObject::MATERIAL , material, gc ) +{ + mMaterial = material; +} + +v8::Handle MaterialWrapper::WrapMaterial(v8::Isolate* isolate, const Dali::Material& material ) +{ + v8::EscapableHandleScope handleScope( isolate ); + v8::Local objectTemplate; + + objectTemplate = GetMaterialTemplate( isolate); + + // create an instance of the template + v8::Local localObject = objectTemplate->NewInstance(); + + // create the Material wrapper + MaterialWrapper* pointer = new MaterialWrapper( material, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() ); + + // assign the JavaScript object to the wrapper. + pointer->SetJavascriptObject( isolate, localObject ); + + return handleScope.Escape( localObject ); +} + +v8::Local MaterialWrapper::GetMaterialTemplate( v8::Isolate* isolate) +{ + v8::EscapableHandleScope handleScope( isolate ); + v8::Local objectTemplate; + + if( mMaterialTemplate.IsEmpty() ) + { + objectTemplate = MakeMaterialTemplate( isolate ); + mMaterialTemplate.Reset( isolate, objectTemplate ); + } + else + { + // get the object template + objectTemplate = v8::Local::New( isolate, mMaterialTemplate ); + } + return handleScope.Escape( objectTemplate ); +} + +v8::Handle MaterialWrapper::MakeMaterialTemplate( v8::Isolate* isolate ) +{ + v8::EscapableHandleScope handleScope( isolate ); + + v8::Local objTemplate = v8::ObjectTemplate::New(); + + // property handle intercepts property getters and setters and signals + HandleWrapper::AddInterceptsToTemplate( isolate, objTemplate ); + + objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT ); + + // add our function properties + ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, MaterialFunctionTable, MaterialFunctionTableCount ); + + return handleScope.Escape( objTemplate ); +} + +void MaterialWrapper::NewMaterial( const v8::FunctionCallbackInfo< v8::Value >& args) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate); + + if(!args.IsConstructCall()) + { + DALI_SCRIPT_EXCEPTION( isolate, "Material constructor called without 'new'"); + return; + } + Dali::Material material = MaterialApi::New( args ); + + if(material) + { + v8::Local localObject = WrapMaterial( isolate, material ); + args.GetReturnValue().Set( localObject ); + } +} + + +Material MaterialWrapper::GetMaterial() +{ + return mMaterial; +} + + +} // namespace V8Plugin + +} // namespace Dali diff --git a/plugins/dali-script-v8/src/rendering/material-wrapper.h b/plugins/dali-script-v8/src/rendering/material-wrapper.h new file mode 100644 index 0000000..45e11ec --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/material-wrapper.h @@ -0,0 +1,107 @@ +#ifndef __DALI_V8PLUGIN_MATERIAL_WRAPPER_H__ +#define __DALI_V8PLUGIN_MATERIAL_WRAPPER_H__ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + +// INTERNAL INCLUDES +#include + +namespace Dali +{ + +namespace V8Plugin +{ + + +/** + * A Material wrapper. + * Provides access to Material specific functionality and V8 memory handling. + */ +class MaterialWrapper : public HandleWrapper +{ + +public: + + /** + * @brief Constructor + * @param[in] material DALi material + * @param[in] gc garbage collection interface + */ + MaterialWrapper( const Material& material, + GarbageCollectorInterface& gc ); + + /** + * @brief Destructor + */ + virtual ~MaterialWrapper() + { + }; + + /** + * @brief Creates a new Material wrapped inside a Javascript Object. + * @param[in] args v8 function call arguments interpreted + */ + static void NewMaterial( const v8::FunctionCallbackInfo< v8::Value >& args); + + /** + * Wraps a sampler + */ + static v8::Handle WrapMaterial(v8::Isolate* isolate, const Dali::Material& ); + + + // The MaterialAttribute ObjectTemplate, we cache templates so we don't have + // keep generating them everytime we want to create a Material + static v8::Persistent mMaterialTemplate; + + /** + * @brief Gets the handle of material + * @return the handle of material + */ + Material GetMaterial(); + + +private: + + // Material + Material mMaterial; + + /** + * @brief Creates the object template that used to create material at runtime + * @param[in] isolate v8 isolated instance + * @return the object template + */ + static v8::Handle MakeMaterialTemplate( v8::Isolate* isolate ); + + /** + * @brief Gets the object template that used to create material at runtime + * @param[in] isolate v8 isolated instance + * @return the object template + */ + static v8::Local GetMaterialTemplate( v8::Isolate* isolate ); + +}; + +} // namespace V8Plugin + +} // namespace Dali + +#endif // __DALI_V8PLUGIN_MATERIAL_WRAPPER_H__ diff --git a/plugins/dali-script-v8/src/rendering/renderer-api.cpp b/plugins/dali-script-v8/src/rendering/renderer-api.cpp new file mode 100644 index 0000000..fb2c0a8 --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/renderer-api.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +// CLASS HEADER +#include "renderer-api.h" + +// EXTERNAL INCLUDES +#include + +// INTERNAL INCLUDES +#include +#include +#include +#include +#include +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +/** + * ## Renderer API + * + * Renderer is a handle to an object that can be used to provide an image to a material. + * + * @class Renderer + * @extends Handle + */ + +Renderer RendererApi::GetRenderer( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::HandleScope handleScope( isolate ); + + v8::Local object = args.This(); + v8::Local field = v8::Local::Cast( object->GetInternalField(0) ); + void* ptr = field->Value(); + + RendererWrapper* wrapper = static_cast< RendererWrapper *>(ptr); + return wrapper->GetRenderer(); +} + +Renderer RendererApi::GetRendererFromParams( int paramIndex, + bool& found, + v8::Isolate* isolate, + const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + found = false; + + v8::HandleScope handleScope( isolate ); + BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::RENDERER, isolate, args ); + if( wrappedObject ) + { + found = true; + RendererWrapper* wrapper = static_cast< RendererWrapper *>(wrappedObject); + return wrapper->GetRenderer(); + } + else + { + return Renderer(); + } +} + +/** + * Create a new renderer object. + * + * @constructor + * @method Renderer + * @for Renderer + * @param {Object} geometry The geometry to be used by this renderer + * @param {Object} material The material to be used by this renderer + * @return {Object} Renderer + */ +Renderer RendererApi::New( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + bool found( false ); + Geometry geometry = GeometryApi::GetGeometryFromParams( 0, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing geometry from param 0" ); + return Renderer(); + } + + found = false; + Material material = MaterialApi::GetMaterialFromParams( 1, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing material from param 0" ); + return Renderer(); + } + + return Renderer::New(geometry, material); +} + +/** + * Sets the geometry to be used by this renderer + * + * @method setGeometry + * @for Renderer + * @param {Object} geometry The geometry to be used by this renderer + */ +void RendererApi::SetGeometry( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Renderer renderer = GetRenderer( isolate, args ); + + bool found( false ); + Geometry geometry = GeometryApi::GetGeometryFromParams( 0, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing geometry from param 0" ); + } + else + { + renderer.SetGeometry(geometry); + } +} + +/** + * Gets the geometry used by this renderer + * + * @method getGeometry + * @for Renderer + * @return {Object} The geometry used by this renderer + */ +void RendererApi::GetGeometry( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Renderer renderer = GetRenderer( isolate, args ); + Geometry geometry = renderer.GetGeometry(); + + // Wrap the geometry + v8::Local localObject = GeometryWrapper::WrapGeometry( isolate, geometry ); + args.GetReturnValue().Set( localObject ); +} + +/** + * Sets the material to be used by this renderer + * + * @method setMaterial + * @for Renderer + * @param {Object} material The material to be used by this renderer + */ +void RendererApi::SetMaterial( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Renderer renderer = GetRenderer( isolate, args ); + + bool found( false ); + Material material = MaterialApi::GetMaterialFromParams( 0, found, isolate, args ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing material from param 0" ); + } + else + { + renderer.SetMaterial(material); + } +} + +/** + * Gets the material used by this renderer + * + * @method getMaterial + * @for Renderer + * @return {Object} The material used by this renderer + */ +void RendererApi::GetMaterial( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Renderer renderer = GetRenderer( isolate, args ); + Material material = renderer.GetMaterial(); + + // Wrap the material + v8::Local localObject = MaterialWrapper::WrapMaterial( isolate, material ); + args.GetReturnValue().Set( localObject ); +} + +} // namespace V8Plugin + +} // namespace Dali diff --git a/plugins/dali-script-v8/src/rendering/renderer-api.h b/plugins/dali-script-v8/src/rendering/renderer-api.h new file mode 100644 index 0000000..e415e74 --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/renderer-api.h @@ -0,0 +1,71 @@ +#ifndef __DALI_V8PLUGIN_RENDERER_API_H__ +#define __DALI_V8PLUGIN_RENDERER_API_H__ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + + +namespace Dali +{ + +namespace V8Plugin +{ + +namespace RendererApi +{ + + /** + * Helper to get renderer from args.This() + */ + Renderer GetRenderer( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * @brief Helper to get renderer from the JavaScript object held in the given function argument + * @param[in] paramIndex Argument index the object is held in + * @param[in] found Whether renderer is found in the given function parameter + * @param[in] isolate v8 isolated instance + * @param[in] args v8 function call arguments interpreted + */ + Renderer GetRendererFromParams( int paramIndex, + bool& found, + v8::Isolate* isolate, + const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * Constructor + */ + Renderer New( const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * Renderer API see renderer.h for a description + */ + void SetGeometry( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetGeometry( const v8::FunctionCallbackInfo< v8::Value >& args ); + void SetMaterial( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetMaterial( const v8::FunctionCallbackInfo< v8::Value >& args ); + +}; // namespace RendererApi + +} // namespace V8Plugin + +} // namespace Dali + +#endif // header __DALI_V8PLUGIN_RENDERER_API_H__ diff --git a/plugins/dali-script-v8/src/rendering/renderer-wrapper.cpp b/plugins/dali-script-v8/src/rendering/renderer-wrapper.cpp new file mode 100644 index 0000000..ecd6649 --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/renderer-wrapper.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// CLASS HEADER +#include "renderer-wrapper.h" + +// INTERNAL INCLUDES +#include +#include +#include +#include +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +v8::Persistent RendererWrapper::mRendererTemplate; + +namespace // un-named name space +{ + +/** + * Contains a list of all functions that can be called + */ +const ApiFunction RendererFunctionTable[]= +{ + /************************************** + * Renderer API (in order of Renderer.h) + **************************************/ + + { "SetGeometry" , RendererApi::SetGeometry }, + { "GetGeometry" , RendererApi::GetGeometry }, + { "SetMaterial" , RendererApi::SetMaterial }, + { "GetMaterial" , RendererApi::GetMaterial }, +}; + +const unsigned int RendererFunctionTableCount = sizeof(RendererFunctionTable)/sizeof(RendererFunctionTable[0]); +} //un-named space + + +RendererWrapper::RendererWrapper( const Dali::Renderer& renderer, GarbageCollectorInterface& gc ) +: HandleWrapper( BaseWrappedObject::RENDERER , renderer, gc ) +{ + mRenderer = renderer; +} + +v8::Handle RendererWrapper::WrapRenderer(v8::Isolate* isolate, const Dali::Renderer& renderer ) +{ + v8::EscapableHandleScope handleScope( isolate ); + v8::Local objectTemplate; + + objectTemplate = GetRendererTemplate( isolate); + + // create an instance of the template + v8::Local localObject = objectTemplate->NewInstance(); + + // create the Renderer wrapper + RendererWrapper* pointer = new RendererWrapper( renderer, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() ); + + // assign the JavaScript object to the wrapper. + pointer->SetJavascriptObject( isolate, localObject ); + + return handleScope.Escape( localObject ); +} + +v8::Local RendererWrapper::GetRendererTemplate( v8::Isolate* isolate) +{ + v8::EscapableHandleScope handleScope( isolate ); + v8::Local objectTemplate; + + if( mRendererTemplate.IsEmpty() ) + { + objectTemplate = MakeRendererTemplate( isolate ); + mRendererTemplate.Reset( isolate, objectTemplate ); + } + else + { + // get the object template + objectTemplate = v8::Local::New( isolate, mRendererTemplate ); + } + return handleScope.Escape( objectTemplate ); +} + +v8::Handle RendererWrapper::MakeRendererTemplate( v8::Isolate* isolate ) +{ + v8::EscapableHandleScope handleScope( isolate ); + + v8::Local objTemplate = v8::ObjectTemplate::New(); + + // property handle intercepts property getters and setters and signals + HandleWrapper::AddInterceptsToTemplate( isolate, objTemplate ); + + objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT ); + + // add our function properties + ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, RendererFunctionTable, RendererFunctionTableCount ); + + return handleScope.Escape( objTemplate ); +} + +void RendererWrapper::NewRenderer( const v8::FunctionCallbackInfo< v8::Value >& args) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate); + + if(!args.IsConstructCall()) + { + DALI_SCRIPT_EXCEPTION( isolate, "Renderer constructor called without 'new'"); + return; + } + Dali::Renderer renderer = RendererApi::New( args ); + + if(renderer) + { + v8::Local localObject = WrapRenderer( isolate, renderer ); + args.GetReturnValue().Set( localObject ); + } +} + + +Renderer RendererWrapper::GetRenderer() +{ + return mRenderer; +} + + +} // namespace V8Plugin + +} // namespace Dali diff --git a/plugins/dali-script-v8/src/rendering/renderer-wrapper.h b/plugins/dali-script-v8/src/rendering/renderer-wrapper.h new file mode 100644 index 0000000..d03669a --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/renderer-wrapper.h @@ -0,0 +1,107 @@ +#ifndef __DALI_V8PLUGIN_RENDERER_WRAPPER_H__ +#define __DALI_V8PLUGIN_RENDERER_WRAPPER_H__ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + +// INTERNAL INCLUDES +#include + +namespace Dali +{ + +namespace V8Plugin +{ + + +/** + * A Renderer wrapper. + * Provides access to Renderer specific functionality and V8 memory handling. + */ +class RendererWrapper : public HandleWrapper +{ + +public: + + /** + * @brief Constructor + * @param[in] renderer DALi renderer + * @param[in] gc garbage collection interface + */ + RendererWrapper( const Renderer& renderer, + GarbageCollectorInterface& gc ); + + /** + * @brief Destructor + */ + virtual ~RendererWrapper() + { + }; + + /** + * @brief Creates a new Renderer wrapped inside a Javascript Object. + * @param[in] args v8 function call arguments interpreted + */ + static void NewRenderer( const v8::FunctionCallbackInfo< v8::Value >& args); + + /** + * Wraps a renderer + */ + static v8::Handle WrapRenderer(v8::Isolate* isolate, const Dali::Renderer& ); + + + // The RendererAttribute ObjectTemplate, we cache templates so we don't have + // keep generating them everytime we want to create a Renderer + static v8::Persistent mRendererTemplate; + + /** + * @brief Gets the handle of renderer + * @return the handle of renderer + */ + Renderer GetRenderer(); + + +private: + + // Renderer + Renderer mRenderer; + + /** + * @brief Creates the object template that used to create renderer at runtime + * @param[in] isolate v8 isolated instance + * @return the object template + */ + static v8::Handle MakeRendererTemplate( v8::Isolate* isolate ); + + /** + * @brief Gets the object template that used to create renderer at runtime + * @param[in] isolate v8 isolated instance + * @return the object template + */ + static v8::Local GetRendererTemplate( v8::Isolate* isolate ); + +}; + +} // namespace V8Plugin + +} // namespace Dali + +#endif // __DALI_V8PLUGIN_RENDERER_WRAPPER_H__ diff --git a/plugins/dali-script-v8/src/rendering/sampler-api.cpp b/plugins/dali-script-v8/src/rendering/sampler-api.cpp new file mode 100644 index 0000000..2f2edce --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/sampler-api.cpp @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// CLASS HEADER +#include "sampler-api.h" + +// EXTERNAL INCLUDES +#include + +// INTERNAL INCLUDES +#include +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +/** + * ## Sampler API + * + * Sampler is a handle to an object that can be used to provide the sampling parameters to sample textures + * + * @class Sampler + */ + +Sampler SamplerApi::GetSampler( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::HandleScope handleScope( isolate ); + + v8::Local object = args.This(); + v8::Local field = v8::Local::Cast( object->GetInternalField(0) ); + void* ptr = field->Value(); + + SamplerWrapper* wrapper = static_cast< SamplerWrapper *>(ptr); + return wrapper->GetSampler(); +} + +Sampler SamplerApi::GetSamplerFromParams( int paramIndex, + bool& found, + v8::Isolate* isolate, + const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + found = false; + + v8::HandleScope handleScope( isolate ); + BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::SAMPLER, isolate, args ); + if( wrappedObject ) + { + found = true; + SamplerWrapper* wrapper = static_cast< SamplerWrapper *>(wrappedObject); + return wrapper->GetSampler(); + } + else + { + return Sampler(); + } +} + +/** + * Create a new sampler object. + * + * @constructor + * @method Sampler + * @for Sampler + * @return {Object} Sampler + */ +Sampler SamplerApi::New( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + return Sampler::New(); +} + +/** + * Set the filter modes for this sampler + * @method setFilterMode + * @for Sampler + * @param {integer} minFilter The minification filter that will be used + * @param {integer} magFilter The magnification filter that will be used + * @example + * // filter mode is one of the following + * dali.FILTER_MODE_NONE // Use GL system defaults (minification NEAREST_MIPMAP_LINEAR, magnification LINEAR) + * dali.FILTER_MODE_DEFAULT // Use dali defaults (minification LINEAR, magnification LINEAR) + * dali.FILTER_MODE_NEAREST // Filter nearest + * dali.FILTER_MODE_LINEAR // Filter linear + * + * sampler.setFilterMode( dali.FILTER_MODE_DEFAULT, dali.FILTER_MODE_LINEAR ); + */ +void SamplerApi::SetFilterMode( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Sampler sampler = GetSampler( isolate, args ); + + bool found( false ); + int minFilter = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing minification filter from param 0" ); + return; + } + + found = false; + int magFilter = V8Utils::GetIntegerParameter( PARAMETER_1, found, isolate, args, 0 /* default */); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing magnification filter from param 1" ); + } + else + { + sampler.SetFilterMode(static_cast(minFilter), static_cast(magFilter)); + } +} + +/** + * Set the wrap modes for this sampler + * @method setWrapMode + * @for Sampler + * @param {integer} uWrap Wrap mode for u coordinates + * @param {integer} vWrap Wrap mode for v coordinates + * @example + * // wrap mode is one of the following + * dali.WRAP_MODE_DEFAULT + * dali.WRAP_MODE_CLAMP_TO_EDGE + * dali.WRAP_MODE_REPEAT + * dali.WRAP_MODE_MIRRORED_REPEAT + * + * sampler.setWrapMode( dali.WRAP_MODE_CLAMP_TO_EDGE, dali.WRAP_MODE_REPEAT ); + */ +void SamplerApi::SetWrapMode( const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + + Sampler sampler = GetSampler( isolate, args ); + + bool found( false ); + int uWrap = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 /* default */); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing wrap mode for u coordinates from param 0" ); + return; + } + + found = false; + int vWrap = V8Utils::GetIntegerParameter( PARAMETER_1, found, isolate, args, 0 /* default */); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing wrap mode for v coordinates from param 1" ); + } + else + { + sampler.SetWrapMode(static_cast(uWrap), static_cast(vWrap)); + } +} + +} // namespace V8Plugin + +} // namespace Dali diff --git a/plugins/dali-script-v8/src/rendering/sampler-api.h b/plugins/dali-script-v8/src/rendering/sampler-api.h new file mode 100644 index 0000000..1eac960 --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/sampler-api.h @@ -0,0 +1,67 @@ +#ifndef __DALI_V8PLUGIN_SAMPLER_API_H__ +#define __DALI_V8PLUGIN_SAMPLER_API_H__ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +namespace SamplerApi +{ + /** + * Helper to get sampler from args.This() + */ + Sampler GetSampler( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * @brief Helper to get sampler from the JavaScript object held in the given function argument + * @param[in] paramIndex Argument index the object is held in + * @param[in] found Whether sampler is found in the given function parameter + * @param[in] isolate v8 isolated instance + * @param[in] args v8 function call arguments interpreted + */ + Sampler GetSamplerFromParams( int paramIndex, + bool& found, + v8::Isolate* isolate, + const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * Constructor + */ + Sampler New( const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * Sampler API see sampler.h for a description + */ + void SetFilterMode( const v8::FunctionCallbackInfo< v8::Value >& args ); + void SetWrapMode( const v8::FunctionCallbackInfo< v8::Value >& args ); + +}; // namespace SamplerApi + +} // namespace V8Plugin + +} // namespace Dali + +#endif // header __DALI_V8PLUGIN_SAMPLER_API_H__ diff --git a/plugins/dali-script-v8/src/rendering/sampler-wrapper.cpp b/plugins/dali-script-v8/src/rendering/sampler-wrapper.cpp new file mode 100644 index 0000000..17b9396 --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/sampler-wrapper.cpp @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// CLASS HEADER +#include "sampler-wrapper.h" + +// INTERNAL INCLUDES +#include +#include +#include +#include +#include + +namespace Dali +{ +namespace V8Plugin +{ + +v8::Persistent SamplerWrapper::mSamplerTemplate; + +namespace +{ + +/** + * Contains a list of all functions that can be called + */ +const ApiFunction SamplerFunctionTable[]= +{ + /************************************** + * Sampler API (in order of sampler.h) + **************************************/ + + { "SetFilterMode", SamplerApi::SetFilterMode }, + { "SetWrapMode", SamplerApi::SetWrapMode }, +}; + +const unsigned int SamplerFunctionTableCount = sizeof(SamplerFunctionTable)/sizeof(SamplerFunctionTable[0]); +} //un-named space + + +SamplerWrapper::SamplerWrapper( const Sampler& sampler, GarbageCollectorInterface& gc ) +: BaseWrappedObject( BaseWrappedObject::SAMPLER , gc ) +{ + mSampler = sampler; +} + +v8::Handle SamplerWrapper::WrapSampler(v8::Isolate* isolate, const Sampler& sampler ) +{ + v8::EscapableHandleScope handleScope( isolate ); + v8::Local objectTemplate; + + objectTemplate = GetSamplerTemplate( isolate); + + // create an instance of the template + v8::Local localObject = objectTemplate->NewInstance(); + + // create the Sampler wrapper + SamplerWrapper* pointer = new SamplerWrapper( sampler, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() ); + + // assign the JavaScript object to the wrapper. + pointer->SetJavascriptObject( isolate, localObject ); + + return handleScope.Escape( localObject ); +} + +v8::Local SamplerWrapper::GetSamplerTemplate( v8::Isolate* isolate) +{ + v8::EscapableHandleScope handleScope( isolate ); + v8::Local objectTemplate; + + if( mSamplerTemplate.IsEmpty() ) + { + objectTemplate = MakeSamplerTemplate( isolate ); + mSamplerTemplate.Reset( isolate, objectTemplate ); + } + else + { + // get the object template + objectTemplate = v8::Local::New( isolate, mSamplerTemplate ); + } + return handleScope.Escape( objectTemplate ); +} + +v8::Handle SamplerWrapper::MakeSamplerTemplate( v8::Isolate* isolate ) +{ + v8::EscapableHandleScope handleScope( isolate ); + + v8::Local objTemplate = v8::ObjectTemplate::New(); + + objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT ); + + // add our function properties + ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, SamplerFunctionTable, SamplerFunctionTableCount ); + + return handleScope.Escape( objTemplate ); +} + +void SamplerWrapper::NewSampler( const v8::FunctionCallbackInfo< v8::Value >& args) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate); + + if(!args.IsConstructCall()) + { + DALI_SCRIPT_EXCEPTION( isolate, "Sampler constructor called without 'new'"); + return; + } + Dali::Sampler sampler = SamplerApi::New( args ); + + if(sampler) + { + v8::Local localObject = WrapSampler( isolate, sampler ); + args.GetReturnValue().Set( localObject ); + } +} + +Sampler SamplerWrapper::GetSampler() +{ + return mSampler; +} + + +} // namespace V8Plugin + +} // namespace Dali diff --git a/plugins/dali-script-v8/src/rendering/sampler-wrapper.h b/plugins/dali-script-v8/src/rendering/sampler-wrapper.h new file mode 100644 index 0000000..5f2df3c --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/sampler-wrapper.h @@ -0,0 +1,103 @@ +#ifndef __DALI_V8PLUGIN_SAMPLER_WRAPPER_H__ +#define __DALI_V8PLUGIN_SAMPLER_WRAPPER_H__ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + +// INTERNAL INCLUDES +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +/** + * A Sampler wrapper. + * Provides access to Sampler specific functionality and V8 memory handling. + */ +class SamplerWrapper : public BaseWrappedObject +{ + +public: + + /** + * @brief Constructor + * @param[in] sampler DALi sampler + * @param[in] gc garbage collection interface + */ + SamplerWrapper( const Sampler& sampler, + GarbageCollectorInterface& gc ); + + /** + * @brief Destructor + */ + virtual ~SamplerWrapper() + { + }; + + /** + * @brief Creates a new Sampler wrapped inside a Javascript Object. + * @param[in] args v8 function call arguments interpreted + */ + static void NewSampler( const v8::FunctionCallbackInfo< v8::Value >& args); + + /** + * Wraps a sampler + */ + static v8::Handle WrapSampler(v8::Isolate* isolate, const Sampler& ); + + // The SamplerAttribute ObjectTemplate, we cache templates so we don't have + // keep generating them everytime we want to create a Sampler + static v8::Persistent mSamplerTemplate; + + /** + * @brief Gets the handle of sampler + * @return the handle of sampler + */ + Sampler GetSampler(); + +private: + + // Sampler + Sampler mSampler; + + /** + * @brief Creates the object template that used to create sampler at runtime + * @param[in] isolate v8 isolated instance + * @return the object template + */ + static v8::Handle MakeSamplerTemplate( v8::Isolate* isolate ); + + /** + * @brief Gets the object template that used to create sampler at runtime + * @param[in] isolate v8 isolated instance + * @return the object template + */ + static v8::Local GetSamplerTemplate( v8::Isolate* isolate ); +}; + +} // namespace V8Plugin + +} // namespace Dali + +#endif // __DALI_V8PLUGIN_SAMPLER_WRAPPER_H__ diff --git a/plugins/dali-script-v8/src/rendering/shader-api.cpp b/plugins/dali-script-v8/src/rendering/shader-api.cpp new file mode 100644 index 0000000..6080bd2 --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/shader-api.cpp @@ -0,0 +1,236 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + + +// CLASS HEADER +#include "shader-api.h" + +// EXTERNAL INCLUDES +#include + +// INTERNAL INCLUDES +#include +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +/** + * ## Shader API + * + * Shaders allows custom vertex and color transformations in the GPU + * + * ### Simple example + * + *``` + * // this will match the default shaders for image + * var vertexShader = " void main() \ + * { \ + * gl_Position = uProjection * uModelView * vec4(aPosition, 1.0); \ + * vTexCoord = aTexCoord; \ + * }; " + * var fragShader = " void main() \ + * { \ + * gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\ + * }; " + * var shaderOptions = { + * vertexShader: vertexShader, + * fragmentShader: fragShader + * }; + * + * var shader = new dali.Shader(shaderOptions); + *``` + * @class Shader + * @extends Handle + */ + +namespace // unnamed namespace +{ + +typedef std::vector< std::string > HintsArray; + +struct ShaderHintPair +{ + const char* name; + Shader::ShaderHints hint; +}; + +const ShaderHintPair ShaderHintTable[]= +{ + {"requiresSelfDepthTest", Shader::HINT_REQUIRES_SELF_DEPTH_TEST }, + {"outputIsTransparent", Shader::HINT_OUTPUT_IS_TRANSPARENT }, + {"outputIsOpaque", Shader::HINT_OUTPUT_IS_OPAQUE }, + {"modifiesGeometry", Shader::HINT_MODIFIES_GEOMETRY }, +}; + +const unsigned int ShaderHintTableCount = sizeof(ShaderHintTable)/sizeof(ShaderHintTable[0]); + +struct ShaderParameters +{ + ShaderParameters() + : mHints( Shader::HINT_NONE ) + { + } + + Shader::ShaderHints GetShaderHint( const std::string& hint ) + { + for( unsigned int i = 0 ; i < ShaderHintTableCount; ++i ) + { + if( hint == ShaderHintTable[i].name ) + { + return ShaderHintTable[i].hint; + } + } + return Shader::HINT_NONE; + } + + void ProcessHintsArray( const HintsArray& hintsArray ) + { + for( HintsArray::const_iterator iter = hintsArray.begin(); iter != hintsArray.end() ; ++iter ) + { + mHints= static_cast( mHints | GetShaderHint( *iter ) ); + } + } + + Shader NewShader() + { + return Shader::New( mVertex, + mFragment, + mHints); + } + + std::string mVertex; + std::string mFragment; + Shader::ShaderHints mHints; +}; + +Shader GetShader( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::HandleScope handleScope( isolate ); + + v8::Local object = args.This(); + v8::Local field = v8::Local::Cast( object->GetInternalField(0) ); + void* ptr = field->Value(); + + ShaderWrapper* wrapper = static_cast< ShaderWrapper *>(ptr); + return wrapper->GetShader(); +} + +} // unnamed space + +/** + * Create a new Shader + * @constructor + * @for Shader + * @method Shader + * @param {Object} shaderOptions + * @param {String} [shaderOptions.vertexShader] VertexShader code for the shader. If not defined, the default version will be used + * @param {String} [shaderOptions.fragmentShader] FragmentShader code for the shader. If not defined, the default version will be used + * @param {Array} [shaderOptions.shaderHints] Hints for rendering/subdividing geometry, e.g. [ "requiresSelfDepthTest", "outputIsTransparent", "outputIsOpaque","modifiesGeometry" ] + * @return {Object} Shader + * @example + + // this will match the default shaders for image + var vertexShader = " void main() \ + { \ + gl_Position = uProjection * uModelView * vec4(aPosition, 1.0); \ + vTexCoord = aTexCoord; \ + }; " + + var fragShader = " void main() \ + { \ + gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\ + }; " + + var shaderOptions = { + vertexShader: vertexShader, + fragmentShader: fragShader + }; + + var shader = new dali.Shader(shaderOptions); + * + */ +Shader ShaderApi::New( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + v8::HandleScope handleScope( isolate ); + + ShaderParameters shaderParams; + + if( args[0]->IsObject() ) + { + v8::Local obj = args[0]->ToObject(); + + v8::Local vertexValue = obj->Get(v8::String::NewFromUtf8( isolate, "vertexShader")); + if( vertexValue->IsString() ) + { + shaderParams.mVertex = V8Utils::v8StringToStdString( vertexValue ); + } + + v8::Local fragmentValue = obj->Get(v8::String::NewFromUtf8( isolate, "fragmentShader")); + if( fragmentValue->IsString() ) + { + shaderParams.mFragment = V8Utils::v8StringToStdString( fragmentValue ); + } + // Get any hints + v8::Local hints = obj->Get(v8::String::NewFromUtf8( isolate, "shaderHints")); + if( hints->IsArray() ) + { + HintsArray hintsArray; + v8::Local array = v8::Local::Cast( hints ); + for( uint32_t i=0; i < array->Length(); ++i) + { + v8::Handle entry = array->Get( v8::Integer::New( isolate, i) ); + if( entry->IsString() ) + { + std::string entryString = V8Utils::v8StringToStdString( entry ); + hintsArray.push_back( entryString ); + } + } + shaderParams.ProcessHintsArray( hintsArray ); + } + } + + return shaderParams.NewShader(); +} + +Shader ShaderApi::GetShaderFromParams( int paramIndex, + bool& found, + v8::Isolate* isolate, + const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + found = false; + + v8::HandleScope handleScope( isolate ); + BaseWrappedObject* wrappedObject = V8Utils::GetWrappedDaliObjectParameter( paramIndex, BaseWrappedObject::SHADER, isolate, args ); + if( wrappedObject ) + { + found = true; + ShaderWrapper* wrapper = static_cast< ShaderWrapper *>(wrappedObject); + return wrapper->GetShader(); + } + else + { + return Shader(); + } +} + +} // namespace V8Plugin + +} // namespace Dali diff --git a/plugins/dali-script-v8/src/rendering/shader-api.h b/plugins/dali-script-v8/src/rendering/shader-api.h new file mode 100644 index 0000000..1c9e1a2 --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/shader-api.h @@ -0,0 +1,58 @@ +#ifndef __DALI_V8PLUGIN_SHADER_API_H__ +#define __DALI_V8PLUGIN_SHADER_API_H__ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + + +namespace Dali +{ + +namespace V8Plugin +{ + +namespace ShaderApi +{ + + /** + * Constructor + */ + Shader New( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ); + + /** + * @brief Helper to get shader from the JavaScript object held in the given function argument + * @param[in] paramIndex Argument index the object is held in + * @param[in] found Whether shader is found in the given function parameter + * @param[in] isolate v8 isolated instance + * @param[in] args v8 function call arguments interpreted + */ + Shader GetShaderFromParams( int paramIndex, + bool& found, + v8::Isolate* isolate, + const v8::FunctionCallbackInfo< v8::Value >& args ); + +}; // namespace ShaderApi + +} // namespace V8Plugin + +} // namespace Dali + +#endif // header __DALI_V8PLUGIN_SHADER_API_H__ diff --git a/plugins/dali-script-v8/src/rendering/shader-wrapper.cpp b/plugins/dali-script-v8/src/rendering/shader-wrapper.cpp new file mode 100644 index 0000000..f3aff6c --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/shader-wrapper.cpp @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// CLASS HEADER +#include "shader-wrapper.h" + +// INTERNAL INCLUDES +#include +#include +#include +#include +#include + +namespace Dali +{ + +namespace V8Plugin +{ + +v8::Persistent ShaderWrapper::mShaderTemplate; + +ShaderWrapper::ShaderWrapper( const Dali::Shader& shader, GarbageCollectorInterface& gc ) +: HandleWrapper( BaseWrappedObject::SHADER , shader, gc ) +{ + mShader = shader; +} + +v8::Handle ShaderWrapper::WrapShader(v8::Isolate* isolate, const Dali::Shader& shader ) +{ + v8::EscapableHandleScope handleScope( isolate ); + v8::Local objectTemplate; + + objectTemplate = GetShaderTemplate( isolate); + + // create an instance of the template + v8::Local localObject = objectTemplate->NewInstance(); + + // create the Shader wrapper + ShaderWrapper* pointer = new ShaderWrapper( shader, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() ); + + // assign the JavaScript object to the wrapper. + pointer->SetJavascriptObject( isolate, localObject ); + + return handleScope.Escape( localObject ); +} + +v8::Local ShaderWrapper::GetShaderTemplate( v8::Isolate* isolate) +{ + v8::EscapableHandleScope handleScope( isolate ); + v8::Local objectTemplate; + + if( mShaderTemplate.IsEmpty() ) + { + objectTemplate = MakeShaderTemplate( isolate ); + mShaderTemplate.Reset( isolate, objectTemplate ); + } + else + { + // get the object template + objectTemplate = v8::Local::New( isolate, mShaderTemplate ); + } + return handleScope.Escape( objectTemplate ); +} + +v8::Handle ShaderWrapper::MakeShaderTemplate( v8::Isolate* isolate ) +{ + v8::EscapableHandleScope handleScope( isolate ); + + v8::Local objTemplate = v8::ObjectTemplate::New(); + + // property handle intercepts property getters and setters and signals + HandleWrapper::AddInterceptsToTemplate( isolate, objTemplate ); + + objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT ); + + return handleScope.Escape( objTemplate ); +} + +void ShaderWrapper::NewShader( const v8::FunctionCallbackInfo< v8::Value >& args) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate); + + if(!args.IsConstructCall()) + { + DALI_SCRIPT_EXCEPTION( isolate, "Shader constructor called without 'new'"); + return; + } + Dali::Shader shader = ShaderApi::New( isolate, args ); + + if(shader) + { + v8::Local localObject = WrapShader( isolate, shader ); + args.GetReturnValue().Set( localObject ); + } +} + + +Shader ShaderWrapper::GetShader() +{ + return mShader; +} + + +} // namespace V8Plugin + +} // namespace Dali diff --git a/plugins/dali-script-v8/src/rendering/shader-wrapper.h b/plugins/dali-script-v8/src/rendering/shader-wrapper.h new file mode 100644 index 0000000..b4cb2a0 --- /dev/null +++ b/plugins/dali-script-v8/src/rendering/shader-wrapper.h @@ -0,0 +1,107 @@ +#ifndef __DALI_V8PLUGIN_SHADER_WRAPPER_H__ +#define __DALI_V8PLUGIN_SHADER_WRAPPER_H__ + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +// EXTERNAL INCLUDES +#include +#include + +// INTERNAL INCLUDES +#include + +namespace Dali +{ + +namespace V8Plugin +{ + + +/** + * A Shader wrapper. + * Provides access to Shader specific functionality and V8 memory handling. + */ +class ShaderWrapper : public HandleWrapper +{ + +public: + + /** + * @brief Constructor + * @param[in] shader DALi shader + * @param[in] gc garbage collection interface + */ + ShaderWrapper( const Shader& shader, + GarbageCollectorInterface& gc ); + + /** + * @brief Destructor + */ + virtual ~ShaderWrapper() + { + }; + + /** + * @brief Creates a new Shader wrapped inside a Javascript Object. + * @param[in] args v8 function call arguments interpreted + */ + static void NewShader( const v8::FunctionCallbackInfo< v8::Value >& args); + + /** + * Wraps a shader + */ + static v8::Handle WrapShader(v8::Isolate* isolate, const Dali::Shader& ); + + + // The ShaderAttribute ObjectTemplate, we cache templates so we don't have + // keep generating them everytime we want to create a Shader + static v8::Persistent mShaderTemplate; + + /** + * @brief Gets the handle of shader + * @return the handle of shader + */ + Shader GetShader(); + + +private: + + // Shader + Shader mShader; + + /** + * @brief Creates the object template that used to create shader at runtime + * @param[in] isolate v8 isolated instance + * @return the object template + */ + static v8::Handle MakeShaderTemplate( v8::Isolate* isolate ); + + /** + * @brief Gets the object template that used to create shader at runtime + * @param[in] isolate v8 isolated instance + * @return the object template + */ + static v8::Local GetShaderTemplate( v8::Isolate* isolate ); + +}; + +} // namespace V8Plugin + +} // namespace Dali + +#endif // __DALI_V8PLUGIN_SHADER_WRAPPER_H__ diff --git a/plugins/dali-script-v8/src/shared/base-wrapped-object.h b/plugins/dali-script-v8/src/shared/base-wrapped-object.h index b9f2cb8..3a0e9d9 100644 --- a/plugins/dali-script-v8/src/shared/base-wrapped-object.h +++ b/plugins/dali-script-v8/src/shared/base-wrapped-object.h @@ -83,6 +83,12 @@ public: RENDER_TASK, RENDER_TASK_LIST, TIMER, + SHADER, + SAMPLER, + MATERIAL, + GEOMETRY, + RENDERER, + PROPERTY_BUFFER, TAP_GESTURE, SHADER_EFFECT, PAN_GESTURE, diff --git a/plugins/dali-script-v8/src/toolkit/builder/builder-api.cpp b/plugins/dali-script-v8/src/toolkit/builder/builder-api.cpp index 3c499a4..55e6ae7 100644 --- a/plugins/dali-script-v8/src/toolkit/builder/builder-api.cpp +++ b/plugins/dali-script-v8/src/toolkit/builder/builder-api.cpp @@ -33,6 +33,7 @@ #include #include #include +#include namespace Dali @@ -292,12 +293,16 @@ void BuilderApi::Create( const v8::FunctionCallbackInfo< v8::Value >& args ) v8::Local image = ImageWrapper::WrapImage(isolate, Image::DownCast(handle) ); args.GetReturnValue().Set( image ); } + if( typeName == "ShaderEffect") + { + v8::Local shaderEffect = ShaderEffectWrapper::WrapShaderEffect(isolate, ShaderEffect::DownCast( handle )); + args.GetReturnValue().Set( shaderEffect ); + } if( typeName == "Shader") { - v8::Local shader = ShaderEffectWrapper::WrapShaderEffect(isolate, ShaderEffect::DownCast( handle )); + v8::Local shader = ShaderWrapper::WrapShader(isolate, Shader::DownCast( handle )); args.GetReturnValue().Set( shader ); } - } void BuilderApi::ApplyStyle( const v8::FunctionCallbackInfo< v8::Value >& args ) { diff --git a/plugins/dali-script-v8/src/utils/v8-utils.cpp b/plugins/dali-script-v8/src/utils/v8-utils.cpp index 3466198..f843d1d 100644 --- a/plugins/dali-script-v8/src/utils/v8-utils.cpp +++ b/plugins/dali-script-v8/src/utils/v8-utils.cpp @@ -390,7 +390,7 @@ Property::Value GetPropertyValueFromObject( bool& found, v8::Isolate* isolate, c { v8::HandleScope handleScope( isolate); - Property::Value daliPropertyValue;// creates a property with Property::INVALID + Property::Value daliPropertyValue;// creates a property with Property::NONE found = false; @@ -445,11 +445,33 @@ Property::Map GetPropertyMapFromObject( v8::Isolate* isolate, const v8::Local value = object->Get( key ); - std::string valueString = V8Utils::v8StringToStdString( value ); - - propertyMap[ keyString ] = valueString.c_str(); + if( value->IsBoolean() ) + { + v8::Local v = value->ToBoolean(); + propertyMap[ keyString ] = v->Value(); + } + else if( value->IsNumber() ) + { + v8::Local v = value->ToNumber(); + propertyMap[ keyString ] = static_cast(v->Value()); + } + else if( value->IsInt32() || value->IsUint32() ) + { + v8::Local v = value->ToInt32(); + propertyMap[ keyString ] = static_cast(v->Value()); + } + else if( value->IsString() ) + { + std::string valueString = V8Utils::v8StringToStdString( value ); + propertyMap[ keyString ] = valueString.c_str(); + } + else if( value->IsArray() ) + { + propertyMap[ keyString ] = PropertyValueWrapper::VectorOrMatrixFromV8Array( isolate, value); + } } + return propertyMap; } @@ -480,6 +502,7 @@ int GetIntegerParameter( unsigned int index, bool& found, v8::Isolate* isolate, found = true; return args[ index ]->Int32Value(); } + else { return defaultValue; } @@ -498,6 +521,7 @@ float GetFloatParameter( unsigned int index, bool& found, v8::Isolate* isolate, found = true; return args[ index ]->NumberValue(); } + else { return defaultValue; } @@ -545,6 +569,24 @@ bool GetBooleanParameter( unsigned int index, bool& found, v8::Isolate* isolate, } } +void* GetArrayBufferViewParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ) +{ + found = false; + unsigned int length = args.Length(); + if( index < length && args[index]->IsArrayBufferView() ) + { + found = true; + v8::ArrayBufferView* bufferView = v8::ArrayBufferView::Cast(*(args[index])); + v8::Handle buffer = bufferView->Buffer(); + v8::ArrayBuffer::Contents contents = buffer->Externalize(); + return contents.Data(); + } + else + { + return NULL; + } +} + Handle GetHandleParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ) { v8::HandleScope handleScope( isolate); @@ -765,6 +807,7 @@ Image GetImageParameter( unsigned int index, bool& found, v8::Isolate* isolate, } } + RenderTask GetRenderTaskParameter( unsigned int paramIndex, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ) { found = false; @@ -781,7 +824,6 @@ RenderTask GetRenderTaskParameter( unsigned int paramIndex, bool& found, v8::Iso } } - BaseWrappedObject* GetWrappedDaliObjectParameter( unsigned int index, BaseWrappedObject::Type type, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ) { v8::HandleScope handleScope( isolate); diff --git a/plugins/dali-script-v8/src/utils/v8-utils.h b/plugins/dali-script-v8/src/utils/v8-utils.h index 534c547..7cd6891 100644 --- a/plugins/dali-script-v8/src/utils/v8-utils.h +++ b/plugins/dali-script-v8/src/utils/v8-utils.h @@ -195,7 +195,7 @@ Property::Value GetPropertyValueFromObject( bool& found, v8::Isolate* isolate, c /** * Given a JavaScript object with * @param [in] object JavaScrript object - * @return DALi ProperyMap from the JavaScript object + * @return DALi ProperyMap from the JavaScript object */ Property::Map GetPropertyMapFromObject( v8::Isolate* isolate, const v8::Local& object); @@ -252,6 +252,13 @@ bool GetBooleanParameter( unsigned int index, bool& found, v8::Isolate* isolate, /** * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2). * @param[out] found whether the parameter was found + * @return ArrayBufferView from the JavaScript function arguments + */ +void* GetArrayBufferViewParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args ); + +/** + * @param [in] index parameter index, e.g. callMyFunc( index0, index1, index2). + * @param[out] found whether the parameter was found * @return DALi Handle value from the JavaScript function arguments */ Handle GetHandleParameter( unsigned int index, bool& found, v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args );