Add event handling support for multiple windows.
[platform/core/uifw/dali-core.git] / dali / internal / event / events / gesture-processor.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/events/gesture-processor.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23 #include <dali/internal/event/actors/actor-impl.h>
24 #include <dali/internal/event/actors/layer-impl.h>
25 #include <dali/internal/event/common/scene-impl.h>
26 #include <dali/internal/event/events/hit-test-algorithm-impl.h>
27 #include <dali/internal/event/events/actor-gesture-data.h>
28 #include <dali/internal/event/render-tasks/render-task-impl.h>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 namespace
37 {
38
39 /**
40  * Functor to check whether an actor requires a particular gesture or not
41  */
42 struct GestureHitTestCheck : public HitTestAlgorithm::HitTestInterface
43 {
44   GestureHitTestCheck( Gesture::Type type )
45   : mType( type )
46   {
47   }
48
49   virtual bool IsActorHittable( Actor* actor )
50   {
51     return actor->IsGestureRequred( mType ) && // Does the Application or derived actor type require the gesture?
52            actor->IsHittable();                // Is actor sensitive, visible and on the scene?
53   }
54
55   virtual bool DescendActorHierarchy( Actor* actor )
56   {
57     return actor->IsVisible() && // Actor is visible, if not visible then none of its children are visible.
58            actor->IsSensitive(); // Actor is sensitive, if insensitive none of its children should be hittable either.
59   }
60
61   virtual bool DoesLayerConsumeHit( Layer* layer )
62   {
63     return layer->IsTouchConsumed();
64   }
65
66   Gesture::Type mType;
67 };
68
69 } // unnamed namespace
70
71
72 GestureProcessor::GestureProcessor( Gesture::Type type )
73 : mType( type ),
74   mCurrentGesturedActor( NULL ),
75   mGesturedActorDisconnected( false )
76 {
77 }
78
79 GestureProcessor::~GestureProcessor()
80 {
81   ResetActor();
82 }
83
84 void GestureProcessor::GetGesturedActor( Actor*& actor, GestureDetectorContainer& gestureDetectors )
85 {
86   while ( actor )
87   {
88     // We may be checking a parent so ensure the parent requires this gesture (and do not unintentionally create the gesture data for the parent)
89     if ( actor->IsGestureRequred( mType ) )
90     {
91       // Retrieve the actor's detectors and check if they satisfy current gesture
92       const GestureDetectorContainer& connectedDetectors( actor->GetGestureData().GetGestureDetectorContainer( mType ) );
93       const GestureDetectorContainer::const_iterator endIter( connectedDetectors.end() );
94       for ( GestureDetectorContainer::const_iterator iter = connectedDetectors.begin(); iter != endIter; ++iter )
95       {
96         GestureDetector* current(*iter);
97
98         // Check deriving class for whether the current gesture satisfies the gesture detector's parameters.
99         if ( CheckGestureDetector( current, actor ) )
100         {
101           gestureDetectors.push_back(current);
102         }
103       }
104
105       // The hit actor or one of the parents is a gestured actor, break out.
106       if ( !gestureDetectors.empty() )
107       {
108         break;
109       }
110     }
111
112     // No match, we should now check the hit actor's parent.
113     actor = actor->GetParent();
114   }
115 }
116
117 void GestureProcessor::ProcessAndEmit( HitTestAlgorithm::Results& hitTestResults )
118 {
119   if ( hitTestResults.actor )
120   {
121     Actor* hitTestActor( &GetImplementation( hitTestResults.actor ) );
122     Actor* actor( hitTestActor );
123
124     while ( actor )
125     {
126       GestureDetectorContainer gestureDetectors;
127       GetGesturedActor( actor, gestureDetectors );
128
129       if ( actor && !gestureDetectors.empty() )
130       {
131         // We have a match but check if the hit point is within the gestured actor's bounds.
132         // If it is not then continue up the actor hierarchy.
133
134         if ( actor == hitTestActor )
135         {
136           // Our gesture detector's attached actor WAS the hit actor so we can can emit the signal.
137           EmitGestureSignal( actor, gestureDetectors, hitTestResults.actorCoordinates );
138           break; // We have found AND emitted a signal on the gestured actor, break out.
139         }
140         else
141         {
142           if ( actor->IsHittable() )
143           {
144             const Vector3 size( actor->GetCurrentSize() );
145
146             if ( ( size.x > 0.0f ) && ( size.y > 0.0f ) )
147             {
148               // Ensure tap is within the actor's area
149               if ( actor->RaySphereTest( hitTestResults.rayOrigin, hitTestResults.rayDirection ) ) // Quick check
150               {
151                 Vector2 hitPointLocal;
152                 float distance( 0.0f );
153                 if( actor->RayActorTest( hitTestResults.rayOrigin, hitTestResults.rayDirection, hitPointLocal, distance ) )
154                 {
155                   // One of the parents was the gestured actor so we can emit the signal for that actor.
156                   EmitGestureSignal( actor, gestureDetectors, hitPointLocal );
157                   break; // We have found AND emitted a signal on the gestured actor, break out.
158                 }
159               }
160             }
161           }
162         }
163       }
164
165       // Continue up hierarchy to see if any of the parents require this gesture.
166       if ( actor )
167       {
168         actor = actor->GetParent();
169       }
170     }
171   }
172 }
173
174 bool GestureProcessor::HitTest( Scene& scene, Vector2 screenCoordinates, HitTestAlgorithm::Results& hitTestResults )
175 {
176   GestureHitTestCheck hitCheck( mType );
177   HitTestAlgorithm::HitTest( scene.GetSize(), scene.GetRenderTaskList(), scene.GetLayerList(), screenCoordinates, hitTestResults, hitCheck );
178   return hitTestResults.renderTask && hitTestResults.actor;
179 }
180
181 void GestureProcessor::SetActor( Actor* actor )
182 {
183   if ( actor && actor != mCurrentGesturedActor )
184   {
185     ResetActor();
186
187     mCurrentGesturedActor = actor;
188     mCurrentGesturedActor->AddObserver( *this );
189   }
190   mGesturedActorDisconnected = false;
191 }
192
193 void GestureProcessor::ResetActor()
194 {
195   if ( mCurrentGesturedActor )
196   {
197     mCurrentGesturedActor->RemoveObserver( *this );
198     mCurrentGesturedActor = NULL;
199     mGesturedActorDisconnected = false;
200   }
201 }
202
203 Actor* GestureProcessor::GetCurrentGesturedActor()
204 {
205   return mGesturedActorDisconnected ? NULL : mCurrentGesturedActor;
206 }
207
208 void GestureProcessor::SceneObjectRemoved(Object& object)
209 {
210   if ( mCurrentGesturedActor == &object &&
211       !mGesturedActorDisconnected )
212   {
213     // Inform deriving classes.
214     OnGesturedActorStageDisconnection();
215
216     // do not call object.RemoveObserver here, object is currently iterating through observers... you wouldnt want to upset object now would you?
217     mGesturedActorDisconnected = true;
218   }
219 }
220
221 void GestureProcessor::ObjectDestroyed(Object& object)
222 {
223   if ( mCurrentGesturedActor == &object )
224   {
225     // Inform deriving classes.
226     OnGesturedActorStageDisconnection();
227
228     mCurrentGesturedActor = NULL;
229   }
230 }
231
232 } // namespace Internal
233
234 } // namespace Dali