[dali_1.4.26] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / plugins / dali-script-v8 / src / animation / path-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 "path-api.h"
20
21 // INTERNAL INCLUDES
22 #include <animation/path-wrapper.h>
23 #include <object/property-value-wrapper.h>
24 #include <v8-utils.h>
25
26 namespace Dali
27 {
28
29 namespace V8Plugin
30 {
31
32 namespace // un named namespace
33 {
34
35 Path GetPath( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
36 {
37   v8::HandleScope handleScope( isolate );
38
39   v8::Local<v8::Object> object = args.This();
40   v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField( 0 ) );
41   void* ptr = field->Value();
42
43   PathWrapper* wrapper = static_cast<PathWrapper *>( ptr );
44   return wrapper->GetPath();
45 }
46
47 } // un-named namespace
48
49
50 /**
51  * Automatic generation of control points. Generated control points which result in a smooth join between the splines of each segment.
52  *
53  * The generating algorithm is as follows:
54  * For a given knot point K[N], find the vector that bisects K[N-1],[N] and [N],[N+1].
55  * Calculate the tangent vector by taking the normal of this bisector.
56  * The in control point is the length of the preceding segment back along this bisector multiplied by the curvature
57  * The out control point is the length of the succeeding segment forward along this bisector multiplied by the curvature
58  *
59  * @method generateControlPoints
60  * @for Path
61  * @param {float} curvature curvature The curvature of the spline. 0 gives straight lines between the knots,
62  * negative values means the spline contains loops, positive values up to
63  * 0.5 result in a smooth curve, positive values between 0.5 and 1 result
64  * in looped curves where the loops are not distinct (i.e. the curve appears
65  * to be non-continuous), positive values higher than 1 result in looped curves.
66  */
67 void PathApi::GenerateControlPoints( const v8::FunctionCallbackInfo< v8::Value >& args )
68 {
69   v8::Isolate* isolate = args.GetIsolate();
70   v8::HandleScope handleScope( isolate );
71
72   bool found( false );
73   float curvature = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 1.0f /* default */);
74   if( !found )
75   {
76     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
77     return;
78   }
79
80   Path path = GetPath( isolate, args );
81   path.GenerateControlPoints( curvature );
82 }
83
84
85 /**
86  * Add a interpolation point.
87  * @method addPoint
88  * @for Path
89  * @param {Vector3} interpolationPoint The new interpolation point to be added
90  * @example
91  *        path.addPoint([ 10,40,0] );
92  */
93 void PathApi::AddPoint( const v8::FunctionCallbackInfo< v8::Value >& args )
94 {
95   v8::Isolate* isolate = args.GetIsolate();
96   v8::HandleScope handleScope( isolate );
97
98   bool found( false );
99   Vector3 value = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
100   if( !found )
101   {
102     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
103     return;
104   }
105
106   Path path = GetPath( isolate, args );
107   path.AddPoint( value );
108 }
109
110
111 /**
112  * Add a control point.
113  * @method addControlPoint
114  * @for Path
115  * @param {Vector3} controlPoint The new control point to be added
116  * @example
117  *        path.addControlPoint([ 10,40,0] );
118  */
119 void PathApi::AddControlPoint( const v8::FunctionCallbackInfo< v8::Value >& args )
120 {
121   v8::Isolate* isolate = args.GetIsolate();
122   v8::HandleScope handleScope( isolate );
123
124   bool found( false );
125   Vector3 value = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
126   if( !found )
127   {
128     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
129     return;
130   }
131
132   Path path = GetPath( isolate, args );
133   path.AddControlPoint( value );
134 }
135
136 /**
137  * Sample path at a given progress. Calculates position and tangent at that point of the curve
138  * @method sample
139  * @for Path
140  * @param {float} Progress  A floating point value between 0.0 and 1.0.
141  * @return {Object} { position: Vector3, tangent:Vector3 }
142  */
143 void PathApi::Sample( const v8::FunctionCallbackInfo< v8::Value >& args )
144 {
145   v8::Isolate* isolate = args.GetIsolate();
146   v8::HandleScope handleScope( isolate );
147
148   bool found( false );
149   float progress = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.0f /* default */);
150   if( !found )
151   {
152     DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
153     return;
154   }
155
156   //Sample the path
157   Path path = GetPath( isolate, args );
158   Vector3 position, tangent;
159   path.Sample( progress, position, tangent );
160
161   //Create v8 object with the result
162   v8::Local<v8::Object> sampleObject = v8::Object::New( isolate );
163
164   Dali::Property::Value valuePosition( position );
165   sampleObject->Set( v8::String::NewFromUtf8( isolate, "position"), PropertyValueWrapper::WrapDaliProperty( isolate, valuePosition));
166
167   Dali::Property::Value valueTangent( tangent );
168   sampleObject->Set( v8::String::NewFromUtf8( isolate, "tangent"), PropertyValueWrapper::WrapDaliProperty( isolate, valueTangent));
169
170   args.GetReturnValue().Set( sampleObject );
171 }
172
173 } // namespace V8Plugin
174
175 } // namespace Dali