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 unique ID.
339 * The actor itself is also considered in the search
342 * @method findChildById
343 * @param {Integer} id
344 * @return {Object} actor on success, empty actor handle if not found
346 void ActorApi::FindChildById( const v8::FunctionCallbackInfo<v8::Value>& args )
348 v8::Isolate* isolate = args.GetIsolate();
349 v8::HandleScope handleScope( isolate );
350 Actor parent = GetActor( isolate, args );
353 int id = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0 );
356 DALI_SCRIPT_EXCEPTION( isolate, "Integer parameter missing" );
359 Actor childActor = parent.FindChildById( id );
363 v8::Local < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, childActor );
364 args.GetReturnValue().Set( wrappedLayer );
370 * retrieve the actor's parent.
374 * @return {Object} actor on success, empty actor handle if actor has no parent
376 void ActorApi::GetParent( const v8::FunctionCallbackInfo<v8::Value>& args )
378 v8::Isolate* isolate = args.GetIsolate();
379 v8::HandleScope handleScope( isolate );
380 Actor actor = GetActor( isolate, args );
381 Actor parent = actor.GetParent();
385 v8::Local < v8::Object > wrappedLayer = ActorWrapper::WrapActor( isolate, parent );
386 args.GetReturnValue().Set( wrappedLayer );
390 * Converts screen coordinates into the actor's coordinate system using the default camera.
392 * The actor coordinates are relative to the top-left (0.0, 0.0, 0.5)
395 * var local = actor.screenToLocal( [ 10, 53 ]);
396 * var xPos = local.x;
397 * var yPos = local.y;
401 * @method screenToLocal
402 * @param {Object} ScreenCoordinates array of 2 objects
403 * @return {Object} local coordinates object with x,y properties
405 void ActorApi::ScreenToLocal( const v8::FunctionCallbackInfo<v8::Value>& args )
407 v8::Isolate* isolate = args.GetIsolate();
408 v8::HandleScope handleScope( isolate );
409 Actor actor = GetActor( isolate, args );
411 //ool ScreenToLocal(float& localX, float& localY, float screenX, float screenY) const;
414 int argCount( args.Length() );
419 vector = V8Utils::GetVector2Parameter( PARAMETER_0, found, isolate, args );
422 DALI_SCRIPT_EXCEPTION( isolate, "invalid parameters (x,y)" );
428 DALI_SCRIPT_EXCEPTION( isolate, "invalid parameters (x,y)" );
431 float localX, localY;
432 actor.ScreenToLocal( localX, localY, vector.x, vector.y );
434 v8::Local < v8::Object > localCoordinates = v8::Object::New( isolate );
436 localCoordinates->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Number::New( isolate, localX ) );
437 localCoordinates->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Number::New( isolate, localY ) );
439 args.GetReturnValue().Set( localCoordinates );
444 * Sets whether the actor should be focusable by keyboard navigation.
447 * @method setKeyboardFocusable
448 * @param {Boolean} folcusable
450 void ActorApi::SetKeyboardFocusable( const v8::FunctionCallbackInfo<v8::Value>& args )
452 v8::Isolate* isolate = args.GetIsolate();
453 v8::HandleScope handleScope( isolate );
454 Actor actor = GetActor( isolate, args );
455 bool parameterFound( false );
456 bool focus = V8Utils::GetBooleanParameter( PARAMETER_0, parameterFound, isolate, args );
457 if( !parameterFound )
459 DALI_SCRIPT_EXCEPTION( isolate, "boolean parameter missing" );
463 actor.SetKeyboardFocusable( focus );
467 * Returns whether the actor is focusable by keyboard navigation.
471 * @method isKeyboardFocusable
472 * @return {Boolean} folcusable
474 void ActorApi::IsKeyboardFocusable( const v8::FunctionCallbackInfo<v8::Value>& args )
476 v8::Isolate* isolate = args.GetIsolate();
477 v8::HandleScope handleScope( isolate );
478 Actor actor = GetActor( isolate, args );
480 args.GetReturnValue().Set( v8::Boolean::New( isolate, actor.IsKeyboardFocusable() ) );
484 * retrieve the actor type
487 * @method getActorType
488 * @return {String} Actor, ImageActor, TextActor, MeshActor, Layer, CameraActor ...
490 void ActorApi::GetActorType( const v8::FunctionCallbackInfo<v8::Value>& args )
492 v8::Isolate* isolate = args.GetIsolate();
493 v8::HandleScope handleScope( isolate );
494 Actor actor = GetActor( isolate, args );
496 std::string name = actor.GetTypeName();
497 v8::Local < v8::String > v8String = v8::String::NewFromUtf8( isolate, name.c_str() );
498 args.GetReturnValue().Set( v8String );
501 * Move an actor relative to its existing position.
505 * actor.translateBy( [20,40,0] );
508 * @method translateBy
509 * @param {object} an array of 3 numbers
511 void ActorApi::TranslateBy( const v8::FunctionCallbackInfo<v8::Value>& args )
513 v8::Isolate* isolate = args.GetIsolate();
514 v8::HandleScope handleScope( isolate );
515 Actor actor = GetActor( isolate, args );
517 //Get displacement vector
519 int argCount( args.Length() );
523 vector = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
526 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
532 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
535 actor.TranslateBy( vector );
541 * Apply a relative rotation to an actor.
544 * var rotation =new dali.Rotation( pitch, roll, yaw );
545 * actor.rotateBy( rotation );
549 * @param {object} dali rotation object
551 void ActorApi::RotateBy( const v8::FunctionCallbackInfo<v8::Value>& args )
553 v8::Isolate* isolate = args.GetIsolate();
554 v8::HandleScope handleScope( isolate );
555 Actor actor = GetActor( isolate, args );
558 Property::Value rotation = V8Utils::GetPropertyValueParameter( PARAMETER_0, found, isolate, args );
560 if( rotation.GetType() != Property::ORIENTATION )
562 DALI_SCRIPT_EXCEPTION( isolate, "Rotation parameter missing" );
565 // the rotation parameter has to be either a AngleAxis or a Quaternion
566 // both will work when calling Get( Quaternion);
568 Quaternion quaternionValue;
569 rotation.Get( quaternionValue );
571 actor.RotateBy( quaternionValue );
575 * Apply a relative scale to an actor.
577 * // Double actor width and height ( keep depth the same )
579 * actor.scaleBy( [2,2,1] );
584 * @param {object} JavaScript array
586 void ActorApi::ScaleBy( const v8::FunctionCallbackInfo<v8::Value>& args )
588 v8::Isolate* isolate = args.GetIsolate();
589 v8::HandleScope handleScope( isolate );
590 Actor actor = GetActor( isolate, args );
593 int argCount( args.Length() );
597 vector = V8Utils::GetVector3Parameter( PARAMETER_0, found, isolate, args );
600 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 move parameter missing" );
606 DALI_SCRIPT_EXCEPTION( isolate, "Vector3 parameter missing" );
609 actor.ScaleBy( vector );
612 } // namespace V8Plugin