[dali_1.4.26] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / animation / animation-wrapper.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-wrapper.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <dali-wrapper.h>
24 #include <animation/animation-api.h>
25 #include <shared/api-function.h>
26 #include <shared/object-template-helper.h>
27
28 namespace Dali
29 {
30 namespace V8Plugin
31 {
32
33 v8::Persistent<v8::ObjectTemplate> AnimationWrapper::mAnimationTemplate;
34
35 namespace
36 {
37
38 /**
39  * Contains a list of all functions that can be called
40  */
41 const ApiFunction AnimationFunctionTable[]=
42 {
43     /**************************************
44     * Animation API (in order of animation.h)
45     **************************************/
46
47     { "SetDuration"             , AnimationApi::SetDuration               },
48     { "GetDuration"             , AnimationApi::GetDuration               },
49     { "SetLooping"              , AnimationApi::SetLooping                },
50     { "IsLooping"               , AnimationApi::IsLooping                 },
51     { "SetEndAction"            , AnimationApi::SetEndAction              },
52     { "GetEndAction"            , AnimationApi::GetEndAction              },
53     { "SetDisconnectAction"     , AnimationApi::SetDisconnectAction       },
54     { "GetDisconnectAction"     , AnimationApi::GetDisconnectAction       },
55     { "SetDefaultAlphaFunction" , AnimationApi::SetDefaultAlphaFunction   },
56     { "GetDefaultAlphaFunction" , AnimationApi::GetDefaultAlphaFunction   },
57     { "GetCurrentProgress"      , AnimationApi::GetCurrentProgress        },
58     { "SetSpeedFactor"          , AnimationApi::SetSpeedFactor            },
59     { "GetSpeedFactor"          , AnimationApi::GetSpeedFactor            },
60     { "SetPlayRange"            , AnimationApi::SetPlayRange              },
61     { "GetPlayRange"            , AnimationApi::GetPlayRange              },
62     { "SetCurrentProgress"      , AnimationApi::SetCurrentProgress        },
63     { "Play"                    , AnimationApi::Play                      },
64     { "PlayFrom"                , AnimationApi::PlayFrom                  },
65     { "Pause"                   , AnimationApi::Pause                     },
66     { "Stop"                    , AnimationApi::Stop                      },
67     { "Clear"                   , AnimationApi::Clear                     },
68     { "Animate"                 , AnimationApi::Animate                   },
69     { "AnimateBy"               , AnimationApi::AnimateBy                 },
70     { "AnimateTo"               , AnimationApi::AnimateTo                 },
71     { "AnimateBetween"          , AnimationApi::AnimateBetween            },
72     { "Show"                    , AnimationApi::Show                      },
73     { "Hide"                    , AnimationApi::Hide                      },
74
75 };
76
77 const unsigned int AnimationFunctionTableCount = sizeof(AnimationFunctionTable)/sizeof(AnimationFunctionTable[0]);
78 } //un-named space
79
80
81 AnimationWrapper::AnimationWrapper( const Dali::Animation& animation, GarbageCollectorInterface& gc )
82 : BaseWrappedObject( BaseWrappedObject::ANIMATION , gc )
83 {
84     mAnimation = animation;
85 }
86
87 AnimationWrapper::~AnimationWrapper()
88 {
89
90 }
91
92 v8::Handle<v8::Object> AnimationWrapper::WrapAnimation(v8::Isolate* isolate, const Dali::Animation& animation )
93 {
94   v8::EscapableHandleScope handleScope( isolate );
95   v8::Local<v8::ObjectTemplate> objectTemplate;
96
97   objectTemplate = GetAnimationTemplate( isolate);
98
99   // create an instance of the template
100   v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
101
102   // create the Animation wrapper
103   AnimationWrapper* pointer =  new AnimationWrapper( animation, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
104
105   // assign the JavaScript object to the wrapper.
106   pointer->SetJavascriptObject( isolate, localObject );
107
108   return handleScope.Escape( localObject );
109 }
110
111 v8::Local<v8::ObjectTemplate> AnimationWrapper::GetAnimationTemplate( v8::Isolate* isolate)
112 {
113   v8::EscapableHandleScope handleScope( isolate );
114   v8::Local<v8::ObjectTemplate> objectTemplate;
115
116   if( mAnimationTemplate.IsEmpty() )
117   {
118     objectTemplate = MakeAnimationTemplate( isolate );
119     mAnimationTemplate.Reset( isolate, objectTemplate );
120   }
121   else
122   {
123     // get the object template
124     objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, mAnimationTemplate );
125   }
126   return handleScope.Escape( objectTemplate );
127 }
128
129 v8::Handle<v8::ObjectTemplate> AnimationWrapper::MakeAnimationTemplate( v8::Isolate* isolate )
130 {
131   v8::EscapableHandleScope handleScope( isolate );
132
133   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
134
135   // add intercepts for Signals, we can't use HandleWrapper::AddIntercepts because Animation doesn't inherit
136   // from Handle ( just baseHandle)
137   ObjectTemplateHelper::AddSignalConnectAndDisconnect( isolate, objTemplate );
138
139   objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
140
141   // add our function properties
142   ObjectTemplateHelper::InstallFunctions( isolate, objTemplate, AnimationFunctionTable, AnimationFunctionTableCount );
143
144   return handleScope.Escape( objTemplate );
145 }
146
147 void AnimationWrapper::NewAnimation( const v8::FunctionCallbackInfo< v8::Value >& args)
148 {
149   v8::Isolate* isolate = args.GetIsolate();
150   v8::HandleScope handleScope( isolate);
151
152   if( !args.IsConstructCall() )
153   {
154     DALI_SCRIPT_EXCEPTION( isolate, "Animation constructor called without 'new'" );
155     return;
156   }
157
158   Dali::Animation animation = AnimationApi::New( args );
159   v8::Local<v8::Object> localObject = WrapAnimation( isolate, animation );
160   args.GetReturnValue().Set( localObject );
161 }
162
163
164 Animation AnimationWrapper::GetAnimation()
165 {
166   return mAnimation;
167 }
168
169
170 } // namespace V8Plugin
171
172 } // namespace Dali