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 * Calculate the width of the actor given a height
523 * The natural size is used for default calculation.
524 * size 0 is treated as aspect ratio 1:1.
526 * @method getWidthForHeight
527 * @param {Float} height to use
528 * @return {Float} Return the width based on the height
530 * myTextLabel.getWidthForHeight(40);
532 * // DALi uses this formula internally
533 * // width = naturalSize.width * height / naturalSize.height;
537 void ActorApi::GetWidthForHeight( const v8::FunctionCallbackInfo<v8::Value>& args )
539 v8::Isolate* isolate = args.GetIsolate();
540 v8::HandleScope handleScope( isolate );
541 Actor actor = GetActor( isolate, args );
544 float height = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f );
547 DALI_SCRIPT_EXCEPTION( isolate, "missing height parameter");
550 args.GetReturnValue().Set( v8::Number::New( isolate, actor.GetWidthForHeight( height ) ) );
554 * Calculate the height of the actor given a width
556 * The natural size is used for default calculation.
557 * size 0 is treated as aspect ratio 1:1.
559 * @method getHeightForWidth
560 * @param {Float} width to use
561 * @return {Float} Return the height based on the width
563 * myTextLabel.getHeightForWidth(250);
565 * // DALi uses this formula internally
566 * // height = naturalSize.height * width / naturalSize.width
568 void ActorApi::GetHeightForWidth( const v8::FunctionCallbackInfo<v8::Value>& args )
570 v8::Isolate* isolate = args.GetIsolate();
571 v8::HandleScope handleScope( isolate );
572 Actor actor = GetActor( isolate, args );
575 float width = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f );
578 DALI_SCRIPT_EXCEPTION( isolate, "missing width parameter");
581 args.GetReturnValue().Set( v8::Number::New( isolate, actor.GetHeightForWidth( width ) ) );
584 * Move an actor relative to its existing position.
588 * actor.translateBy( [20,40,0] );
591 * @method translateBy
592 * @param {object} an array of 3 numbers
594 void ActorApi::TranslateBy( const v8::FunctionCallbackInfo<v8::Value>& args )
596 v8::Isolate* isolate = args.GetIsolate();
597 v8::HandleScope handleScope( isolate );
598 Actor actor = GetActor( isolate, args );
600 //Get displacement vector
602 int argCount( args.Length() );
606 vector = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
609 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
615 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
618 actor.TranslateBy( vector );
624 * Apply a relative rotation to an actor.
627 * var rotation =new dali.Rotation( pitch, roll, yaw );
628 * actor.rotateBy( rotation );
632 * @param {object} dali rotation object
634 void ActorApi::RotateBy( const v8::FunctionCallbackInfo<v8::Value>& args )
636 v8::Isolate* isolate = args.GetIsolate();
637 v8::HandleScope handleScope( isolate );
638 Actor actor = GetActor( isolate, args );
641 Property::Value rotation = V8Utils::GetPropertyValueParameter( PARAMETER_0, found, isolate, args );
643 if( rotation.GetType() != Property::ROTATION )
645 DALI_SCRIPT_EXCEPTION( isolate, "Rotation parameter missing" );
648 // the rotation parameter has to be either a AngleAxis or a Quaternion
649 // both will work when calling Get( Quaternion);
651 Quaternion quaternionValue;
652 rotation.Get( quaternionValue );
654 actor.RotateBy( quaternionValue );
658 * Apply a relative scale to an actor.
660 * // Double actor width and height ( keep depth the same )
662 * actor.scaleBy( [2,2,1] );
667 * @param {object} JavaScript array
669 void ActorApi::ScaleBy( const v8::FunctionCallbackInfo<v8::Value>& args )
671 v8::Isolate* isolate = args.GetIsolate();
672 v8::HandleScope handleScope( isolate );
673 Actor actor = GetActor( isolate, args );
676 int argCount( args.Length() );
680 vector = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
683 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
689 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 parameter missing" );
692 actor.ScaleBy( vector );
696 * Add a renderer to this actor.
699 * var renderer = new dali.Renderer( geometry, material );
700 * actor.addRenderer( renderer );
703 * @method addRenderer
704 * @param {object} renderer Renderer to add to the actor
705 * @return {integer} The index of the Renderer that was added
707 void ActorApi::AddRenderer( const v8::FunctionCallbackInfo<v8::Value>& args )
709 v8::Isolate* isolate = args.GetIsolate();
710 v8::HandleScope handleScope( isolate );
711 Actor actor = GetActor( isolate, args );
713 unsigned int index = 0;
716 Renderer renderer = RendererApi::GetRendererFromParams( 0, found, isolate, args );
719 index = actor.AddRenderer(renderer);
723 DALI_SCRIPT_EXCEPTION( isolate, "Renderer parameter missing" );
727 args.GetReturnValue().Set( v8::Integer::New( isolate, index ) );
731 * Get the number of renderers on this actor.
734 * var count = actor.getRendererCount();
737 * @method getRendererCount
738 * @return {integer} the number of renderers on this actor
740 void ActorApi::GetRendererCount( const v8::FunctionCallbackInfo<v8::Value>& args )
742 v8::Isolate* isolate = args.GetIsolate();
743 v8::HandleScope handleScope( isolate );
745 Actor actor = GetActor( isolate, args );
746 args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetRendererCount() ) );
750 * Get a Renderer by index.
753 * var renderer = actor.getRendererAt( 0 );
756 * @method getRendererAt
757 * @param {integer} index The index of the renderer to fetch, which must be between 0 and getRendererCount()-1
758 * @return {object} The renderer at the specified index
760 void ActorApi::GetRendererAt( const v8::FunctionCallbackInfo<v8::Value>& args )
762 v8::Isolate* isolate = args.GetIsolate();
763 v8::HandleScope handleScope( isolate );
764 Actor actor = GetActor( isolate, args );
769 int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0);
772 DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
777 renderer = actor.GetRendererAt(static_cast<unsigned int>(index));
780 DALI_SCRIPT_EXCEPTION( isolate, "renderer not found" );
786 v8::Local<v8::Object> localObject = RendererWrapper::WrapRenderer( isolate, renderer );
787 args.GetReturnValue().Set( localObject );
791 * Remove an renderer from the actor by index.
794 * actor.removeRenderer( 0 );
797 * @method removeRenderer
798 * @param {integer} index Index of the renderer to be removed, which must be between 0 and getRendererCount()-1
800 void ActorApi::RemoveRenderer( const v8::FunctionCallbackInfo<v8::Value>& args )
802 v8::Isolate* isolate = args.GetIsolate();
803 v8::HandleScope handleScope( isolate );
804 Actor actor = GetActor( isolate, args );
807 int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0);
810 DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" );
814 actor.RemoveRenderer(static_cast<unsigned int>(index));
819 } // namespace V8Plugin