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/text-actor-api.h>
29 #include <actors/mesh-actor-api.h>
30 #include <actors/camera-actor-api.h>
31 #include <actors/renderable-actor-api.h>
33 #include <dali-wrapper.h>
41 v8::Persistent<v8::ObjectTemplate> ActorWrapper::mActorTemplate;
42 v8::Persistent<v8::ObjectTemplate> ActorWrapper::mImageActorTemplate;
43 v8::Persistent<v8::ObjectTemplate> ActorWrapper::mTextActorTemplate;
44 v8::Persistent<v8::ObjectTemplate> ActorWrapper::mMeshActorTemplate;
45 v8::Persistent<v8::ObjectTemplate> ActorWrapper::mCameraActorTemplate;
46 v8::Persistent<v8::ObjectTemplate> ActorWrapper::mLayerActorTemplate;
47 v8::Persistent<v8::ObjectTemplate> ActorWrapper::mTextViewTemplate;
54 * pointer to a persistent template handle
58 v8::Persistent<v8::ObjectTemplate>* actorTemplate;
62 * array of templates for each type of actor
64 const ActorTemplate ActorTemplateLookup[]=
66 { &ActorWrapper::mActorTemplate }, // ACTOR
67 { &ActorWrapper::mImageActorTemplate }, // IMAGE_ACTOR
68 { &ActorWrapper::mTextActorTemplate }, // TEXT_ACTOR
69 { &ActorWrapper::mMeshActorTemplate }, // MESH_ACTOR
70 { &ActorWrapper::mLayerActorTemplate }, // LAYER_ACTOR
71 { &ActorWrapper::mCameraActorTemplate}, // CAMERA_ACTOR
72 { &ActorWrapper::mTextViewTemplate }
76 * Bitmask of API's that an actor can support
81 RENDERABLE_ACTOR_API = 1 << 1,
82 IMAGE_ACTOR_API = 1 << 2,
83 TEXT_ACTOR_API = 1 << 3,
84 MESH_ACTOR_API = 1 << 4,
86 CAMERA_ACTOR_API = 1 << 6,
90 * structure used for the ActorApiLookup.
94 const char* actorName;
95 ActorWrapper::ActorType actorType;
96 Actor (*constructor)( const v8::FunctionCallbackInfo< v8::Value >& args);
101 * Lookup table to match a actor type with a constructor and supported API's.
103 const ActorApiStruct ActorApiLookup[]=
105 {"Actor", ActorWrapper::ACTOR, ActorApi::New, ACTOR_API },
106 {"ImageActor", ActorWrapper::IMAGE_ACTOR, ImageActorApi::New, ACTOR_API | RENDERABLE_ACTOR_API | IMAGE_ACTOR_API },
107 {"TextActor", ActorWrapper::TEXT_ACTOR, TextActorApi::New, ACTOR_API | RENDERABLE_ACTOR_API | TEXT_ACTOR_API },
108 {"MeshActor", ActorWrapper::MESH_ACTOR, MeshActorApi::New, ACTOR_API | RENDERABLE_ACTOR_API | MESH_ACTOR_API },
109 {"Layer", ActorWrapper::LAYER_ACTOR, LayerApi::New, ACTOR_API | LAYER_API },
110 {"CameraActor",ActorWrapper::CAMERA_ACTOR, CameraActorApi::New, ACTOR_API | CAMERA_ACTOR_API },
111 {"TextView", ActorWrapper::TEXT_VIEW, TextViewApi::New, ACTOR_API },
115 const unsigned int ActorApiLookupCount = sizeof(ActorApiLookup)/sizeof(ActorApiLookup[0]);
120 * Creates an actor given a type name
121 * Uses the type registry to create an actor of the correct type
123 Actor CreateActor( const v8::FunctionCallbackInfo< v8::Value >& args,
124 const std::string& typeName )
128 ActorWrapper::ActorType actorType = ActorWrapper::GetActorType( typeName );
130 // if we don't currently support the actor type, then use type registry to create it
131 if( actorType == ActorWrapper::UNKNOWN_ACTOR )
133 Dali::TypeInfo typeInfo = Dali::TypeRegistry::Get().GetTypeInfo( typeName );
134 if( typeInfo ) // handle, check if it has a value
136 Dali::BaseHandle handle = typeInfo.CreateInstance();
139 actor = Actor::DownCast( handle );
144 DALI_SCRIPT_EXCEPTION(args.GetIsolate(),"Unknown actor type");
150 // run the constructor for this type of actor so it can pull out
151 // custom parameters, e.g. new TextActor("hello world"); or ImageActor( MyImage );
152 actor = (ActorApiLookup[actorType].constructor)( args );
160 * given an actor type return what api's it supports
162 int GetActorSupportedApis( ActorWrapper::ActorType type )
164 return ActorApiLookup[ type].supportApis;
168 * Used for the ActorFunctionTable to map function names to functions
169 * with for a specific API
171 struct ActorFunctions
173 const char* name; ///< function name
174 void (*function)( const v8::FunctionCallbackInfo< v8::Value >& args);
179 * Contains a list of all functions that can be called an
180 * actor / image-actor / mesh-actor/ layer / camera-actor
182 const ActorFunctions ActorFunctionTable[]=
184 /**************************************
185 * Actor API (in order of actor.h)
186 * Any properties that have accessor functions are ignored to avoid duplication
187 **************************************/
188 // ignore. GetName() use Actor.name
189 // ignore. SetName() use Actor.name
190 { "GetId", ActorApi::GetId, ACTOR_API },
191 { "IsRoot", ActorApi::IsRoot, ACTOR_API },
192 { "OnStage", ActorApi::OnStage, ACTOR_API },
193 { "IsLayer", ActorApi::IsLayer, ACTOR_API },
194 { "GetLayer", ActorApi::GetLayer, ACTOR_API },
195 { "Add", ActorApi::AddActor, ACTOR_API },
196 { "Remove", ActorApi::RemoveActor, ACTOR_API },
197 { "IsEqualTo" , ActorApi::IsEqualTo, ACTOR_API },
198 { "Unparent", ActorApi::Unparent, ACTOR_API },
199 { "GetChildCount", ActorApi::GetChildCount, ACTOR_API },
200 { "GetChildAt" , ActorApi::GetChildAt, ACTOR_API },
201 { "FindChildByName", ActorApi::FindChildByName, ACTOR_API },
202 { "FindChildById", ActorApi::FindChildById, ACTOR_API },
203 { "GetParent" , ActorApi::GetParent, ACTOR_API },
204 { "GetActorType" , ActorApi::GetActorType, ACTOR_API }, // custom for javascript
206 // ignore. SetParentOrigin() use Actor.parentOrigin
207 // ignore. GetCurrentParentOrigin() use Actor.parentOrigin
208 // ignore. SetAnchorPoint() use Actor.anchorPoint
209 // ignore. GetCurrentAnchorPoint() use Actor.anchorPoint
210 // ignore. SetSize() use Actor.size
211 // ignore. GetCurrentSize() use Actor.size
212 // ignore. SetPosition(....) use Actor.position
213 // ignore. SetX, SetY, SetZ, use Actor.position.x, Actor.position.y, Actor.position.z
214 { "MoveBy", ActorApi::MoveBy, ACTOR_API },
215 // ignore GetCurrentPosition(). use Actor.position
216 // ignore GetCurrentWorldPosition() use Actor.worldPosition
217 // ignore SetPositionInheritanceMode() use Actor.positionInheritance
218 // ignore GetPositionInheritanceMode() use Actor.positionInheritance
219 // ignore SetRotation() use Actor.rotation
220 { "RotateBy", ActorApi::RotateBy, ACTOR_API },
221 // ignore GetCurrentRotation() use Actor.rotation
222 // ignore SetInheritRotation() use Actor.inheritRotation
223 // ignore IsRotationInherited() use Actor.inheritRotation
224 // ignore GetCurrentWorldRotation() use Actor.worldRotation
225 // ignore SetScale() use Actor.scale
226 { "ScaleBy", ActorApi::ScaleBy, ACTOR_API },
227 // ignore GetCurrentScale() use Actor.scale
228 // ignore GetCurrentWorldScale() use Actor.worldScale
229 // ignore SetInheritScale() use Actor.inheritScale
230 // ignore IsScaleInherited() use Actor.inheritScale
231 // ignore GetCurrentWorldMatrix() use Actor.worldMatrix
232 // ignore SetVisible() use Actor.visible
233 // ignore IsVisible() use Actor.visible
234 // ignore SetOpacity() use Actor.opacity
235 { "OpactiyBy", ActorApi::OpacityBy, ACTOR_API },
236 // ignore GetCurrentOpacity() use Actor.opacity
237 // ignore SetColor() use Actor.color
238 { "ColorBy", ActorApi::ColorBy, ACTOR_API },
239 // ignore GetCurrentColor() use Actor.color
240 // ignore SetColorMode() use Actor.colorMode
241 // ignore GetColorMode() use Actor.colorMode
242 // ignore GetCurrentWorldColor() use Actor.worldColor
243 // ignore SetInheritShaderEffect() use Actor.inheritShaderEffect
244 // ignore GetInheritShaderEffect() use Actor.inheritShaderEffect
245 // ignore SetDrawMode() use Actor.drawMode
246 // ignore GetDrawMode() use Actor.drawMode
247 // ignore SetSensitive() use Actor.sensitve
248 // ignore IsSensitive() use Actor.sensitive
249 { "ScreenToLocal" , ActorApi::ScreenToLocal, ACTOR_API},
250 // ignore SetLeaveRequired() use Actor.leaveRequired
251 // ignore GetLeaveRequired() use Actor.leaveRequired
252 { "SetKeyboardFocusable", ActorApi::SetKeyboardFocusable, ACTOR_API }, //-- should this be a property???
253 { "IsKeyboardFocusable" , ActorApi::IsKeyboardFocusable, ACTOR_API }, //-- should this be a property???
255 /**************************************
256 * Renderable Actor API (in order of renderable-actor.h)
257 **************************************/
258 { "SetSortModifier", RenderableActorApi::SetSortModifier, RENDERABLE_ACTOR_API },
259 { "GetSortModifier", RenderableActorApi::GetSortModifier, RENDERABLE_ACTOR_API },
260 { "SetCullFace", RenderableActorApi::SetCullFace, RENDERABLE_ACTOR_API },
261 { "GetCullFace", RenderableActorApi::GetCullFace, RENDERABLE_ACTOR_API },
262 { "SetBlendMode", RenderableActorApi::SetBlendMode, RENDERABLE_ACTOR_API },
263 { "GetBlendMode", RenderableActorApi::GetBlendMode, RENDERABLE_ACTOR_API },
264 { "SetBlendFunc", RenderableActorApi::SetBlendFunc, RENDERABLE_ACTOR_API },
265 { "GetBlendFunc", RenderableActorApi::GetBlendFunc, RENDERABLE_ACTOR_API },
266 { "SetBlendEquation", RenderableActorApi::SetBlendEquation, RENDERABLE_ACTOR_API },
267 { "GetBlendEquation", RenderableActorApi::GetBlendEquation, RENDERABLE_ACTOR_API },
268 { "SetBlendColor", RenderableActorApi::SetBlendColor, RENDERABLE_ACTOR_API },
269 { "GetBlendColor", RenderableActorApi::GetBlendColor, RENDERABLE_ACTOR_API },
270 { "SetShaderEffect", RenderableActorApi::SetShaderEffect, RENDERABLE_ACTOR_API },
271 { "GetShaderEffect", RenderableActorApi::GetShaderEffect, RENDERABLE_ACTOR_API },
272 { "RemoveShaderEffect", RenderableActorApi::RemoveShaderEffect,RENDERABLE_ACTOR_API },
277 /**************************************
278 * Layer API (in order of layer.h)
279 **************************************/
280 { "GetDepth", LayerApi::GetDepth, LAYER_API },
281 { "Raise", LayerApi::Raise, LAYER_API },
282 { "Lower", LayerApi::Lower, LAYER_API },
283 { "RaiseAbove", LayerApi::RaiseAbove, LAYER_API },
284 { "RaiseBelow", LayerApi::LowerBelow, LAYER_API },
285 { "RaiseToTop", LayerApi::RaiseToTop, LAYER_API },
286 { "LowerToBottom", LayerApi::ToBottom, LAYER_API },
287 { "MoveAbove", LayerApi::MoveAbove, LAYER_API },
288 { "MoveBelow", LayerApi::MoveBelow, LAYER_API },
289 // ignore SetClipping, use layer.clippingEnable
290 // ignore IsClipping, use layer.clippingEnable
291 // ignore SetClippingBox, use layer.clippingBox
292 { "SetDepthTestDisabled", LayerApi::SetDepthTestDisabled, LAYER_API },
293 { "IsDepthTestDisabled", LayerApi::IsDepthTestDisabled, LAYER_API },
294 // @todo SetSortFunction
296 /**************************************
297 * Image Actor API (in order of image-actor.h)
298 **************************************/
300 { "SetImage", ImageActorApi::SetImage, IMAGE_ACTOR_API },
301 { "GetImage", ImageActorApi::GetImage, IMAGE_ACTOR_API },
302 { "SetToNaturalSize", ImageActorApi::SetToNaturalSize, IMAGE_ACTOR_API },
303 // ignore SetPixelArea, use imageActor.pixelArea
304 // ignore GetPixelArea, use imageActor.pixelArea
305 { "IsPixelAreaSet", ImageActorApi::IsPixelAreaSet, IMAGE_ACTOR_API },
306 { "ClearPixelArea", ImageActorApi::ClearPixelArea, IMAGE_ACTOR_API },
307 // ignore SetStyle, use imageActor.style
308 // ignore GetStyle, use imageActor.style
309 // ignore SetNinePatchBorder use imageActor.border
310 // ignore GetNinePatchBorder use imageActor.border
311 // ignore SetFadeIn use imageActor.fadeIn
312 // ignore GetFadeIn use imageActor.fadeIn
313 // ignore SetFadeInDuration use imageActor.fadeInDuration
314 // ignore GetFadeInDuration use imageActor.fadeInDuration
315 //{ "GetCurrentImageSize", ImageActorApi::GetCurrentImageSize, IMAGE_ACTOR_API },
318 /**************************************
319 * Text Actor API (in order of text-actor.h)
320 **************************************/
321 //ignore SetText use textActor.text
322 { "SetToNaturalSize", TextActorApi::SetToNaturalSize, TEXT_ACTOR_API },
323 // ignore GetFont use textActor.font
324 // ignore SetFont use textActor.font
325 // ignore SetGradient use textActor.gradientColor
326 // ignore GetGradient textActor.gradientColor
327 // ignore SetGradientStartPoint use textActor.gradientStartPoint
328 // ignore GetGradientStartPoint textActor.gradientStartPoint
329 // ignore SetGradientEndPoint use textActor.gradientEndPoint
330 // ignore GetGradientEndPoint textActor.gradientEndPoint
331 // @todo? SetTextStyle ( can use individual properties as a work around )
332 // @todo? GetTextStyle ( can use individual properties as a work around )
333 // ignore SetTextColor use textActor.textColor
334 // ignore GetTextColor use textActor.textColor
335 // ignore SetSmoothEdge use textActor.smoothEdge
336 // ignore SetOutline use textActor.outLineEnable, outlineColor, thicknessWidth
337 // ignore SetGlow use textActor.glowEnable, glowColor, glowIntensity
338 // ignore SetShadow use textActor.shadowEnable, shadowColor, shadowOffset, shadowSize
339 // ignore SetItalics use textActor.italicsAngle ?
340 // ignore GetItalics @todo add italics flag? or just stick with angle
341 // ignore GetItalicsAngle use textActor.italicsAngle
342 // ignore SetUnderline use textActor.underline
343 // ignore GetUnderline use textActor.underline
344 // ignore SetWeight use textActor.weight
345 // ignore GetWeight use textActor.weight
346 // ignore SetFontDetectionAutomatic use textActor.fontDetectionAutomatic
347 // ignore IsFontDetectionAutomatic use textActor.fontDetectionAutomatic
348 // ignore GetLoadingState text is loaded synchronously
349 // ignore TextAvailableSignal text is loaded synchronously
351 /**************************************
352 * Mesh Actor API (in order of mesh-actor.h)
353 **************************************/
354 // @todo a version of MeshActor::New( mesh )
355 // @todo a version of MeshActor::New( AnimatableMesh )
358 // @todo BindBonesToMesh
360 /**************************************
361 * Camera Actor API (in order of camera.h)
362 **************************************/
363 // ignore SetType use camera.type
364 // ignore GetType use camera.type
365 // ignore SetProjectionMode use camera.projectionMode
366 // ignore GetProjectionMode use camera.projectionMode
367 // ignore SetFieldOfView use camera.fieldOfView
368 // ignore GetFieldOfView use camera.fieldOfView
369 // ignore SetAspectRatio use camera.aspectRatio
370 // ignore GetAspectRatio use camera.aspectRatio
371 // ignore SetNearClippingPlane use camera.nearPlaneDistance
372 // ignore GetNearClippingPlane use camera.nearPlaneDistance
373 // ignore SetFarClippingPlane use camera.farPlaneDistance
374 // ignore GetFarClippingPlane use camera.farPlaneDistance
375 // ignore GetTargetPosition use camera.targetPosition
376 // ignore SetInvertYAxis use camera.invertYAxis
377 // ignore GetInvertYAxis use camera.invertYAxis
378 { "SetPerspectiveProjection", CameraActorApi::SetPerspectiveProjection, CAMERA_ACTOR_API },
379 { "SetOrthographicProjection", CameraActorApi::SetOrthographicProjection, CAMERA_ACTOR_API },
383 const unsigned int ActorFunctionTableCount = sizeof(ActorFunctionTable)/sizeof(ActorFunctionTable[0]);
387 ActorWrapper::ActorWrapper( Actor actor,
388 GarbageCollectorInterface& gc )
389 : HandleWrapper( BaseWrappedObject::ACTOR , actor, gc ),
395 v8::Handle<v8::Object> ActorWrapper::WrapActor(v8::Isolate* isolate, Actor actor )
397 v8::EscapableHandleScope handleScope( isolate );
398 v8::Local<v8::Object> object = WrapActor( isolate, actor, GetActorType( actor.GetTypeName() ) );
400 return handleScope.Escape( object );
403 Actor ActorWrapper::GetActor()
408 v8::Handle<v8::Object> ActorWrapper::WrapActor( v8::Isolate* isolate, Actor actor, ActorType actorType )
410 v8::EscapableHandleScope handleScope( isolate );
411 v8::Local<v8::ObjectTemplate> objectTemplate;
413 objectTemplate = GetActorTemplate( isolate, actorType );
415 // create an instance of the template
416 v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
418 // create teh actor object
419 ActorWrapper* pointer = new ActorWrapper( actor, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
421 // assign the JavaScript object to the wrapper.
422 // This also stores Dali object, in an internal field inside the JavaScript object.
423 pointer->SetJavascriptObject( isolate, localObject );
425 return handleScope.Escape( localObject );
428 v8::Local<v8::ObjectTemplate> ActorWrapper::GetActorTemplate( v8::Isolate* isolate, ActorWrapper::ActorType type )
430 v8::EscapableHandleScope handleScope( isolate );
431 v8::Local<v8::ObjectTemplate> objectTemplate;
433 if( ActorTemplateLookup[type].actorTemplate->IsEmpty() )
435 objectTemplate = MakeDaliActorTemplate( isolate, type );
436 ActorTemplateLookup[type].actorTemplate->Reset( isolate, objectTemplate );
440 // get the object template
441 objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, *ActorTemplateLookup[type].actorTemplate );
444 return handleScope.Escape( objectTemplate );
447 v8::Handle<v8::ObjectTemplate> ActorWrapper::MakeDaliActorTemplate( v8::Isolate* isolate, ActorType actorType )
449 v8::EscapableHandleScope handleScope( isolate );
451 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
453 objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
455 // find out what API's this actor supports
456 int supportApis = GetActorSupportedApis( actorType );
458 // add our function properties
459 for( unsigned int i = 0; i < ActorFunctionTableCount; ++i )
461 const ActorFunctions property = ActorFunctionTable[i];
463 // check to see if the actor supports a certain type of API
464 // e.g. ImageActor will support ACTOR_API, RENDERABLE_API and IMAGE_ACTOR_API
465 if( supportApis & property.api )
467 std::string funcName = V8Utils::GetJavaScriptFunctionName( property.name);
469 objTemplate->Set( v8::String::NewFromUtf8( isolate, funcName.c_str() ),
470 v8::FunctionTemplate::New( isolate, property.function ) );
474 // property handle intercepts property getters and setters and signals
475 HandleWrapper::AddInterceptsToTemplate( isolate, objTemplate );
478 return handleScope.Escape( objTemplate );
481 void ActorWrapper::NewActor( const v8::FunctionCallbackInfo< v8::Value >& args)
483 v8::Isolate* isolate = args.GetIsolate();
484 v8::HandleScope handleScope( isolate );
486 if( !args.IsConstructCall() )
488 DALI_SCRIPT_EXCEPTION( isolate, "constructor called without 'new" );
492 // find out the callee function name...e.g. TextActor, ImageActor, MeshActor
493 v8::Local<v8::Function> callee = args.Callee();
494 v8::Local<v8::Value> v8String = callee->GetName();
495 std::string typeName = V8Utils::v8StringToStdString( v8String );
497 // create a new actor based on type, using the type registry.
498 Actor actor = CreateActor( args, typeName );
500 v8::Local<v8::Object> localObject = WrapActor( isolate, actor );
502 args.GetReturnValue().Set( localObject );
506 * given an actor type name, e.g. ImageActor returns the type, e.g. ActorWrapper::IMAGE_ACTOR
508 ActorWrapper::ActorType ActorWrapper::GetActorType( const std::string& name )
510 for( unsigned int i = 0 ; i < ActorApiLookupCount ; i++ )
512 if( ActorApiLookup[i].actorName == name )
514 return ActorApiLookup[i].actorType;
517 return ActorWrapper::UNKNOWN_ACTOR;
522 } // namespace V8Plugin