Updates after RenderableActor removal
[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  * Sets the blending mode.
196  *
197  * If blending is disabled (BLENDING_OFF) fade in and fade out animations do not work.
198  *
199  * @example
200  *      // blend mode is one of the following
201  *      dali.BLENDING_OFF       // Blending is disabled.
202  *      dali.BLENDING_AUTO      // Blending is enabled if there is alpha channel.
203  *      dali.BLENDING_ON        // Blending is enabled.
204  *      actor.SetBlendMode( dali.BLENDING_AUTO );
205  *
206  * @for ImageActor
207  * @method setBlendMode
208  * @param { Number } blendMode
209  */
210 void ImageActorApi::SetBlendMode( const v8::FunctionCallbackInfo<v8::Value>& args )
211 {
212   v8::Isolate* isolate = args.GetIsolate();
213   v8::HandleScope handleScope( isolate );
214   ImageActor imageActor = GetImageActor( isolate, args );
215
216   bool found( false );
217   int mode = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
218   if( !found )
219   {
220     DALI_SCRIPT_EXCEPTION( isolate, "invalid BlendMode parameter" );
221     return;
222   }
223   imageActor.SetBlendMode( static_cast<Dali::BlendingMode::Type>( mode ) );
224
225 }
226
227 /**
228  * @for ImageActor
229  * @method getBlendMode
230  * @return { Number } blendMode
231  * @example returns one of the following:
232  *
233  *        dali.BLENDING_OFF       // Blending is disabled.
234  *        dali.BLENDING_AUTO      // Blending is enabled if there is alpha channel.
235  *        dali.BLENDING_ON        // Blending is enabled.
236  *
237  */
238 void ImageActorApi::GetBlendMode( const v8::FunctionCallbackInfo<v8::Value>& args )
239 {
240   v8::Isolate* isolate = args.GetIsolate();
241   v8::HandleScope handleScope( isolate );
242   ImageActor imageActor = GetImageActor( isolate, args );
243
244   args.GetReturnValue().Set( v8::Integer::New( isolate, imageActor.GetBlendMode() ) );
245
246 }
247
248 /**
249  * @for ImageActor
250  * @method setBlendFunc
251  * @param {Number} SourceBlending RGB
252  * @param {Number} DestinationBlending RGB
253  * @param {Number} SourceBlending Alpha
254  * @param {Number} DestinatinoBlending Alpha
255  * @example
256  *      //blending constants
257       dali.BLEND_FACTOR_ZERO
258       dali.BLEND_FACTOR_ONE
259       dali.BLEND_FACTOR_SRC_COLOR
260       dali.BLEND_FACTOR_ONE_MINUS_SRC_COLOR
261       dali.BLEND_FACTOR_SRC_ALPHA
262       dali.BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
263       dali.BLEND_FACTOR_DST_ALPHA
264       dali.BLEND_FACTOR_ONE_MINUS_DST_ALPHA
265       dali.BLEND_FACTOR_DST_COLOR
266       dali.BLEND_FACTOR_ONE_MINUS_DST_COLOR
267       dali.BLEND_FACTOR_SRC_ALPHA_SATURATE
268       dali.BLEND_FACTOR_CONSTANT_COLOR
269       dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR
270       dali.BLEND_FACTOR_CONSTANT_ALPHA
271       dali.BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA
272
273       actor.setBlendFunc(  dali.BLEND_FACTOR_CONSTANT_COLOR, BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
274                                 dali.BLEND_FACTOR_CONSTANT_ALPHA, BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA);
275
276       );
277  */
278 void ImageActorApi::SetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
279 {
280   v8::Isolate* isolate = args.GetIsolate();
281   v8::HandleScope handleScope( isolate );
282   ImageActor imageActor = GetImageActor( isolate, args );
283
284   int params[4];
285   bool foundAllParams(false);
286   V8Utils::ReadIntegerArguments( foundAllParams, &params[0], 4, args,0 );
287   if( foundAllParams )
288   {
289     imageActor.SetBlendFunc(  static_cast< Dali::BlendingFactor::Type>(params[0]),
290                          static_cast< Dali::BlendingFactor::Type>(params[1]),
291                          static_cast< Dali::BlendingFactor::Type>(params[2]),
292                          static_cast< Dali::BlendingFactor::Type>(params[3]));
293   }
294   else
295   {
296     DALI_SCRIPT_EXCEPTION( isolate, "invalid BlendMode parameter");
297     return;
298   }
299 }
300
301 /**
302  * @for ImageActor
303  * @method GetBlendFunc
304  * @return {Object} BlendProperties
305  * @example Blend properties object has 4 fields
306  *
307  *      blendProperties.sourceRgb // source rgb enum
308  *      blendProperties.destinationRgb  // destination rgb enum
309  *      blendProperties.sourceAlpha source // alpha enum
310  *      blendProperties.destinationAlpha // destination alpha enum
311  */
312 void ImageActorApi::GetBlendFunc( const v8::FunctionCallbackInfo< v8::Value >& args )
313 {
314   // @todo pass by reference doesn't work in Javascript so need to decide what to return
315   // for now just return a vector 4...
316
317   BlendingFactor::Type srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha;
318   v8::Isolate* isolate = args.GetIsolate();
319   v8::HandleScope handleScope( isolate );
320   ImageActor imageActor = GetImageActor( isolate, args );
321
322   imageActor.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
323
324   v8::Local<v8::Object> blendInfo = v8::Object::New( isolate );
325
326   blendInfo->Set( v8::String::NewFromUtf8( isolate, "sourceRgb" ),        v8::Integer::New( isolate, srcFactorRgb) );
327   blendInfo->Set( v8::String::NewFromUtf8( isolate, "destinationRgb" ),   v8::Integer::New( isolate, destFactorRgb ) );
328   blendInfo->Set( v8::String::NewFromUtf8( isolate, "sourceAlpha" ),      v8::Integer::New( isolate, srcFactorAlpha  ) );
329   blendInfo->Set( v8::String::NewFromUtf8( isolate, "destinationAlpha" ), v8::Integer::New( isolate, destFactorAlpha ) );
330
331   args.GetReturnValue().Set( blendInfo );
332
333 }
334
335 /**
336  * @for ImageActor
337  * @method getShaderEffect
338  * @return {Object} ShaderEffect object
339  *
340  * Retrieve the shader effect for the Actor.
341  *
342  * @example
343  *    var shaderEffect = actor.getShaderEffect();
344  *
345  */
346 void ImageActorApi::GetShaderEffect( const v8::FunctionCallbackInfo<v8::Value>& args )
347 {
348   v8::Isolate* isolate = args.GetIsolate();
349   v8::HandleScope handleScope( isolate );
350   ImageActor imageActor = GetImageActor( isolate, args );
351
352   v8::Local < v8::Object > object = ShaderEffectWrapper::WrapShaderEffect( isolate, imageActor.GetShaderEffect() );
353   args.GetReturnValue().Set( object );
354
355 }
356
357 /**
358  * @for ImageActor
359  * @method setShaderEffect
360  * @param {Object} shaderEffect The shader effect.
361  *
362  * Sets the shader effect for the Actor.
363  *
364  * Shader effects provide special effects like rippling and bending.
365  * Setting a shader effect removes any shader effect previously set by SetShaderEffect.
366  * @example
367  *      // first create the shaderOptions, then the shaderEffect
368  *      var shader = new dali.ShaderEffect( shaderOptions );
369  *      actor.setShaderEffect( shader );
370  *
371  */
372 void ImageActorApi::SetShaderEffect( const v8::FunctionCallbackInfo<v8::Value>& args )
373 {
374   v8::Isolate* isolate = args.GetIsolate();
375   v8::HandleScope handleScope( isolate );
376   ImageActor imageActor = GetImageActor( isolate, args );
377
378   bool found( false );
379   ShaderEffect effect = ShaderEffectApi::GetShaderEffectFromParams( 0, found, isolate, args );
380   if( found )
381   {
382     imageActor.SetShaderEffect( effect );
383   }
384   else
385   {
386     DALI_SCRIPT_EXCEPTION( isolate, "shader effect parameter missing" );
387   }
388 }
389
390 /**
391  *
392  * Removes the current shader effect
393  *
394  * @example
395  *    actor.removeShaderEffect();
396  *
397  * @for ImageActor
398  * @method removeShaderEffect
399  */
400 void ImageActorApi::RemoveShaderEffect( const v8::FunctionCallbackInfo<v8::Value>& args )
401 {
402   v8::Isolate* isolate = args.GetIsolate();
403   v8::HandleScope handleScope( isolate );
404   ImageActor imageActor = GetImageActor( isolate, args );
405   imageActor.RemoveShaderEffect();
406 }
407
408 } // namespace V8Plugin
409
410 } // namespace Dali