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-api.h"
22 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
26 #include <actors/actor-wrapper.h>
27 #include <animation/path-constraint-wrapper.h>
35 namespace // unanmed namespace
37 Actor GetActor( v8::Isolate* isolate, const v8::FunctionCallbackInfo<v8::Value>& args )
39 HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, args.This() );
40 return Actor::DownCast( handleWrapper->mHandle );
45 namespace TextLabelApi
47 Actor New( const v8::FunctionCallbackInfo< v8::Value >& args )
49 return Dali::Toolkit::TextLabel::New();
53 /***************************************
55 ****************************************/
62 * @return {Object} actor
64 Actor ActorApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
70 * get the actors unique id
74 * @return {Integer} id
76 void ActorApi::GetId( const v8::FunctionCallbackInfo<v8::Value>& args )
78 v8::Isolate* isolate = args.GetIsolate();
79 v8::HandleScope handleScope( isolate );
80 Actor actor = GetActor( isolate, args );
82 args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetId() ) );
86 * Query whether an actor is the root actor, which is owned by the Stage
90 * @return {Boolean} true if it is root
93 void ActorApi::IsRoot( const v8::FunctionCallbackInfo<v8::Value>& args )
95 v8::Isolate* isolate = args.GetIsolate();
96 v8::HandleScope handleScope( isolate );
97 Actor actor = GetActor( isolate, args );
99 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsRoot() ) );
104 * Query whether the actor is connected to the Stage.
105 * When an actor is connected, it will be directly or indirectly parented to the root Actor.
106 * The root Actor is provided automatically by dali.stage, and is always considered to be connected.
110 * @return {Boolean} True if the actor is connected to the Stage
112 void ActorApi::OnStage( const v8::FunctionCallbackInfo<v8::Value>& args )
114 v8::Isolate* isolate = args.GetIsolate();
115 v8::HandleScope handleScope( isolate );
116 Actor actor = GetActor( isolate, args );
118 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.OnStage() ) );
122 * Query whether an actor is a layer
126 * @return {Boolean} true if it is a layer
128 void ActorApi::IsLayer( const v8::FunctionCallbackInfo<v8::Value>& args )
130 v8::Isolate* isolate = args.GetIsolate();
131 v8::HandleScope handleScope( isolate );
132 Actor actor = GetActor( isolate, args );
134 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsLayer() ) );
138 * Gets the layer in which the actor is present.
142 * @return {Object} Layer
144 void ActorApi::GetLayer( const v8::FunctionCallbackInfo<v8::Value>& args )
146 v8::Isolate* isolate = args.GetIsolate();
147 v8::HandleScope handleScope( isolate );
148 Actor actor = GetActor( isolate, args );
149 Layer layer = actor.GetLayer();
150 if( layer ) // actors don't always have a layer
152 v8::Handle < v8::Object > wrappedLayer = ActorWrapper::ActorWrapper::WrapActor( isolate, layer, ActorWrapper::LAYER_ACTOR );
153 args.GetReturnValue().Set( wrappedLayer );
155 // else return an empty object
159 * Adds a child Actor to this Actor.
161 * NOTE! if the child already has a parent, it will be removed from old parent
162 * and reparented to this actor. This may change childs position, color, shader effect,
163 * scale etc as it now inherits them from this actor
166 * - The child actor is not the same as the parent actor.
167 * - The actor is not the Root actor
169 * Once added The child will be referenced by its parent. This means that the child will be kept alive,
170 * even if the handle passed into this method is reset or destroyed.
174 * @param {Object} Actor
176 void ActorApi::AddActor( const v8::FunctionCallbackInfo<v8::Value>& args )
178 v8::Isolate* isolate = args.GetIsolate();
179 v8::HandleScope handleScope( isolate );
180 Actor parent = GetActor( isolate, args );
182 Actor child = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
189 DALI_SCRIPT_EXCEPTION( isolate, "child parameter missing" );
194 * Removes a child Actor from this Actor.
196 * If the actor was not a child of this actor, this is a no-op.
199 * - The child actor is not the same as the parent actor.
202 * @param{Object} Actor the child actor
205 void ActorApi::RemoveActor( const v8::FunctionCallbackInfo<v8::Value>& args )
207 v8::Isolate* isolate = args.GetIsolate();
208 v8::HandleScope handleScope( isolate );
209 Actor parent = GetActor( isolate, args );
211 Actor child = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
215 parent.Remove( child );
219 DALI_SCRIPT_EXCEPTION( isolate, "child parameter missing" );
224 * Checks whether an Actor is equal to this Actor.
228 * @param {Object} Actor
230 void ActorApi::IsEqualTo( const v8::FunctionCallbackInfo<v8::Value>& args )
232 v8::Isolate* isolate = args.GetIsolate();
233 v8::HandleScope handleScope( isolate );
234 Actor self = GetActor( isolate, args );
237 Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
240 args.GetReturnValue().Set( v8::Boolean::New( isolate, (actor == self) ) );
244 DALI_SCRIPT_EXCEPTION( isolate, "actor parameter missing" );
248 /** Removes an actor from its parent.
250 * If the actor has no parent, this method does nothing.
255 void ActorApi::Unparent( const v8::FunctionCallbackInfo< v8::Value >& args)
257 v8::Isolate* isolate = args.GetIsolate();
258 v8::HandleScope handleScope( isolate );
259 Actor actor = GetActor( isolate, args );
264 * get number of child actors
267 * @method getChildCount
268 * @return {Integer} count
270 void ActorApi::GetChildCount( const v8::FunctionCallbackInfo<v8::Value>& args )
272 v8::Isolate* isolate = args.GetIsolate();
273 v8::HandleScope handleScope( isolate );
274 Actor actor = GetActor( isolate, args );
276 args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetChildCount() ) );
280 * Retrieve and child actor by index.
284 * @param {Integer} actor index
285 * @return {Object} actor on success, empty actor handle if not found
287 void ActorApi::GetChildAt( const v8::FunctionCallbackInfo<v8::Value>& args )
289 v8::Isolate* isolate = args.GetIsolate();
290 v8::HandleScope handleScope( isolate );
291 Actor parent = GetActor( isolate, args );
293 int id = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
296 DALI_SCRIPT_EXCEPTION( isolate, "Integer parameter missing" );
299 Actor childActor = parent.GetChildAt( id );
303 v8::Handle < v8::Object > wrappedActor = ActorWrapper::WrapActor( isolate, childActor );
304 args.GetReturnValue().Set( wrappedActor );
309 * Search through this actor's hierarchy for an actor with the given name
310 * The actor itself is also considered in the search
313 * @method findChildByName
314 * @param {String} actor name
315 * @return {Object} actor on success, empty actor handle if not found
317 void ActorApi::FindChildByName( const v8::FunctionCallbackInfo<v8::Value>& args )
319 v8::Isolate* isolate = args.GetIsolate();
320 v8::HandleScope handleScope( isolate );
321 Actor parent = GetActor( isolate, args );
323 std::string name = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
326 DALI_SCRIPT_EXCEPTION( isolate, "string parameter missing" );
329 Actor childActor = parent.FindChildByName( name );
333 v8::Handle < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, childActor );
334 args.GetReturnValue().Set( wrappedLayer );
339 * Search through this actor's hierarchy for an actor with the given unique ID.
340 * The actor itself is also considered in the search
343 * @method findChildById
344 * @param {Integer} id
345 * @return {Object} actor on success, empty actor handle if not found
347 void ActorApi::FindChildById( const v8::FunctionCallbackInfo<v8::Value>& args )
349 v8::Isolate* isolate = args.GetIsolate();
350 v8::HandleScope handleScope( isolate );
351 Actor parent = GetActor( isolate, args );
354 int id = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
357 DALI_SCRIPT_EXCEPTION( isolate, "Integer parameter missing" );
360 Actor childActor = parent.FindChildById( id );
364 v8::Local < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, childActor );
365 args.GetReturnValue().Set( wrappedLayer );
371 * retrieve the actor's parent.
375 * @return {Object} actor on success, empty actor handle if actor has no parent
377 void ActorApi::GetParent( const v8::FunctionCallbackInfo<v8::Value>& args )
379 v8::Isolate* isolate = args.GetIsolate();
380 v8::HandleScope handleScope( isolate );
381 Actor actor = GetActor( isolate, args );
382 Actor parent = actor.GetParent();
386 v8::Local < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, parent );
387 args.GetReturnValue().Set( wrappedLayer );
391 * Converts screen coordinates into the actor's coordinate system using the default camera.
393 * The actor coordinates are relative to the top-left (0.0, 0.0, 0.5)
396 * var local = actor.screenToLocal( [ 10, 53 ]);
397 * var xPos = local.x;
398 * var yPos = local.y;
402 * @method screenToLocal
403 * @param {Object} ScreenCoordinates array of 2 objects
404 * @return {Object} local coordinates object with x,y properties
406 void ActorApi::ScreenToLocal( const v8::FunctionCallbackInfo<v8::Value>& args )
408 v8::Isolate* isolate = args.GetIsolate();
409 v8::HandleScope handleScope( isolate );
410 Actor actor = GetActor( isolate, args );
412 //ool ScreenToLocal(float& localX, float& localY, float screenX, float screenY) const;
415 int argCount( args.Length() );
420 vector = V8Utils::GetVector2Parameter( PARAMETER_0, found, isolate, args );
423 DALI_SCRIPT_EXCEPTION( isolate, "invalid parameters (x,y)" );
429 DALI_SCRIPT_EXCEPTION( isolate, "invalid parameters (x,y)" );
432 float localX, localY;
433 actor.ScreenToLocal( localX, localY, vector.x, vector.y );
435 v8::Local < v8::Object > localCoordinates = v8::Object::New( isolate );
437 localCoordinates->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Number::New( isolate, localX ) );
438 localCoordinates->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Number::New( isolate, localY ) );
440 args.GetReturnValue().Set( localCoordinates );
445 * Sets whether the actor should be focusable by keyboard navigation.
448 * @method setKeyboardFocusable
449 * @param {Boolean} folcusable
451 void ActorApi::SetKeyboardFocusable( const v8::FunctionCallbackInfo<v8::Value>& args )
453 v8::Isolate* isolate = args.GetIsolate();
454 v8::HandleScope handleScope( isolate );
455 Actor actor = GetActor( isolate, args );
456 bool parameterFound( false );
457 bool focus = V8Utils::GetBooleanParameter( PARAMETER_0, parameterFound, isolate, args );
458 if( !parameterFound )
460 DALI_SCRIPT_EXCEPTION( isolate, "boolean parameter missing" );
464 actor.SetKeyboardFocusable( focus );
468 * Returns whether the actor is focusable by keyboard navigation.
472 * @method isKeyboardFocusable
473 * @return {Boolean} folcusable
475 void ActorApi::IsKeyboardFocusable( const v8::FunctionCallbackInfo<v8::Value>& args )
477 v8::Isolate* isolate = args.GetIsolate();
478 v8::HandleScope handleScope( isolate );
479 Actor actor = GetActor( isolate, args );
481 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsKeyboardFocusable() ) );
485 * retrieve the actor type
488 * @method getActorType
489 * @return {String} Actor, ImageActor, MeshActor, Layer, CameraActor ...
491 void ActorApi::GetActorType( const v8::FunctionCallbackInfo<v8::Value>& args )
493 v8::Isolate* isolate = args.GetIsolate();
494 v8::HandleScope handleScope( isolate );
495 Actor actor = GetActor( isolate, args );
497 std::string name = actor.GetTypeName();
498 v8::Local < v8::String > v8String = v8::String::NewFromUtf8( isolate, name.c_str() );
499 args.GetReturnValue().Set( v8String );
502 * Move an actor relative to its existing position.
506 * actor.translateBy( [20,40,0] );
509 * @method translateBy
510 * @param {object} an array of 3 numbers
512 void ActorApi::TranslateBy( const v8::FunctionCallbackInfo<v8::Value>& args )
514 v8::Isolate* isolate = args.GetIsolate();
515 v8::HandleScope handleScope( isolate );
516 Actor actor = GetActor( isolate, args );
518 //Get displacement vector
520 int argCount( args.Length() );
524 vector = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
527 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
533 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
536 actor.TranslateBy( vector );
542 * Apply a relative rotation to an actor.
545 * var rotation =new dali.Rotation( pitch, roll, yaw );
546 * actor.rotateBy( rotation );
550 * @param {object} dali rotation object
552 void ActorApi::RotateBy( const v8::FunctionCallbackInfo<v8::Value>& args )
554 v8::Isolate* isolate = args.GetIsolate();
555 v8::HandleScope handleScope( isolate );
556 Actor actor = GetActor( isolate, args );
559 Property::Value rotation = V8Utils::GetPropertyValueParameter( PARAMETER_0, found, isolate, args );
561 if( rotation.GetType() != Property::ROTATION )
563 DALI_SCRIPT_EXCEPTION( isolate, "Rotation parameter missing" );
566 // the rotation parameter has to be either a AngleAxis or a Quaternion
567 // both will work when calling Get( Quaternion);
569 Quaternion quaternionValue;
570 rotation.Get( quaternionValue );
572 actor.RotateBy( quaternionValue );
576 * Apply a relative scale to an actor.
578 * // Double actor width and height ( keep depth the same )
580 * actor.scaleBy( [2,2,1] );
585 * @param {object} JavaScript array
587 void ActorApi::ScaleBy( const v8::FunctionCallbackInfo<v8::Value>& args )
589 v8::Isolate* isolate = args.GetIsolate();
590 v8::HandleScope handleScope( isolate );
591 Actor actor = GetActor( isolate, args );
594 int argCount( args.Length() );
598 vector = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
601 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
607 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 parameter missing" );
610 actor.ScaleBy( vector );
613 void ActorApi::ApplyPathConstraint( const v8::FunctionCallbackInfo< v8::Value >& args )
615 v8::Isolate* isolate = args.GetIsolate();
616 v8::HandleScope handleScope( isolate );
619 Actor targetActor = GetActor( isolate, args );
623 Handle pathConstraintHandle = V8Utils::GetHandleParameter(PARAMETER_0, found, isolate, args );
626 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 0 (PathConstraint)" );
629 PathConstraint pathConstraint = PathConstraint::DownCast(pathConstraintHandle);
631 //Get target property
632 std::string propertyName = V8Utils::GetStringParameter( PARAMETER_1, found, isolate, args );
635 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 1 (Property name)" );
639 // try both properties with dashes and without
640 Property::Index targetPropertyIndex = targetActor.GetPropertyIndex( propertyName );
641 if( targetPropertyIndex == Property::INVALID_INDEX )
643 std::string convertedName = V8Utils::JavaScriptNameToPropertyName( propertyName );
644 targetPropertyIndex = targetActor.GetPropertyIndex( convertedName );
646 if( targetPropertyIndex == Property::INVALID_INDEX )
648 DALI_SCRIPT_EXCEPTION( isolate, "Property not found" );
653 Actor sourceActor = V8Utils::GetActorParameter( PARAMETER_2, found, isolate, args );
656 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 2 (Actor)" );
659 // try both properties with dashes and without
660 propertyName = V8Utils::GetStringParameter( PARAMETER_3, found, isolate, args );
663 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 3 (Property name)" );
667 Property::Index sourcePropertyIndex = sourceActor.GetPropertyIndex( propertyName );
668 if( sourcePropertyIndex == Property::INVALID_INDEX )
670 std::string convertedName = V8Utils::JavaScriptNameToPropertyName( propertyName );
671 sourcePropertyIndex = sourceActor.GetPropertyIndex( convertedName );
673 if( sourcePropertyIndex == Property::INVALID_INDEX )
675 DALI_SCRIPT_EXCEPTION( isolate, "Property not found" );
680 //Check if forward vector is specified
681 Vector3 forward( 0.0f,0.0f,0.0f);
682 if( args.Length() > 4 )
684 forward = V8Utils::GetVector3Parameter( PARAMETER_4, found, isolate, args );
687 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 4 (Vector3)" );
692 pathConstraint.Apply( Dali::Property(sourceActor,sourcePropertyIndex),
693 Dali::Property(targetActor,targetPropertyIndex),
698 void ActorApi::RemovePathConstraint( const v8::FunctionCallbackInfo< v8::Value >& args )
700 v8::Isolate* isolate = args.GetIsolate();
701 v8::HandleScope handleScope( isolate );
704 Actor actor = GetActor( isolate, args );
708 Handle pathConstraintHandle = V8Utils::GetHandleParameter(PARAMETER_0, found, isolate, args );
711 DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 0 (PathConstraint)" );
714 PathConstraint pathConstraint = PathConstraint::DownCast(pathConstraintHandle);
715 pathConstraint.Remove(actor);
718 } // namespace V8Plugin