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.
19 #include "constrainer-api.h"
22 #include <cfloat> //For FLT_MAX
25 #include <animation/linear-constrainer-wrapper.h>
26 #include <animation/path-constrainer-wrapper.h>
27 #include <object/property-value-wrapper.h>
36 namespace // un named namespace
39 PathConstrainer GetPathConstrainer( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
41 v8::HandleScope handleScope( isolate );
43 v8::Local<v8::Object> object = args.This();
44 v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField( 0 ) );
45 void* ptr = field->Value();
47 PathConstrainerWrapper* wrapper = static_cast<PathConstrainerWrapper *>( ptr );
48 return wrapper->GetPathConstrainer();
51 LinearConstrainer GetLinearConstrainer( v8::Isolate* isolate, const v8::FunctionCallbackInfo< v8::Value >& args )
53 v8::HandleScope handleScope( isolate );
55 v8::Local<v8::Object> object = args.This();
56 v8::Local<v8::External> field = v8::Local<v8::External>::Cast( object->GetInternalField( 0 ) );
57 void* ptr = field->Value();
59 LinearConstrainerWrapper* wrapper = static_cast<LinearConstrainerWrapper *>( ptr );
60 return wrapper->GetLinearConstrainer();
63 bool GetApplyParameters( const v8::FunctionCallbackInfo< v8::Value >& args, Actor& targetActor, Property::Index& targetPropertyIndex,
64 Actor& sourceActor, Property::Index& sourcePropertyIndex, Vector2& range, Vector2& wrap )
66 v8::Isolate* isolate = args.GetIsolate();
67 v8::HandleScope handleScope( isolate );
69 if( args[0]->IsObject() )
73 v8::Local<v8::Object> obj = args[0]->ToObject();
74 v8::Local<v8::Value> member = obj->Get( v8::String::NewFromUtf8( isolate, "target" ) );
77 if( member->IsObject() )
79 v8::Local<v8::Object> targetActorObject = member->ToObject();
80 targetActor = V8Utils::GetActorFromObject( isolate, found, targetActorObject );
83 DALI_SCRIPT_EXCEPTION( isolate, "Target actor not found" );
89 DALI_SCRIPT_EXCEPTION( isolate, "Target actor not specified" );
94 member = obj->Get( v8::String::NewFromUtf8( isolate, "source" ) );
95 if( member->IsObject() )
97 v8::Local<v8::Object> sourceActorObject = member->ToObject();
98 sourceActor = V8Utils::GetActorFromObject( isolate, found, sourceActorObject );
101 DALI_SCRIPT_EXCEPTION( isolate, "Source actor not found" );
107 DALI_SCRIPT_EXCEPTION( isolate, "Source actor not specified" );
111 //Get target property
112 member = obj->Get( v8::String::NewFromUtf8( isolate, "targetProperty" ) );
113 if( member->IsString() )
115 std::string propertyName = V8Utils::v8StringToStdString( member );
116 targetPropertyIndex = targetActor.GetPropertyIndex( propertyName );
117 if( targetPropertyIndex == Property::INVALID_INDEX )
119 targetPropertyIndex = targetActor.GetPropertyIndex( propertyName );
121 if( targetPropertyIndex == Property::INVALID_INDEX )
123 DALI_SCRIPT_EXCEPTION( isolate, "Target property not found" );
130 DALI_SCRIPT_EXCEPTION( isolate, "Target property not specified" );
134 //Get source property
135 member = obj->Get( v8::String::NewFromUtf8( isolate, "sourceProperty" ) );
136 if( member->IsString() )
138 std::string propertyName = V8Utils::v8StringToStdString( member );
139 sourcePropertyIndex = targetActor.GetPropertyIndex( propertyName );
140 if( sourcePropertyIndex == Property::INVALID_INDEX )
142 sourcePropertyIndex = targetActor.GetPropertyIndex( propertyName );
144 if( sourcePropertyIndex == Property::INVALID_INDEX )
146 DALI_SCRIPT_EXCEPTION( isolate, "Source property not found" );
153 DALI_SCRIPT_EXCEPTION( isolate, "Source property not specified" );
158 member = obj->Get( v8::String::NewFromUtf8( isolate, "range" ) );
159 if( member->IsObject() )
161 v8::Local<v8::Object> rangeObject = member->ToObject();
162 Property::Value value = V8Utils::GetPropertyValueFromObject( found, isolate, rangeObject );
167 DALI_SCRIPT_EXCEPTION( isolate, "Range not specified" );
172 member = obj->Get( v8::String::NewFromUtf8( isolate, "wrap" ) );
173 if( member->IsObject() )
175 v8::Local<v8::Object> wrapObject = member->ToObject();
176 Property::Value value = V8Utils::GetPropertyValueFromObject( found, isolate, wrapObject );
181 wrap = Vector2(-FLT_MAX, FLT_MAX);
188 DALI_SCRIPT_EXCEPTION( isolate, "Bad parameter (Object)" );
193 } // un-named namespace
196 * Apply the constraint
197 * @method applyConstraint
198 * @for PathConstrainer and LinearConstrainer
199 * @param {Object} Constraint
200 * @param {Object} Constraint.targetActor
201 * @param {String} Constraint.targetProperty
202 * @param {String} Constraint.sourceProperty
203 * @param {Vector2} Constraint.range
204 * @param {Vector2} Constraint.wrap
208 * var constraintPosition = { "target":targetActor,
209 * "targetProperty":"position",
210 * "source":sourceActor,
211 * "sourceProperty":"colorAlpha",
215 * pathConstrainer.applyConstraint( constraintPosition );
217 void ConstrainerApi::Apply( const v8::FunctionCallbackInfo< v8::Value >& args )
219 v8::Isolate* isolate = args.GetIsolate();
220 v8::HandleScope handleScope( isolate );
222 Actor target, source;
223 Property::Index targetPropertyIndex = Property::INVALID_INDEX;
224 Property::Index sourcePropertyIndex = Property::INVALID_INDEX;
227 if( GetApplyParameters(args, target, targetPropertyIndex, source, sourcePropertyIndex, range, wrap ) )
230 PathConstrainer pathConstrainer = GetPathConstrainer( isolate, args );
231 if( pathConstrainer )
233 pathConstrainer.Apply( Property(target, targetPropertyIndex),
234 Property(source, sourcePropertyIndex),
239 LinearConstrainer linearConstrainer = GetLinearConstrainer( isolate, args );
240 if( linearConstrainer )
242 linearConstrainer.Apply( Property(target, targetPropertyIndex),
243 Property(source, sourcePropertyIndex),
251 * Remove the constraint
253 * @for PathConstrainer
254 * @param {Object} Actor
256 * pathConstrainer.remove( targetActor );
258 void ConstrainerApi::Remove( const v8::FunctionCallbackInfo< v8::Value >& args )
260 v8::Isolate* isolate = args.GetIsolate();
261 v8::HandleScope handleScope( isolate );
265 Actor targetActor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
268 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 0 (Actor)" );
273 PathConstrainer pathConstrainer = GetPathConstrainer( isolate, args );
274 if( pathConstrainer )
276 pathConstrainer.Remove(targetActor);
280 LinearConstrainer linearConstrainer = GetLinearConstrainer( isolate, args );
281 if( linearConstrainer )
283 linearConstrainer.Remove(targetActor);
288 } // namespace V8Plugin