2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <animation/path-wrapper.h>
23 #include <object/property-value-wrapper.h>
32 namespace // un named namespace
35 Path GetPath( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
37 v8::HandleScope handleScope( isolate );
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();
43 PathWrapper* wrapper = static_cast<PathWrapper *>( ptr );
44 return wrapper->GetPath();
47 } // un-named namespace
51 * Automatic generation of control points. Generated control points which result in a smooth join between the splines of each segment.
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
59 * @method generateControlPoints
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.
67 void PathApi::GenerateControlPoints( const v8::FunctionCallbackInfo< v8::Value >& args )
69 v8::Isolate* isolate = args.GetIsolate();
70 v8::HandleScope handleScope( isolate );
73 float curvature = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 1.0f /* default */);
76 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
80 Path path = GetPath( isolate, args );
81 path.GenerateControlPoints( curvature );
86 * Add a interpolation point.
89 * @param {Vector3} interpolationPoint The new interpolation point to be added
91 * path.addPoint([ 10,40,0] );
93 void PathApi::AddPoint( const v8::FunctionCallbackInfo< v8::Value >& args )
95 v8::Isolate* isolate = args.GetIsolate();
96 v8::HandleScope handleScope( isolate );
99 Vector3 value = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
102 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
106 Path path = GetPath( isolate, args );
107 path.AddPoint( value );
112 * Add a control point.
113 * @method addControlPoint
115 * @param {Vector3} controlPoint The new control point to be added
117 * path.addControlPoint([ 10,40,0] );
119 void PathApi::AddControlPoint( const v8::FunctionCallbackInfo< v8::Value >& args )
121 v8::Isolate* isolate = args.GetIsolate();
122 v8::HandleScope handleScope( isolate );
125 Vector3 value = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
128 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
132 Path path = GetPath( isolate, args );
133 path.AddControlPoint( value );
137 * Sample path at a given progress. Calculates position and tangent at that point of the curve
140 * @param {float} Progress A floating point value between 0.0 and 1.0.
141 * @return {Object} { position: Vector3, tangent:Vector3 }
143 void PathApi::Sample( const v8::FunctionCallbackInfo< v8::Value >& args )
145 v8::Isolate* isolate = args.GetIsolate();
146 v8::HandleScope handleScope( isolate );
149 float progress = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.0f /* default */);
152 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter" );
157 Path path = GetPath( isolate, args );
158 Vector3 position, tangent;
159 path.Sample( progress, position, tangent );
161 //Create v8 object with the result
162 v8::Local<v8::Object> sampleObject = v8::Object::New( isolate );
164 Dali::Property::Value valuePosition( position );
165 sampleObject->Set( v8::String::NewFromUtf8( isolate, "position"), PropertyValueWrapper::WrapDaliProperty( isolate, valuePosition));
167 Dali::Property::Value valueTangent( tangent );
168 sampleObject->Set( v8::String::NewFromUtf8( isolate, "tangent"), PropertyValueWrapper::WrapDaliProperty( isolate, valueTangent));
170 args.GetReturnValue().Set( sampleObject );
173 } // namespace V8Plugin