Reduce ImageActor & RenderableActor APIs
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / actors / renderable-actor-api.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "renderable-actor-api.h"
20
21 // INTERNAL INCLUDES
22 #include <object/handle-wrapper.h>
23 #include <v8-utils.h>
24 #include <object/property-value-wrapper.h>
25 #include <shader-effects/shader-effect-api.h>
26 #include <shader-effects/shader-effect-wrapper.h>
27
28 namespace Dali
29 {
30
31 namespace V8Plugin
32 {
33
34 namespace //unnamed name space
35 {
36 RenderableActor GetRenderableActor( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
37 {
38   HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, args.This() );
39   return RenderableActor::DownCast( handleWrapper->mHandle );
40 }
41 }
42 /***************************************
43  * RENDERABLE ACTOR FUNCTIONS
44  *
45  ****************************************/
46 /**
47  * Allows modification of an actors position in the depth sort algorithm.
48  *
49  * The offset can be altered for each coplanar actor hence allowing an order of painting.
50  * @param { Number }  depthOffset the offset to be given to the actor. Positive values pushing it further back.
51  * @for RenderableActor
52  * @method setSortModifier
53  */
54 void RenderableActorApi::SetSortModifier( const v8::FunctionCallbackInfo<v8::Value>& args )
55 {
56   v8::Isolate* isolate = args.GetIsolate();
57   v8::HandleScope handleScope( isolate );
58   RenderableActor actor = GetRenderableActor( isolate, args );
59
60   bool found( false );
61   float value = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f );
62   if( !found )
63   {
64     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
65     return;
66   }
67   actor.SetSortModifier( value );
68 }
69
70 /**
71  * Retrieves the offset used to modify an actors position in the depth sort algorithm.
72  * @for RenderableActor
73  * @method getSortModifier .
74  * @return { Number} the offset that has been given to the actor. Positive values pushing it further back
75  */
76 void RenderableActorApi::GetSortModifier( const v8::FunctionCallbackInfo<v8::Value>& args )
77 {
78   v8::Isolate* isolate = args.GetIsolate();
79   v8::HandleScope handleScope( isolate );
80   RenderableActor actor = GetRenderableActor( isolate, args );
81
82   args.GetReturnValue().Set( v8::Number::New( isolate, actor.GetSortModifier() ) );
83
84 }
85
86 /**
87  * Set the face-culling mode for this actor.
88  * @for RenderableActor
89  * @method setCullFace
90  * @param {Number} cullMode
91  * @example
92  *      // cull mode should be one of the following constants
93  *      dali.CULL_FACE_DISABLE        // Face culling disabled
94  *      dali.CULL_FRONT_FACE          // Cull front facing polygons
95  *      dali.CULL_BACK_FACE           // Cull back facing polygons
96  *      dali.CULL_FRONT_AND_BACK_FACE // Cull front and back facing polygons
97  *      actor.SetCullFace( dali.CULL_FRONT_FACE );
98  */
99 void RenderableActorApi::SetCullFace( const v8::FunctionCallbackInfo<v8::Value>& args )
100 {
101   v8::Isolate* isolate = args.GetIsolate();
102   v8::HandleScope handleScope( isolate );
103   RenderableActor actor = GetRenderableActor( isolate, args );
104
105   bool found( false );
106   int cullMode = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
107   if( !found )
108   {
109     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
110     return;
111   }
112
113   actor.SetCullFace(  static_cast<Dali::CullFaceMode>( cullMode ) );
114
115 }
116
117 /**
118  * Retrieve the face-culling mode for this actor.
119  * @for RenderableActor
120  * @method getCullFace
121  * @return {Number} cullMode
122  * @example
123  *      // cull mode is one of the following
124  *      dali.CULL_FACE_DISABLE        // Face culling disabled
125  *      dali.CULL_FRONT_FACE          // Cull front facing polygons
126  *      dali.CULL_BACK_FACE           // Cull back facing polygons
127  *      dali.CULL_FRONT_AND_BACK_FACE // Cull front and back facing polygon
128  */
129 void RenderableActorApi::GetCullFace( const v8::FunctionCallbackInfo<v8::Value>& args )
130 {
131   v8::Isolate* isolate = args.GetIsolate();
132   v8::HandleScope handleScope( isolate );
133   RenderableActor actor = GetRenderableActor( isolate, args );
134
135   args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetCullFace() ) );
136
137 }
138
139 /**
140  * Sets the blending mode.
141  *
142  * If blending is disabled (BLENDING_OFF) fade in and fade out animations do not work.
143  *
144  * @example
145  *      // blend mode is one of the following
146  *      dali.BLENDING_OFF       // Blending is disabled.
147  *      dali.BLENDING_AUTO      // Blending is enabled if there is alpha channel.
148  *      dali.BLENDING_ON        // Blending is enabled.
149  *      actor.SetBlendMode( dali.BLENDING_AUTO );
150  *
151  * @for RenderableActor
152  * @method setBlendMode
153  * @param { Number } blendMode
154  */
155 void RenderableActorApi::SetBlendMode( const v8::FunctionCallbackInfo<v8::Value>& args )
156 {
157   v8::Isolate* isolate = args.GetIsolate();
158   v8::HandleScope handleScope( isolate );
159   RenderableActor actor = GetRenderableActor( isolate, args );
160
161   bool found( false );
162   int mode = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
163   if( !found )
164   {
165     DALI_SCRIPT_EXCEPTION( isolate, "invalid BlendMode parameter" );
166     return;
167   }
168   actor.SetBlendMode( static_cast<Dali::BlendingMode::Type>( mode ) );
169
170 }
171
172 /**
173  * @for RenderableActor
174  * @method getBlendMode
175  * @return { Number } blendMode
176  * @example returns one of the following:
177  *
178  *        dali.BLENDING_OFF       // Blending is disabled.
179  *        dali.BLENDING_AUTO      // Blending is enabled if there is alpha channel.
180  *        dali.BLENDING_ON        // Blending is enabled.
181  *
182  */
183 void RenderableActorApi::GetBlendMode( const v8::FunctionCallbackInfo<v8::Value>& args )
184 {
185   v8::Isolate* isolate = args.GetIsolate();
186   v8::HandleScope handleScope( isolate );
187   RenderableActor actor = GetRenderableActor( isolate, args );
188
189   args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetBlendMode() ) );
190
191 }
192
193 /**
194  * @for RenderableActor
195  * @method setBlendFunc
196  * @param {Number} SourceBlending RGB
197  * @param {Number} DestinationBlending RGB
198  * @param {Number} SourceBlending Alpha
199  * @param {Number} DestinatinoBlending Alpha
200  * @example
201  *      //blending constants
202       dali.BLEND_FACTOR_ZERO
203       dali.BLEND_FACTOR_ONE
204       dali.BLEND_FACTOR_SRC_COLOR
205       dali.BLEND_FACTOR_ONE_MINUS_SRC_COLOR
206       dali.BLEND_FACTOR_SRC_ALPHA
207       dali.BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
208       dali.BLEND_FACTOR_DST_ALPHA
209       dali.BLEND_FACTOR_ONE_MINUS_DST_ALPHA
210       dali.BLEND_FACTOR_DST_COLOR
211       dali.BLEND_FACTOR_ONE_MINUS_DST_COLOR
212       dali.BLEND_FACTOR_SRC_ALPHA_SATURATE
213       dali.BLEND_FACTOR_CONSTANT_COLOR
214       dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR
215       dali.BLEND_FACTOR_CONSTANT_ALPHA
216       dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA
217
218       actor.setBlendFunc(  dali.BLEND_FACTOR_CONSTANT_COLOR, BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
219                                 dali.BLEND_FACTOR_CONSTANT_ALPHA, BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA);
220
221       );
222  */
223 void RenderableActorApi::SetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
224 {
225   v8::Isolate* isolate = args.GetIsolate();
226   v8::HandleScope handleScope( isolate );
227   RenderableActor actor =  GetRenderableActor( isolate, args );
228
229   int params[4];
230   bool foundAllParams(false);
231   V8Utils::ReadIntegerArguments( foundAllParams, &params[0], 4, args,0 );
232   if( foundAllParams )
233   {
234     actor.SetBlendFunc(  static_cast< Dali::BlendingFactor::Type>(params[0]),
235                          static_cast< Dali::BlendingFactor::Type>(params[1]),
236                          static_cast< Dali::BlendingFactor::Type>(params[2]),
237                          static_cast< Dali::BlendingFactor::Type>(params[3]));
238   }
239   else
240   {
241     DALI_SCRIPT_EXCEPTION( isolate, "invalid BlendMode parameter");
242     return;
243   }
244 }
245
246 /**
247  * @for RenderableActor
248  * @method GetBlendFunc
249  * @return {Object} BlendProperties
250  * @example Blend properties object has 4 fields
251  *
252  *      blendProperties.sourceRgb // source rgb enum
253  *      blendProperties.destinationRgb  // destination rgb enum
254  *      blendProperties.sourceAlpha source // alpha enum
255  *      blendProperties.destinationAlpha // destination alpha enum
256  */
257 void RenderableActorApi::GetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
258 {
259   // @todo pass by reference doesn't work in Javascript so need to decide what to return
260   // for now just return a vector 4...
261
262   BlendingFactor::Type srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha;
263   v8::Isolate* isolate = args.GetIsolate();
264   v8::HandleScope handleScope( isolate );
265   RenderableActor actor = GetRenderableActor( isolate, args );
266
267   actor.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
268
269   v8::Local<v8::Object> blendInfo = v8::Object::New( isolate );
270
271   blendInfo->Set( v8::String::NewFromUtf8( isolate, "sourceRgb" ),        v8::Integer::New( isolate, srcFactorRgb) );
272   blendInfo->Set( v8::String::NewFromUtf8( isolate, "destinationRgb" ),   v8::Integer::New( isolate, destFactorRgb ) );
273   blendInfo->Set( v8::String::NewFromUtf8( isolate, "sourceAlpha" ),      v8::Integer::New( isolate, srcFactorAlpha  ) );
274   blendInfo->Set( v8::String::NewFromUtf8( isolate, "destinationAlpha" ), v8::Integer::New( isolate, destFactorAlpha ) );
275
276   args.GetReturnValue().Set( blendInfo );
277
278 }
279
280 /**
281  * @for RenderableActor
282  * @method getShaderEffect
283  * @return {Object} ShaderEffect object
284  *
285  * Retrieve the shader effect for the Actor.
286  *
287  * @example
288  *    var shaderEffect = actor.getShaderEffect();
289  *
290  */
291 void RenderableActorApi::GetShaderEffect( const v8::FunctionCallbackInfo<v8::Value>& args )
292 {
293   v8::Isolate* isolate = args.GetIsolate();
294   v8::HandleScope handleScope( isolate );
295   RenderableActor actor = GetRenderableActor( isolate, args );
296
297   v8::Local < v8::Object > object = ShaderEffectWrapper::WrapShaderEffect( isolate, actor.GetShaderEffect() );
298   args.GetReturnValue().Set( object );
299
300 }
301
302 /**
303  * @for RenderableActor
304  * @method setShaderEffect
305  * @param {Object} shaderEffect The shader effect.
306  *
307  * Sets the shader effect for the Actor.
308  *
309  * Shader effects provide special effects like rippling and bending.
310  * Setting a shader effect removes any shader effect previously set by SetShaderEffect.
311  * @example
312  *      // first create the shaderOptions, then the shaderEffect
313  *      var shader = new dali.ShaderEffect( shaderOptions );
314  *      actor.setShaderEffect( shader );
315  *
316  */
317 void RenderableActorApi::SetShaderEffect( const v8::FunctionCallbackInfo<v8::Value>& args )
318 {
319   v8::Isolate* isolate = args.GetIsolate();
320   v8::HandleScope handleScope( isolate );
321   RenderableActor actor = GetRenderableActor( isolate, args );
322
323   bool found( false );
324   ShaderEffect effect = ShaderEffectApi::GetShaderEffectFromParams( 0, found, isolate, args );
325   if( found )
326   {
327     actor.SetShaderEffect( effect );
328   }
329   else
330   {
331     DALI_SCRIPT_EXCEPTION( isolate, "shader effect parameter missing" );
332   }
333 }
334
335 /**
336  *
337  * Removes the current shader effect
338  *
339  * @example
340  *    actor.removeShaderEffect();
341  *
342  * @for RenderableActor
343  * @method removeShaderEffect
344  */
345 void RenderableActorApi::RemoveShaderEffect( const v8::FunctionCallbackInfo<v8::Value>& args )
346 {
347   v8::Isolate* isolate = args.GetIsolate();
348   v8::HandleScope handleScope( isolate );
349   RenderableActor actor = GetRenderableActor( isolate, args );
350   actor.RemoveShaderEffect();
351 }
352
353
354
355 } // namespace V8Plugin
356
357 } // namespace Dali