72434d971996f83cdac0b4acd6faef1499ed4a61
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / animation / path-constraint-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 "path-constraint-wrapper.h"
20
21 // INTERNAL INCLUDES
22 #include <v8-utils.h>
23 #include <dali-wrapper.h>
24 #include <shared/object-template-helper.h>
25
26 namespace Dali
27 {
28
29 namespace V8Plugin
30 {
31
32 PathConstraintWrapper::PathConstraintWrapper( PathConstraint pathConstraint, GarbageCollectorInterface& gc )
33 :HandleWrapper( BaseWrappedObject::PATH_CONSTRAINT, pathConstraint, gc ),
34  mPathConstraint( pathConstraint )
35 {
36 }
37
38 v8::Handle<v8::ObjectTemplate> PathConstraintWrapper::MakePathConstraintTemplate( v8::Isolate* isolate )
39 {
40   v8::EscapableHandleScope handleScope( isolate );
41
42   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
43   objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
44
45   // property handle intercepts property getters and setters and signals
46   HandleWrapper::AddInterceptsToTemplate( isolate, objTemplate );
47
48   return handleScope.Escape( objTemplate );
49 }
50
51 v8::Handle<v8::Object> PathConstraintWrapper::WrapPathConstraint( v8::Isolate* isolate, PathConstraint pathConstraint )
52 {
53   v8::EscapableHandleScope handleScope( isolate );
54   v8::Local<v8::ObjectTemplate> objectTemplate;
55
56   objectTemplate = MakePathConstraintTemplate( isolate );
57
58   // create an instance of the template
59   v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
60
61   // create the pathconstraint object
62   PathConstraintWrapper* pointer = new PathConstraintWrapper( pathConstraint, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
63
64   // assign the JavaScript object to the wrapper.
65   // This also stores Dali object, in an internal field inside the JavaScript object.
66   pointer->SetJavascriptObject( isolate, localObject );
67
68   return handleScope.Escape( localObject );
69 }
70
71 PathConstraint PathConstraintWrapper::GetPathConstraint()
72 {
73   return mPathConstraint;
74 }
75
76 /**
77  * Create an initialized PathConstraint handle.
78  * @constructor
79  * @for Path
80  * @method Path
81  */
82 void PathConstraintWrapper::NewPathConstraint( const v8::FunctionCallbackInfo< v8::Value >& args)
83 {
84   v8::Isolate* isolate = args.GetIsolate();
85   v8::HandleScope handleScope( isolate );
86
87   if( !args.IsConstructCall() )
88   {
89     DALI_SCRIPT_EXCEPTION( isolate, "constructor called without 'new" );
90     return;
91   }
92
93   //Extract Path Handle
94   bool parameterFound;
95   Handle pathHandle = V8Utils::GetHandleParameter( PARAMETER_0, parameterFound, isolate, args );
96   if( !parameterFound )
97   {
98     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 0 (Path)" );
99     return;
100   }
101   Dali::Path path = Path::DownCast(pathHandle);
102
103   //Extract range
104   Vector2 range = V8Utils::GetVector2Parameter( PARAMETER_1, parameterFound, isolate, args );
105   if( !parameterFound )
106   {
107     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 1 (Range)" );
108     return;
109   }
110
111   PathConstraint pathConstraint = PathConstraint::New(path, range );
112   v8::Local<v8::Object> localObject = WrapPathConstraint( isolate, pathConstraint );
113   args.GetReturnValue().Set( localObject );
114 }
115
116
117 } // namespace V8Plugin
118
119 } // namespace Dali