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 { "TranslateBy", ActorApi::TranslateBy, 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 SetOrientation() use Actor.orientation
220 { "RotateBy", ActorApi::RotateBy, ACTOR_API },
221 // ignore GetCurrentOrientation() use Actor.orientation
222 // ignore SetInheritOrientation() use Actor.inheritOrientation
223 // ignore IsOrientationInherited() use Actor.inheritOrientation
224 // ignore GetCurrentWorldOrientation() use Actor.worldOrientation
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 // ignore GetCurrentOpacity() use Actor.opacity
236 // ignore SetColor() use Actor.color
237 // ignore GetCurrentColor() use Actor.color
238 // ignore SetColorMode() use Actor.colorMode
239 // ignore GetColorMode() use Actor.colorMode
240 // ignore GetCurrentWorldColor() use Actor.worldColor
241 // ignore SetInheritShaderEffect() use Actor.inheritShaderEffect
242 // ignore GetInheritShaderEffect() use Actor.inheritShaderEffect
243 // ignore SetDrawMode() use Actor.drawMode
244 // ignore GetDrawMode() use Actor.drawMode
245 // ignore SetSensitive() use Actor.sensitve
246 // ignore IsSensitive() use Actor.sensitive
247 { "ScreenToLocal" , ActorApi::ScreenToLocal, ACTOR_API},
248 // ignore SetLeaveRequired() use Actor.leaveRequired
249 // ignore GetLeaveRequired() use Actor.leaveRequired
250 { "SetKeyboardFocusable", ActorApi::SetKeyboardFocusable, ACTOR_API }, //-- should this be a property???
251 { "IsKeyboardFocusable" , ActorApi::IsKeyboardFocusable, ACTOR_API }, //-- should this be a property???
253 /**************************************
254 * Renderable Actor API (in order of renderable-actor.h)
255 **************************************/
256 { "SetSortModifier", RenderableActorApi::SetSortModifier, RENDERABLE_ACTOR_API },
257 { "GetSortModifier", RenderableActorApi::GetSortModifier, RENDERABLE_ACTOR_API },
258 { "SetCullFace", RenderableActorApi::SetCullFace, RENDERABLE_ACTOR_API },
259 { "GetCullFace", RenderableActorApi::GetCullFace, RENDERABLE_ACTOR_API },
260 { "SetBlendMode", RenderableActorApi::SetBlendMode, RENDERABLE_ACTOR_API },
261 { "GetBlendMode", RenderableActorApi::GetBlendMode, RENDERABLE_ACTOR_API },
262 { "SetBlendFunc", RenderableActorApi::SetBlendFunc, RENDERABLE_ACTOR_API },
263 { "GetBlendFunc", RenderableActorApi::GetBlendFunc, RENDERABLE_ACTOR_API },
264 { "SetBlendEquation", RenderableActorApi::SetBlendEquation, RENDERABLE_ACTOR_API },
265 { "GetBlendEquation", RenderableActorApi::GetBlendEquation, RENDERABLE_ACTOR_API },
266 { "SetBlendColor", RenderableActorApi::SetBlendColor, RENDERABLE_ACTOR_API },
267 { "GetBlendColor", RenderableActorApi::GetBlendColor, RENDERABLE_ACTOR_API },
268 { "SetShaderEffect", RenderableActorApi::SetShaderEffect, RENDERABLE_ACTOR_API },
269 { "GetShaderEffect", RenderableActorApi::GetShaderEffect, RENDERABLE_ACTOR_API },
270 { "RemoveShaderEffect", RenderableActorApi::RemoveShaderEffect,RENDERABLE_ACTOR_API },
275 /**************************************
276 * Layer API (in order of layer.h)
277 **************************************/
278 { "GetDepth", LayerApi::GetDepth, LAYER_API },
279 { "Raise", LayerApi::Raise, LAYER_API },
280 { "Lower", LayerApi::Lower, LAYER_API },
281 { "RaiseAbove", LayerApi::RaiseAbove, LAYER_API },
282 { "RaiseBelow", LayerApi::LowerBelow, LAYER_API },
283 { "RaiseToTop", LayerApi::RaiseToTop, LAYER_API },
284 { "LowerToBottom", LayerApi::ToBottom, LAYER_API },
285 { "MoveAbove", LayerApi::MoveAbove, LAYER_API },
286 { "MoveBelow", LayerApi::MoveBelow, LAYER_API },
287 // ignore SetClipping, use layer.clippingEnable
288 // ignore IsClipping, use layer.clippingEnable
289 // ignore SetClippingBox, use layer.clippingBox
290 { "SetDepthTestDisabled", LayerApi::SetDepthTestDisabled, LAYER_API },
291 { "IsDepthTestDisabled", LayerApi::IsDepthTestDisabled, LAYER_API },
292 // @todo SetSortFunction
294 /**************************************
295 * Image Actor API (in order of image-actor.h)
296 **************************************/
298 { "SetImage", ImageActorApi::SetImage, IMAGE_ACTOR_API },
299 { "GetImage", ImageActorApi::GetImage, IMAGE_ACTOR_API },
300 { "SetToNaturalSize", ImageActorApi::SetToNaturalSize, IMAGE_ACTOR_API },
301 // ignore SetPixelArea, use imageActor.pixelArea
302 // ignore GetPixelArea, use imageActor.pixelArea
303 { "IsPixelAreaSet", ImageActorApi::IsPixelAreaSet, IMAGE_ACTOR_API },
304 { "ClearPixelArea", ImageActorApi::ClearPixelArea, IMAGE_ACTOR_API },
305 // ignore SetStyle, use imageActor.style
306 // ignore GetStyle, use imageActor.style
307 // ignore SetNinePatchBorder use imageActor.border
308 // ignore GetNinePatchBorder use imageActor.border
309 // ignore SetFadeIn use imageActor.fadeIn
310 // ignore GetFadeIn use imageActor.fadeIn
311 // ignore SetFadeInDuration use imageActor.fadeInDuration
312 // ignore GetFadeInDuration use imageActor.fadeInDuration
313 //{ "GetCurrentImageSize", ImageActorApi::GetCurrentImageSize, IMAGE_ACTOR_API },
316 /**************************************
317 * Text Actor API (in order of text-actor.h)
318 **************************************/
319 //ignore SetText use textActor.text
320 { "SetToNaturalSize", TextActorApi::SetToNaturalSize, TEXT_ACTOR_API },
321 // ignore GetFont use textActor.font
322 // ignore SetFont use textActor.font
323 // ignore SetGradient use textActor.gradientColor
324 // ignore GetGradient textActor.gradientColor
325 // ignore SetGradientStartPoint use textActor.gradientStartPoint
326 // ignore GetGradientStartPoint textActor.gradientStartPoint
327 // ignore SetGradientEndPoint use textActor.gradientEndPoint
328 // ignore GetGradientEndPoint textActor.gradientEndPoint
329 // @todo? SetTextStyle ( can use individual properties as a work around )
330 // @todo? GetTextStyle ( can use individual properties as a work around )
331 // ignore SetTextColor use textActor.textColor
332 // ignore GetTextColor use textActor.textColor
333 // ignore SetSmoothEdge use textActor.smoothEdge
334 // ignore SetOutline use textActor.outLineEnable, outlineColor, thicknessWidth
335 // ignore SetGlow use textActor.glowEnable, glowColor, glowIntensity
336 // ignore SetShadow use textActor.shadowEnable, shadowColor, shadowOffset, shadowSize
337 // ignore SetItalics use textActor.italicsAngle ?
338 // ignore GetItalics @todo add italics flag? or just stick with angle
339 // ignore GetItalicsAngle use textActor.italicsAngle
340 // ignore SetUnderline use textActor.underline
341 // ignore GetUnderline use textActor.underline
342 // ignore SetWeight use textActor.weight
343 // ignore GetWeight use textActor.weight
344 // ignore SetFontDetectionAutomatic use textActor.fontDetectionAutomatic
345 // ignore IsFontDetectionAutomatic use textActor.fontDetectionAutomatic
346 // ignore GetLoadingState text is loaded synchronously
347 // ignore TextAvailableSignal text is loaded synchronously
349 /**************************************
350 * Mesh Actor API (in order of mesh-actor.h)
351 **************************************/
352 // @todo a version of MeshActor::New( mesh )
353 // @todo a version of MeshActor::New( AnimatableMesh )
356 // @todo BindBonesToMesh
358 /**************************************
359 * Camera Actor API (in order of camera.h)
360 **************************************/
361 // ignore SetType use camera.type
362 // ignore GetType use camera.type
363 // ignore SetProjectionMode use camera.projectionMode
364 // ignore GetProjectionMode use camera.projectionMode
365 // ignore SetFieldOfView use camera.fieldOfView
366 // ignore GetFieldOfView use camera.fieldOfView
367 // ignore SetAspectRatio use camera.aspectRatio
368 // ignore GetAspectRatio use camera.aspectRatio
369 // ignore SetNearClippingPlane use camera.nearPlaneDistance
370 // ignore GetNearClippingPlane use camera.nearPlaneDistance
371 // ignore SetFarClippingPlane use camera.farPlaneDistance
372 // ignore GetFarClippingPlane use camera.farPlaneDistance
373 // ignore GetTargetPosition use camera.targetPosition
374 // ignore SetInvertYAxis use camera.invertYAxis
375 // ignore GetInvertYAxis use camera.invertYAxis
376 { "SetPerspectiveProjection", CameraActorApi::SetPerspectiveProjection, CAMERA_ACTOR_API },
377 { "SetOrthographicProjection", CameraActorApi::SetOrthographicProjection, CAMERA_ACTOR_API },
381 const unsigned int ActorFunctionTableCount = sizeof(ActorFunctionTable)/sizeof(ActorFunctionTable[0]);
385 ActorWrapper::ActorWrapper( Actor actor,
386 GarbageCollectorInterface& gc )
387 : HandleWrapper( BaseWrappedObject::ACTOR , actor, gc ),
393 v8::Handle<v8::Object> ActorWrapper::WrapActor(v8::Isolate* isolate, Actor actor )
395 v8::EscapableHandleScope handleScope( isolate );
396 v8::Local<v8::Object> object = WrapActor( isolate, actor, GetActorType( actor.GetTypeName() ) );
398 return handleScope.Escape( object );
401 Actor ActorWrapper::GetActor()
406 v8::Handle<v8::Object> ActorWrapper::WrapActor( v8::Isolate* isolate, Actor actor, ActorType actorType )
408 v8::EscapableHandleScope handleScope( isolate );
409 v8::Local<v8::ObjectTemplate> objectTemplate;
411 objectTemplate = GetActorTemplate( isolate, actorType );
413 // create an instance of the template
414 v8::Local<v8::Object> localObject = objectTemplate->NewInstance();
416 // create teh actor object
417 ActorWrapper* pointer = new ActorWrapper( actor, Dali::V8Plugin::DaliWrapper::Get().GetDaliGarbageCollector() );
419 // assign the JavaScript object to the wrapper.
420 // This also stores Dali object, in an internal field inside the JavaScript object.
421 pointer->SetJavascriptObject( isolate, localObject );
423 return handleScope.Escape( localObject );
426 v8::Local<v8::ObjectTemplate> ActorWrapper::GetActorTemplate( v8::Isolate* isolate, ActorWrapper::ActorType type )
428 v8::EscapableHandleScope handleScope( isolate );
429 v8::Local<v8::ObjectTemplate> objectTemplate;
431 if( ActorTemplateLookup[type].actorTemplate->IsEmpty() )
433 objectTemplate = MakeDaliActorTemplate( isolate, type );
434 ActorTemplateLookup[type].actorTemplate->Reset( isolate, objectTemplate );
438 // get the object template
439 objectTemplate = v8::Local<v8::ObjectTemplate>::New( isolate, *ActorTemplateLookup[type].actorTemplate );
442 return handleScope.Escape( objectTemplate );
445 v8::Handle<v8::ObjectTemplate> ActorWrapper::MakeDaliActorTemplate( v8::Isolate* isolate, ActorType actorType )
447 v8::EscapableHandleScope handleScope( isolate );
449 v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New();
451 objTemplate->SetInternalFieldCount( BaseWrappedObject::FIELD_COUNT );
453 // find out what API's this actor supports
454 int supportApis = GetActorSupportedApis( actorType );
456 // add our function properties
457 for( unsigned int i = 0; i < ActorFunctionTableCount; ++i )
459 const ActorFunctions property = ActorFunctionTable[i];
461 // check to see if the actor supports a certain type of API
462 // e.g. ImageActor will support ACTOR_API, RENDERABLE_API and IMAGE_ACTOR_API
463 if( supportApis & property.api )
465 std::string funcName = V8Utils::GetJavaScriptFunctionName( property.name);
467 objTemplate->Set( v8::String::NewFromUtf8( isolate, funcName.c_str() ),
468 v8::FunctionTemplate::New( isolate, property.function ) );
472 // property handle intercepts property getters and setters and signals
473 HandleWrapper::AddInterceptsToTemplate( isolate, objTemplate );
476 return handleScope.Escape( objTemplate );
479 void ActorWrapper::NewActor( const v8::FunctionCallbackInfo< v8::Value >& args)
481 v8::Isolate* isolate = args.GetIsolate();
482 v8::HandleScope handleScope( isolate );
484 if( !args.IsConstructCall() )
486 DALI_SCRIPT_EXCEPTION( isolate, "constructor called without 'new" );
490 // find out the callee function name...e.g. TextActor, ImageActor, MeshActor
491 v8::Local<v8::Function> callee = args.Callee();
492 v8::Local<v8::Value> v8String = callee->GetName();
493 std::string typeName = V8Utils::v8StringToStdString( v8String );
495 // create a new actor based on type, using the type registry.
496 Actor actor = CreateActor( args, typeName );
498 v8::Local<v8::Object> localObject = WrapActor( isolate, actor );
500 args.GetReturnValue().Set( localObject );
504 * given an actor type name, e.g. ImageActor returns the type, e.g. ActorWrapper::IMAGE_ACTOR
506 ActorWrapper::ActorType ActorWrapper::GetActorType( const std::string& name )
508 for( unsigned int i = 0 ; i < ActorApiLookupCount ; i++ )
510 if( ActorApiLookup[i].actorName == name )
512 return ActorApiLookup[i].actorType;
515 return ActorWrapper::UNKNOWN_ACTOR;
520 } // namespace V8Plugin