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 "actor-wrapper.h"
22 #include <dali/public-api/object/type-registry.h>
25 #include <actors/layer-api.h>
26 #include <actors/actor-api.h>
27 #include <actors/image-actor-api.h>
28 #include <actors/camera-actor-api.h>
30 #include <dali-wrapper.h>
38 v8::Persistent<v8::ObjectTemplate> ActorWrapper::mActorTemplate;
39 v8::Persistent<v8::ObjectTemplate> ActorWrapper::mImageActorTemplate;
40 v8::Persistent<v8::ObjectTemplate> ActorWrapper::mCameraActorTemplate;
41 v8::Persistent<v8::ObjectTemplate> ActorWrapper::mLayerActorTemplate;
48 * pointer to a persistent template handle
52 v8::Persistent<v8::ObjectTemplate>* actorTemplate;
56 * array of templates for each type of actor
58 const ActorTemplate ActorTemplateLookup[]=
60 { &ActorWrapper::mActorTemplate }, // ACTOR
61 { &ActorWrapper::mImageActorTemplate }, // IMAGE_ACTOR
62 { &ActorWrapper::mLayerActorTemplate }, // LAYER_ACTOR
63 { &ActorWrapper::mCameraActorTemplate} // CAMERA_ACTOR
67 * Bitmask of API's that an actor can support
72 IMAGE_ACTOR_API = 1 << 1,
74 CAMERA_ACTOR_API = 1 << 3,
78 * structure used for the ActorApiLookup.
82 const char* actorName;
83 ActorWrapper::ActorType actorType;
84 Actor (*constructor)( const v8::FunctionCallbackInfo< v8::Value >& args);
89 * Lookup table to match a actor type with a constructor and supported API's.
90 * HandleWrapper::ActorType is used to index this table
92 const ActorApiStruct ActorApiLookup[]=
94 {"Actor", ActorWrapper::ACTOR, ActorApi::New, ACTOR_API },
95 {"ImageActor", ActorWrapper::IMAGE_ACTOR, ImageActorApi::New, ACTOR_API | IMAGE_ACTOR_API },
96 {"Layer", ActorWrapper::LAYER_ACTOR, LayerApi::New, ACTOR_API | LAYER_API },
97 {"CameraActor",ActorWrapper::CAMERA_ACTOR, CameraActorApi::New, ACTOR_API | CAMERA_ACTOR_API },
100 const unsigned int ActorApiLookupCount = sizeof(ActorApiLookup)/sizeof(ActorApiLookup[0]);
105 * Creates an actor given a type name
106 * Uses the type registry to create an actor of the correct type
108 Actor CreateActor( const v8::FunctionCallbackInfo< v8::Value >& args,
109 const std::string& typeName )
113 ActorWrapper::ActorType actorType = ActorWrapper::GetActorType( typeName );
115 // if we don't currently support the actor type, then use type registry to create it
116 if( actorType == ActorWrapper::UNKNOWN_ACTOR )
118 DALI_SCRIPT_EXCEPTION( args.GetIsolate(), "Unknown actor type" );
123 // run the constructor for this type of actor so it can pull out
124 // custom parameters, e.g. new ImageActor( MyImage );
125 actor = (ActorApiLookup[actorType].constructor)( args );
133 * given an actor type return what api's it supports
135 int GetActorSupportedApis( ActorWrapper::ActorType type )
137 return ActorApiLookup[ type].supportApis;
141 * Used for the ActorFunctionTable to map function names to functions
142 * with for a specific API
144 struct ActorFunctions
146 const char* name; ///< function name
147 void (*function)( const v8::FunctionCallbackInfo< v8::Value >& args);
152 * Contains a list of all functions that can be called an
153 * actor / image-actor / layer / camera-actor
155 const ActorFunctions ActorFunctionTable[]=
157 /**************************************
158 * Actor API (in order of actor.h)
159 * Any properties that have accessor functions are ignored to avoid duplication
160 **************************************/
161 // ignore. GetName() use Actor.name
162 // ignore. SetName() use Actor.name
163 { "GetId", ActorApi::GetId, ACTOR_API },
164 { "IsRoot", ActorApi::IsRoot, ACTOR_API },
165 { "OnStage", ActorApi::OnStage, ACTOR_API },
166 { "IsLayer", ActorApi::IsLayer, ACTOR_API },
167 { "GetLayer", ActorApi::GetLayer, ACTOR_API },
168 { "Add", ActorApi::AddActor, ACTOR_API },
169 { "Remove", ActorApi::RemoveActor, ACTOR_API },
170 { "IsEqualTo" , ActorApi::IsEqualTo, ACTOR_API },
171 { "Unparent", ActorApi::Unparent, ACTOR_API },
172 { "GetChildCount", ActorApi::GetChildCount, ACTOR_API },
173 { "GetChildAt" , ActorApi::GetChildAt, ACTOR_API },
174 { "FindChildByName", ActorApi::FindChildByName, ACTOR_API },
175 { "FindChildById", ActorApi::FindChildById, ACTOR_API },
176 { "GetParent" , ActorApi::GetParent, ACTOR_API },
177 { "GetActorType" , ActorApi::GetActorType, ACTOR_API }, // custom for javascript
179 // ignore. SetParentOrigin() use Actor.parentOrigin
180 // ignore. GetCurrentParentOrigin() use Actor.parentOrigin
181 // ignore. SetAnchorPoint() use Actor.anchorPoint
182 // ignore. GetCurrentAnchorPoint() use Actor.anchorPoint
183 // ignore. SetSize() use Actor.size
184 // ignore. GetCurrentSize() use Actor.size
185 // ignore. SetPosition(....) use Actor.position
186 // ignore. SetX, SetY, SetZ, use Actor.position.x, Actor.position.y, Actor.position.z
187 { "TranslateBy", ActorApi::TranslateBy, ACTOR_API },
188 // ignore GetCurrentPosition(). use Actor.position
189 // ignore GetCurrentWorldPosition() use Actor.worldPosition
190 // ignore SetPositionInheritanceMode() use Actor.positionInheritance
191 // ignore GetPositionInheritanceMode() use Actor.positionInheritance
192 // ignore SetOrientation() use Actor.orientation
193 { "RotateBy", ActorApi::RotateBy, ACTOR_API },
194 // ignore GetCurrentOrientation() use Actor.orientation
195 // ignore SetInheritOrientation() use Actor.inheritOrientation
196 // ignore IsOrientationInherited() use Actor.inheritOrientation
197 // ignore GetCurrentWorldOrientation() use Actor.worldOrientation
198 // ignore SetScale() use Actor.scale
199 { "ScaleBy", ActorApi::ScaleBy, ACTOR_API },
200 // ignore GetCurrentScale() use Actor.scale
201 // ignore GetCurrentWorldScale() use Actor.worldScale
202 // ignore SetInheritScale() use Actor.inheritScale
203 // ignore IsScaleInherited() use Actor.inheritScale
204 // ignore GetCurrentWorldMatrix() use Actor.worldMatrix
205 // ignore SetVisible() use Actor.visible
206 // ignore IsVisible() use Actor.visible
207 // ignore SetOpacity() use Actor.opacity
208 // ignore GetCurrentOpacity() use Actor.opacity
209 // ignore SetColor() use Actor.color
210 // ignore GetCurrentColor() use Actor.color
211 // ignore SetColorMode() use Actor.colorMode
212 // ignore GetColorMode() use Actor.colorMode
213 // ignore GetCurrentWorldColor() use Actor.worldColor
214 // ignore SetInheritShaderEffect() use Actor.inheritShaderEffect
215 // ignore GetInheritShaderEffect() use Actor.inheritShaderEffect
216 // ignore SetDrawMode() use Actor.drawMode
217 // ignore GetDrawMode() use Actor.drawMode
218 // ignore SetSensitive() use Actor.sensitve
219 // ignore IsSensitive() use Actor.sensitive
220 { "ScreenToLocal" , ActorApi::ScreenToLocal, ACTOR_API},
221 // ignore SetLeaveRequired() use Actor.leaveRequired
222 // ignore GetLeaveRequired() use Actor.leaveRequired
223 { "SetKeyboardFocusable", ActorApi::SetKeyboardFocusable, ACTOR_API }, //-- should this be a property???
224 { "IsKeyboardFocusable" , ActorApi::IsKeyboardFocusable, ACTOR_API }, //-- should this be a property???
226 /**************************************
227 * Layer API (in order of layer.h)
228 **************************************/
229 { "GetDepth", LayerApi::GetDepth, LAYER_API },
230 { "Raise", LayerApi::Raise, LAYER_API },
231 { "Lower", LayerApi::Lower, LAYER_API },
232 { "RaiseAbove", LayerApi::RaiseAbove, LAYER_API },
233 { "RaiseBelow", LayerApi::LowerBelow, LAYER_API },
234 { "RaiseToTop", LayerApi::RaiseToTop, LAYER_API },
235 { "LowerToBottom", LayerApi::ToBottom, LAYER_API },
236 { "MoveAbove", LayerApi::MoveAbove, LAYER_API },
237 { "MoveBelow", LayerApi::MoveBelow, LAYER_API },
238 // ignore SetClipping, use layer.clippingEnable
239 // ignore IsClipping, use layer.clippingEnable
240 // ignore SetClippingBox, use layer.clippingBox
241 { "SetDepthTestDisabled", LayerApi::SetDepthTestDisabled, LAYER_API },
242 { "IsDepthTestDisabled", LayerApi::IsDepthTestDisabled, LAYER_API },
243 // @todo SetSortFunction
245 /**************************************
246 * Image Actor API (in order of image-actor.h)
247 **************************************/
249 { "SetImage", ImageActorApi::SetImage, IMAGE_ACTOR_API },
250 { "GetImage", ImageActorApi::GetImage, IMAGE_ACTOR_API },
251 // ignore SetPixelArea, use imageActor.pixelArea
252 // ignore GetPixelArea, use imageActor.pixelArea
253 // ignore SetStyle, use imageActor.style
254 // ignore GetStyle, use imageActor.style
255 // ignore SetNinePatchBorder use imageActor.border
256 // ignore GetNinePatchBorder use imageActor.border
257 { "SetSortModifier", ImageActorApi::SetSortModifier, IMAGE_ACTOR_API },
258 { "GetSortModifier", ImageActorApi::GetSortModifier, IMAGE_ACTOR_API },
259 { "SetCullFace", ImageActorApi::SetCullFace, IMAGE_ACTOR_API },
260 { "GetCullFace", ImageActorApi::GetCullFace, IMAGE_ACTOR_API },
261 { "SetBlendMode", ImageActorApi::SetBlendMode, IMAGE_ACTOR_API },
262 { "GetBlendMode", ImageActorApi::GetBlendMode, IMAGE_ACTOR_API },
263 { "SetBlendFunc", ImageActorApi::SetBlendFunc, IMAGE_ACTOR_API },
264 { "GetBlendFunc", ImageActorApi::GetBlendFunc, IMAGE_ACTOR_API },
265 { "SetShaderEffect", ImageActorApi::SetShaderEffect, IMAGE_ACTOR_API },
266 { "GetShaderEffect", ImageActorApi::GetShaderEffect, IMAGE_ACTOR_API },
267 { "RemoveShaderEffect", ImageActorApi::RemoveShaderEffect,IMAGE_ACTOR_API },
268 // ignore SetFadeIn use imageActor.fadeIn
269 // ignore GetFadeIn use imageActor.fadeIn
270 // ignore SetFadeInDuration use imageActor.fadeInDuration
271 // ignore GetFadeInDuration use imageActor.fadeInDuration
272 //{ "GetCurrentImageSize", ImageActorApi::GetCurrentImageSize, IMAGE_ACTOR_API },
274 /**************************************
275 * Mesh Actor API (in order of mesh-actor.h)
276 **************************************/
277 // @todo a version of MeshActor::New( mesh )
278 // @todo a version of MeshActor::New( AnimatableMesh )
281 // @todo BindBonesToMesh
283 /**************************************
284 * Camera Actor API (in order of camera.h)
285 **************************************/
286 // ignore SetType use camera.type
287 // ignore GetType use camera.type
288 // ignore SetProjectionMode use camera.projectionMode
289 // ignore GetProjectionMode use camera.projectionMode
290 // ignore SetFieldOfView use camera.fieldOfView
291 // ignore GetFieldOfView use camera.fieldOfView
292 // ignore SetAspectRatio use camera.aspectRatio
293 // ignore GetAspectRatio use camera.aspectRatio
294 // ignore SetNearClippingPlane use camera.nearPlaneDistance
295 // ignore GetNearClippingPlane use camera.nearPlaneDistance
296 // ignore SetFarClippingPlane use camera.farPlaneDistance
297 // ignore GetFarClippingPlane use camera.farPlaneDistance
298 // ignore GetTargetPosition use camera.targetPosition
299 // ignore SetInvertYAxis use camera.invertYAxis
300 // ignore GetInvertYAxis use camera.invertYAxis
301 { "SetPerspectiveProjection", CameraActorApi::SetPerspectiveProjection, CAMERA_ACTOR_API },
302 { "SetOrthographicProjection", CameraActorApi::SetOrthographicProjection, CAMERA_ACTOR_API },
306 const unsigned int ActorFunctionTableCount = sizeof(ActorFunctionTable)/sizeof(ActorFunctionTable[0]);
310 ActorWrapper::ActorWrapper( Actor actor,
311 GarbageCollectorInterface& gc )
312 : HandleWrapper( BaseWrappedObject::ACTOR , actor, gc ),
318 v8::Handle<v8::Object> ActorWrapper::WrapActor(v8::Isolate* isolate, Actor actor )
320 v8::EscapableHandleScope handleScope( isolate );
321 v8::Local<v8::Object> object = WrapActor( isolate, actor, GetActorType( actor.GetTypeName() ) );
323 return handleScope.Escape( object );
326 Actor ActorWrapper::GetActor()
331 v8::Handle<v8::Object> ActorWrapper::WrapActor( v8::Isolate* isolate, Actor actor, ActorType actorType )
333 v8::EscapableHandleScope handleScope( isolate );
334 v8::Local<v8::ObjectTemplate> objectTemplate;
336 objectTemplate = GetActorTemplate( isolate, actorType );
338 // create an instance of the template
339 v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
341 // create teh actor object
342 ActorWrapper* pointer = new ActorWrapper( actor, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
344 // assign the JavaScript object to the wrapper.
345 // This also stores Dali object, in an internal field inside the JavaScript object.
346 pointer->SetJavascriptObject( isolate, localObject );
348 return handleScope.Escape( localObject );
351 v8::Local<v8::ObjectTemplate> ActorWrapper::GetActorTemplate( v8::Isolate* isolate, ActorWrapper::ActorType type )
353 v8::EscapableHandleScope handleScope( isolate );
354 v8::Local<v8::ObjectTemplate> objectTemplate;
356 if( ActorTemplateLookup[type].actorTemplate->IsEmpty() )
358 objectTemplate = MakeDaliActorTemplate( isolate, type );
359 ActorTemplateLookup[type].actorTemplate->Reset( isolate, objectTemplate );
363 // get the object template
364 objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, *ActorTemplateLookup[type].actorTemplate );
367 return handleScope.Escape( objectTemplate );
370 v8::Handle<v8::ObjectTemplate> ActorWrapper::MakeDaliActorTemplate( v8::Isolate* isolate, ActorType actorType )
372 v8::EscapableHandleScope handleScope( isolate );
374 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
376 objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
378 // find out what API's this actor supports
379 int supportApis = GetActorSupportedApis( actorType );
381 // add our function properties
382 for( unsigned int i = 0; i < ActorFunctionTableCount; ++i )
384 const ActorFunctions property = ActorFunctionTable[i];
386 // check to see if the actor supports a certain type of API
387 // e.g. ImageActor will support ACTOR_API, RENDERABLE_API and IMAGE_ACTOR_API
388 if( supportApis & property.api )
390 std::string funcName = V8Utils::GetJavaScriptFunctionName( property.name);
392 objTemplate->Set( v8::String::NewFromUtf8( isolate, funcName.c_str() ),
393 v8::FunctionTemplate::New( isolate, property.function ) );
397 // property handle intercepts property getters and setters and signals
398 HandleWrapper::AddInterceptsToTemplate( isolate, objTemplate );
401 return handleScope.Escape( objTemplate );
404 void ActorWrapper::NewActor( const v8::FunctionCallbackInfo< v8::Value >& args)
406 v8::Isolate* isolate = args.GetIsolate();
407 v8::HandleScope handleScope( isolate );
409 if( !args.IsConstructCall() )
411 DALI_SCRIPT_EXCEPTION( isolate, "constructor called without 'new" );
415 // find out the callee function name...e.g. ImageActor, MeshActor
416 v8::Local<v8::Function> callee = args.Callee();
417 v8::Local<v8::Value> v8String = callee->GetName();
418 std::string typeName = V8Utils::v8StringToStdString( v8String );
420 // create a new actor based on type, using the type registry.
421 Actor actor = CreateActor( args, typeName );
423 v8::Local<v8::Object> localObject = WrapActor( isolate, actor );
425 args.GetReturnValue().Set( localObject );
428 void ActorWrapper::NewControl( const v8::FunctionCallbackInfo< v8::Value >& args)
430 v8::Isolate* isolate = args.GetIsolate();
431 v8::HandleScope handleScope( isolate );
433 if( !args.IsConstructCall() )
435 DALI_SCRIPT_EXCEPTION( isolate, "constructor called without 'new" );
440 std::string controlName = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
444 DALI_SCRIPT_EXCEPTION( isolate, "missing control name" );
448 Dali::TypeInfo typeInfo = Dali::TypeRegistry::Get().GetTypeInfo( controlName );
449 if( typeInfo ) // handle, check if it has a value
451 Dali::BaseHandle handle = typeInfo.CreateInstance();
454 control = Actor::DownCast( handle );
458 v8::Local<v8::Object> localObject = WrapActor( isolate, control, ACTOR );
460 args.GetReturnValue().Set( localObject );
465 * given an actor type name, e.g. ImageActor returns the type, e.g. ActorWrapper::IMAGE_ACTOR
467 ActorWrapper::ActorType ActorWrapper::GetActorType( const std::string& name )
469 for( unsigned int i = 0 ; i < ActorApiLookupCount ; i++ )
471 if( ActorApiLookup[i].actorName == name )
473 return ActorApiLookup[i].actorType;
476 return ActorWrapper::UNKNOWN_ACTOR;
481 } // namespace V8Plugin