From e8b6a55bb502bb265b14521313a41b7851aa42ab Mon Sep 17 00:00:00 2001 From: Adeel Kazmi Date: Mon, 11 Jul 2016 17:06:02 +0100 Subject: [PATCH] (V8 Plugin) Use new Touch API Change-Id: I64f81314ca8c0c4f26af00ac2de2abbc7d88fdd6 --- plugins/dali-script-v8/docs/content/actor.js | 61 ++++++------ .../src/events/event-object-generator.cpp | 102 +++++++++++++++++++-- .../src/events/event-object-generator.h | 4 +- .../src/signals/dali-any-javascript-converter.cpp | 4 +- .../dali-script-v8/src/signals/signal-manager.cpp | 26 +++--- 5 files changed, 144 insertions(+), 53 deletions(-) diff --git a/plugins/dali-script-v8/docs/content/actor.js b/plugins/dali-script-v8/docs/content/actor.js index 9b4b4ff..5ad2180 100644 --- a/plugins/dali-script-v8/docs/content/actor.js +++ b/plugins/dali-script-v8/docs/content/actor.js @@ -73,7 +73,7 @@ The actor provides the following call back events | Name | Description | Parameters passed to call back | |-----------------|----------------------------------------|--------------------------| -|touched | touch event | (actor, touchEvent ) | +|touch | touch | (actor, touchData ) | |hovered | mouse or pointer hovering over actor | (actor, hoverEvent) | |mouseWheelEvent | mouse wheel events | (actor, wheelEvent) | |onStage | actor has been moved on stage | (actor) | @@ -83,38 +83,42 @@ The actor provides the following call back events #### Touch event Used to detect multiple touch events on the actor. The state of each touch point can be: -+ "down" = touch down -+ "up" = Touch up -+ "motion" = Finger dragged or hovered -+ "leave" = Leave the boundary of an actor -+ "stationary" = No change from last event. Useful when a multi-point event occurs where -all points are sent but indicates that this particular point has not changed since the last time -+ "interrupted" = A system event has occurred which has interrupted the touch or hover event sequence ++ "DOWN" = touch down ++ "UP" = Touch up ++ "MOTION" = Finger dragged or hovered ++ "LEAVE" = Leave the boundary of an actor ++ "STATIONARY" = No change from last event. Useful when a multi-point event occurs where + all points are sent but indicates that this particular point has not changed + since the last time ++ "INTERRUPTED" = A system event has occurred which has interrupted the touch or hover event sequence ``` -touchEvent = { +touchData = { pointCount: int, // number of points touched ( multi-touch ) time: int, // The time in milliseconds that the touch event occurred. - points = [ touchPoints ], // array of TouchPoints, to support + points = [ Points ], // array of Points - TouchPoint = { - - "deviceId" : int, // Each touch point has a unique device ID - "state" : string, // touch state ="down,up,motion,leave,stationary, interrupted } - "sourceActor" : actor, // the actor that is emitting the callback (the actor that is hit maybe a child of it) - "hitActor" : actor, // actor that was hit - "local" : {x,y}, // co-ordinates of top left of hit actor (local.x, local.y) - "screen" : {x,y} // co-ordinates of top left of hit actor (screen.x, screen.y) - } + Point = { + "deviceId" : int, // Each touch point has a unique device ID + "state" : string, // touch state ="DOWN","UP","MOTION","LEAVE","STATIONARY","INTERRUPTED" + "sourceActor" : actor, // the actor that is emitting the callback (the actor that is hit maybe a child of it) + "hitActor" : actor, // actor that was hit + "local" : {x,y}, // co-ordinates of top left of hit actor (local.x, local.y) + "screen" : {x,y}, // co-ordinates of top left of hit actor (screen.x, screen.y) + "radius" : float, // radius of the press point (average of both the horizontal & vertical radii) + "ellipseRadius" : {x,y}, // both the horizontal and the vertical radii of the press point + "pressure" : float, // the touch pressure + "angle" : float // angle of the press point relative to the Y-Axis (in degrees) + } } -function OnPressed( actor, touchEvent ) +function onPressed( actor, touchData ) { - var firstPoint = touchEvent.points[0]; - log("first touch point = " + firstPoint.screen.x + "," +firstPoint.screen.x + "actor= "+firstPoint.hitActor ); + var firstPoint = touchData.points[0]; + log("first touch point = " + firstPoint.screen.x + "," + firstPoint.screen.x + "actor= " + firstPoint.hitActor ); var anim = new dali.Animation( 4 ); var rotation = new dali.Rotation( 90, 0, 0 ); // pitch, yaw, roll @@ -124,7 +128,7 @@ function OnPressed( actor, touchEvent ) } // connect to touch events -myActor.on( "touched", onPressed ); +myActor.on( "touch", onPressed ); ``` @@ -138,7 +142,12 @@ hoverEvent = { points[] // array of TouchPoints TouchPoint = { - // See touchEvent TouchPoint object + "deviceId" : int, // Each touch point has a unique device ID + "state" : string, // touch state ="down,up,motion,leave,stationary, interrupted } + "sourceActor" : actor, // the actor that is emitting the callback (the actor that is hit maybe a child of it) + "hitActor" : actor, // actor that was hit + "local" : {x,y}, // co-ordinates of top left of hit actor (local.x, local.y) + "screen" : {x,y} // co-ordinates of top left of hit actor (screen.x, screen.y) } } ``` @@ -487,9 +496,9 @@ WORLD_COLOR /** * Actors sensitive flag - * brief Sets whether an actor should emit touch event signals; @see SignalTouched(). + * brief Sets whether an actor should emit touch event signals; @see SignalTouch(). * - * An actor is sensitive by default, which means that as soon as an application connects to the SignalTouched(), + * An actor is sensitive by default, which means that as soon as an application connects to the SignalTouch(), * the touch event signal will be emitted. * * If the application wishes to temporarily disable the touch event signal emission, then they can do so by calling diff --git a/plugins/dali-script-v8/src/events/event-object-generator.cpp b/plugins/dali-script-v8/src/events/event-object-generator.cpp index e90fe8c..e9d8955 100644 --- a/plugins/dali-script-v8/src/events/event-object-generator.cpp +++ b/plugins/dali-script-v8/src/events/event-object-generator.cpp @@ -32,13 +32,33 @@ namespace V8Plugin namespace { -struct PointState +struct TouchDataPointState +{ + PointState::Type state; + const char* name; +}; + +const TouchDataPointState TouchDataPointStateLookup[]= +{ + { PointState::DOWN, "DOWN" }, + { PointState::UP, "UP" }, + { PointState::MOTION, "MOTION" }, + { PointState::LEAVE, "LEAVE" }, + { PointState::STATIONARY, "STATIONARY" }, + { PointState::INTERRUPTED, "INTERRUPT" }, + { PointState::STARTED, "STARTED" }, + { PointState::FINISHED, "FINISHED" }, +}; + +const unsigned int TouchDataPointStateLookupCount = sizeof(TouchDataPointStateLookup)/sizeof(TouchDataPointStateLookup[0]); + +struct TouchPointState { TouchPoint::State state; const char* name; }; -const PointState PointStateLookup[]= +const TouchPointState PointStateLookup[]= { { TouchPoint::Down, "down" }, /**< Screen touched */ { TouchPoint::Up, "up" }, /**< Touch stopped */ @@ -70,6 +90,19 @@ const GestureState GestureStateLookup[]= const unsigned int GestureStateLookupCount = sizeof(GestureStateLookup)/sizeof(GestureStateLookup[0]); +const char* GetTouchDataPointStateName( PointState::Type state ) +{ + // could use the enum as index, but dali-core may change, so for now just do a lookup + for( unsigned int i = 0; i < TouchDataPointStateLookupCount; i++ ) + { + if( TouchDataPointStateLookup[i].state == state ) + { + return TouchDataPointStateLookup[i].name; + } + } + return "error point state not found"; +} + const char* GetTouchPointStateName( TouchPoint::State state ) { // could use the enum as index, but dali-core may change, so for now just do a lookup @@ -146,21 +179,25 @@ v8::Local CreateTouchPoint( v8::Isolate* isolate, const TouchPoint& } // un-named namespace -v8::Handle EventObjectGenerator::CreateTouchEvent( v8::Isolate* isolate, const TouchEvent& touchEvent) +v8::Handle EventObjectGenerator::CreateTouchData( v8::Isolate* isolate, const TouchData& touch ) { // we are creating a touch event object that looks like this // // event.pointCount = points touched // event.time = The time (in ms) that the touch event occurred. - // event.point[] = array of TouchPoints + // event.point[] = array of Points // - // A TouchPoint = + // A Point = // { "deviceId", int } Each touch point has a unique device ID // { "state", string } touch state ="Down,Up,Motion,Leave,Stationary, Interrupted } // { "sourceActor", actor } the actor that is emitting the callback (the actor that is hit maybe a child of it) // { "hitActor", actor } actor that was hit // { "local", {x,y} } co-ordinates of top left of hit actor // { "screen", {x,y} } co-ordinates of top left of hit actor + // { "radius", float } radius of the press point (average of both the horizontal & vertical radii) + // { "ellipseRadius", {x,y} } both the horizontal and the vertical radii of the press point + // { "pressure", float } the touch pressure + // { "angle", float } angle of the press point relative to the Y-Axis // v8::EscapableHandleScope handleScope( isolate ); @@ -168,19 +205,64 @@ v8::Handle EventObjectGenerator::CreateTouchEvent( v8::Isolate* isol v8::Local touchObject = v8::Object::New( isolate ); // Set the pointCount - touchObject->Set( v8::String::NewFromUtf8( isolate, "pointCount" ), v8::Integer::New( isolate, touchEvent.GetPointCount() ) ); + touchObject->Set( v8::String::NewFromUtf8( isolate, "pointCount" ), v8::Integer::New( isolate, touch.GetPointCount() ) ); // Set the time - touchObject->Set( v8::String::NewFromUtf8( isolate, "time" ), v8::Number::New( isolate, touchEvent.time ) ); + touchObject->Set( v8::String::NewFromUtf8( isolate, "time" ), v8::Number::New( isolate, touch.GetTime() ) ); // Set the emitting actor // touchObject->Set( v8::String::NewFromUtf8( isolate, "sourceActor" ), ActorWrapper::WrapActor(isolate, emittingActor)); // Create the array of touch points - v8::Local < v8::Array > pointArrayObject = v8::Array::New( isolate, touchEvent.GetPointCount() ); - for( unsigned int i = 0 ; i < touchEvent.GetPointCount() ; ++i ) + v8::Local < v8::Array > pointArrayObject = v8::Array::New( isolate, touch.GetPointCount() ); + for( unsigned int i = 0 ; i < touch.GetPointCount() ; ++i ) { - v8::Local < v8::Object > pointObject = CreateTouchPoint( isolate, touchEvent.points[i] ); + v8::Local pointObject = v8::Object::New( isolate ); + + // set device id + pointObject->Set( v8::String::NewFromUtf8( isolate, "deviceId" ), v8::Integer::New( isolate, touch.GetDeviceId( i ) ) ); + + // set state + pointObject->Set( v8::String::NewFromUtf8( isolate, "state" ), v8::String::NewFromUtf8( isolate, GetTouchDataPointStateName( touch.GetState( i ) ) ) ); + + Actor hitActor = touch.GetHitActor( i ); + if( hitActor ) + { + // set the hit actor + pointObject->Set( v8::String::NewFromUtf8( isolate, "hitActor" ), ActorWrapper::WrapActor( isolate, hitActor ) ); + } + + // set the local co-ordinates + const Vector2& local = touch.GetLocalPosition( i ); + v8::Local localPointObject = v8::Object::New( isolate ); + localPointObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate, local.x ) ); + localPointObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate, local.y ) ); + pointObject->Set( v8::String::NewFromUtf8( isolate, "local" ), localPointObject ); + + // set the screen co-ordinates + const Vector2& screen = touch.GetScreenPosition( i ); + v8::Local screenPointObject = v8::Object::New( isolate ); + screenPointObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate, screen.x ) ); + screenPointObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate, screen.y ) ); + pointObject->Set( v8::String::NewFromUtf8( isolate, "screen" ), screenPointObject ); + + // set the radius + pointObject->Set( v8::String::NewFromUtf8( isolate, "radius" ), v8::Integer::New( isolate, touch.GetRadius( i ) ) ); + + // set the ellipse Radius + const Vector2& ellipse = touch.GetEllipseRadius( i ); + v8::Local ellipseObject = v8::Object::New( isolate ); + ellipseObject->Set( v8::String::NewFromUtf8( isolate, "x" ), v8::Integer::New( isolate, ellipse.x ) ); + ellipseObject->Set( v8::String::NewFromUtf8( isolate, "y" ), v8::Integer::New( isolate, ellipse.y ) ); + pointObject->Set( v8::String::NewFromUtf8( isolate, "ellipseRadius" ), ellipseObject ); + + // set the pressure + pointObject->Set( v8::String::NewFromUtf8( isolate, "pressure" ), v8::Integer::New( isolate, touch.GetPressure( i ) ) ); + + // set the angle + pointObject->Set( v8::String::NewFromUtf8( isolate, "angle" ), v8::Integer::New( isolate, touch.GetAngle( i ).degree ) ); + + // add the point pointArrayObject->Set( v8::Number::New( isolate, i ), pointObject ); } diff --git a/plugins/dali-script-v8/src/events/event-object-generator.h b/plugins/dali-script-v8/src/events/event-object-generator.h index d1d6296..0bbd47a 100644 --- a/plugins/dali-script-v8/src/events/event-object-generator.h +++ b/plugins/dali-script-v8/src/events/event-object-generator.h @@ -21,7 +21,7 @@ // EXTERNAL INCLUDES #include -#include +#include #include #include #include @@ -42,7 +42,7 @@ namespace V8Plugin namespace EventObjectGenerator { - v8::Handle CreateTouchEvent( v8::Isolate* isolate, const TouchEvent& touchEvent); + v8::Handle CreateTouchData( v8::Isolate* isolate, const TouchData& touch ); v8::Handle CreateHoverEvent( v8::Isolate* isolate, const HoverEvent& hoverEvent); v8::Handle CreateWheelEvent( v8::Isolate* isolate, const WheelEvent& wheelEvent); v8::Handle CreateKeyEvent( v8::Isolate* isolate, const KeyEvent& keyEvent); diff --git a/plugins/dali-script-v8/src/signals/dali-any-javascript-converter.cpp b/plugins/dali-script-v8/src/signals/dali-any-javascript-converter.cpp index faeaf25..b675779 100644 --- a/plugins/dali-script-v8/src/signals/dali-any-javascript-converter.cpp +++ b/plugins/dali-script-v8/src/signals/dali-any-javascript-converter.cpp @@ -66,9 +66,9 @@ v8::Local DaliAnyConverter::ConvertToJavaScriptObject(v8::Isolate* is { returnValue = PropertyValueWrapper::WrapDaliProperty( isolate, value.Get() ); } - else if( typeInfo == typeid( Dali::TouchEvent ) ) + else if( typeInfo == typeid( Dali::TouchData ) ) { - returnValue = EventObjectGenerator::CreateTouchEvent( isolate, value.Get() ); + returnValue = EventObjectGenerator::CreateTouchData( isolate, value.Get() ); } else if( typeInfo == typeid( Dali::HoverEvent ) ) { diff --git a/plugins/dali-script-v8/src/signals/signal-manager.cpp b/plugins/dali-script-v8/src/signals/signal-manager.cpp index b943903..d806913 100644 --- a/plugins/dali-script-v8/src/signals/signal-manager.cpp +++ b/plugins/dali-script-v8/src/signals/signal-manager.cpp @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include @@ -54,7 +54,7 @@ namespace V8Plugin namespace // un-named namespace { -const char* const SIGNAL_TOUCHED = "touched"; +const char* const SIGNAL_TOUCH = "touch"; const char* const SIGNAL_HOVERED = "hovered"; const char* const SIGNAL_WHEEL_EVENT = "wheelEvent"; const char* const SIGNAL_ON_STAGE = "onStage"; @@ -64,7 +64,7 @@ const char* const SIGNAL_PAN_DETECTED = "panDetected"; const char* const STAGE_SIGNAL_KEY_EVENT = "keyEvent"; const char* const STAGE_SIGNAL_EVENT_PROCESSING_FINISHED = "eventProcessingFinished"; -const char* const STAGE_SIGNAL_TOUCHED = "touched"; +const char* const STAGE_SIGNAL_TOUCH = "touch"; const char* const SIGNAL_IMAGE_LOADING_FINISHED = "imageLoadingFinished"; const char* const SIGNAL_IMAGE_UPLOADED = "uploaded"; @@ -185,12 +185,12 @@ public: mActor(actor) { } - bool OnTouch( Actor actor, const TouchEvent& event) + bool OnTouch( Actor actor, const TouchData& touch ) { std::vector< Dali::Any > arguments; // Dali::Vector considers Dali::Any to be a non trivial type so won't compile Dali::Any returnValue(false); arguments.push_back( actor ); - arguments.push_back( event ); + arguments.push_back( touch ); CallJavaScript( returnValue, arguments ); bool ret; returnValue.Get(ret); @@ -308,11 +308,11 @@ public: Dali::Any returnValue; //no return CallJavaScript( returnValue, arguments ); } - void TouchedSignal( const TouchEvent& touchEvent) + void TouchSignal( const TouchData& touch ) { std::vector< Dali::Any > arguments; Dali::Any returnValue; //no return - arguments.push_back( touchEvent ); + arguments.push_back( touch ); CallJavaScript( returnValue, arguments ); } }; @@ -398,9 +398,9 @@ void ActorConnection( v8::Isolate* isolate, ActorCallback* callback =new ActorCallback( isolate, javaScriptCallback, signalName, actor ); - if( strcmp( signalName.c_str(), SIGNAL_TOUCHED ) == 0 ) + if( strcmp( signalName.c_str(), SIGNAL_TOUCH ) == 0 ) { - actor.TouchedSignal().Connect( callback, &ActorCallback::OnTouch ); + actor.TouchSignal().Connect( callback, &ActorCallback::OnTouch ); } else if( strcmp( signalName.c_str(), SIGNAL_HOVERED ) == 0 ) { @@ -494,9 +494,9 @@ void StageConnection( v8::Isolate* isolate, { stage.EventProcessingFinishedSignal().Connect( callback, &StageCallback::EventProcessingFinishedSignal ); } - else if (strcmp( signalName.c_str(), STAGE_SIGNAL_TOUCHED ) == 0 ) + else if (strcmp( signalName.c_str(), STAGE_SIGNAL_TOUCH ) == 0 ) { - stage.TouchedSignal().Connect( callback, &StageCallback::TouchedSignal ); + stage.TouchSignal().Connect( callback, &StageCallback::TouchSignal ); } else { @@ -598,10 +598,10 @@ void SignalManager::SignalConnect( const v8::FunctionCallbackInfo< v8::Value >& // first paramter = signal to connect to // Second parameter = function ( to run ) // args.This() = myActor - // e.g. myActor.Connect("touched", myJavaScriptActorTouched ); + // e.g. myActor.Connect("touch", myJavaScriptActorTouched ); // Inside Callback on myJavaScriptActorTouched - // myActor.Disconnect("touched", myJavaScriptActorTouched ); + // myActor.Disconnect("touch", myJavaScriptActorTouched ); v8::Isolate* isolate = args.GetIsolate(); v8::HandleScope handleScope( isolate ); -- 2.7.4