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 { "FindChildByAlias", ActorApi::FindChildByAlias, ACTOR_API },
203 { "FindChildById", ActorApi::FindChildById, ACTOR_API },
204 { "GetParent" , ActorApi::GetParent, ACTOR_API },
205 { "GetActorType" , ActorApi::GetActorType, ACTOR_API }, // custom for javascript
207 // ignore. SetParentOrigin() use Actor.parentOrigin
208 // ignore. GetCurrentParentOrigin() use Actor.parentOrigin
209 // ignore. SetAnchorPoint() use Actor.anchorPoint
210 // ignore. GetCurrentAnchorPoint() use Actor.anchorPoint
211 // ignore. SetSize() use Actor.size
212 // ignore. GetCurrentSize() use Actor.size
213 // ignore. SetPosition(....) use Actor.position
214 // ignore. SetX, SetY, SetZ, use Actor.position.x, Actor.position.y, Actor.position.z
215 { "MoveBy", ActorApi::MoveBy, ACTOR_API },
216 // ignore GetCurrentPosition(). use Actor.position
217 // ignore GetCurrentWorldPosition() use Actor.worldPosition
218 // ignore SetPositionInheritanceMode() use Actor.positionInheritance
219 // ignore GetPositionInheritanceMode() use Actor.positionInheritance
220 // ignore SetRotation() use Actor.rotation
221 { "RotateBy", ActorApi::RotateBy, ACTOR_API },
222 // ignore GetCurrentRotation() use Actor.rotation
223 // ignore SetInheritRotation() use Actor.inheritRotation
224 // ignore IsRotationInherited() use Actor.inheritRotation
225 // ignore GetCurrentWorldRotation() use Actor.worldRotation
226 // ignore SetScale() use Actor.scale
227 { "ScaleBy", ActorApi::ScaleBy, ACTOR_API },
228 // ignore GetCurrentScale() use Actor.scale
229 // ignore GetCurrentWorldScale() use Actor.worldScale
230 // ignore SetInheritScale() use Actor.inheritScale
231 // ignore IsScaleInherited() use Actor.inheritScale
232 // ignore GetCurrentWorldMatrix() use Actor.worldMatrix
233 // ignore SetVisible() use Actor.visible
234 // ignore IsVisible() use Actor.visible
235 // ignore SetOpacity() use Actor.opacity
236 { "OpactiyBy", ActorApi::OpacityBy, ACTOR_API },
237 // ignore GetCurrentOpacity() use Actor.opacity
238 // ignore SetColor() use Actor.color
239 { "ColorBy", ActorApi::ColorBy, ACTOR_API },
240 // ignore GetCurrentColor() use Actor.color
241 // ignore SetColorMode() use Actor.colorMode
242 // ignore GetColorMode() use Actor.colorMode
243 // ignore GetCurrentWorldColor() use Actor.worldColor
244 // ignore SetInheritShaderEffect() use Actor.inheritShaderEffect
245 // ignore GetInheritShaderEffect() use Actor.inheritShaderEffect
246 // ignore SetDrawMode() use Actor.drawMode
247 // ignore GetDrawMode() use Actor.drawMode
248 // ignore SetSensitive() use Actor.sensitve
249 // ignore IsSensitive() use Actor.sensitive
250 { "ScreenToLocal" , ActorApi::ScreenToLocal, ACTOR_API},
251 // ignore SetLeaveRequired() use Actor.leaveRequired
252 // ignore GetLeaveRequired() use Actor.leaveRequired
253 { "SetKeyboardFocusable", ActorApi::SetKeyboardFocusable, ACTOR_API }, //-- should this be a property???
254 { "IsKeyboardFocusable" , ActorApi::IsKeyboardFocusable, ACTOR_API }, //-- should this be a property???
256 /**************************************
257 * Renderable Actor API (in order of renderable-actor.h)
258 **************************************/
259 { "SetSortModifier", RenderableActorApi::SetSortModifier, RENDERABLE_ACTOR_API },
260 { "GetSortModifier", RenderableActorApi::GetSortModifier, RENDERABLE_ACTOR_API },
261 { "SetCullFace", RenderableActorApi::SetCullFace, RENDERABLE_ACTOR_API },
262 { "GetCullFace", RenderableActorApi::GetCullFace, RENDERABLE_ACTOR_API },
263 { "SetBlendMode", RenderableActorApi::SetBlendMode, RENDERABLE_ACTOR_API },
264 { "GetBlendMode", RenderableActorApi::GetBlendMode, RENDERABLE_ACTOR_API },
265 { "SetBlendFunc", RenderableActorApi::SetBlendFunc, RENDERABLE_ACTOR_API },
266 { "GetBlendFunc", RenderableActorApi::GetBlendFunc, RENDERABLE_ACTOR_API },
267 { "SetBlendEquation", RenderableActorApi::SetBlendEquation, RENDERABLE_ACTOR_API },
268 { "GetBlendEquation", RenderableActorApi::GetBlendEquation, RENDERABLE_ACTOR_API },
269 { "SetBlendColor", RenderableActorApi::SetBlendColor, RENDERABLE_ACTOR_API },
270 { "GetBlendColor", RenderableActorApi::GetBlendColor, RENDERABLE_ACTOR_API },
271 { "SetShaderEffect", RenderableActorApi::SetShaderEffect, RENDERABLE_ACTOR_API },
272 { "GetShaderEffect", RenderableActorApi::GetShaderEffect, RENDERABLE_ACTOR_API },
273 { "RemoveShaderEffect", RenderableActorApi::RemoveShaderEffect,RENDERABLE_ACTOR_API },
278 /**************************************
279 * Layer API (in order of layer.h)
280 **************************************/
281 { "GetDepth", LayerApi::GetDepth, LAYER_API },
282 { "Raise", LayerApi::Raise, LAYER_API },
283 { "Lower", LayerApi::Lower, LAYER_API },
284 { "RaiseAbove", LayerApi::RaiseAbove, LAYER_API },
285 { "RaiseBelow", LayerApi::LowerBelow, LAYER_API },
286 { "RaiseToTop", LayerApi::RaiseToTop, LAYER_API },
287 { "LowerToBottom", LayerApi::ToBottom, LAYER_API },
288 { "MoveAbove", LayerApi::MoveAbove, LAYER_API },
289 { "MoveBelow", LayerApi::MoveBelow, LAYER_API },
290 // ignore SetClipping, use layer.clippingEnable
291 // ignore IsClipping, use layer.clippingEnable
292 // ignore SetClippingBox, use layer.clippingBox
293 { "SetDepthTestDisabled", LayerApi::SetDepthTestDisabled, LAYER_API },
294 { "IsDepthTestDisabled", LayerApi::IsDepthTestDisabled, LAYER_API },
295 // @todo SetSortFunction
297 /**************************************
298 * Image Actor API (in order of image-actor.h)
299 **************************************/
301 { "SetImage", ImageActorApi::SetImage, IMAGE_ACTOR_API },
302 { "GetImage", ImageActorApi::GetImage, IMAGE_ACTOR_API },
303 { "SetToNaturalSize", ImageActorApi::SetToNaturalSize, IMAGE_ACTOR_API },
304 // ignore SetPixelArea, use imageActor.pixelArea
305 // ignore GetPixelArea, use imageActor.pixelArea
306 { "IsPixelAreaSet", ImageActorApi::IsPixelAreaSet, IMAGE_ACTOR_API },
307 { "ClearPixelArea", ImageActorApi::ClearPixelArea, IMAGE_ACTOR_API },
308 // ignore SetStyle, use imageActor.style
309 // ignore GetStyle, use imageActor.style
310 // ignore SetNinePatchBorder use imageActor.border
311 // ignore GetNinePatchBorder use imageActor.border
312 // ignore SetFadeIn use imageActor.fadeIn
313 // ignore GetFadeIn use imageActor.fadeIn
314 // ignore SetFadeInDuration use imageActor.fadeInDuration
315 // ignore GetFadeInDuration use imageActor.fadeInDuration
316 //{ "GetCurrentImageSize", ImageActorApi::GetCurrentImageSize, IMAGE_ACTOR_API },
319 /**************************************
320 * Text Actor API (in order of text-actor.h)
321 **************************************/
322 //ignore SetText use textActor.text
323 { "SetToNaturalSize", TextActorApi::SetToNaturalSize, TEXT_ACTOR_API },
324 // ignore GetFont use textActor.font
325 // ignore SetFont use textActor.font
326 // ignore SetGradient use textActor.gradientColor
327 // ignore GetGradient textActor.gradientColor
328 // ignore SetGradientStartPoint use textActor.gradientStartPoint
329 // ignore GetGradientStartPoint textActor.gradientStartPoint
330 // ignore SetGradientEndPoint use textActor.gradientEndPoint
331 // ignore GetGradientEndPoint textActor.gradientEndPoint
332 // @todo? SetTextStyle ( can use individual properties as a work around )
333 // @todo? GetTextStyle ( can use individual properties as a work around )
334 // ignore SetTextColor use textActor.textColor
335 // ignore GetTextColor use textActor.textColor
336 // ignore SetSmoothEdge use textActor.smoothEdge
337 // ignore SetOutline use textActor.outLineEnable, outlineColor, thicknessWidth
338 // ignore SetGlow use textActor.glowEnable, glowColor, glowIntensity
339 // ignore SetShadow use textActor.shadowEnable, shadowColor, shadowOffset, shadowSize
340 // ignore SetItalics use textActor.italicsAngle ?
341 // ignore GetItalics @todo add italics flag? or just stick with angle
342 // ignore GetItalicsAngle use textActor.italicsAngle
343 // ignore SetUnderline use textActor.underline
344 // ignore GetUnderline use textActor.underline
345 // ignore SetWeight use textActor.weight
346 // ignore GetWeight use textActor.weight
347 // ignore SetFontDetectionAutomatic use textActor.fontDetectionAutomatic
348 // ignore IsFontDetectionAutomatic use textActor.fontDetectionAutomatic
349 // ignore GetLoadingState text is loaded synchronously
350 // ignore TextAvailableSignal text is loaded synchronously
352 /**************************************
353 * Mesh Actor API (in order of mesh-actor.h)
354 **************************************/
355 // @todo a version of MeshActor::New( mesh )
356 // @todo a version of MeshActor::New( AnimatableMesh )
359 // @todo BindBonesToMesh
361 /**************************************
362 * Camera Actor API (in order of camera.h)
363 **************************************/
364 // ignore SetType use camera.type
365 // ignore GetType use camera.type
366 // ignore SetProjectionMode use camera.projectionMode
367 // ignore GetProjectionMode use camera.projectionMode
368 // ignore SetFieldOfView use camera.fieldOfView
369 // ignore GetFieldOfView use camera.fieldOfView
370 // ignore SetAspectRatio use camera.aspectRatio
371 // ignore GetAspectRatio use camera.aspectRatio
372 // ignore SetNearClippingPlane use camera.nearPlaneDistance
373 // ignore GetNearClippingPlane use camera.nearPlaneDistance
374 // ignore SetFarClippingPlane use camera.farPlaneDistance
375 // ignore GetFarClippingPlane use camera.farPlaneDistance
376 // ignore GetTargetPosition use camera.targetPosition
377 // ignore SetInvertYAxis use camera.invertYAxis
378 // ignore GetInvertYAxis use camera.invertYAxis
379 { "SetPerspectiveProjection", CameraActorApi::SetPerspectiveProjection, CAMERA_ACTOR_API },
380 { "SetOrthographicProjection", CameraActorApi::SetOrthographicProjection, CAMERA_ACTOR_API },
384 const unsigned int ActorFunctionTableCount = sizeof(ActorFunctionTable)/sizeof(ActorFunctionTable[0]);
388 ActorWrapper::ActorWrapper( Actor actor,
389 GarbageCollectorInterface& gc )
390 : HandleWrapper( BaseWrappedObject::ACTOR , actor, gc ),
396 v8::Handle<v8::Object> ActorWrapper::WrapActor(v8::Isolate* isolate, Actor actor )
398 v8::EscapableHandleScope handleScope( isolate );
399 v8::Local<v8::Object> object = WrapActor( isolate, actor, GetActorType( actor.GetTypeName() ) );
401 return handleScope.Escape( object );
404 Actor ActorWrapper::GetActor()
409 v8::Handle<v8::Object> ActorWrapper::WrapActor( v8::Isolate* isolate, Actor actor, ActorType actorType )
411 v8::EscapableHandleScope handleScope( isolate );
412 v8::Local<v8::ObjectTemplate> objectTemplate;
414 objectTemplate = GetActorTemplate( isolate, actorType );
416 // create an instance of the template
417 v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
419 // create teh actor object
420 ActorWrapper* pointer = new ActorWrapper( actor, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
422 // assign the JavaScript object to the wrapper.
423 // This also stores Dali object, in an internal field inside the JavaScript object.
424 pointer->SetJavascriptObject( isolate, localObject );
426 return handleScope.Escape( localObject );
429 v8::Local<v8::ObjectTemplate> ActorWrapper::GetActorTemplate( v8::Isolate* isolate, ActorWrapper::ActorType type )
431 v8::EscapableHandleScope handleScope( isolate );
432 v8::Local<v8::ObjectTemplate> objectTemplate;
434 if( ActorTemplateLookup[type].actorTemplate->IsEmpty() )
436 objectTemplate = MakeDaliActorTemplate( isolate, type );
437 ActorTemplateLookup[type].actorTemplate->Reset( isolate, objectTemplate );
441 // get the object template
442 objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, *ActorTemplateLookup[type].actorTemplate );
445 return handleScope.Escape( objectTemplate );
448 v8::Handle<v8::ObjectTemplate> ActorWrapper::MakeDaliActorTemplate( v8::Isolate* isolate, ActorType actorType )
450 v8::EscapableHandleScope handleScope( isolate );
452 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
454 objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
456 // find out what API's this actor supports
457 int supportApis = GetActorSupportedApis( actorType );
459 // add our function properties
460 for( unsigned int i = 0; i < ActorFunctionTableCount; ++i )
462 const ActorFunctions property = ActorFunctionTable[i];
464 // check to see if the actor supports a certain type of API
465 // e.g. ImageActor will support ACTOR_API, RENDERABLE_API and IMAGE_ACTOR_API
466 if( supportApis & property.api )
468 std::string funcName = V8Utils::GetJavaScriptFunctionName( property.name);
470 objTemplate->Set( v8::String::NewFromUtf8( isolate, funcName.c_str() ),
471 v8::FunctionTemplate::New( isolate, property.function ) );
475 // property handle intercepts property getters and setters and signals
476 HandleWrapper::AddInterceptsToTemplate( isolate, objTemplate );
479 return handleScope.Escape( objTemplate );
482 void ActorWrapper::NewActor( const v8::FunctionCallbackInfo< v8::Value >& args)
484 v8::Isolate* isolate = args.GetIsolate();
485 v8::HandleScope handleScope( isolate );
487 if( !args.IsConstructCall() )
489 DALI_SCRIPT_EXCEPTION( isolate, "constructor called without 'new" );
493 // find out the callee function name...e.g. TextActor, ImageActor, MeshActor
494 v8::Local<v8::Function> callee = args.Callee();
495 v8::Local<v8::Value> v8String = callee->GetName();
496 std::string typeName = V8Utils::v8StringToStdString( v8String );
498 // create a new actor based on type, using the type registry.
499 Actor actor = CreateActor( args, typeName );
501 v8::Local<v8::Object> localObject = WrapActor( isolate, actor );
503 args.GetReturnValue().Set( localObject );
507 * given an actor type name, e.g. ImageActor returns the type, e.g. ActorWrapper::IMAGE_ACTOR
509 ActorWrapper::ActorType ActorWrapper::GetActorType( const std::string& name )
511 for( unsigned int i = 0 ; i < ActorApiLookupCount ; i++ )
513 if( ActorApiLookup[i].actorName == name )
515 return ActorApiLookup[i].actorType;
518 return ActorWrapper::UNKNOWN_ACTOR;
523 } // namespace V8Plugin