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-constrainer-wrapper.h>
28 #include <rendering/renderer-wrapper.h>
29 #include <rendering/renderer-api.h>
37 namespace // unanmed namespace
39 Actor GetActor( v8::Isolate* isolate, const v8::FunctionCallbackInfo<v8::Value>& args )
41 HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, args.This() );
42 return Actor::DownCast( handleWrapper->mHandle );
47 namespace TextLabelApi
49 Actor New( const v8::FunctionCallbackInfo< v8::Value >& args )
51 return Dali::Toolkit::TextLabel::New();
55 /***************************************
57 ****************************************/
64 * @return {Object} actor
66 Actor ActorApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
72 * get the actors unique id
76 * @return {Integer} id
78 void ActorApi::GetId( const v8::FunctionCallbackInfo<v8::Value>& args )
80 v8::Isolate* isolate = args.GetIsolate();
81 v8::HandleScope handleScope( isolate );
82 Actor actor = GetActor( isolate, args );
84 args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetId() ) );
88 * Query whether an actor is the root actor, which is owned by the Stage
92 * @return {Boolean} true if it is root
95 void ActorApi::IsRoot( const v8::FunctionCallbackInfo<v8::Value>& args )
97 v8::Isolate* isolate = args.GetIsolate();
98 v8::HandleScope handleScope( isolate );
99 Actor actor = GetActor( isolate, args );
101 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsRoot() ) );
106 * Query whether the actor is connected to the Stage.
107 * When an actor is connected, it will be directly or indirectly parented to the root Actor.
108 * The root Actor is provided automatically by dali.stage, and is always considered to be connected.
112 * @return {Boolean} True if the actor is connected to the Stage
114 void ActorApi::OnStage( const v8::FunctionCallbackInfo<v8::Value>& args )
116 v8::Isolate* isolate = args.GetIsolate();
117 v8::HandleScope handleScope( isolate );
118 Actor actor = GetActor( isolate, args );
120 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.OnStage() ) );
124 * Query whether an actor is a layer
128 * @return {Boolean} true if it is a layer
130 void ActorApi::IsLayer( const v8::FunctionCallbackInfo<v8::Value>& args )
132 v8::Isolate* isolate = args.GetIsolate();
133 v8::HandleScope handleScope( isolate );
134 Actor actor = GetActor( isolate, args );
136 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsLayer() ) );
140 * Gets the layer in which the actor is present.
144 * @return {Object} Layer
146 void ActorApi::GetLayer( const v8::FunctionCallbackInfo<v8::Value>& args )
148 v8::Isolate* isolate = args.GetIsolate();
149 v8::HandleScope handleScope( isolate );
150 Actor actor = GetActor( isolate, args );
151 Layer layer = actor.GetLayer();
152 if( layer ) // actors don't always have a layer
154 v8::Handle < v8::Object > wrappedLayer = ActorWrapper::ActorWrapper::WrapActor( isolate, layer, ActorWrapper::LAYER_ACTOR );
155 args.GetReturnValue().Set( wrappedLayer );
157 // else return an empty object
161 * Adds a child Actor to this Actor.
163 * NOTE! if the child already has a parent, it will be removed from old parent
164 * and reparented to this actor. This may change childs position, color, shader effect,
165 * scale etc as it now inherits them from this actor
168 * - The child actor is not the same as the parent actor.
169 * - The actor is not the Root actor
171 * Once added The child will be referenced by its parent. This means that the child will be kept alive,
172 * even if the handle passed into this method is reset or destroyed.
176 * @param {Object} Actor
178 void ActorApi::AddActor( const v8::FunctionCallbackInfo<v8::Value>& args )
180 v8::Isolate* isolate = args.GetIsolate();
181 v8::HandleScope handleScope( isolate );
182 Actor parent = GetActor( isolate, args );
184 Actor child = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
191 DALI_SCRIPT_EXCEPTION( isolate, "child parameter missing" );
196 * Removes a child Actor from this Actor.
198 * If the actor was not a child of this actor, this is a no-op.
201 * - The child actor is not the same as the parent actor.
204 * @param{Object} Actor the child actor
207 void ActorApi::RemoveActor( const v8::FunctionCallbackInfo<v8::Value>& args )
209 v8::Isolate* isolate = args.GetIsolate();
210 v8::HandleScope handleScope( isolate );
211 Actor parent = GetActor( isolate, args );
213 Actor child = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
217 parent.Remove( child );
221 DALI_SCRIPT_EXCEPTION( isolate, "child parameter missing" );
226 * Checks whether an Actor is equal to this Actor.
230 * @param {Object} Actor
232 void ActorApi::IsEqualTo( const v8::FunctionCallbackInfo<v8::Value>& args )
234 v8::Isolate* isolate = args.GetIsolate();
235 v8::HandleScope handleScope( isolate );
236 Actor self = GetActor( isolate, args );
239 Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
242 args.GetReturnValue().Set( v8::Boolean::New( isolate, (actor == self) ) );
246 DALI_SCRIPT_EXCEPTION( isolate, "actor parameter missing" );
250 /** Removes an actor from its parent.
252 * If the actor has no parent, this method does nothing.
257 void ActorApi::Unparent( const v8::FunctionCallbackInfo< v8::Value >& args)
259 v8::Isolate* isolate = args.GetIsolate();
260 v8::HandleScope handleScope( isolate );
261 Actor actor = GetActor( isolate, args );
266 * get number of child actors
269 * @method getChildCount
270 * @return {Integer} count
272 void ActorApi::GetChildCount( const v8::FunctionCallbackInfo<v8::Value>& args )
274 v8::Isolate* isolate = args.GetIsolate();
275 v8::HandleScope handleScope( isolate );
276 Actor actor = GetActor( isolate, args );
278 args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetChildCount() ) );
282 * Retrieve and child actor by index.
286 * @param {Integer} actor index
287 * @return {Object} actor on success, empty actor handle if not found
289 void ActorApi::GetChildAt( const v8::FunctionCallbackInfo<v8::Value>& args )
291 v8::Isolate* isolate = args.GetIsolate();
292 v8::HandleScope handleScope( isolate );
293 Actor parent = GetActor( isolate, args );
295 int id = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
298 DALI_SCRIPT_EXCEPTION( isolate, "Integer parameter missing" );
301 Actor childActor = parent.GetChildAt( id );
305 v8::Handle < v8::Object > wrappedActor = ActorWrapper::WrapActor( isolate, childActor );
306 args.GetReturnValue().Set( wrappedActor );
311 * Search through this actor's hierarchy for an actor with the given name
312 * The actor itself is also considered in the search
315 * @method findChildByName
316 * @param {String} actor name
317 * @return {Object} actor on success, empty actor handle if not found
319 void ActorApi::FindChildByName( const v8::FunctionCallbackInfo<v8::Value>& args )
321 v8::Isolate* isolate = args.GetIsolate();
322 v8::HandleScope handleScope( isolate );
323 Actor parent = GetActor( isolate, args );
325 std::string name = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
328 DALI_SCRIPT_EXCEPTION( isolate, "string parameter missing" );
331 Actor childActor = parent.FindChildByName( name );
335 v8::Handle < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, childActor );
336 args.GetReturnValue().Set( wrappedLayer );
341 * Search through this actor's hierarchy for an actor with the given unique ID.
342 * The actor itself is also considered in the search
345 * @method findChildById
346 * @param {Integer} id
347 * @return {Object} actor on success, empty actor handle if not found
349 void ActorApi::FindChildById( const v8::FunctionCallbackInfo<v8::Value>& args )
351 v8::Isolate* isolate = args.GetIsolate();
352 v8::HandleScope handleScope( isolate );
353 Actor parent = GetActor( isolate, args );
356 int id = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
359 DALI_SCRIPT_EXCEPTION( isolate, "Integer parameter missing" );
362 Actor childActor = parent.FindChildById( id );
366 v8::Local < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, childActor );
367 args.GetReturnValue().Set( wrappedLayer );
373 * retrieve the actor's parent.
377 * @return {Object} actor on success, empty actor handle if actor has no parent
379 void ActorApi::GetParent( const v8::FunctionCallbackInfo<v8::Value>& args )
381 v8::Isolate* isolate = args.GetIsolate();
382 v8::HandleScope handleScope( isolate );
383 Actor actor = GetActor( isolate, args );
384 Actor parent = actor.GetParent();
388 v8::Local < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, parent );
389 args.GetReturnValue().Set( wrappedLayer );
393 * Converts screen coordinates into the actor's coordinate system using the default camera.
395 * The actor coordinates are relative to the top-left (0.0, 0.0, 0.5)
398 * var local = actor.screenToLocal( [ 10, 53 ]);
399 * var xPos = local.x;
400 * var yPos = local.y;
404 * @method screenToLocal
405 * @param {Object} ScreenCoordinates array of 2 objects
406 * @return {Object} local coordinates object with x,y properties
408 void ActorApi::ScreenToLocal( const v8::FunctionCallbackInfo<v8::Value>& args )
410 v8::Isolate* isolate = args.GetIsolate();
411 v8::HandleScope handleScope( isolate );
412 Actor actor = GetActor( isolate, args );
414 //ool ScreenToLocal(float& localX, float& localY, float screenX, float screenY) const;
417 int argCount( args.Length() );
422 vector = V8Utils::GetVector2Parameter( PARAMETER_0, found, isolate, args );
425 DALI_SCRIPT_EXCEPTION( isolate, "invalid parameters (x,y)" );
431 DALI_SCRIPT_EXCEPTION( isolate, "invalid parameters (x,y)" );
434 float localX, localY;
435 actor.ScreenToLocal( localX, localY, vector.x, vector.y );
437 v8::Local < v8::Object > localCoordinates = v8::Object::New( isolate );
439 localCoordinates->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Number::New( isolate, localX ) );
440 localCoordinates->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Number::New( isolate, localY ) );
442 args.GetReturnValue().Set( localCoordinates );
447 * Sets whether the actor should be focusable by keyboard navigation.
450 * @method setKeyboardFocusable
451 * @param {Boolean} folcusable
453 void ActorApi::SetKeyboardFocusable( const v8::FunctionCallbackInfo<v8::Value>& args )
455 v8::Isolate* isolate = args.GetIsolate();
456 v8::HandleScope handleScope( isolate );
457 Actor actor = GetActor( isolate, args );
458 bool parameterFound( false );
459 bool focus = V8Utils::GetBooleanParameter( PARAMETER_0, parameterFound, isolate, args );
460 if( !parameterFound )
462 DALI_SCRIPT_EXCEPTION( isolate, "boolean parameter missing" );
466 actor.SetKeyboardFocusable( focus );
470 * Returns whether the actor is focusable by keyboard navigation.
474 * @method isKeyboardFocusable
475 * @return {Boolean} folcusable
477 void ActorApi::IsKeyboardFocusable( const v8::FunctionCallbackInfo<v8::Value>& args )
479 v8::Isolate* isolate = args.GetIsolate();
480 v8::HandleScope handleScope( isolate );
481 Actor actor = GetActor( isolate, args );
483 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsKeyboardFocusable() ) );
487 * retrieve the actor type
490 * @method getActorType
491 * @return {String} Actor, Layer, CameraActor ...
493 void ActorApi::GetActorType( const v8::FunctionCallbackInfo<v8::Value>& args )
495 v8::Isolate* isolate = args.GetIsolate();
496 v8::HandleScope handleScope( isolate );
497 Actor actor = GetActor( isolate, args );
499 std::string name = actor.GetTypeName();
500 v8::Local < v8::String > v8String = v8::String::NewFromUtf8( isolate, name.c_str() );
501 args.GetReturnValue().Set( v8String );
504 * Return the natural size of the actor.
507 * @method getNaturalSize
508 * @return {Object} { x, y, z }
510 void ActorApi::GetNaturalSize( const v8::FunctionCallbackInfo<v8::Value>& args )
512 v8::Isolate* isolate = args.GetIsolate();
513 v8::HandleScope handleScope( isolate );
514 Actor actor = GetActor( isolate, args );
516 Vector3 size( actor.GetNaturalSize() );
518 v8::Local<v8::Object> sizeObject = v8::Object::New( isolate );
520 sizeObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate, size.width ) );
521 sizeObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate, size.height ) );
522 sizeObject->Set( v8::String::NewFromUtf8( isolate, "z" ), v8::Integer::New( isolate, size.depth ) );
524 args.GetReturnValue().Set( sizeObject );
528 * Calculate the width of the actor given a height
530 * The natural size is used for default calculation.
531 * size 0 is treated as aspect ratio 1:1.
533 * @method getWidthForHeight
534 * @param {Float} height to use
535 * @return {Float} Return the width based on the height
537 * myTextLabel.getWidthForHeight(40);
539 * // DALi uses this formula internally
540 * // width = naturalSize.width * height / naturalSize.height;
544 void ActorApi::GetWidthForHeight( const v8::FunctionCallbackInfo<v8::Value>& args )
546 v8::Isolate* isolate = args.GetIsolate();
547 v8::HandleScope handleScope( isolate );
548 Actor actor = GetActor( isolate, args );
551 float height = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f );
554 DALI_SCRIPT_EXCEPTION( isolate, "missing height parameter");
557 args.GetReturnValue().Set( v8::Number::New( isolate, actor.GetWidthForHeight( height ) ) );
561 * Calculate the height of the actor given a width
563 * The natural size is used for default calculation.
564 * size 0 is treated as aspect ratio 1:1.
566 * @method getHeightForWidth
567 * @param {Float} width to use
568 * @return {Float} Return the height based on the width
570 * myTextLabel.getHeightForWidth(250);
572 * // DALi uses this formula internally
573 * // height = naturalSize.height * width / naturalSize.width
575 void ActorApi::GetHeightForWidth( const v8::FunctionCallbackInfo<v8::Value>& args )
577 v8::Isolate* isolate = args.GetIsolate();
578 v8::HandleScope handleScope( isolate );
579 Actor actor = GetActor( isolate, args );
582 float width = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f );
585 DALI_SCRIPT_EXCEPTION( isolate, "missing width parameter");
588 args.GetReturnValue().Set( v8::Number::New( isolate, actor.GetHeightForWidth( width ) ) );
591 * Move an actor relative to its existing position.
595 * actor.translateBy( [20,40,0] );
598 * @method translateBy
599 * @param {object} an array of 3 numbers
601 void ActorApi::TranslateBy( const v8::FunctionCallbackInfo<v8::Value>& args )
603 v8::Isolate* isolate = args.GetIsolate();
604 v8::HandleScope handleScope( isolate );
605 Actor actor = GetActor( isolate, args );
607 //Get displacement vector
609 int argCount( args.Length() );
613 vector = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
616 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
622 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
625 actor.TranslateBy( vector );
631 * Apply a relative rotation to an actor.
634 * var rotation =new dali.Rotation( pitch, roll, yaw );
635 * actor.rotateBy( rotation );
639 * @param {object} dali rotation object
641 void ActorApi::RotateBy( const v8::FunctionCallbackInfo<v8::Value>& args )
643 v8::Isolate* isolate = args.GetIsolate();
644 v8::HandleScope handleScope( isolate );
645 Actor actor = GetActor( isolate, args );
648 Property::Value rotation = V8Utils::GetPropertyValueParameter( PARAMETER_0, found, isolate, args );
650 if( rotation.GetType() != Property::ROTATION )
652 DALI_SCRIPT_EXCEPTION( isolate, "Rotation parameter missing" );
655 // the rotation parameter has to be either a AngleAxis or a Quaternion
656 // both will work when calling Get( Quaternion);
658 Quaternion quaternionValue;
659 rotation.Get( quaternionValue );
661 actor.RotateBy( quaternionValue );
665 * Apply a relative scale to an actor.
667 * // Double actor width and height ( keep depth the same )
669 * actor.scaleBy( [2,2,1] );
674 * @param {object} JavaScript array
676 void ActorApi::ScaleBy( const v8::FunctionCallbackInfo<v8::Value>& args )
678 v8::Isolate* isolate = args.GetIsolate();
679 v8::HandleScope handleScope( isolate );
680 Actor actor = GetActor( isolate, args );
683 int argCount( args.Length() );
687 vector = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
690 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
696 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 parameter missing" );
699 actor.ScaleBy( vector );
703 * Add a renderer to this actor.
706 * var renderer = new dali.Renderer( geometry, material );
707 * actor.addRenderer( renderer );
710 * @method addRenderer
711 * @param {object} renderer Renderer to add to the actor
712 * @return {integer} The index of the Renderer that was added
714 void ActorApi::AddRenderer( const v8::FunctionCallbackInfo<v8::Value>& args )
716 v8::Isolate* isolate = args.GetIsolate();
717 v8::HandleScope handleScope( isolate );
718 Actor actor = GetActor( isolate, args );
720 unsigned int index = 0;
723 Renderer renderer = RendererApi::GetRendererFromParams( 0, found, isolate, args );
726 index = actor.AddRenderer(renderer);
730 DALI_SCRIPT_EXCEPTION( isolate, "Renderer parameter missing" );
734 args.GetReturnValue().Set( v8::Integer::New( isolate, index ) );
738 * Get the number of renderers on this actor.
741 * var count = actor.getRendererCount();
744 * @method getRendererCount
745 * @return {integer} the number of renderers on this actor
747 void ActorApi::GetRendererCount( const v8::FunctionCallbackInfo<v8::Value>& args )
749 v8::Isolate* isolate = args.GetIsolate();
750 v8::HandleScope handleScope( isolate );
752 Actor actor = GetActor( isolate, args );
753 args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetRendererCount() ) );
757 * Get a Renderer by index.
760 * var renderer = actor.getRendererAt( 0 );
763 * @method getRendererAt
764 * @param {integer} index The index of the renderer to fetch, which must be between 0 and getRendererCount()-1
765 * @return {object} The renderer at the specified index
767 void ActorApi::GetRendererAt( const v8::FunctionCallbackInfo<v8::Value>& args )
769 v8::Isolate* isolate = args.GetIsolate();
770 v8::HandleScope handleScope( isolate );
771 Actor actor = GetActor( isolate, args );
776 int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0);
779 DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
784 renderer = actor.GetRendererAt(static_cast<unsigned int>(index));
787 DALI_SCRIPT_EXCEPTION( isolate, "renderer not found" );
793 v8::Local<v8::Object> localObject = RendererWrapper::WrapRenderer( isolate, renderer );
794 args.GetReturnValue().Set( localObject );
798 * Remove an renderer from the actor by index.
801 * actor.removeRenderer( 0 );
804 * @method removeRenderer
805 * @param {integer} index Index of the renderer to be removed, which must be between 0 and getRendererCount()-1
807 void ActorApi::RemoveRenderer( const v8::FunctionCallbackInfo<v8::Value>& args )
809 v8::Isolate* isolate = args.GetIsolate();
810 v8::HandleScope handleScope( isolate );
811 Actor actor = GetActor( isolate, args );
814 int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0);
817 DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
821 actor.RemoveRenderer(static_cast<unsigned int>(index));
826 } // namespace V8Plugin