[Tizen] Not execute the remove callback
[platform/core/uifw/dali-core.git] / dali / internal / event / events / gesture-processor.cpp
1 /*
2  * Copyright (c) 2021 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   GestureType::Value mType;
65 };
66
67 } // unnamed namespace
68
69 GestureProcessor::GestureProcessor(GestureType::Value type)
70 : mGestureRecognizer(),
71   mNeedsUpdate(false),
72   mType(type),
73   mCurrentGesturedActor(nullptr),
74   mGesturedActorDisconnected(false)
75 {
76 }
77
78 GestureProcessor::~GestureProcessor()
79 {
80   ResetActor();
81 }
82
83 void GestureProcessor::ProcessTouch(Scene& scene, const Integration::TouchEvent& event)
84 {
85   if(mGestureRecognizer)
86   {
87     mGestureRecognizer->SendEvent(scene, event);
88   }
89 }
90
91 void GestureProcessor::GetGesturedActor(Actor*& actor, GestureDetectorContainer& gestureDetectors)
92 {
93   while(actor)
94   {
95     // We may be checking a parent so ensure the parent requires this gesture (and do not unintentionally create the gesture data for the parent)
96     if(actor->IsGestureRequired(mType))
97     {
98       // Retrieve the actor's detectors and check if they satisfy current gesture
99       const GestureDetectorContainer&                connectedDetectors(actor->GetGestureData().GetGestureDetectorContainer(mType));
100       const GestureDetectorContainer::const_iterator endIter(connectedDetectors.end());
101       for(GestureDetectorContainer::const_iterator iter = connectedDetectors.begin(); iter != endIter; ++iter)
102       {
103         GestureDetector* current(*iter);
104
105         // Check deriving class for whether the current gesture satisfies the gesture detector's parameters.
106         if(CheckGestureDetector(current, actor))
107         {
108           gestureDetectors.push_back(current);
109         }
110       }
111
112       // The hit actor or one of the parents is a gestured actor, break out.
113       if(!gestureDetectors.empty())
114       {
115         break;
116       }
117     }
118
119     // No match, we should now check the hit actor's parent.
120     actor = actor->GetParent();
121   }
122 }
123
124 void GestureProcessor::ProcessAndEmit(HitTestAlgorithm::Results& hitTestResults)
125 {
126   if(hitTestResults.actor)
127   {
128     Actor*  hitTestActor(&GetImplementation(hitTestResults.actor));
129     Actor*  actor(hitTestActor);
130     RayTest rayTest;
131
132     while(actor)
133     {
134       GestureDetectorContainer gestureDetectors;
135       GetGesturedActor(actor, gestureDetectors);
136
137       if(actor && !gestureDetectors.empty())
138       {
139         // We have a match but check if the hit point is within the gestured actor's bounds.
140         // If it is not then continue up the actor hierarchy.
141
142         if(actor == hitTestActor)
143         {
144           // Our gesture detector's attached actor WAS the hit actor so we can can emit the signal.
145           EmitGestureSignal(actor, gestureDetectors, hitTestResults.actorCoordinates);
146           break; // We have found AND emitted a signal on the gestured actor, break out.
147         }
148         else
149         {
150           if(actor->IsHittable())
151           {
152             const Vector3 size(actor->GetCurrentSize());
153
154             if((size.x > 0.0f) && (size.y > 0.0f))
155             {
156               // Ensure tap is within the actor's area
157               if(rayTest.SphereTest(*actor, hitTestResults.rayOrigin, hitTestResults.rayDirection)) // Quick check
158               {
159                 Vector2 hitPointLocal;
160                 float   distance(0.0f);
161                 if(rayTest.ActorTest(*actor, hitTestResults.rayOrigin, hitTestResults.rayDirection, hitPointLocal, distance))
162                 {
163                   // One of the parents was the gestured actor so we can emit the signal for that actor.
164                   EmitGestureSignal(actor, gestureDetectors, hitPointLocal);
165                   break; // We have found AND emitted a signal on the gestured actor, break out.
166                 }
167               }
168             }
169           }
170         }
171       }
172
173       // Continue up hierarchy to see if any of the parents require this gesture.
174       if(actor)
175       {
176         actor = actor->GetParent();
177       }
178     }
179   }
180 }
181
182 bool GestureProcessor::HitTest(Scene& scene, Vector2 screenCoordinates, HitTestAlgorithm::Results& hitTestResults)
183 {
184   GestureHitTestCheck hitCheck(mType);
185   HitTestAlgorithm::HitTest(scene.GetSize(), scene.GetRenderTaskList(), scene.GetLayerList(), screenCoordinates, hitTestResults, hitCheck);
186   return hitTestResults.renderTask && hitTestResults.actor;
187 }
188
189 void GestureProcessor::SetActor(Actor* actor)
190 {
191   if(actor && actor != mCurrentGesturedActor)
192   {
193     ResetActor();
194
195     mCurrentGesturedActor = actor;
196     mCurrentGesturedActor->AddObserver(*this);
197   }
198   mGesturedActorDisconnected = false;
199 }
200
201 void GestureProcessor::ResetActor()
202 {
203   if(mCurrentGesturedActor)
204   {
205     mCurrentGesturedActor->RemoveObserver(*this);
206     mCurrentGesturedActor      = nullptr;
207     mGesturedActorDisconnected = false;
208   }
209 }
210
211 Actor* GestureProcessor::GetCurrentGesturedActor()
212 {
213   return mGesturedActorDisconnected ? nullptr : mCurrentGesturedActor;
214 }
215
216 void GestureProcessor::SceneObjectRemoved(Object& object)
217 {
218   if(mCurrentGesturedActor == &object &&
219      !mGesturedActorDisconnected)
220   {
221     // Inform deriving classes.
222     OnGesturedActorStageDisconnection();
223
224     // do not call object.RemoveObserver here, object is currently iterating through observers... you wouldnt want to upset object now would you?
225     mGesturedActorDisconnected = true;
226   }
227 }
228
229 void GestureProcessor::ObjectDestroyed(Object& object)
230 {
231   if(mCurrentGesturedActor == &object)
232   {
233     // Inform deriving classes.
234     OnGesturedActorStageDisconnection();
235
236     mCurrentGesturedActor = nullptr;
237   }
238 }
239
240 } // namespace Internal
241
242 } // namespace Dali