7394fa030782553b5b029bef533149a5dfdff595
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / actors / image-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 "image-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 <image/image-api.h>
26 #include <image/image-wrapper.h>
27 #include <shader-effects/shader-effect-api.h>
28 #include <shader-effects/shader-effect-wrapper.h>
29
30 namespace Dali
31 {
32
33 namespace V8Plugin
34 {
35
36 namespace //unnamed name space
37 {
38
39 ImageActor GetImageActor( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
40 {
41   HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, args.This() );
42   return ImageActor::DownCast( handleWrapper->mHandle );
43 }
44
45 } //unnamed name space
46
47 /**
48  * Constructor
49  *
50  * @constructor
51  * @method ImageActor
52  * @for ImageActor
53  * @param {Object} [image] Image object
54  * @param {Object} [pixelArea] Vector4
55  * @return {Object} ImageActor
56  * @example
57  *    new DALI.imageActor( image, [10,23,35,56] );
58  */
59 Actor ImageActorApi::New( const v8::FunctionCallbackInfo<v8::Value>& args )
60 {
61   v8::Isolate* isolate = args.GetIsolate();
62   v8::HandleScope handleScope( isolate );
63
64   // Image actor has 3 different constructors
65   // New();
66   // New( image);
67   // New( image, PixelArea pixelArea);
68
69   if( args.Length() == 0 )
70   {
71     return ImageActor::New();
72   }
73
74   // args.Length> 0, must have an Image parameter
75   bool found( false );
76   Image image = V8Utils::GetImageParameter( PARAMETER_0, found, isolate, args );
77   if( !found )
78   {
79     DALI_SCRIPT_EXCEPTION( isolate, "missing image from param 0" );
80     return ImageActor();
81   }
82
83   // check for PixelArea, accept a DALI Vector4 object ( which can be a JavaScript array)
84   // e.g.  new DALI.imageActor( image, [10,23,35,56] );
85   // or    new DALI.imageActor( image, Vector4 );
86
87   if( args.Length() > 1 )
88   {
89     Vector4 rect = V8Utils::GetVector4Parameter( PARAMETER_1, found, isolate, args );
90     if( !found )
91     {
92       DALI_SCRIPT_EXCEPTION( isolate, " bad parameters" );
93       return ImageActor();
94     }
95     Rect<int>rectangle( static_cast<int>(rect.x),
96                         static_cast<int>(rect.y),
97                         static_cast<int>(rect.z),
98                         static_cast<int>(rect.w));
99
100     return ImageActor::New( image, rectangle );
101   }
102   else
103   {
104     return ImageActor::New( image );
105   }
106 }
107
108 /**
109  * Set the image rendered by the actor.
110  *
111  * When the image is loaded the actor's size will be reset to the image size,
112  * unless a custom size was chosen, e.g. via actor.size or a pixel area
113  * was set.
114  * Note: The old image will continue to be displayed until the given image has loaded.
115  * @for ImageActor
116  * @method setImage
117  * @param {Object} image The image to display.
118  *
119  */
120 void ImageActorApi::SetImage( const v8::FunctionCallbackInfo<v8::Value>& args )
121 {
122   v8::Isolate* isolate = args.GetIsolate();
123   v8::HandleScope handleScope( isolate );
124
125   bool found( false );
126   Image image = V8Utils::GetImageParameter( PARAMETER_0, found, isolate, args );
127   if( !found )
128   {
129     DALI_SCRIPT_EXCEPTION( isolate, "bad parameters" );
130     return;
131   }
132   ImageActor imageActor = GetImageActor( isolate, args );
133   imageActor.SetImage( image );
134 }
135
136 /**
137  * brief Retrieve the image rendered by the actor's attachment.
138  * @for ImageActor
139  * @method getImage
140  * @return {Object} the image.
141  */
142 void ImageActorApi::GetImage( const v8::FunctionCallbackInfo<v8::Value>& args )
143 {
144   v8::Isolate* isolate = args.GetIsolate();
145   v8::HandleScope handleScope( isolate );
146   ImageActor imageActor = GetImageActor( isolate, args );
147   Image image = imageActor.GetImage();
148
149   // wrap the image
150   v8::Local<v8::Object> localObject = ImageWrapper::WrapImage( isolate, image );
151   args.GetReturnValue().Set( localObject );
152 }
153
154 /**
155  * Allows modification of an actors position in the depth sort algorithm.
156  *
157  * The offset can be altered for each coplanar actor hence allowing an order of painting.
158  * @param { Number }  depthOffset the offset to be given to the actor. Positive values pushing it further back.
159  * @for ImageActor
160  * @method setSortModifier
161  */
162 void ImageActorApi::SetSortModifier( const v8::FunctionCallbackInfo<v8::Value>& args )
163 {
164   v8::Isolate* isolate = args.GetIsolate();
165   v8::HandleScope handleScope( isolate );
166   ImageActor imageActor = GetImageActor( isolate, args );
167
168   bool found( false );
169   float value = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f );
170   if( !found )
171   {
172     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
173     return;
174   }
175   imageActor.SetSortModifier( value );
176 }
177
178 /**
179  * Retrieves the offset used to modify an actors position in the depth sort algorithm.
180  * @for ImageActor
181  * @method getSortModifier .
182  * @return { Number} the offset that has been given to the actor. Positive values pushing it further back
183  */
184 void ImageActorApi::GetSortModifier( const v8::FunctionCallbackInfo<v8::Value>& args )
185 {
186   v8::Isolate* isolate = args.GetIsolate();
187   v8::HandleScope handleScope( isolate );
188   ImageActor imageActor = GetImageActor( isolate, args );
189
190   args.GetReturnValue().Set( v8::Number::New( isolate, imageActor.GetSortModifier() ) );
191
192 }
193
194 /**
195  * Set the face-culling mode for this actor.
196  * @for ImageActor
197  * @method setCullFace
198  * @param {Number} cullMode
199  * @example
200  *      // cull mode should be one of the following constants
201  *      dali.CULL_FACE_DISABLE        // Face culling disabled
202  *      dali.CULL_FRONT_FACE          // Cull front facing polygons
203  *      dali.CULL_BACK_FACE           // Cull back facing polygons
204  *      dali.CULL_FRONT_AND_BACK_FACE // Cull front and back facing polygons
205  *      actor.SetCullFace( dali.CULL_FRONT_FACE );
206  */
207 void ImageActorApi::SetCullFace( const v8::FunctionCallbackInfo<v8::Value>& args )
208 {
209   v8::Isolate* isolate = args.GetIsolate();
210   v8::HandleScope handleScope( isolate );
211   ImageActor imageActor = GetImageActor( isolate, args );
212
213   bool found( false );
214   int cullMode = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
215   if( !found )
216   {
217     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
218     return;
219   }
220
221   imageActor.SetCullFace(  static_cast<Dali::CullFaceMode>( cullMode ) );
222
223 }
224
225 /**
226  * Retrieve the face-culling mode for this actor.
227  * @for ImageActor
228  * @method getCullFace
229  * @return {Number} cullMode
230  * @example
231  *      // cull mode is one of the following
232  *      dali.CULL_FACE_DISABLE        // Face culling disabled
233  *      dali.CULL_FRONT_FACE          // Cull front facing polygons
234  *      dali.CULL_BACK_FACE           // Cull back facing polygons
235  *      dali.CULL_FRONT_AND_BACK_FACE // Cull front and back facing polygon
236  */
237 void ImageActorApi::GetCullFace( const v8::FunctionCallbackInfo<v8::Value>& args )
238 {
239   v8::Isolate* isolate = args.GetIsolate();
240   v8::HandleScope handleScope( isolate );
241   ImageActor imageActor = GetImageActor( isolate, args );
242
243   args.GetReturnValue().Set( v8::Integer::New( isolate, imageActor.GetCullFace() ) );
244
245 }
246
247 /**
248  * Sets the blending mode.
249  *
250  * If blending is disabled (BLENDING_OFF) fade in and fade out animations do not work.
251  *
252  * @example
253  *      // blend mode is one of the following
254  *      dali.BLENDING_OFF       // Blending is disabled.
255  *      dali.BLENDING_AUTO      // Blending is enabled if there is alpha channel.
256  *      dali.BLENDING_ON        // Blending is enabled.
257  *      actor.SetBlendMode( dali.BLENDING_AUTO );
258  *
259  * @for ImageActor
260  * @method setBlendMode
261  * @param { Number } blendMode
262  */
263 void ImageActorApi::SetBlendMode( const v8::FunctionCallbackInfo<v8::Value>& args )
264 {
265   v8::Isolate* isolate = args.GetIsolate();
266   v8::HandleScope handleScope( isolate );
267   ImageActor imageActor = GetImageActor( isolate, args );
268
269   bool found( false );
270   int mode = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
271   if( !found )
272   {
273     DALI_SCRIPT_EXCEPTION( isolate, "invalid BlendMode parameter" );
274     return;
275   }
276   imageActor.SetBlendMode( static_cast<Dali::BlendingMode::Type>( mode ) );
277
278 }
279
280 /**
281  * @for ImageActor
282  * @method getBlendMode
283  * @return { Number } blendMode
284  * @example returns one of the following:
285  *
286  *        dali.BLENDING_OFF       // Blending is disabled.
287  *        dali.BLENDING_AUTO      // Blending is enabled if there is alpha channel.
288  *        dali.BLENDING_ON        // Blending is enabled.
289  *
290  */
291 void ImageActorApi::GetBlendMode( const v8::FunctionCallbackInfo<v8::Value>& args )
292 {
293   v8::Isolate* isolate = args.GetIsolate();
294   v8::HandleScope handleScope( isolate );
295   ImageActor imageActor = GetImageActor( isolate, args );
296
297   args.GetReturnValue().Set( v8::Integer::New( isolate, imageActor.GetBlendMode() ) );
298
299 }
300
301 /**
302  * @for ImageActor
303  * @method setBlendFunc
304  * @param {Number} SourceBlending RGB
305  * @param {Number} DestinationBlending RGB
306  * @param {Number} SourceBlending Alpha
307  * @param {Number} DestinatinoBlending Alpha
308  * @example
309  *      //blending constants
310       dali.BLEND_FACTOR_ZERO
311       dali.BLEND_FACTOR_ONE
312       dali.BLEND_FACTOR_SRC_COLOR
313       dali.BLEND_FACTOR_ONE_MINUS_SRC_COLOR
314       dali.BLEND_FACTOR_SRC_ALPHA
315       dali.BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
316       dali.BLEND_FACTOR_DST_ALPHA
317       dali.BLEND_FACTOR_ONE_MINUS_DST_ALPHA
318       dali.BLEND_FACTOR_DST_COLOR
319       dali.BLEND_FACTOR_ONE_MINUS_DST_COLOR
320       dali.BLEND_FACTOR_SRC_ALPHA_SATURATE
321       dali.BLEND_FACTOR_CONSTANT_COLOR
322       dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR
323       dali.BLEND_FACTOR_CONSTANT_ALPHA
324       dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA
325
326       actor.setBlendFunc(  dali.BLEND_FACTOR_CONSTANT_COLOR, BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
327                                 dali.BLEND_FACTOR_CONSTANT_ALPHA, BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA);
328
329       );
330  */
331 void ImageActorApi::SetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
332 {
333   v8::Isolate* isolate = args.GetIsolate();
334   v8::HandleScope handleScope( isolate );
335   ImageActor imageActor = GetImageActor( isolate, args );
336
337   int params[4];
338   bool foundAllParams(false);
339   V8Utils::ReadIntegerArguments( foundAllParams, &params[0], 4, args,0 );
340   if( foundAllParams )
341   {
342     imageActor.SetBlendFunc(  static_cast< Dali::BlendingFactor::Type>(params[0]),
343                          static_cast< Dali::BlendingFactor::Type>(params[1]),
344                          static_cast< Dali::BlendingFactor::Type>(params[2]),
345                          static_cast< Dali::BlendingFactor::Type>(params[3]));
346   }
347   else
348   {
349     DALI_SCRIPT_EXCEPTION( isolate, "invalid BlendMode parameter");
350     return;
351   }
352 }
353
354 /**
355  * @for ImageActor
356  * @method GetBlendFunc
357  * @return {Object} BlendProperties
358  * @example Blend properties object has 4 fields
359  *
360  *      blendProperties.sourceRgb // source rgb enum
361  *      blendProperties.destinationRgb  // destination rgb enum
362  *      blendProperties.sourceAlpha source // alpha enum
363  *      blendProperties.destinationAlpha // destination alpha enum
364  */
365 void ImageActorApi::GetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
366 {
367   // @todo pass by reference doesn't work in Javascript so need to decide what to return
368   // for now just return a vector 4...
369
370   BlendingFactor::Type srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha;
371   v8::Isolate* isolate = args.GetIsolate();
372   v8::HandleScope handleScope( isolate );
373   ImageActor imageActor = GetImageActor( isolate, args );
374
375   imageActor.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
376
377   v8::Local<v8::Object> blendInfo = v8::Object::New( isolate );
378
379   blendInfo->Set( v8::String::NewFromUtf8( isolate, "sourceRgb" ),        v8::Integer::New( isolate, srcFactorRgb) );
380   blendInfo->Set( v8::String::NewFromUtf8( isolate, "destinationRgb" ),   v8::Integer::New( isolate, destFactorRgb ) );
381   blendInfo->Set( v8::String::NewFromUtf8( isolate, "sourceAlpha" ),      v8::Integer::New( isolate, srcFactorAlpha  ) );
382   blendInfo->Set( v8::String::NewFromUtf8( isolate, "destinationAlpha" ), v8::Integer::New( isolate, destFactorAlpha ) );
383
384   args.GetReturnValue().Set( blendInfo );
385
386 }
387
388 /**
389  * @for ImageActor
390  * @method getShaderEffect
391  * @return {Object} ShaderEffect object
392  *
393  * Retrieve the shader effect for the Actor.
394  *
395  * @example
396  *    var shaderEffect = actor.getShaderEffect();
397  *
398  */
399 void ImageActorApi::GetShaderEffect( const v8::FunctionCallbackInfo<v8::Value>& args )
400 {
401   v8::Isolate* isolate = args.GetIsolate();
402   v8::HandleScope handleScope( isolate );
403   ImageActor imageActor = GetImageActor( isolate, args );
404
405   v8::Local < v8::Object > object = ShaderEffectWrapper::WrapShaderEffect( isolate, imageActor.GetShaderEffect() );
406   args.GetReturnValue().Set( object );
407
408 }
409
410 /**
411  * @for ImageActor
412  * @method setShaderEffect
413  * @param {Object} shaderEffect The shader effect.
414  *
415  * Sets the shader effect for the Actor.
416  *
417  * Shader effects provide special effects like rippling and bending.
418  * Setting a shader effect removes any shader effect previously set by SetShaderEffect.
419  * @example
420  *      // first create the shaderOptions, then the shaderEffect
421  *      var shader = new dali.ShaderEffect( shaderOptions );
422  *      actor.setShaderEffect( shader );
423  *
424  */
425 void ImageActorApi::SetShaderEffect( const v8::FunctionCallbackInfo<v8::Value>& args )
426 {
427   v8::Isolate* isolate = args.GetIsolate();
428   v8::HandleScope handleScope( isolate );
429   ImageActor imageActor = GetImageActor( isolate, args );
430
431   bool found( false );
432   ShaderEffect effect = ShaderEffectApi::GetShaderEffectFromParams( 0, found, isolate, args );
433   if( found )
434   {
435     imageActor.SetShaderEffect( effect );
436   }
437   else
438   {
439     DALI_SCRIPT_EXCEPTION( isolate, "shader effect parameter missing" );
440   }
441 }
442
443 /**
444  *
445  * Removes the current shader effect
446  *
447  * @example
448  *    actor.removeShaderEffect();
449  *
450  * @for ImageActor
451  * @method removeShaderEffect
452  */
453 void ImageActorApi::RemoveShaderEffect( const v8::FunctionCallbackInfo<v8::Value>& args )
454 {
455   v8::Isolate* isolate = args.GetIsolate();
456   v8::HandleScope handleScope( isolate );
457   ImageActor imageActor = GetImageActor( isolate, args );
458   imageActor.RemoveShaderEffect();
459 }
460
461 } // namespace V8Plugin
462
463 } // namespace Dali