Revert "[Tizen] Not execute the remove callback"
[platform/core/uifw/dali-core.git] / dali / internal / event / events / gesture-processor.cpp
1 /*
2  * Copyright (c) 2022 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/actor-gesture-data.h>
27 #include <dali/internal/event/events/hit-test-algorithm-impl.h>
28 #include <dali/internal/event/events/ray-test.h>
29 #include <dali/internal/event/render-tasks/render-task-impl.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35 namespace
36 {
37 /**
38  * Functor to check whether an actor requires a particular gesture or not
39  */
40 struct GestureHitTestCheck : public HitTestAlgorithm::HitTestInterface
41 {
42   GestureHitTestCheck(GestureType::Value type)
43   : mType(type)
44   {
45   }
46
47   bool IsActorHittable(Actor* actor) override
48   {
49     return actor->IsGestureRequired(mType) && // Does the Application or derived actor type require the gesture?
50            actor->IsHittable();               // Is actor sensitive, visible and on the scene?
51   }
52
53   bool DescendActorHierarchy(Actor* actor) override
54   {
55     return actor->IsVisible() && // Actor is visible, if not visible then none of its children are visible.
56            actor->IsSensitive(); // Actor is sensitive, if insensitive none of its children should be hittable either.
57   }
58
59   bool DoesLayerConsumeHit(Layer* layer) override
60   {
61     return layer->IsTouchConsumed();
62   }
63
64   bool ActorRequiresHitResultCheck(Actor* actor, Integration::Point point, Vector2 hitPointLocal, uint32_t timeStamp) override
65   {
66     return actor->EmitHitTestResultSignal(point, hitPointLocal, timeStamp);
67   }
68
69   GestureType::Value mType;
70 };
71
72 } // unnamed namespace
73
74 GestureProcessor::GestureProcessor(GestureType::Value type)
75 : mGestureRecognizer(),
76   mNeedsUpdate(false),
77   mType(type),
78   mCurrentGesturedActor(nullptr),
79   mPoint(),
80   mEventTime(0u),
81   mGesturedActorDisconnected(false)
82 {
83 }
84
85 GestureProcessor::~GestureProcessor()
86 {
87   ResetActor();
88 }
89
90 void GestureProcessor::ProcessTouch(Scene& scene, const Integration::TouchEvent& event)
91 {
92   if(mGestureRecognizer)
93   {
94     if(!event.points.empty())
95     {
96       mPoint     = event.points[0];
97       mEventTime = event.time;
98     }
99     mGestureRecognizer->SendEvent(scene, event);
100   }
101 }
102
103 void GestureProcessor::GetGesturedActor(Actor*& actor, GestureDetectorContainer& gestureDetectors)
104 {
105   while(actor)
106   {
107     // We may be checking a parent so ensure the parent requires this gesture (and do not unintentionally create the gesture data for the parent)
108     if(actor->IsGestureRequired(mType))
109     {
110       // Retrieve the actor's detectors and check if they satisfy current gesture
111       const GestureDetectorContainer&                connectedDetectors(actor->GetGestureData().GetGestureDetectorContainer(mType));
112       const GestureDetectorContainer::const_iterator endIter(connectedDetectors.end());
113       for(GestureDetectorContainer::const_iterator iter = connectedDetectors.begin(); iter != endIter; ++iter)
114       {
115         GestureDetector* current(*iter);
116
117         // Check deriving class for whether the current gesture satisfies the gesture detector's parameters.
118         if(CheckGestureDetector(current, actor))
119         {
120           gestureDetectors.push_back(current);
121         }
122       }
123
124       // The hit actor or one of the parents is a gestured actor, break out.
125       if(!gestureDetectors.empty())
126       {
127         break;
128       }
129     }
130
131     // No match, we should now check the hit actor's parent.
132     actor = actor->GetParent();
133   }
134 }
135
136 void GestureProcessor::ProcessAndEmit(HitTestAlgorithm::Results& hitTestResults)
137 {
138   if(hitTestResults.actor)
139   {
140     Actor*  hitTestActor(&GetImplementation(hitTestResults.actor));
141     Actor*  actor(hitTestActor);
142     RayTest rayTest;
143
144     while(actor)
145     {
146       GestureDetectorContainer gestureDetectors;
147       GetGesturedActor(actor, gestureDetectors);
148
149       if(actor && !gestureDetectors.empty())
150       {
151         // We have a match but check if the hit point is within the gestured actor's bounds.
152         // If it is not then continue up the actor hierarchy.
153
154         if(actor == hitTestActor)
155         {
156           // Our gesture detector's attached actor WAS the hit actor so we can can emit the signal.
157           EmitGestureSignal(actor, gestureDetectors, hitTestResults.actorCoordinates);
158           // If NeedGesturePropagation is true, it passes the gesture to the parent.
159           if(!actor->NeedGesturePropagation())
160           {
161             break; // We have found AND emitted a signal on the gestured actor, break out.
162           }
163           actor->SetNeedGesturePropagation(false);
164         }
165         else
166         {
167           if(actor->IsHittable())
168           {
169             const Vector3 size(actor->GetCurrentSize());
170
171             if((size.x > 0.0f) && (size.y > 0.0f))
172             {
173               // Ensure tap is within the actor's area
174               if(rayTest.SphereTest(*actor, hitTestResults.rayOrigin, hitTestResults.rayDirection)) // Quick check
175               {
176                 Vector2 hitPointLocal;
177                 float   distance(0.0f);
178                 if(rayTest.ActorTest(*actor, hitTestResults.rayOrigin, hitTestResults.rayDirection, hitPointLocal, distance))
179                 {
180                   // One of the parents was the gestured actor so we can emit the signal for that actor.
181                   EmitGestureSignal(actor, gestureDetectors, hitPointLocal);
182                   // If NeedGesturePropagation is true, it passes the gesture to the parent.
183                   if(!actor->NeedGesturePropagation())
184                   {
185                     break; // We have found AND emitted a signal on the gestured actor, break out.
186                   }
187                   actor->SetNeedGesturePropagation(false);
188                 }
189               }
190             }
191           }
192         }
193       }
194
195       // Continue up hierarchy to see if any of the parents require this gesture.
196       if(actor)
197       {
198         actor = actor->GetParent();
199       }
200     }
201   }
202 }
203
204 bool GestureProcessor::HitTest(Scene& scene, Vector2 screenCoordinates, HitTestAlgorithm::Results& hitTestResults)
205 {
206   GestureHitTestCheck hitCheck(mType);
207   hitTestResults.point     = mPoint;
208   hitTestResults.eventTime = mEventTime;
209   HitTestAlgorithm::HitTest(scene.GetSize(), scene.GetRenderTaskList(), scene.GetLayerList(), screenCoordinates, hitTestResults, hitCheck);
210   return hitTestResults.renderTask && hitTestResults.actor;
211 }
212
213 void GestureProcessor::SetActor(Actor* actor)
214 {
215   if(actor && actor != mCurrentGesturedActor)
216   {
217     ResetActor();
218
219     mCurrentGesturedActor = actor;
220     mCurrentGesturedActor->AddObserver(*this);
221   }
222   mGesturedActorDisconnected = false;
223 }
224
225 void GestureProcessor::ResetActor()
226 {
227   if(mCurrentGesturedActor)
228   {
229     mCurrentGesturedActor->RemoveObserver(*this);
230     mCurrentGesturedActor      = nullptr;
231     mGesturedActorDisconnected = false;
232   }
233 }
234
235 Actor* GestureProcessor::GetCurrentGesturedActor()
236 {
237   return mGesturedActorDisconnected ? nullptr : mCurrentGesturedActor;
238 }
239
240 void GestureProcessor::SceneObjectRemoved(Object& object)
241 {
242   if(mCurrentGesturedActor == &object &&
243      !mGesturedActorDisconnected)
244   {
245     // Inform deriving classes.
246     OnGesturedActorStageDisconnection();
247
248     // do not call object.RemoveObserver here, object is currently iterating through observers... you wouldnt want to upset object now would you?
249     mGesturedActorDisconnected = true;
250   }
251 }
252
253 void GestureProcessor::ObjectDestroyed(Object& object)
254 {
255   if(mCurrentGesturedActor == &object)
256   {
257     // Inform deriving classes.
258     OnGesturedActorStageDisconnection();
259
260     mCurrentGesturedActor = nullptr;
261   }
262 }
263
264 } // namespace Internal
265
266 } // namespace Dali