2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
18 #include <dali/internal/event/events/gesture-processor.h>
21 #include <dali/integration-api/debug.h>
22 #include <dali/internal/event/actors/actor-impl.h>
23 #include <dali/internal/event/render-tasks/render-task-impl.h>
24 #include <dali/internal/event/events/hit-test-algorithm-impl.h>
36 * Functor to check whether an actor requires a particular gesture or not
38 struct GestureHitTestCheck : public HitTestAlgorithm::HitTestInterface
40 GestureHitTestCheck( Gesture::Type type )
45 virtual bool IsActorHittable( Actor* actor )
47 return actor->IsGestureRequred( mType ) && // Does the Application or derived actor type require the gesture?
48 actor->IsHittable(); // Is actor sensitive, visible and on the scene?
51 virtual bool DescendActorHierarchy( Actor* actor )
53 return actor->IsVisible() && // Actor is visible, if not visible then none of its children are visible.
54 actor->IsSensitive(); // Actor is sensitive, if insensitive none of its children should be hittable either.
60 } // unnamed namespace
63 GestureProcessor::GestureProcessor( Gesture::Type type )
65 mCurrentGesturedActor( NULL ),
66 mGesturedActorDisconnected( false )
70 GestureProcessor::~GestureProcessor()
75 void GestureProcessor::GetGesturedActor( Dali::Actor& actor, const GestureDetectorContainer& connectedDetectors, std::vector<GestureDetector*>& gestureDetectors, Functor& functor )
79 Actor* actorImpl( &GetImplementation(actor) );
81 // Check if our hit actor or any of its parents are attached to any registered detector.
82 // Find all detectors that have the actor attached.
83 for ( GestureDetectorContainer::const_iterator iter = connectedDetectors.begin(), endIter = connectedDetectors.end(); iter != endIter; ++iter )
85 GestureDetector* current(*iter);
87 // Check whether the actor is attached to the gesture detector and then call the functor to
88 // check whether the gesture detector satisfies the current gesture's parameters.
89 if ( current->IsAttached( *actorImpl ) && functor( current, actorImpl ) )
91 gestureDetectors.push_back(current);
95 // The hit actor or one of the parents is a gestured actor, break out.
96 if ( !gestureDetectors.empty() )
101 // No match, we should now check the hit actor's parent.
102 actor = actor.GetParent();
106 void GestureProcessor::ProcessAndEmit( const HitTestAlgorithm::Results& hitTestResults, const GestureDetectorContainer& connectedDetectors, Functor& functor )
108 Dali::Actor actor( hitTestResults.actor );
112 std::vector<GestureDetector*> gestureDetectors;
114 GetGesturedActor( actor, connectedDetectors, gestureDetectors, functor );
116 if ( actor && !gestureDetectors.empty() )
118 // We have a match but check if the hit point is within the gestured actor's bounds.
119 // If it is not then continue up the actor hierarchy.
121 if ( actor == hitTestResults.actor )
123 // Our gesture detector's attached actor WAS the hit actor so we can call the emitting functor.
124 functor( actor, gestureDetectors, hitTestResults.actorCoordinates );
125 break; // We have found AND emitted a signal on the gestured actor, break out.
129 if ( GetImplementation(actor).IsHittable() )
131 const Vector3 size( actor.GetCurrentSize() );
133 if ( ( size.x > 0.0f ) && ( size.y > 0.0f ) )
135 // Ensure tap is within the actor's area
136 Actor& actorImpl = GetImplementation(actor);
137 if ( actorImpl.RaySphereTest( hitTestResults.rayOrigin, hitTestResults.rayDirection ) ) // Quick check
139 Vector4 hitPointLocal;
140 float distance( 0.0f );
141 if( actorImpl.RayActorTest( hitTestResults.rayOrigin, hitTestResults.rayDirection, hitPointLocal, distance ) )
143 // One of the hit actor's parents was the gestured actor so call the emitting functor.
144 functor( actor, gestureDetectors, Vector2( hitPointLocal.x, hitPointLocal.y ) );
145 break; // We have found AND emitted a signal on the gestured actor, break out.
153 // Continue up hierarchy to see if any of the parents require this gesture.
156 actor = actor.GetParent();
161 bool GestureProcessor::HitTest(
163 Vector2 screenCoordinates,
164 HitTestAlgorithm::Results& hitTestResults)
166 GestureHitTestCheck hitCheck( mType );
167 HitTestAlgorithm::HitTest( stage, screenCoordinates, hitTestResults, hitCheck );
168 return hitTestResults.renderTask && hitTestResults.actor;
171 void GestureProcessor::SetActor( Dali::Actor actor )
173 if ( actor && actor != mCurrentGesturedActor )
177 mCurrentGesturedActor = &GetImplementation( actor );
178 mCurrentGesturedActor->AddObserver( *this );
180 mGesturedActorDisconnected = false;
183 void GestureProcessor::ResetActor()
185 if ( mCurrentGesturedActor )
187 mCurrentGesturedActor->RemoveObserver( *this );
188 mCurrentGesturedActor = NULL;
189 mGesturedActorDisconnected = false;
193 Actor* GestureProcessor::GetCurrentGesturedActor()
195 return mGesturedActorDisconnected ? NULL : mCurrentGesturedActor;
198 void GestureProcessor::SceneObjectRemoved(ProxyObject& proxy)
200 if ( mCurrentGesturedActor == &proxy &&
201 !mGesturedActorDisconnected )
203 // Inform deriving classes.
204 OnGesturedActorStageDisconnection();
206 // do not call proxy.RemoveObserver here, proxy is currently iterating through observers... you wouldnt want to upset proxy now would you?
207 mGesturedActorDisconnected = true;
211 void GestureProcessor::ProxyDestroyed(ProxyObject& proxy)
213 if ( mCurrentGesturedActor == &proxy )
215 // Inform deriving classes.
216 OnGesturedActorStageDisconnection();
218 mCurrentGesturedActor = NULL;
222 } // namespace Internal