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
40 Actor GetActor( v8::Isolate* isolate, const v8::FunctionCallbackInfo<v8::Value>& args )
42 HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, args.This() );
43 return Actor::DownCast( handleWrapper->mHandle );
48 /***************************************
50 ****************************************/
57 * @return {Object} actor
59 Actor ActorApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
65 * get the actors unique id
69 * @return {Integer} id
71 void ActorApi::GetId( const v8::FunctionCallbackInfo<v8::Value>& args )
73 v8::Isolate* isolate = args.GetIsolate();
74 v8::HandleScope handleScope( isolate );
75 Actor actor = GetActor( isolate, args );
77 args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetId() ) );
81 * Query whether an actor is the root actor, which is owned by the Stage
85 * @return {Boolean} true if it is root
88 void ActorApi::IsRoot( const v8::FunctionCallbackInfo<v8::Value>& args )
90 v8::Isolate* isolate = args.GetIsolate();
91 v8::HandleScope handleScope( isolate );
92 Actor actor = GetActor( isolate, args );
94 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsRoot() ) );
99 * Query whether the actor is connected to the Stage.
100 * When an actor is connected, it will be directly or indirectly parented to the root Actor.
101 * The root Actor is provided automatically by dali.stage, and is always considered to be connected.
105 * @return {Boolean} True if the actor is connected to the Stage
107 void ActorApi::OnStage( const v8::FunctionCallbackInfo<v8::Value>& args )
109 v8::Isolate* isolate = args.GetIsolate();
110 v8::HandleScope handleScope( isolate );
111 Actor actor = GetActor( isolate, args );
113 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.OnStage() ) );
117 * Query whether an actor is a layer
121 * @return {Boolean} true if it is a layer
123 void ActorApi::IsLayer( const v8::FunctionCallbackInfo<v8::Value>& args )
125 v8::Isolate* isolate = args.GetIsolate();
126 v8::HandleScope handleScope( isolate );
127 Actor actor = GetActor( isolate, args );
129 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsLayer() ) );
133 * Gets the layer in which the actor is present.
137 * @return {Object} Layer
139 void ActorApi::GetLayer( const v8::FunctionCallbackInfo<v8::Value>& args )
141 v8::Isolate* isolate = args.GetIsolate();
142 v8::HandleScope handleScope( isolate );
143 Actor actor = GetActor( isolate, args );
144 Layer layer = actor.GetLayer();
145 if( layer ) // actors don't always have a layer
147 v8::Handle < v8::Object > wrappedLayer = ActorWrapper::ActorWrapper::WrapActor( isolate, layer, ActorWrapper::LAYER_ACTOR );
148 args.GetReturnValue().Set( wrappedLayer );
150 // else return an empty object
154 * Adds a child Actor to this Actor.
156 * NOTE! if the child already has a parent, it will be removed from old parent
157 * and reparented to this actor. This may change childs position, color, shader effect,
158 * scale etc as it now inherits them from this actor
161 * - The child actor is not the same as the parent actor.
162 * - The actor is not the Root actor
164 * Once added The child will be referenced by its parent. This means that the child will be kept alive,
165 * even if the handle passed into this method is reset or destroyed.
169 * @param {Object} Actor
171 void ActorApi::AddActor( const v8::FunctionCallbackInfo<v8::Value>& args )
173 v8::Isolate* isolate = args.GetIsolate();
174 v8::HandleScope handleScope( isolate );
175 Actor parent = GetActor( isolate, args );
177 Actor child = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
184 DALI_SCRIPT_EXCEPTION( isolate, "child parameter missing" );
189 * Removes a child Actor from this Actor.
191 * If the actor was not a child of this actor, this is a no-op.
194 * - The child actor is not the same as the parent actor.
197 * @param{Object} Actor the child actor
200 void ActorApi::RemoveActor( const v8::FunctionCallbackInfo<v8::Value>& args )
202 v8::Isolate* isolate = args.GetIsolate();
203 v8::HandleScope handleScope( isolate );
204 Actor parent = GetActor( isolate, args );
206 Actor child = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
210 parent.Remove( child );
214 DALI_SCRIPT_EXCEPTION( isolate, "child parameter missing" );
219 * Checks whether an Actor is equal to this Actor.
223 * @param {Object} Actor
225 void ActorApi::IsEqualTo( const v8::FunctionCallbackInfo<v8::Value>& args )
227 v8::Isolate* isolate = args.GetIsolate();
228 v8::HandleScope handleScope( isolate );
229 Actor self = GetActor( isolate, args );
232 Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
235 args.GetReturnValue().Set( v8::Boolean::New( isolate, (actor == self) ) );
239 DALI_SCRIPT_EXCEPTION( isolate, "actor parameter missing" );
243 /** Removes an actor from its parent.
245 * If the actor has no parent, this method does nothing.
250 void ActorApi::Unparent( const v8::FunctionCallbackInfo< v8::Value >& args)
252 v8::Isolate* isolate = args.GetIsolate();
253 v8::HandleScope handleScope( isolate );
254 Actor actor = GetActor( isolate, args );
259 * get number of child actors
262 * @method getChildCount
263 * @return {Integer} count
265 void ActorApi::GetChildCount( const v8::FunctionCallbackInfo<v8::Value>& args )
267 v8::Isolate* isolate = args.GetIsolate();
268 v8::HandleScope handleScope( isolate );
269 Actor actor = GetActor( isolate, args );
271 args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetChildCount() ) );
275 * Retrieve a child actor by index.
279 * @param {Integer} actor index
280 * @return {Object} actor on success, empty actor handle if not found
282 void ActorApi::GetChildAt( const v8::FunctionCallbackInfo<v8::Value>& args )
284 v8::Isolate* isolate = args.GetIsolate();
285 v8::HandleScope handleScope( isolate );
286 Actor parent = GetActor( isolate, args );
288 int id = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
291 DALI_SCRIPT_EXCEPTION( isolate, "Integer parameter missing" );
294 Actor childActor = parent.GetChildAt( id );
298 v8::Handle < v8::Object > wrappedActor = ActorWrapper::WrapActor( isolate, childActor );
299 args.GetReturnValue().Set( wrappedActor );
304 * Search through this actor's hierarchy for an actor with the given name
305 * The actor itself is also considered in the search
308 * @method findChildByName
309 * @param {String} actor name
310 * @return {Object} actor on success, empty actor handle if not found
312 void ActorApi::FindChildByName( const v8::FunctionCallbackInfo<v8::Value>& args )
314 v8::Isolate* isolate = args.GetIsolate();
315 v8::HandleScope handleScope( isolate );
316 Actor parent = GetActor( isolate, args );
318 std::string name = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
321 DALI_SCRIPT_EXCEPTION( isolate, "string parameter missing" );
324 Actor childActor = parent.FindChildByName( name );
328 v8::Handle < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, childActor );
329 args.GetReturnValue().Set( wrappedLayer );
334 * Search through this actor's hierarchy for an actor with the given unique ID.
335 * The actor itself is also considered in the search
338 * @method findChildById
339 * @param {Integer} id
340 * @return {Object} actor on success, empty actor handle if not found
342 void ActorApi::FindChildById( const v8::FunctionCallbackInfo<v8::Value>& args )
344 v8::Isolate* isolate = args.GetIsolate();
345 v8::HandleScope handleScope( isolate );
346 Actor parent = GetActor( isolate, args );
349 int id = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
352 DALI_SCRIPT_EXCEPTION( isolate, "Integer parameter missing" );
355 Actor childActor = parent.FindChildById( id );
359 v8::Local < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, childActor );
360 args.GetReturnValue().Set( wrappedLayer );
366 * retrieve the actor's parent.
370 * @return {Object} actor on success, empty actor handle if actor has no parent
372 void ActorApi::GetParent( const v8::FunctionCallbackInfo<v8::Value>& args )
374 v8::Isolate* isolate = args.GetIsolate();
375 v8::HandleScope handleScope( isolate );
376 Actor actor = GetActor( isolate, args );
377 Actor parent = actor.GetParent();
381 v8::Local < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, parent );
382 args.GetReturnValue().Set( wrappedLayer );
386 * Converts screen coordinates into the actor's coordinate system using the default camera.
388 * The actor coordinates are relative to the top-left (0.0, 0.0, 0.5)
391 * var local = actor.screenToLocal( [ 10, 53 ]);
392 * var xPos = local.x;
393 * var yPos = local.y;
397 * @method screenToLocal
398 * @param {Object} ScreenCoordinates array of 2 objects
399 * @return {Object} local coordinates object with x,y properties
401 void ActorApi::ScreenToLocal( const v8::FunctionCallbackInfo<v8::Value>& args )
403 v8::Isolate* isolate = args.GetIsolate();
404 v8::HandleScope handleScope( isolate );
405 Actor actor = GetActor( isolate, args );
407 //ool ScreenToLocal(float& localX, float& localY, float screenX, float screenY) const;
410 int argCount( args.Length() );
415 vector = V8Utils::GetVector2Parameter( PARAMETER_0, found, isolate, args );
418 DALI_SCRIPT_EXCEPTION( isolate, "invalid parameters (x,y)" );
424 DALI_SCRIPT_EXCEPTION( isolate, "invalid parameters (x,y)" );
427 float localX, localY;
428 actor.ScreenToLocal( localX, localY, vector.x, vector.y );
430 v8::Local < v8::Object > localCoordinates = v8::Object::New( isolate );
432 localCoordinates->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Number::New( isolate, localX ) );
433 localCoordinates->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Number::New( isolate, localY ) );
435 args.GetReturnValue().Set( localCoordinates );
440 * Sets whether the actor should be focusable by keyboard navigation.
443 * @method setKeyboardFocusable
444 * @param {Boolean} folcusable
446 void ActorApi::SetKeyboardFocusable( const v8::FunctionCallbackInfo<v8::Value>& args )
448 v8::Isolate* isolate = args.GetIsolate();
449 v8::HandleScope handleScope( isolate );
450 Actor actor = GetActor( isolate, args );
451 bool parameterFound( false );
452 bool focus = V8Utils::GetBooleanParameter( PARAMETER_0, parameterFound, isolate, args );
453 if( !parameterFound )
455 DALI_SCRIPT_EXCEPTION( isolate, "boolean parameter missing" );
459 actor.SetKeyboardFocusable( focus );
463 * Returns whether the actor is focusable by keyboard navigation.
467 * @method isKeyboardFocusable
468 * @return {Boolean} folcusable
470 void ActorApi::IsKeyboardFocusable( const v8::FunctionCallbackInfo<v8::Value>& args )
472 v8::Isolate* isolate = args.GetIsolate();
473 v8::HandleScope handleScope( isolate );
474 Actor actor = GetActor( isolate, args );
476 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsKeyboardFocusable() ) );
480 * retrieve the actor type
483 * @method getActorType
484 * @return {String} Actor, Layer, CameraActor ...
486 void ActorApi::GetActorType( const v8::FunctionCallbackInfo<v8::Value>& args )
488 v8::Isolate* isolate = args.GetIsolate();
489 v8::HandleScope handleScope( isolate );
490 Actor actor = GetActor( isolate, args );
492 std::string name = actor.GetTypeName();
493 v8::Local < v8::String > v8String = v8::String::NewFromUtf8( isolate, name.c_str() );
494 args.GetReturnValue().Set( v8String );
497 * Return the natural size of the actor.
500 * @method getNaturalSize
501 * @return {Object} { x, y, z }
503 void ActorApi::GetNaturalSize( const v8::FunctionCallbackInfo<v8::Value>& args )
505 v8::Isolate* isolate = args.GetIsolate();
506 v8::HandleScope handleScope( isolate );
507 Actor actor = GetActor( isolate, args );
509 Vector3 size( actor.GetNaturalSize() );
511 v8::Local<v8::Object> sizeObject = v8::Object::New( isolate );
513 sizeObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate, size.width ) );
514 sizeObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate, size.height ) );
515 sizeObject->Set( v8::String::NewFromUtf8( isolate, "z" ), v8::Integer::New( isolate, size.depth ) );
517 args.GetReturnValue().Set( sizeObject );
521 * Return the value of negotiated dimension for the given dimension
524 * @method getRelayoutSize
525 * @param {Integer} dimension The dimension of layout to retrieve (either dali.DIMENSION_WIDTH or dali.DIMENSION_HEIGHT)
526 * @return {Number} The value of the negotiated dimension
528 void ActorApi::GetRelayoutSize( const v8::FunctionCallbackInfo<v8::Value>& args )
530 v8::Isolate* isolate = args.GetIsolate();
531 v8::HandleScope handleScope( isolate );
532 Actor actor = GetActor( isolate, args );
535 int dimension = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
538 DALI_SCRIPT_EXCEPTION( isolate, "missing dimension parameter");
542 args.GetReturnValue().Set( v8::Number::New( isolate, actor.GetRelayoutSize( static_cast<Dimension::Type>(dimension) ) ) );
546 * Calculate the width of the actor given a height
548 * The natural size is used for default calculation.
549 * size 0 is treated as aspect ratio 1:1.
551 * @method getWidthForHeight
552 * @param {Float} height to use
553 * @return {Float} Return the width based on the height
555 * myTextLabel.getWidthForHeight(40);
557 * // DALi uses this formula internally
558 * // width = naturalSize.width * height / naturalSize.height;
562 void ActorApi::GetWidthForHeight( const v8::FunctionCallbackInfo<v8::Value>& args )
564 v8::Isolate* isolate = args.GetIsolate();
565 v8::HandleScope handleScope( isolate );
566 Actor actor = GetActor( isolate, args );
569 float height = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f );
572 DALI_SCRIPT_EXCEPTION( isolate, "missing height parameter");
575 args.GetReturnValue().Set( v8::Number::New( isolate, actor.GetWidthForHeight( height ) ) );
579 * Calculate the height of the actor given a width
581 * The natural size is used for default calculation.
582 * size 0 is treated as aspect ratio 1:1.
584 * @method getHeightForWidth
585 * @param {Float} width to use
586 * @return {Float} Return the height based on the width
588 * myTextLabel.getHeightForWidth(250);
590 * // DALi uses this formula internally
591 * // height = naturalSize.height * width / naturalSize.width
593 void ActorApi::GetHeightForWidth( const v8::FunctionCallbackInfo<v8::Value>& args )
595 v8::Isolate* isolate = args.GetIsolate();
596 v8::HandleScope handleScope( isolate );
597 Actor actor = GetActor( isolate, args );
600 float width = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f );
603 DALI_SCRIPT_EXCEPTION( isolate, "missing width parameter");
606 args.GetReturnValue().Set( v8::Number::New( isolate, actor.GetHeightForWidth( width ) ) );
609 * Move an actor relative to its existing position.
613 * actor.translateBy( [20,40,0] );
616 * @method translateBy
617 * @param {object} an array of 3 numbers
619 void ActorApi::TranslateBy( const v8::FunctionCallbackInfo<v8::Value>& args )
621 v8::Isolate* isolate = args.GetIsolate();
622 v8::HandleScope handleScope( isolate );
623 Actor actor = GetActor( isolate, args );
625 //Get displacement vector
627 int argCount( args.Length() );
631 vector = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
634 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
640 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
643 actor.TranslateBy( vector );
649 * Apply a relative rotation to an actor.
652 * var rotation =new dali.Rotation( pitch, roll, yaw );
653 * actor.rotateBy( rotation );
657 * @param {object} dali rotation object
659 void ActorApi::RotateBy( const v8::FunctionCallbackInfo<v8::Value>& args )
661 v8::Isolate* isolate = args.GetIsolate();
662 v8::HandleScope handleScope( isolate );
663 Actor actor = GetActor( isolate, args );
666 Property::Value rotation = V8Utils::GetPropertyValueParameter( PARAMETER_0, found, isolate, args );
668 if( rotation.GetType() != Property::ROTATION )
670 DALI_SCRIPT_EXCEPTION( isolate, "Rotation parameter missing" );
673 // the rotation parameter has to be either a AngleAxis or a Quaternion
674 // both will work when calling Get( Quaternion);
676 Quaternion quaternionValue;
677 rotation.Get( quaternionValue );
679 actor.RotateBy( quaternionValue );
683 * Apply a relative scale to an actor.
685 * // Double actor width and height ( keep depth the same )
687 * actor.scaleBy( [2,2,1] );
692 * @param {object} JavaScript array
694 void ActorApi::ScaleBy( const v8::FunctionCallbackInfo<v8::Value>& args )
696 v8::Isolate* isolate = args.GetIsolate();
697 v8::HandleScope handleScope( isolate );
698 Actor actor = GetActor( isolate, args );
701 int argCount( args.Length() );
705 vector = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
708 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
714 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 parameter missing" );
717 actor.ScaleBy( vector );
721 * Add a renderer to this actor.
724 * var renderer = new dali.Renderer( geometry, shader );
725 * actor.addRenderer( renderer );
728 * @method addRenderer
729 * @param {object} renderer Renderer to add to the actor
730 * @return {integer} The index of the Renderer that was added
732 void ActorApi::AddRenderer( const v8::FunctionCallbackInfo<v8::Value>& args )
734 v8::Isolate* isolate = args.GetIsolate();
735 v8::HandleScope handleScope( isolate );
736 Actor actor = GetActor( isolate, args );
738 unsigned int index = 0;
741 Renderer renderer = RendererApi::GetRendererFromParams( 0, found, isolate, args );
744 index = actor.AddRenderer(renderer);
748 DALI_SCRIPT_EXCEPTION( isolate, "Renderer parameter missing" );
752 args.GetReturnValue().Set( v8::Integer::New( isolate, index ) );
756 * Get the number of renderers on this actor.
759 * var count = actor.getRendererCount();
762 * @method getRendererCount
763 * @return {integer} the number of renderers on this actor
765 void ActorApi::GetRendererCount( const v8::FunctionCallbackInfo<v8::Value>& args )
767 v8::Isolate* isolate = args.GetIsolate();
768 v8::HandleScope handleScope( isolate );
770 Actor actor = GetActor( isolate, args );
771 args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetRendererCount() ) );
775 * Get a Renderer by index.
778 * var renderer = actor.getRendererAt( 0 );
781 * @method getRendererAt
782 * @param {integer} index The index of the renderer to fetch, which must be between 0 and getRendererCount()-1
783 * @return {object} The renderer at the specified index
785 void ActorApi::GetRendererAt( const v8::FunctionCallbackInfo<v8::Value>& args )
787 v8::Isolate* isolate = args.GetIsolate();
788 v8::HandleScope handleScope( isolate );
789 Actor actor = GetActor( isolate, args );
794 int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0);
797 DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
802 renderer = actor.GetRendererAt(static_cast<unsigned int>(index));
805 DALI_SCRIPT_EXCEPTION( isolate, "renderer not found" );
811 v8::Local<v8::Object> localObject = RendererWrapper::WrapRenderer( isolate, renderer );
812 args.GetReturnValue().Set( localObject );
816 * Remove an renderer from the actor by index.
819 * actor.removeRenderer( 0 );
822 * @method removeRenderer
823 * @param {integer} index Index of the renderer to be removed, which must be between 0 and getRendererCount()-1
825 void ActorApi::RemoveRenderer( const v8::FunctionCallbackInfo<v8::Value>& args )
827 v8::Isolate* isolate = args.GetIsolate();
828 v8::HandleScope handleScope( isolate );
829 Actor actor = GetActor( isolate, args );
832 int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0);
835 DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
839 actor.RemoveRenderer(static_cast<unsigned int>(index));
844 } // namespace V8Plugin