From: Nick Holland Date: Mon, 3 Aug 2015 06:52:45 +0000 (+0100) Subject: Added getNaturalSize, getWidthForHeight, getHeightForWidth JS API's X-Git-Tag: dali_1.0.52~7 X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=commitdiff_plain;h=1b74a860953d1584ae2a3eecbdd1508f6a614557 Added getNaturalSize, getWidthForHeight, getHeightForWidth JS API's Change-Id: I8056dedb24bf9cb27dc3d824ef752a1f67d9ccf0 --- diff --git a/plugins/dali-script-v8/src/actors/actor-api.cpp b/plugins/dali-script-v8/src/actors/actor-api.cpp index b36f68a..f712893 100644 --- a/plugins/dali-script-v8/src/actors/actor-api.cpp +++ b/plugins/dali-script-v8/src/actors/actor-api.cpp @@ -499,6 +499,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 * diff --git a/plugins/dali-script-v8/src/actors/actor-api.h b/plugins/dali-script-v8/src/actors/actor-api.h index 8543f11..82f41d3 100644 --- a/plugins/dali-script-v8/src/actors/actor-api.h +++ b/plugins/dali-script-v8/src/actors/actor-api.h @@ -64,6 +64,9 @@ namespace ActorApi void ScreenToLocal( const v8::FunctionCallbackInfo< v8::Value >& args ); void SetKeyboardFocusable( const v8::FunctionCallbackInfo< v8::Value >& args ); void IsKeyboardFocusable( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetNaturalSize( const v8::FunctionCallbackInfo< v8::Value >& args ); + void GetWidthForHeight( const v8::FunctionCallbackInfo& args ); + void GetHeightForWidth( const v8::FunctionCallbackInfo& args ); void TranslateBy( const v8::FunctionCallbackInfo< v8::Value >& args ); void RotateBy( const v8::FunctionCallbackInfo< v8::Value >& args ); void ScaleBy( const v8::FunctionCallbackInfo< v8::Value >& args ); diff --git a/plugins/dali-script-v8/src/actors/actor-wrapper.cpp b/plugins/dali-script-v8/src/actors/actor-wrapper.cpp index b242721..9b81f41 100644 --- a/plugins/dali-script-v8/src/actors/actor-wrapper.cpp +++ b/plugins/dali-script-v8/src/actors/actor-wrapper.cpp @@ -183,6 +183,9 @@ const ActorFunctions ActorFunctionTable[]= // ignore. GetCurrentAnchorPoint() use Actor.anchorPoint // ignore. SetSize() use Actor.size // ignore. GetCurrentSize() use Actor.size + { "GetNaturalSize", ActorApi::GetNaturalSize, ACTOR_API }, + { "GetWidthForHeight",ActorApi::GetWidthForHeight, ACTOR_API }, + { "GetHeightForWidth",ActorApi::GetHeightForWidth, ACTOR_API }, // ignore. SetPosition(....) use Actor.position // ignore. SetX, SetY, SetZ, use Actor.position.x, Actor.position.y, Actor.position.z { "TranslateBy", ActorApi::TranslateBy, ACTOR_API },