Merge "Fix JavaScript docs, where orientation property still called rotation" into...
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / animation / animation-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 "animation-api.h"
20 #include "path-wrapper.h"
21
22 // EXTERNAL INCLUDES
23 #include <dali/integration-api/debug.h>
24
25
26 // INTERNAL INCLUDES
27 #include <v8-utils.h>
28 #include <animation/animation-wrapper.h>
29 #include <object/property-value-wrapper.h>
30
31 namespace Dali
32 {
33
34 namespace V8Plugin
35 {
36
37 namespace // un named namespace
38 {
39
40 struct AnimationParameters
41 {
42   AnimationParameters( const Animation& anim)
43   : propertyIndex( Property::INVALID_INDEX  ),
44     alphaFunction( AlphaFunction::DEFAULT),
45     delay( 0.f ),
46     duration(anim.GetDuration()),
47     optionsFound( false )
48   {
49   }
50
51   Handle target;
52   Property::Index propertyIndex;
53   Property::Value value;
54   KeyFrames keyFrames;
55   AlphaFunction alphaFunction;
56   float delay;
57   float duration;
58   bool optionsFound;
59 };
60
61 void GetAnimationOptions( v8::Isolate* isolate,
62                           v8::Local<v8::Value > options,
63                           AnimationParameters& animParams )
64 {
65   // animation options is an optional parameter passed in which holds
66   // optional settings
67   // var animOptions = {
68   //  alpha: "Bounce",
69   //  delay: 5,
70   //  duration: 20
71   // };
72   v8::HandleScope handleScope( isolate );
73
74   if( options->IsObject() )
75   {
76     v8::Local<v8::Object> obj = options->ToObject();
77     v8::Local<v8::Value> alphaValue = obj->Get( v8::String::NewFromUtf8( isolate, "alpha" ) );
78     if( alphaValue->IsUint32() )
79     {
80       animParams.optionsFound = true;
81       animParams.alphaFunction = static_cast<AlphaFunction::BuiltinFunction>(alphaValue->ToUint32()->Value());
82     }
83
84     v8::Local<v8::Value> delayValue = obj->Get( v8::String::NewFromUtf8( isolate, "delay" ) );
85     if( delayValue->IsNumber() )
86     {
87       animParams.optionsFound = true;
88       v8::Local<v8::Number> num = delayValue->ToNumber();
89       animParams.delay = num->Value();
90     }
91
92     v8::Local<v8::Value> durationValue = obj->Get( v8::String::NewFromUtf8( isolate, "duration" ) );
93     if( durationValue->IsNumber() )
94     {
95       animParams.optionsFound = true;
96       v8::Local<v8::Number> num = durationValue->ToNumber();
97       animParams.duration = num->Value();
98     }
99
100   }
101 }
102 KeyFrames GetKeyFrames( v8::Isolate* isolate, v8::Local<v8::Value > keyFrameArray )
103 {
104   // keyframe object is an array of
105   // {
106   //   float: progress
107   //   value: property value ( position/ rotation etc)
108   //   alpha: function
109   //
110   v8::HandleScope handleScope( isolate );
111
112   if( !keyFrameArray->IsObject() || !keyFrameArray->IsArray() )
113   {
114     DALI_SCRIPT_EXCEPTION( isolate, "missing keyframe array" );
115     return KeyFrames();
116   }
117
118   KeyFrames keyframes = KeyFrames::New();
119
120   v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast( keyFrameArray );
121   for( uint32_t i=0; i < array->Length() ; ++i)
122   {
123     v8::Handle<v8::Value> arrayItem =  array->Get( i );
124
125     if(!arrayItem->IsObject() )
126     {
127       DALI_SCRIPT_EXCEPTION( isolate, "missing keyframe object" );
128       return KeyFrames();
129     }
130     v8::Handle<v8::Object> keyFrameObject = arrayItem->ToObject();
131
132     // get keyframe.progress
133     v8::Handle<v8::Value> progress = keyFrameObject->Get(v8::String::NewFromUtf8( isolate, "progress"));
134     if( !progress->IsNumber() )
135     {
136       DALI_SCRIPT_EXCEPTION( isolate, "keyframe missing progress property" );
137       return keyframes;
138     }
139
140     // get keyframe.value
141     bool found(false);
142     v8::Handle<v8::Value> propertyValue = keyFrameObject->Get(v8::String::NewFromUtf8( isolate, "value"));
143     Property::Value value =  V8Utils::GetPropertyValueFromObject( found, isolate, propertyValue );
144     if( !found )
145     {
146       DALI_SCRIPT_EXCEPTION( isolate, "keyframe missing value property" );
147       return keyframes;
148     }
149
150     // get keyframe.alpha
151     v8::Handle<v8::Value> alphaValue = keyFrameObject->Get(v8::String::NewFromUtf8( isolate, "alpha"));
152     if( alphaValue->IsUint32() )
153     {
154       AlphaFunction alphaFunction = static_cast<AlphaFunction::BuiltinFunction>(alphaValue->ToUint32()->Value());
155       keyframes.Add( progress->NumberValue(), value, alphaFunction );
156     }
157     else
158     {
159       keyframes.Add( progress->NumberValue(), value );
160     }
161
162   }
163   return keyframes;
164
165 }
166 bool GetAnimationParameters(  v8::Isolate* isolate,
167                               const v8::FunctionCallbackInfo< v8::Value >& args,
168                               AnimationParameters& animParams,
169                               AnimationApi::AnimationParameterType type)
170 {
171   // used for things like anim.AnimateBy(  myImageView, property-name,  property-value (or Javascript number array));
172   // 1 extract property handle from param1.
173   // 2 extract property name from param2  ( in the format "uColor" )
174   // 3 extract PropertyValue from param3
175   // 4 extract animation options ( delay, duration, alpha func)
176
177   // 1 extract HANDLE
178   bool foundHandle;
179   animParams.target = V8Utils::GetHandleParameter( PARAMETER_0, foundHandle, isolate, args );
180   if( !foundHandle )
181   {
182     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 0 (Handle)" );
183     return false;
184   }
185
186   // 2 extract property name
187   bool foundPropName;
188   std::string propertyName = V8Utils::GetStringParameter( PARAMETER_1, foundPropName, isolate, args );
189   if( !foundPropName )
190   {
191     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 1 ( PropertyName )" );
192     return false;
193   }
194   // try both properties with dashes and without
195   Property::Index index = animParams.target.GetPropertyIndex( propertyName );
196
197   if( index == Property::INVALID_INDEX )
198   {
199     index = animParams.target.GetPropertyIndex( propertyName );
200   }
201
202   animParams.propertyIndex = index;
203
204   if( type == AnimationApi::PROPERTY_VALUE )
205   {
206     // 3 extract property value
207     bool foundPropValue( false );
208     animParams.value = V8Utils::GetPropertyValueParameter( PARAMETER_2, foundPropValue, isolate, args );
209     if( !foundPropValue )
210     {
211       DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 2 ( value )" );
212       return false;
213     }
214   }
215   else  // type == KEYFRAMES
216   {
217     animParams.keyFrames = GetKeyFrames(isolate, args[2]);
218   }
219   // 4 extract animation options
220   GetAnimationOptions( isolate, args[3], animParams );
221
222   return true;
223 }
224
225 Animation GetAnimation( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
226 {
227   v8::HandleScope handleScope( isolate );
228
229   v8::Local<v8::Object> object = args.This();
230   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField( 0 ) );
231   void* ptr = field->Value();
232
233   AnimationWrapper* wrapper = static_cast<AnimationWrapper *>( ptr );
234   return wrapper->GetAnimation();
235 }
236
237
238 } // un-named namespace
239
240 /**
241  * Constructor
242  *
243  * @constructor
244  * @for Animation
245  * @method Animation
246  * @param {float} duration
247  *
248  */
249 Animation AnimationApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
250 {
251   v8::Isolate* isolate = args.GetIsolate();
252   v8::HandleScope handleScope( isolate );
253
254   bool found( false );
255   float value = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 1.f /* default */);
256   if( !found )
257   {
258     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
259     return Animation();
260   }
261   // get the duration
262   return Animation::New( value );
263 }
264
265 /**
266  * Set the animation duration.
267  * @method setDuration
268  * @for Animation
269  * @param {float} duration in seconds
270  *
271  */
272 void AnimationApi::SetDuration( const v8::FunctionCallbackInfo< v8::Value >& args )
273 {
274   v8::Isolate* isolate = args.GetIsolate();
275   v8::HandleScope handleScope( isolate );
276
277   Animation anim = GetAnimation( isolate, args );
278
279   bool found( false );
280   float value = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 1.f /* default */);
281   if( !found )
282   {
283     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
284     return;
285   }
286   anim.SetDuration( value );
287 }
288 /**
289  * Get the animation duration.
290  * @method getDuration
291  * @for Animation
292  * @return {float} duration in seconds
293  *
294  */
295 void AnimationApi::GetDuration( const v8::FunctionCallbackInfo< v8::Value >& args )
296 {
297   v8::Isolate* isolate = args.GetIsolate();
298   v8::HandleScope handleScope( isolate );
299
300   Animation anim = GetAnimation( isolate, args );
301
302   args.GetReturnValue().Set( v8::Number::New( isolate, anim.GetDuration() ) );
303
304 }
305 /**
306  * Set whether the animation will loop.
307  * @method setLooping
308  * @for Animation
309  * @param {bool} looping enabled
310  *
311  */
312 void AnimationApi::SetLooping( const v8::FunctionCallbackInfo< v8::Value >& args )
313 {
314   v8::Isolate* isolate = args.GetIsolate();
315   v8::HandleScope handleScope( isolate );
316
317   Animation anim = GetAnimation( isolate, args );
318
319   bool found( false );
320   bool value = V8Utils::GetBooleanParameter( PARAMETER_0, found, isolate, args );
321   if( !found )
322   {
323     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
324   }
325   else
326   {
327     anim.SetLooping( value );
328   }
329
330 }
331 /**
332  * Query whether the animation will loop.
333  * @method isLooping
334  * @for Animation
335  * @return {bool} looping enabled
336  *
337  */
338 void AnimationApi::IsLooping( const v8::FunctionCallbackInfo< v8::Value >& args )
339 {
340   v8::Isolate* isolate = args.GetIsolate();
341   v8::HandleScope handleScope( isolate );
342
343   Animation anim = GetAnimation( isolate, args );
344
345   args.GetReturnValue().Set( v8::Boolean::New( isolate, anim.IsLooping() ) );
346 }
347
348 /**
349  * Set the end action of the animation.
350  *
351  * This action is performed when the animation ends.
352  * Default end action is bake
353  * @method setEndAction
354  * @for Animation
355  * @param {integer} bake mode
356  * @example
357  *       anim.setEndAction( dali.ANIMATION_BAKE ); // When the animation ends, the animated property values are saved.
358  *       anim.setEndAction( dali.ANIMATION_DISCARD ); //  When the animation ends, the animated property values are forgotten.
359  *       anim.setEndAction( dali.ANIMATION_BAKE_FINAL ); // If the animation is stopped, the animated property values are saved as if the animation had run to completion, otherwise behaves like Bake.
360  */
361 void AnimationApi::SetEndAction( const v8::FunctionCallbackInfo< v8::Value >& args )
362 {
363   v8::Isolate* isolate = args.GetIsolate();
364   v8::HandleScope handleScope( isolate );
365
366   Animation anim = GetAnimation( isolate, args );
367
368   bool found( false );
369   int value = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
370   if( !found )
371   {
372     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
373   }
374   else
375   {
376     anim.SetEndAction( static_cast<Animation::EndAction>( value ) );
377   }
378 }
379
380 /**
381  * Returns the end action of the animation.
382
383  * @method getEndAction
384  * @for Animation
385  * @return {integer} bake mode
386  *
387  * There are 3 different bake modes
388  * @example
389  *     dali.ANIMATION_BAKE  // When the animation ends, the animated property values are saved.
390  *     dali.ANIMATION_DISCARD // When the animation ends, the animated property values are forgotten.
391  *     dali.ANIMATION_BAKE_FINAL  // If the animation is stopped, the animated property values are saved as if the animation had run to completion, otherwise behaves like Bake.
392  */
393 void AnimationApi::GetEndAction( const v8::FunctionCallbackInfo< v8::Value >& args )
394 {
395   v8::Isolate* isolate = args.GetIsolate();
396   v8::HandleScope handleScope( isolate );
397
398   Animation anim = GetAnimation( isolate, args );
399
400   args.GetReturnValue().Set( v8::Integer::New( isolate, anim.GetEndAction() ) );
401 }
402
403 /**
404  * Set the disconnect action of the animation.
405  * If any of the animated property owners are disconnected from the stage, this action is performed.
406  * Default disconnection action is BakeFinal.
407  * @method setDisconnectAction
408  * @for Animation
409  * @param {integer} end mode
410  *
411  * There are 3 different end modes
412  * @example
413  *     dali.ANIMATION_BAKE  // When the animation is destroyed, the animated property values are saved.
414  *     dali.ANIMATION_DISCARD // When the animation is destroyed, the animated property values are forgotten.
415  *     dali.ANIMATION_BAKE_FINAL  // When the animation is destroyed, the animated property values are saved as if the animation had run to completion, otherwise behaves like Bake.
416  */
417 void AnimationApi::SetDisconnectAction( const v8::FunctionCallbackInfo< v8::Value >& args )
418 {
419   v8::Isolate* isolate = args.GetIsolate();
420   v8::HandleScope handleScope( isolate );
421
422   Animation anim = GetAnimation( isolate, args );
423
424   bool found( false );
425   int value = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
426   if( !found )
427   {
428     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
429   }
430   else
431   {
432     anim.SetDisconnectAction( static_cast<Animation::EndAction>( value ) );
433   }
434 }
435
436 /**
437  * Returns the disconnect action of the animation.
438  * @method getDisconnectAction
439  * @for Animation
440  * @return {integer} end mode
441  */
442 void AnimationApi::GetDisconnectAction( const v8::FunctionCallbackInfo< v8::Value >& args )
443 {
444   v8::Isolate* isolate = args.GetIsolate();
445   v8::HandleScope handleScope( isolate );
446
447   Animation anim = GetAnimation( isolate, args );
448
449   args.GetReturnValue().Set( v8::Integer::New( isolate, anim.GetDisconnectAction() ) );
450 }
451 /**
452  * Set the default alpha function for an animation.
453  * @method setDefaultAlphaFunction
454  * @for Animation
455  * @param {Integer} alpha function
456  */
457 void AnimationApi::SetDefaultAlphaFunction( const v8::FunctionCallbackInfo< v8::Value >& args )
458 {
459   v8::Isolate* isolate = args.GetIsolate();
460   v8::HandleScope handleScope( isolate );
461
462   Animation anim = GetAnimation( isolate, args );
463
464   bool found( false );
465   int alphaFunc = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
466   if( !found )
467   {
468     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
469   }
470   else
471   {
472     AlphaFunction func = static_cast<AlphaFunction::BuiltinFunction>(alphaFunc);
473     anim.SetDefaultAlphaFunction( func );
474   }
475 }
476 /**
477  * Get the default alpha function for an animation.
478  * @method getDefaultAlphaFunction
479  * @for Animation
480  * @return {Integer} alpha function
481  */
482 void AnimationApi::GetDefaultAlphaFunction( const v8::FunctionCallbackInfo<v8::Value>& args )
483 {
484   v8::Isolate* isolate = args.GetIsolate();
485   v8::HandleScope handleScope( isolate );
486
487   Animation anim = GetAnimation( isolate, args );
488
489   AlphaFunction alphaFunc = anim.GetDefaultAlphaFunction();
490
491
492   args.GetReturnValue().Set( v8::Integer::New( isolate, alphaFunc.GetBuiltinFunction() ) );
493 }
494
495 /**
496  * Get the current progress of the animation.
497  * @method getCurrentProgress
498  * @for Animation
499  * @return {float} The current progress as a normalized value between [0..1].
500  *
501  */
502 void AnimationApi::GetCurrentProgress( const v8::FunctionCallbackInfo< v8::Value >& args )
503 {
504   v8::Isolate* isolate = args.GetIsolate();
505   v8::HandleScope handleScope( isolate );
506
507   Animation anim = GetAnimation( isolate, args );
508
509   args.GetReturnValue().Set( v8::Number::New( isolate, anim.GetCurrentProgress() ) );
510 }
511
512 /**
513  * Specifies an speed factor for the animation.
514  *
515  * The speed factor is a multiplier of the normal velocity of the animation. Values between [0,1] will
516  * slow down the animation and values above one will speed up the animation. It is also possible to specify a negative multiplier
517  * to play the animation in reverse.
518  *
519  * @method setSpeedFactor
520  * @for Animation
521  * @param {float}  value which will multiply the velocity.
522  * @example
523  *     anim.setSpeedFactor(2);
524  *     anim.play();             // plays the animation twice as fast
525  *
526  *
527  *     anim.setSpeedFactor(0.5);
528  *     anim.play();             // plays the animation half speed
529  *
530  */
531 void AnimationApi::SetSpeedFactor( const v8::FunctionCallbackInfo< v8::Value >& args )
532 {
533   v8::Isolate* isolate = args.GetIsolate();
534   v8::HandleScope handleScope( isolate );
535
536   Animation anim = GetAnimation( isolate, args );
537
538   bool found( false );
539   float speedFactor = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f );
540   if( !found )
541   {
542     DALI_SCRIPT_EXCEPTION( isolate, "float parameter missing" );
543   }
544   else
545   {
546     anim.SetSpeedFactor( speedFactor );
547   }
548
549 }
550
551 /**
552  * Retrieve the speed factor of the animation
553  *
554  *
555  * @method getSpeedFactor
556  * @for Animation
557  * @return {float} speed factor
558  */
559 void AnimationApi::GetSpeedFactor( const v8::FunctionCallbackInfo< v8::Value >& args )
560 {
561   v8::Isolate* isolate = args.GetIsolate();
562   v8::HandleScope handleScope( isolate );
563
564   Animation anim = GetAnimation( isolate, args );
565
566   args.GetReturnValue().Set( v8::Number::New( isolate, anim.GetSpeedFactor() ) );
567 }
568
569 /**
570  * Set the playing range.
571  * Animation will play between the values specified.
572  * Both values ( range.x and range.y ) should be between 0-1,
573  * otherwise they will be ignored.
574  * If the range provided is not in proper order ( minimum,maximum), it will be reordered.
575  * @method setPlayRange
576  * @for Animation
577  * @param {Object} Range
578  * @param {Float} Range.start
579  * @param {Float} Range.end
580  * @example
581  *     var range = {  start:0.1, end:0.6 };
582  *     anim.setPlayRange( range );
583  */
584 void AnimationApi::SetPlayRange( const v8::FunctionCallbackInfo< v8::Value >& args )
585 {
586   v8::Isolate* isolate = args.GetIsolate();
587   v8::HandleScope handleScope( isolate );
588
589   Animation anim = GetAnimation( isolate, args );
590   Vector2 range(0,0);
591
592   if( args.Length() != 1 )
593   {
594     DALI_SCRIPT_EXCEPTION( isolate, "missing / invalid parameters" );
595     return;
596   }
597   v8::Local<v8::Value > rangeValue = args[0];
598   if( !rangeValue->IsObject() )
599   {
600     DALI_SCRIPT_EXCEPTION( isolate, "invalid parameters" );
601     return;
602   }
603   v8::Local<v8::Object> obj = rangeValue->ToObject();
604   v8::Local<v8::Value> startValue = obj->Get( v8::String::NewFromUtf8( isolate, "start" ) );
605   v8::Local<v8::Value> endValue = obj->Get( v8::String::NewFromUtf8( isolate, "end" ) );
606
607   if( startValue->IsNumber() &&  endValue->IsNumber())
608   {
609         range.x= startValue->ToNumber()->Value();
610         range.y = endValue->ToNumber()->Value();
611   }
612   else
613   {
614     DALI_SCRIPT_EXCEPTION( isolate, "missing start/end value" );
615     return;
616   }
617
618   anim.SetPlayRange( range );
619
620 }
621
622 /**
623  * Get the playing range.
624  * @method getPlayRange
625  * @for Animation
626  * @return {Object} Range with { start: ,  end: } properties.
627  */
628 void AnimationApi::GetPlayRange( const v8::FunctionCallbackInfo< v8::Value >& args )
629 {
630   v8::Isolate* isolate = args.GetIsolate();
631   v8::EscapableHandleScope handleScope( isolate );
632   Animation anim = GetAnimation( isolate, args );
633
634
635   v8::Local<v8::Object> rangeObject = v8::Object::New( isolate );
636
637   Vector2 range = anim.GetPlayRange();
638
639  // set device id
640   rangeObject->Set( v8::String::NewFromUtf8( isolate, "start"), v8::Number::New( isolate,range.x ));
641
642    // set state
643   rangeObject->Set( v8::String::NewFromUtf8( isolate, "end"), v8::Number::New( isolate,range.y ));
644
645   args.GetReturnValue().Set( rangeObject );
646 }
647
648
649 /**
650  * Sets the progress of the animation.
651  * The animation will play (or continue playing) from this point. The progress
652  * must be in the 0-1 interval or in the play range interval if defined ( See SetPlayRange ),
653  * otherwise, it will be ignored.
654  *
655  * @method setCurrentProgress
656  * @for Animation
657  * @param {float}  progress The new progress as a normalized value between [0,1] or between the
658  * play range if specified.
659  */
660 void AnimationApi::SetCurrentProgress( const v8::FunctionCallbackInfo< v8::Value >& args )
661 {
662   v8::Isolate* isolate = args.GetIsolate();
663   v8::HandleScope handleScope( isolate );
664
665   Animation anim = GetAnimation( isolate, args );
666
667   bool found( false );
668
669   float progress = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f );
670   if( !found )
671   {
672     DALI_SCRIPT_EXCEPTION( isolate, "float parameter missing" );
673     return;
674   }
675   anim.SetCurrentProgress( progress );
676
677 }
678
679 /**
680  *
681  *  Play the animation from a given point.
682  * The progress must be in the 0-1 interval or in the play range interval if defined ( See SetPlayRange ),
683  * otherwise, it will be ignored.
684  * @method playFrom
685  * @for Animation
686  * @param {float} progress A value between [0,1], or between the play range if specified, form where the animation should start playing
687  */
688 void AnimationApi::PlayFrom( const v8::FunctionCallbackInfo< v8::Value >& args )
689 {
690   v8::Isolate* isolate = args.GetIsolate();
691   v8::HandleScope handleScope( isolate );
692
693   Animation anim = GetAnimation( isolate, args );
694
695   bool found( false );
696
697   float progress = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f );
698   if( !found )
699   {
700     DALI_SCRIPT_EXCEPTION( isolate, "float parameter missing" );
701     return;
702   }
703   anim.PlayFrom( progress );
704
705 }
706
707 /**
708  * Play the animation
709  * @method play
710  * @for Animation
711  */
712 void AnimationApi::Play( const v8::FunctionCallbackInfo<v8::Value>& args )
713 {
714   v8::Isolate* isolate = args.GetIsolate();
715   v8::HandleScope handleScope( isolate );
716
717   Animation anim = GetAnimation( isolate, args );
718   anim.Play();
719 }
720 /**
721  * Pause the animation
722  * @method pause
723  * @for Animation
724  */
725 void AnimationApi::Pause( const v8::FunctionCallbackInfo<v8::Value>& args )
726 {
727   v8::Isolate* isolate = args.GetIsolate();
728   v8::HandleScope handleScope( isolate );
729
730   Animation anim = GetAnimation( isolate, args );
731   anim.Pause();
732 }
733 /**
734  * Stop the animation
735  * @method stop
736  * @for Animation
737  */
738 void AnimationApi::Stop( const v8::FunctionCallbackInfo<v8::Value>& args )
739 {
740   v8::Isolate* isolate = args.GetIsolate();
741   v8::HandleScope handleScope( isolate );
742
743   Animation anim = GetAnimation( isolate, args );
744   anim.Stop();
745 }
746 /**
747  * Clear the animation
748  * This disconnects any objects that were being animated, effectively stopping the animation.
749  * @method clear
750  * @for Animation
751  */
752 void AnimationApi::Clear( const v8::FunctionCallbackInfo<v8::Value>& args )
753 {
754   v8::Isolate* isolate = args.GetIsolate();
755   v8::HandleScope handleScope( isolate );
756
757   Animation anim = GetAnimation( isolate, args );
758   anim.Clear();
759 }
760
761
762 void AnimationApi::Animate( const v8::FunctionCallbackInfo<v8::Value>& args )
763 {
764   v8::Isolate* isolate = args.GetIsolate();
765   v8::HandleScope handleScope( isolate );
766
767   Animation anim = GetAnimation( isolate, args );
768   AnimationParameters animParams( anim );
769   bool found(false);
770
771   //Get actor
772   Dali::Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
773   if( !found )
774   {
775     DALI_SCRIPT_EXCEPTION( isolate, "Missing actor parameter");
776     return;
777   }
778
779   //Get path
780   Dali::Path path;
781   BaseWrappedObject* wrapper = V8Utils::GetWrappedDaliObjectParameter( PARAMETER_1, BaseWrappedObject::PATH, isolate, args);
782   PathWrapper* pathWrapper = static_cast< PathWrapper*>( wrapper );
783   if( !pathWrapper )
784   {
785     return;
786   }
787   path = pathWrapper->GetPath();
788
789   //Get forward vector
790   bool bFound(false);
791   Vector3 forward = V8Utils::GetVector3Parameter( PARAMETER_2, bFound, isolate, args );
792   if( !bFound )
793   {
794     return;
795   }
796
797   //Get animation options
798   GetAnimationOptions( isolate, args[3], animParams );
799   if( animParams.optionsFound )
800   {
801     anim.Animate( actor, path, forward, animParams.alphaFunction, TimePeriod( animParams.delay, animParams.duration) );
802   }
803   else
804   {
805     anim.Animate( actor, path, forward );
806   }
807 }
808
809 /**
810  *
811  * Animate a property value by a relative amount.
812  *
813  * The effect will start & end when the animation begins & ends.
814  * @method animateBy
815  * @for Animation
816  * @param {Object} target object that contains a property to be animated (e.g. myActor )
817  * @param {String} property name (e.g. "position" )
818  * @param {Object} relativeValue The property value will change by this amount.
819  * @param {Object} [options] Animation options.
820  * @param {Float} [options.delay] amount to delay the start of the animation in seconds
821  * @param {Float} [options.duration] duration of the animation
822  * @param {String} [options.alpha] Animation alpha function (e.g. "linear")
823  *
824  * @example
825  *
826  *     // animation x position
827  *     var anim = new dali.Animation( 1 );
828  *     anim.animateBy( imageActor,"positionX", 30 );
829  *     anim.play();
830  *
831  *     // animate x,y,z position with the optional animation options
832  *     var options = {
833  *        delay: 3,     // 3 second delay before starting
834  *        duration: 5,  // 5 second duration
835  *        alpha:"easeInOutSine"   // Speeds up and slows to a gradual stop
836  *     }
837  *
838  *     anim.animateBy( imageActor,"position", [100,200,0], options );
839  *
840  */
841 void AnimationApi::AnimateBy( const v8::FunctionCallbackInfo<v8::Value>& args )
842 {
843   v8::Isolate* isolate = args.GetIsolate();
844   v8::HandleScope handleScope( isolate );
845
846   Animation anim = GetAnimation( isolate, args );
847   AnimationParameters animParams( anim );
848
849   // grab all the parameters
850   bool ok = GetAnimationParameters( isolate, args, animParams, AnimationApi::PROPERTY_VALUE);
851   if( !ok )
852   {
853     // GetAnimationParameters will log
854     return;
855   }
856
857   if( animParams.optionsFound )
858   {
859     anim.AnimateBy( Property( animParams.target, animParams.propertyIndex ),
860                               animParams.value,
861                               animParams.alphaFunction,
862                               TimePeriod( animParams.delay, animParams.duration) );
863   }
864   else
865   {
866     anim.AnimateBy( Property( animParams.target, animParams.propertyIndex ), animParams.value );
867   }
868
869 }
870
871 /**
872  *
873  * Animate a property to a destination value.
874  *
875  * The effect will start & end when the animation begins & ends.
876  * @method animateTo
877  * @for Animation
878  * @param {Object} target object that contains a property to be animated (e.g. myActor )
879  * @param {String} property name (e.g. "position" )
880  * @param {Object} destinationValue The property value will changed to this value
881  * @param {Object} [options] Animation options.
882  * @param {Float} [options.delay] amount to delay the start of the animation in seconds
883  * @param {Float} [options.duration] duration of the animation
884  * @param {String} [options.alpha] Animation alpha function (e.g. "linear")
885  *
886  * @example
887  *
888  *     var anim = new dali.Animation( 1 );
889  *     anim.animateTo( imageActor,"positionX", 30 );
890  *     anim.play();
891  *
892  *
893  *     // with the optional animation options object
894  *     var options = {
895  *        delay: 3,     // 3 second delay before starting
896  *        duration: 5,  // 5 second duration
897  *        alpha:"easeInOutSine"   // Speeds up and slows to a gradual stop
898  *     }
899  *
900  *     anim.animateTo( imageActor,"position", [100,200,0], options );
901  *
902  */
903 void AnimationApi::AnimateTo( const v8::FunctionCallbackInfo< v8::Value >& args )
904 {
905   v8::Isolate* isolate = args.GetIsolate();
906   v8::HandleScope handleScope( isolate );
907
908   Animation anim = GetAnimation( isolate, args );
909   AnimationParameters animParams( anim );
910
911   // grab all the parameters
912   bool ok = GetAnimationParameters( isolate, args, animParams, AnimationApi::PROPERTY_VALUE );
913   if( !ok )
914   {
915     // GetAnimationParameters will log
916     return;
917   }
918
919   if( animParams.optionsFound )
920   {
921
922     anim.AnimateTo( Property( animParams.target,animParams.propertyIndex ),
923                                   animParams.value,
924                                   animParams.alphaFunction,
925                                   TimePeriod( animParams.delay, animParams.duration) );
926   }
927   else
928   {
929     anim.AnimateTo( Property( animParams.target, animParams.propertyIndex ), animParams.value );
930   }
931 }
932
933 /**
934  *
935  * Animate a property between keyframes.
936  *
937  * The effect will start & end when the animation begins & ends.
938  * @method animateBetween
939  * @for Animation
940  * @param {Object} target object that contains a property to be animated (e.g. myActor )
941  * @param {String} property name (e.g. "position" )
942  * @param {Object} keyframes array of keyframe objects
943  * @param {Object} [options] Animation options.
944  * @param {Float} [options.delay] amount to delay the start of the animation in seconds
945  * @param {Float} [options.duration] duration of the animation
946  * @param {String} [options.alpha] Animation alpha function (e.g. "linear")
947  *
948  *
949  * @example
950  *
951  *  create some keyframes to move an actor around a square, and return to the start
952  * </br >
953  *  <img src="../assets/img/animation/keyframe-animation.png">
954  *
955  *
956  *     var keyframes = [
957  *     {
958  *       progress:0.0,
959  *       value: [0,0,0]
960  *     },
961  *     {
962  *       progress:0.25,
963  *       value: [500,0,0]
964  *     },
965  *
966  *     {
967  *       progress:0.5,
968  *       value: [500,500,0]
969  *     },
970  *     {
971  *       progress:0.75,
972  *       value: [0,500,0]
973  *     },
974  *     {
975  *       progress:1.0,
976  *       value: [0,0,0]
977  *     } ];
978  *
979  *
980  *     anim.animateBetween( imageActor,"position", keyframes );
981  *
982  */
983 void AnimationApi::AnimateBetween( const v8::FunctionCallbackInfo< v8::Value >& args )
984 {
985   v8::Isolate* isolate = args.GetIsolate();
986   v8::HandleScope handleScope( isolate );
987
988   Animation anim = GetAnimation( isolate, args );
989   AnimationParameters animParams( anim );
990
991   // grab all the parameters
992   bool ok = GetAnimationParameters( isolate, args, animParams, AnimationApi::KEYFRAMES );
993   if( !ok )
994   {
995     // GetAnimationParameters will log
996     return;
997   }
998
999   // if animation options exist...
1000   if( animParams.optionsFound )
1001   {
1002
1003     anim.AnimateBetween( Property( animParams.target,animParams.propertyIndex ),
1004                                   animParams.keyFrames,
1005                                   animParams.alphaFunction,
1006                                   TimePeriod( animParams.delay, animParams.duration) );
1007   }
1008   else
1009   {
1010     anim.AnimateBetween( Property( animParams.target, animParams.propertyIndex ), animParams.keyFrames );
1011   }
1012 }
1013
1014 /**
1015  * show an actor during the animation.
1016  *
1017  * This is a helper, which simulates animating the visibility property of an actor
1018  * with zero duration ( it is just a boolean).
1019  * e.g. it performs  anim.AnimateTo( actor,"visible",true, { delay:delay: duration:0 } );
1020  * @method show
1021  * @for Animation
1022  * @param {Object} Actor
1023  * @param {float} delay until the actor is shown
1024  */
1025 void AnimationApi::Show( const v8::FunctionCallbackInfo< v8::Value >& args )
1026 {
1027   v8::Isolate* isolate = args.GetIsolate();
1028   v8::HandleScope handleScope( isolate );
1029   Animation anim = GetAnimation( isolate, args );
1030   bool found( false );
1031
1032   Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
1033   if( !found)
1034   {
1035     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 0 actor" );
1036     return;
1037   }
1038   // get the duration
1039   float delay = V8Utils::GetFloatParameter( PARAMETER_1, found, isolate, args, 1.f /* default */);
1040   if( !found )
1041   {
1042     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 1 delay" );
1043     return;
1044   }
1045   anim.Show( actor, delay );
1046
1047 }
1048
1049 /**
1050  * hide an actor during the animation.
1051  *
1052  * This is a helper, which simulates animating the visibility property of an actor
1053  * with zero duration ( it is just a boolean).
1054  * e.g. it performs  anim.AnimateTo( actor,"visible",false, { delay:delay: duration:0 } );
1055  * @method hide
1056  * @for Animation
1057  * @param {Object} Actor
1058  * @param {float} delay until the actor is hidden
1059  */
1060 void AnimationApi::Hide( const v8::FunctionCallbackInfo< v8::Value >& args )
1061 {
1062   v8::Isolate* isolate = args.GetIsolate();
1063   v8::HandleScope handleScope( isolate );
1064   Animation anim = GetAnimation( isolate, args );
1065   bool found( false );
1066   Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
1067   if( !found )
1068   {
1069     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 0 actor" );
1070     return;
1071   }
1072
1073   // get the duration
1074   float delay = V8Utils::GetFloatParameter( PARAMETER_1, found, isolate, args, 1.f /* default */);
1075   if( !found )
1076   {
1077     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 1 delay" );
1078     return;
1079   }
1080   anim.Hide( actor, delay );
1081 }
1082
1083
1084
1085
1086 } // namespace V8Plugin
1087
1088 } // namespace Dali