X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=plugins%2Fdali-script-v8%2Fsrc%2Factors%2Factor-api.cpp;h=1ae60ecd42a026367e2b300c6e0d6d7c628f6d77;hp=926c5cd4d50288366437f3d619eb21af12972ab7;hb=8c31a5ca493d17693e53f9909a4453b1fa058ab3;hpb=826a077bb3183b5d317bfb22e14ab4e217d26f40 diff --git a/plugins/dali-script-v8/src/actors/actor-api.cpp b/plugins/dali-script-v8/src/actors/actor-api.cpp index 926c5cd..1ae60ec 100644 --- a/plugins/dali-script-v8/src/actors/actor-api.cpp +++ b/plugins/dali-script-v8/src/actors/actor-api.cpp @@ -24,7 +24,9 @@ // INTERNAL INCLUDES #include #include -#include +#include +#include +#include namespace Dali { @@ -499,6 +501,93 @@ void ActorApi::GetActorType( const v8::FunctionCallbackInfo& args ) args.GetReturnValue().Set( v8String ); } /** + * Return the natural size of the actor. + * + * @for Actor + * @method getNaturalSize + * @return {Object} { x, y, z } + */ +void ActorApi::GetNaturalSize( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + Actor actor = GetActor( isolate, args ); + + Vector3 size( actor.GetNaturalSize() ); + + v8::Local sizeObject = v8::Object::New( isolate ); + + sizeObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate, size.width ) ); + sizeObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate, size.height ) ); + sizeObject->Set( v8::String::NewFromUtf8( isolate, "z" ), v8::Integer::New( isolate, size.depth ) ); + + args.GetReturnValue().Set( sizeObject ); +} + +/** + * Calculate the width of the actor given a height + * + * The natural size is used for default calculation. + * size 0 is treated as aspect ratio 1:1. + * @for Actor + * @method getWidthForHeight + * @param {Float} height to use + * @return {Float} Return the width based on the height + * @example + * myTextLabel.getWidthForHeight(40); + * + * // DALi uses this formula internally + * // width = naturalSize.width * height / naturalSize.height; + * + * + */ +void ActorApi::GetWidthForHeight( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + Actor actor = GetActor( isolate, args ); + + bool found; + float height = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing height parameter"); + return; + } + args.GetReturnValue().Set( v8::Number::New( isolate, actor.GetWidthForHeight( height ) ) ); +} + +/** + * Calculate the height of the actor given a width + * + * The natural size is used for default calculation. + * size 0 is treated as aspect ratio 1:1. + * @for Actor + * @method getHeightForWidth + * @param {Float} width to use + * @return {Float} Return the height based on the width + * @example + * myTextLabel.getHeightForWidth(250); + * + * // DALi uses this formula internally + * // height = naturalSize.height * width / naturalSize.width + */ +void ActorApi::GetHeightForWidth( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + Actor actor = GetActor( isolate, args ); + + bool found; + float width = V8Utils::GetFloatParameter( PARAMETER_0, found, isolate, args, 0.f ); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "missing width parameter"); + return; + } + args.GetReturnValue().Set( v8::Number::New( isolate, actor.GetHeightForWidth( width ) ) ); +} +/** * Move an actor relative to its existing position. * @example * @@ -610,111 +699,130 @@ void ActorApi::ScaleBy( const v8::FunctionCallbackInfo& args ) actor.ScaleBy( vector ); } -void ActorApi::ApplyPathConstraint( const v8::FunctionCallbackInfo< v8::Value >& args ) +/** + * Add a renderer to this actor. + * @example + * + * var renderer = new dali.Renderer( geometry, material ); + * actor.addRenderer( renderer ); + * + * @for Actor + * @method addRenderer + * @param {object} renderer Renderer to add to the actor + * @return {integer} The index of the Renderer that was added + */ +void ActorApi::AddRenderer( const v8::FunctionCallbackInfo& args ) { v8::Isolate* isolate = args.GetIsolate(); v8::HandleScope handleScope( isolate ); + Actor actor = GetActor( isolate, args ); - //Get target actor - Actor targetActor = GetActor( isolate, args ); + unsigned int index = 0; - //Get PathConstraint - bool found(false); - Handle pathConstraintHandle = V8Utils::GetHandleParameter(PARAMETER_0, found, isolate, args ); - if( !found ) + bool found( false ); + Renderer renderer = RendererApi::GetRendererFromParams( 0, found, isolate, args ); + if( found ) { - DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 0 (PathConstraint)" ); - return; + index = actor.AddRenderer(renderer); } - PathConstraint pathConstraint = PathConstraint::DownCast(pathConstraintHandle); - - //Get target property - std::string propertyName = V8Utils::GetStringParameter( PARAMETER_1, found, isolate, args ); - if(!found) + else { - DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 1 (Property name)" ); + DALI_SCRIPT_EXCEPTION( isolate, "Renderer parameter missing" ); return; } - // try both properties with dashes and without - Property::Index targetPropertyIndex = targetActor.GetPropertyIndex( propertyName ); - if( targetPropertyIndex == Property::INVALID_INDEX ) - { - std::string convertedName = V8Utils::JavaScriptNameToPropertyName( propertyName ); - targetPropertyIndex = targetActor.GetPropertyIndex( convertedName ); + args.GetReturnValue().Set( v8::Integer::New( isolate, index ) ); +} - if( targetPropertyIndex == Property::INVALID_INDEX ) - { - DALI_SCRIPT_EXCEPTION( isolate, "Property not found" ); - } - } +/** + * Get the number of renderers on this actor. + * @example + * + * var count = actor.getRendererCount(); + * + * @for Actor + * @method getRendererCount + * @return {integer} the number of renderers on this actor + */ +void ActorApi::GetRendererCount( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); - //Get source actor - Actor sourceActor = V8Utils::GetActorParameter( PARAMETER_2, found, isolate, args ); - if( !found ) - { - DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 2 (Actor)" ); - } + Actor actor = GetActor( isolate, args ); + args.GetReturnValue().Set( v8::Integer::New( isolate, actor.GetRendererCount() ) ); +} - // try both properties with dashes and without - propertyName = V8Utils::GetStringParameter( PARAMETER_3, found, isolate, args ); - if(!found) - { - DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 3 (Property name)" ); - return; - } +/** + * Get a Renderer by index. + * @example + * + * var renderer = actor.getRendererAt( 0 ); + * + * @for Actor + * @method getRendererAt + * @param {integer} index The index of the renderer to fetch, which must be between 0 and getRendererCount()-1 + * @return {object} The renderer at the specified index + */ +void ActorApi::GetRendererAt( const v8::FunctionCallbackInfo& args ) +{ + v8::Isolate* isolate = args.GetIsolate(); + v8::HandleScope handleScope( isolate ); + Actor actor = GetActor( isolate, args ); - Property::Index sourcePropertyIndex = sourceActor.GetPropertyIndex( propertyName ); - if( sourcePropertyIndex == Property::INVALID_INDEX ) - { - std::string convertedName = V8Utils::JavaScriptNameToPropertyName( propertyName ); - sourcePropertyIndex = sourceActor.GetPropertyIndex( convertedName ); + Renderer renderer; - if( sourcePropertyIndex == Property::INVALID_INDEX ) - { - DALI_SCRIPT_EXCEPTION( isolate, "Property not found" ); - return; - } + bool found( false ); + int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0); + if( !found ) + { + DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" ); + return; } - - //Check if forward vector is specified - Vector3 forward( 0.0f,0.0f,0.0f); - if( args.Length() > 4 ) + else { - forward = V8Utils::GetVector3Parameter( PARAMETER_4, found, isolate, args ); - if( !found ) + renderer = actor.GetRendererAt(static_cast(index)); + if( !renderer ) { - DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 4 (Vector3)" ); + DALI_SCRIPT_EXCEPTION( isolate, "renderer not found" ); return; } } - pathConstraint.Apply( Dali::Property(sourceActor,sourcePropertyIndex), - Dali::Property(targetActor,targetPropertyIndex), - forward ); - + // Wrap the renderer + v8::Local localObject = RendererWrapper::WrapRenderer( isolate, renderer ); + args.GetReturnValue().Set( localObject ); } -void ActorApi::RemovePathConstraint( const v8::FunctionCallbackInfo< v8::Value >& args ) +/** + * Remove an renderer from the actor by index. + * @example + * + * actor.removeRenderer( 0 ); + * + * @for Actor + * @method removeRenderer + * @param {integer} index Index of the renderer to be removed, which must be between 0 and getRendererCount()-1 + */ +void ActorApi::RemoveRenderer( const v8::FunctionCallbackInfo& args ) { v8::Isolate* isolate = args.GetIsolate(); v8::HandleScope handleScope( isolate ); - - //Get target actor Actor actor = GetActor( isolate, args ); - //Get PathConstraint - bool found(false); - Handle pathConstraintHandle = V8Utils::GetHandleParameter(PARAMETER_0, found, isolate, args ); + bool found( false ); + int index = V8Utils::GetIntegerParameter( PARAMETER_0, found, isolate, args, 0); if( !found ) { - DALI_SCRIPT_EXCEPTION( isolate, "bad parameter 0 (PathConstraint)" ); - return; + DALI_SCRIPT_EXCEPTION( isolate, "invalid index parameter" ); + } + else + { + actor.RemoveRenderer(static_cast(index)); } - PathConstraint pathConstraint = PathConstraint::DownCast(pathConstraintHandle); - pathConstraint.Remove(actor); } + } // namespace V8Plugin } // namespace Dali