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-view/text-view.h>
26 #include <actors/actor-wrapper.h>
34 namespace // unanmed namespace
36 Actor GetActor( v8::Isolate* isolate, const v8::FunctionCallbackInfo<v8::Value>& args )
38 HandleWrapper* handleWrapper = HandleWrapper::Unwrap( isolate, args.This() );
39 return Actor::DownCast( handleWrapper->mHandle );
46 Actor New( const v8::FunctionCallbackInfo< v8::Value >& args )
48 return Dali::Toolkit::TextView::New();
52 /***************************************
54 ****************************************/
61 * @return {Object} actor
63 Actor ActorApi::New( const v8::FunctionCallbackInfo< v8::Value >& args )
69 * get the actors unique id
73 * @return {Integer} id
75 void ActorApi::GetId( const v8::FunctionCallbackInfo<v8::Value>& args )
77 v8::Isolate* isolate = args.GetIsolate();
78 v8::HandleScope handleScope( isolate );
79 Actor actor = GetActor( isolate, args );
81 args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetId() ) );
85 * Query whether an actor is the root actor, which is owned by the Stage
89 * @return {Boolean} true if it is root
92 void ActorApi::IsRoot( const v8::FunctionCallbackInfo<v8::Value>& args )
94 v8::Isolate* isolate = args.GetIsolate();
95 v8::HandleScope handleScope( isolate );
96 Actor actor = GetActor( isolate, args );
98 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsRoot() ) );
103 * Query whether the actor is connected to the Stage.
104 * When an actor is connected, it will be directly or indirectly parented to the root Actor.
105 * The root Actor is provided automatically by dali.stage, and is always considered to be connected.
109 * @return {Boolean} True if the actor is connected to the Stage
111 void ActorApi::OnStage( const v8::FunctionCallbackInfo<v8::Value>& args )
113 v8::Isolate* isolate = args.GetIsolate();
114 v8::HandleScope handleScope( isolate );
115 Actor actor = GetActor( isolate, args );
117 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.OnStage() ) );
121 * Query whether an actor is a layer
125 * @return {Boolean} true if it is a layer
127 void ActorApi::IsLayer( const v8::FunctionCallbackInfo<v8::Value>& args )
129 v8::Isolate* isolate = args.GetIsolate();
130 v8::HandleScope handleScope( isolate );
131 Actor actor = GetActor( isolate, args );
133 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsLayer() ) );
137 * Gets the layer in which the actor is present.
141 * @return {Object} Layer
143 void ActorApi::GetLayer( const v8::FunctionCallbackInfo<v8::Value>& args )
145 v8::Isolate* isolate = args.GetIsolate();
146 v8::HandleScope handleScope( isolate );
147 Actor actor = GetActor( isolate, args );
148 Layer layer = actor.GetLayer();
149 if( layer ) // actors don't always have a layer
151 v8::Handle < v8::Object > wrappedLayer = ActorWrapper::ActorWrapper::WrapActor( isolate, layer, ActorWrapper::LAYER_ACTOR );
152 args.GetReturnValue().Set( wrappedLayer );
154 // else return an empty object
158 * Adds a child Actor to this Actor.
160 * NOTE! if the child already has a parent, it will be removed from old parent
161 * and reparented to this actor. This may change childs position, color, shader effect,
162 * scale etc as it now inherits them from this actor
165 * - The child actor is not the same as the parent actor.
166 * - The actor is not the Root actor
168 * Once added The child will be referenced by its parent. This means that the child will be kept alive,
169 * even if the handle passed into this method is reset or destroyed.
173 * @param {Object} Actor
175 void ActorApi::AddActor( const v8::FunctionCallbackInfo<v8::Value>& args )
177 v8::Isolate* isolate = args.GetIsolate();
178 v8::HandleScope handleScope( isolate );
179 Actor parent = GetActor( isolate, args );
181 Actor child = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
188 DALI_SCRIPT_EXCEPTION( isolate, "child parameter missing" );
193 * Removes a child Actor from this Actor.
195 * If the actor was not a child of this actor, this is a no-op.
198 * - The child actor is not the same as the parent actor.
201 * @param{Object} Actor the child actor
204 void ActorApi::RemoveActor( const v8::FunctionCallbackInfo<v8::Value>& args )
206 v8::Isolate* isolate = args.GetIsolate();
207 v8::HandleScope handleScope( isolate );
208 Actor parent = GetActor( isolate, args );
210 Actor child = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
214 parent.Remove( child );
218 DALI_SCRIPT_EXCEPTION( isolate, "child parameter missing" );
223 * Checks whether an Actor is equal to this Actor.
227 * @param {Object} Actor
229 void ActorApi::IsEqualTo( const v8::FunctionCallbackInfo<v8::Value>& args )
231 v8::Isolate* isolate = args.GetIsolate();
232 v8::HandleScope handleScope( isolate );
233 Actor self = GetActor( isolate, args );
236 Actor actor = V8Utils::GetActorParameter( PARAMETER_0, found, isolate, args );
239 args.GetReturnValue().Set( v8::Boolean::New( isolate, (actor == self) ) );
243 DALI_SCRIPT_EXCEPTION( isolate, "actor parameter missing" );
247 /** Removes an actor from its parent.
249 * If the actor has no parent, this method does nothing.
254 void ActorApi::Unparent( const v8::FunctionCallbackInfo< v8::Value >& args)
256 v8::Isolate* isolate = args.GetIsolate();
257 v8::HandleScope handleScope( isolate );
258 Actor actor = GetActor( isolate, args );
263 * get number of child actors
266 * @method getChildCount
267 * @return {Integer} count
269 void ActorApi::GetChildCount( const v8::FunctionCallbackInfo<v8::Value>& args )
271 v8::Isolate* isolate = args.GetIsolate();
272 v8::HandleScope handleScope( isolate );
273 Actor actor = GetActor( isolate, args );
275 args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetChildCount() ) );
279 * Retrieve and child actor by index.
283 * @param {Integer} actor index
284 * @return {Object} actor on success, empty actor handle if not found
286 void ActorApi::GetChildAt( const v8::FunctionCallbackInfo<v8::Value>& args )
288 v8::Isolate* isolate = args.GetIsolate();
289 v8::HandleScope handleScope( isolate );
290 Actor parent = GetActor( isolate, args );
292 int id = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
295 DALI_SCRIPT_EXCEPTION( isolate, "Integer parameter missing" );
298 Actor childActor = parent.GetChildAt( id );
302 v8::Handle < v8::Object > wrappedActor = ActorWrapper::WrapActor( isolate, childActor );
303 args.GetReturnValue().Set( wrappedActor );
308 * Search through this actor's hierarchy for an actor with the given name
309 * The actor itself is also considered in the search
312 * @method findChildByName
313 * @param {String} actor name
314 * @return {Object} actor on success, empty actor handle if not found
316 void ActorApi::FindChildByName( const v8::FunctionCallbackInfo<v8::Value>& args )
318 v8::Isolate* isolate = args.GetIsolate();
319 v8::HandleScope handleScope( isolate );
320 Actor parent = GetActor( isolate, args );
322 std::string name = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
325 DALI_SCRIPT_EXCEPTION( isolate, "string parameter missing" );
328 Actor childActor = parent.FindChildByName( name );
332 v8::Handle < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, childActor );
333 args.GetReturnValue().Set( wrappedLayer );
338 * Search through this actor's hierarchy for an actor with the given name or alias.
340 * Actors can customize this function to provide actors with preferred alias'
341 * For example 'previous' could return the last selected child.
342 * If no aliased actor is found then FindChildByName() is called.
345 * @method findChildByAlias
346 * @param {String} actor alias
347 * @return {Object} actor on success, empty actor handle if not found
349 void ActorApi::FindChildByAlias( const v8::FunctionCallbackInfo<v8::Value>& args )
351 v8::Isolate* isolate = args.GetIsolate();
352 v8::HandleScope handleScope( isolate );
353 Actor parent = GetActor( isolate, args );
355 std::string name = V8Utils::GetStringParameter( PARAMETER_0, found, isolate, args );
358 DALI_SCRIPT_EXCEPTION( isolate, "String parameter not found" );
361 Actor childActor = parent.FindChildByAlias( name );
365 v8::Local < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, childActor );
366 args.GetReturnValue().Set( wrappedLayer );
371 * Search through this actor's hierarchy for an actor with the given unique ID.
372 * The actor itself is also considered in the search
375 * @method findChildById
376 * @param {Integer} id
377 * @return {Object} actor on success, empty actor handle if not found
379 void ActorApi::FindChildById( const v8::FunctionCallbackInfo<v8::Value>& args )
381 v8::Isolate* isolate = args.GetIsolate();
382 v8::HandleScope handleScope( isolate );
383 Actor parent = GetActor( isolate, args );
386 int id = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
389 DALI_SCRIPT_EXCEPTION( isolate, "Integer parameter missing" );
392 Actor childActor = parent.FindChildById( id );
396 v8::Local < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, childActor );
397 args.GetReturnValue().Set( wrappedLayer );
403 * retrieve the actor's parent.
407 * @return {Object} actor on success, empty actor handle if actor has no parent
409 void ActorApi::GetParent( const v8::FunctionCallbackInfo<v8::Value>& args )
411 v8::Isolate* isolate = args.GetIsolate();
412 v8::HandleScope handleScope( isolate );
413 Actor actor = GetActor( isolate, args );
414 Actor parent = actor.GetParent();
418 v8::Local < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, parent );
419 args.GetReturnValue().Set( wrappedLayer );
423 * Converts screen coordinates into the actor's coordinate system using the default camera.
425 * The actor coordinates are relative to the top-left (0.0, 0.0, 0.5)
428 * var local = actor.screenToLocal( [ 10, 53 ]);
429 * var xPos = local.x;
430 * var yPos = local.y;
434 * @method screenToLocal
435 * @param {Object} ScreenCoordinates array of 2 objects
436 * @return {Object} local coordinates object with x,y properties
438 void ActorApi::ScreenToLocal( const v8::FunctionCallbackInfo<v8::Value>& args )
440 v8::Isolate* isolate = args.GetIsolate();
441 v8::HandleScope handleScope( isolate );
442 Actor actor = GetActor( isolate, args );
444 //ool ScreenToLocal(float& localX, float& localY, float screenX, float screenY) const;
447 int argCount( args.Length() );
452 vector = V8Utils::GetVector2Parameter( PARAMETER_0, found, isolate, args );
455 DALI_SCRIPT_EXCEPTION( isolate, "invalid parameters (x,y)" );
461 DALI_SCRIPT_EXCEPTION( isolate, "invalid parameters (x,y)" );
464 float localX, localY;
465 actor.ScreenToLocal( localX, localY, vector.x, vector.y );
467 v8::Local < v8::Object > localCoordinates = v8::Object::New( isolate );
469 localCoordinates->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Number::New( isolate, localX ) );
470 localCoordinates->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Number::New( isolate, localY ) );
472 args.GetReturnValue().Set( localCoordinates );
477 * Sets whether the actor should be focusable by keyboard navigation.
480 * @method setKeyboardFocusable
481 * @param {Boolean} folcusable
483 void ActorApi::SetKeyboardFocusable( const v8::FunctionCallbackInfo<v8::Value>& args )
485 v8::Isolate* isolate = args.GetIsolate();
486 v8::HandleScope handleScope( isolate );
487 Actor actor = GetActor( isolate, args );
488 bool parameterFound( false );
489 bool focus = V8Utils::GetBooleanParameter( PARAMETER_0, parameterFound, isolate, args );
490 if( !parameterFound )
492 DALI_SCRIPT_EXCEPTION( isolate, "boolean parameter missing" );
496 actor.SetKeyboardFocusable( focus );
500 * Returns whether the actor is focusable by keyboard navigation.
504 * @method isKeyboardFocusable
505 * @return {Boolean} folcusable
507 void ActorApi::IsKeyboardFocusable( const v8::FunctionCallbackInfo<v8::Value>& args )
509 v8::Isolate* isolate = args.GetIsolate();
510 v8::HandleScope handleScope( isolate );
511 Actor actor = GetActor( isolate, args );
513 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsKeyboardFocusable() ) );
517 * retrieve the actor type
520 * @method getActorType
521 * @return {String} Actor, ImageActor, TextActor, MeshActor, Layer, CameraActor ...
523 void ActorApi::GetActorType( const v8::FunctionCallbackInfo<v8::Value>& args )
525 v8::Isolate* isolate = args.GetIsolate();
526 v8::HandleScope handleScope( isolate );
527 Actor actor = GetActor( isolate, args );
529 std::string name = actor.GetTypeName();
530 v8::Local < v8::String > v8String = v8::String::NewFromUtf8( isolate, name.c_str() );
531 args.GetReturnValue().Set( v8String );
534 * Move an actor relative to its existing position.
538 * actor.moveBy( [20,40,0] );
542 * @param {object} an array of 3 numbers
544 void ActorApi::MoveBy( const v8::FunctionCallbackInfo<v8::Value>& args )
546 v8::Isolate* isolate = args.GetIsolate();
547 v8::HandleScope handleScope( isolate );
548 Actor actor = GetActor( isolate, args );
550 //Get displacement vector
552 int argCount( args.Length() );
556 vector = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
559 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
565 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
568 actor.MoveBy( vector );
574 * Apply a relative rotation to an actor.
577 * var rotation =new dali.Rotation( pitch, roll, yaw );
578 * actor.rotateBy( rotation );
582 * @param {object} dali rotation object
584 void ActorApi::RotateBy( const v8::FunctionCallbackInfo<v8::Value>& args )
586 v8::Isolate* isolate = args.GetIsolate();
587 v8::HandleScope handleScope( isolate );
588 Actor actor = GetActor( isolate, args );
591 Property::Value rotation = V8Utils::GetPropertyValueParameter( PARAMETER_0, found, isolate, args );
593 if( rotation.GetType() != Property::ROTATION )
595 DALI_SCRIPT_EXCEPTION( isolate, "Rotation parameter missing" );
598 // the rotation parameter has to be either a AngleAxis or a Quaternion
599 // both will work when calling Get( Quaternion);
601 Quaternion quaternionValue;
602 rotation.Get( quaternionValue );
604 actor.RotateBy( quaternionValue );
608 * Apply a relative scale to an actor.
610 * // Double actor width and height ( keep depth the same )
612 * actor.scaleBy( [2,2,1] );
617 * @param {object} JavaScript array
619 void ActorApi::ScaleBy( const v8::FunctionCallbackInfo<v8::Value>& args )
621 v8::Isolate* isolate = args.GetIsolate();
622 v8::HandleScope handleScope( isolate );
623 Actor actor = GetActor( isolate, args );
626 int argCount( args.Length() );
630 vector = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
633 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
639 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 parameter missing" );
642 actor.ScaleBy( vector );
645 * Apply a relative scale to an actor.
646 * Actor opacity ranges from 0 (see through ) to 1 ( solid )
648 * // reduce actor opactiy by a half
649 * actor.opaictyBy(-0.5);
653 * @param {float} relative opacity
655 void ActorApi::OpacityBy( const v8::FunctionCallbackInfo<v8::Value>& args )
657 v8::Isolate* isolate = args.GetIsolate();
658 v8::HandleScope handleScope( isolate );
659 Actor actor = GetActor( isolate, args );
661 // void OpacityBy(float relativeOpacity);
663 float opacity = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f );
666 DALI_SCRIPT_EXCEPTION( isolate, "float parameter missing" );
669 actor.OpacityBy( opacity );
673 * Apply a relative color change to an actor.
676 * // increase actor red by half
677 * actor.colorBy( [0.5, 0, 0, 0]);
682 * @param {Object} Color JavaScript array
684 void ActorApi::ColorBy( const v8::FunctionCallbackInfo<v8::Value>& args )
686 v8::Isolate* isolate = args.GetIsolate();
687 v8::HandleScope handleScope( isolate );
688 Actor actor = GetActor( isolate, args );
691 int argCount( args.Length() );
696 color = V8Utils::GetVector4Parameter( PARAMETER_0, found, isolate, args );
699 DALI_SCRIPT_EXCEPTION( isolate, "Vector4 parameter missing" );
705 DALI_SCRIPT_EXCEPTION( isolate, "Vector4 parameter missing" );
709 actor.ColorBy( color );
713 } // namespace V8Plugin