Touch and Hover event propagrated by geometry way.(5)
[platform/core/uifw/dali-core.git] / dali / internal / event / events / pinch-gesture / pinch-gesture-processor.cpp
1 /*
2  * Copyright (c) 2023 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/pinch-gesture/pinch-gesture-processor.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/trace.h>
27 #include <dali/internal/event/common/scene-impl.h>
28 #include <dali/internal/event/events/gesture-requests.h>
29 #include <dali/internal/event/events/pinch-gesture/pinch-gesture-event.h>
30 #include <dali/internal/event/events/pinch-gesture/pinch-gesture-impl.h>
31 #include <dali/internal/event/events/pinch-gesture/pinch-gesture-recognizer.h>
32 #include <dali/internal/event/render-tasks/render-task-impl.h>
33 #include <dali/public-api/actors/actor.h>
34 #include <dali/public-api/events/pinch-gesture.h>
35 #include <dali/public-api/math/vector2.h>
36
37 namespace Dali
38 {
39 namespace Internal
40 {
41 namespace
42 {
43 DALI_INIT_TRACE_FILTER(gTraceFilter, DALI_TRACE_PERFORMANCE_MARKER, false);
44 const uint32_t MINIMUM_TOUCH_EVENTS_REQUIRED             = 4u;
45 const uint32_t MINIMUM_TOUCH_EVENTS_REQUIRED_AFTER_START = 4u;
46
47 /**
48  * Creates a PinchGesture and asks the specified detector to emit its detected signal.
49  * @param[in]  actor             The actor that has been pinched.
50  * @param[in]  gestureDetectors  The gesture detector container that should emit the signal.
51  * @param[in]  pinchEvent        The pinchEvent received from the adaptor.
52  * @param[in]  localCenter       Relative to the actor attached to the detector.
53  */
54 void EmitPinchSignal(
55   Actor*                          actor,
56   const GestureDetectorContainer& gestureDetectors,
57   const PinchGestureEvent&        pinchEvent,
58   Vector2                         localCenter)
59 {
60   Internal::PinchGesturePtr pinch(new Internal::PinchGesture(pinchEvent.state));
61   pinch->SetTime(pinchEvent.time);
62
63   pinch->SetScale(pinchEvent.scale);
64   pinch->SetSpeed(pinchEvent.speed);
65   pinch->SetScreenCenterPoint(pinchEvent.centerPoint);
66
67   pinch->SetLocalCenterPoint(localCenter);
68   pinch->SetSourceType(pinchEvent.sourceType);
69   pinch->SetSourceData(pinchEvent.sourceData);
70
71   DALI_TRACE_BEGIN_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_EMIT_PINCH_GESTURE_SIGNAL", [&](std::ostringstream& oss) {
72     oss << "[" << gestureDetectors.size() << "]";
73   });
74
75   Dali::Actor                                    actorHandle(actor);
76   const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
77   for(GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter)
78   {
79     static_cast<PinchGestureDetector*>(*iter)->EmitPinchGestureSignal(actorHandle, Dali::PinchGesture(pinch.Get()));
80   }
81
82   DALI_TRACE_END_WITH_MESSAGE_GENERATOR(gTraceFilter, "DALI_EMIT_PINCH_GESTURE_SIGNAL", [&](std::ostringstream& oss) {
83     oss << "[" << gestureDetectors.size() << "]";
84   });
85 }
86
87 /**
88  * Functor which checks whether the specified actor is attached to the gesture detector.
89  * It returns true if it is no longer attached.  This can be used in remove_if functions.
90  */
91 struct IsNotAttachedFunctor
92 {
93   /**
94    * Constructor
95    * @param[in]  actor  The actor to check whether it is attached.
96    */
97   IsNotAttachedFunctor(Actor* actor)
98   : actorToCheck(actor)
99   {
100   }
101
102   /**
103    * Returns true if not attached, false if it is still attached.
104    * @param[in]  detector  The detector to check.
105    * @return true, if not attached, false otherwise.
106    */
107   bool operator()(const GestureDetector* detector) const
108   {
109     return !detector->IsAttached(*actorToCheck);
110   }
111
112   Actor* actorToCheck; ///< The actor to check whether it is attached or not.
113 };
114
115 } // unnamed namespace
116
117 PinchGestureProcessor::PinchGestureProcessor()
118 : GestureProcessor(GestureType::PINCH),
119   mPinchGestureDetectors(),
120   mCurrentPinchEmitters(),
121   mCurrentPinchEvent(nullptr),
122   mMinimumPinchDistance(-1.0f),
123   mMinimumTouchEvents(MINIMUM_TOUCH_EVENTS_REQUIRED),
124   mMinimumTouchEventsAfterStart(MINIMUM_TOUCH_EVENTS_REQUIRED_AFTER_START)
125 {
126 }
127
128 PinchGestureProcessor::~PinchGestureProcessor() = default;
129
130 void PinchGestureProcessor::SetMinimumPinchDistance(float value)
131 {
132   mMinimumPinchDistance = value;
133
134   if(mGestureRecognizer)
135   {
136     PinchGestureRecognizer* pinchRecognizer = dynamic_cast<PinchGestureRecognizer*>(mGestureRecognizer.Get());
137     if(pinchRecognizer)
138     {
139       pinchRecognizer->SetMinimumPinchDistance(value);
140     }
141   }
142 }
143
144 void PinchGestureProcessor::SetMinimumTouchEvents(uint32_t value)
145 {
146   if(value > 1u && mMinimumTouchEvents != value)
147   {
148     mMinimumTouchEvents = value;
149
150     if(mGestureRecognizer)
151     {
152       PinchGestureRecognizer* pinchRecognizer = dynamic_cast<PinchGestureRecognizer*>(mGestureRecognizer.Get());
153       if(pinchRecognizer)
154       {
155         pinchRecognizer->SetMinimumTouchEvents(value);
156       }
157     }
158   }
159 }
160
161 void PinchGestureProcessor::SetMinimumTouchEventsAfterStart(uint32_t value)
162 {
163   if(value > 1u && mMinimumTouchEventsAfterStart != value)
164   {
165     mMinimumTouchEventsAfterStart = value;
166
167     if(mGestureRecognizer)
168     {
169       PinchGestureRecognizer* pinchRecognizer = dynamic_cast<PinchGestureRecognizer*>(mGestureRecognizer.Get());
170       if(pinchRecognizer)
171       {
172         pinchRecognizer->SetMinimumTouchEventsAfterStart(value);
173       }
174     }
175   }
176 }
177
178 void PinchGestureProcessor::Process(Scene& scene, const PinchGestureEvent& pinchEvent)
179 {
180   DALI_TRACE_SCOPE(gTraceFilter, "DALI_PROCESS_PINCH_GESTURE");
181   switch(pinchEvent.state)
182   {
183     case GestureState::STARTED:
184     {
185       // The pinch gesture should only be sent to the gesture detector which first received it so that
186       // it can be told when the gesture ends as well.
187
188       mCurrentPinchEmitters.clear();
189       ResetActor();
190
191       HitTestAlgorithm::Results hitTestResults;
192       if(GetFeededActor())
193       {
194         hitTestResults.actor = Dali::Actor(GetFeededActor());
195         hitTestResults.renderTask = GetFeededRenderTask();
196
197         // Record the current render-task for Screen->Actor coordinate conversions
198         mCurrentRenderTask = hitTestResults.renderTask;
199
200         Vector2     actorCoords;
201         GetFeededActor()->ScreenToLocal(*mCurrentRenderTask.Get(), actorCoords.x, actorCoords.y, pinchEvent.centerPoint.x, pinchEvent.centerPoint.y);
202         hitTestResults.actorCoordinates = actorCoords;
203
204         // Set mCurrentPinchEvent to use inside overridden methods called from ProcessAndEmit()
205         mCurrentPinchEvent = &pinchEvent;
206         ProcessAndEmitActor(hitTestResults, GetFeededGestureDetector());
207         mCurrentPinchEvent = nullptr;
208       }
209       else if(HitTest(scene, pinchEvent.centerPoint, hitTestResults))
210       {
211         // Record the current render-task for Screen->Actor coordinate conversions
212         mCurrentRenderTask = hitTestResults.renderTask;
213
214         // Set mCurrentPinchEvent to use inside overridden methods called from ProcessAndEmit()
215         mCurrentPinchEvent = &pinchEvent;
216         ProcessAndEmit(hitTestResults);
217         mCurrentPinchEvent = nullptr;
218       }
219       break;
220     }
221
222     case GestureState::CONTINUING:
223     case GestureState::FINISHED:
224     case GestureState::CANCELLED:
225     {
226       // Only send subsequent pinch gesture signals if we processed the pinch gesture when it started.
227       // Check if actor is still touchable.
228
229       Actor* currentGesturedActor = GetCurrentGesturedActor();
230       if(currentGesturedActor)
231       {
232         if(currentGesturedActor->IsHittable() && !mCurrentPinchEmitters.empty() && mCurrentRenderTask)
233         {
234           // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
235           GestureDetectorContainer::iterator endIter = std::remove_if(mCurrentPinchEmitters.begin(), mCurrentPinchEmitters.end(), IsNotAttachedFunctor(currentGesturedActor));
236           mCurrentPinchEmitters.erase(endIter, mCurrentPinchEmitters.end());
237           if(GetFeededActor() && GetFeededGestureDetector())
238           {
239             mCurrentPinchEmitters.push_back(GetFeededGestureDetector());
240           }
241
242           if(!mCurrentPinchEmitters.empty())
243           {
244             Vector2     actorCoords;
245             RenderTask& renderTaskImpl(*mCurrentRenderTask.Get());
246             currentGesturedActor->ScreenToLocal(renderTaskImpl, actorCoords.x, actorCoords.y, pinchEvent.centerPoint.x, pinchEvent.centerPoint.y);
247
248             EmitPinchSignal(currentGesturedActor, mCurrentPinchEmitters, pinchEvent, actorCoords);
249           }
250           else
251           {
252             // If we have no current emitters then clear pinched actor as well.
253             ResetActor();
254           }
255
256           // Clear current emitters if pinch gesture has ended or been cancelled.
257           if(pinchEvent.state == GestureState::FINISHED || pinchEvent.state == GestureState::CANCELLED)
258           {
259             mCurrentPinchEmitters.clear();
260             ResetActor();
261           }
262         }
263         else
264         {
265           mCurrentPinchEmitters.clear();
266           ResetActor();
267         }
268       }
269       break;
270     }
271
272     case GestureState::CLEAR:
273     {
274       DALI_ABORT("Incorrect state received from Integration layer: CLEAR\n");
275       break;
276     }
277     case GestureState::POSSIBLE:
278     {
279       DALI_ABORT("Incorrect state received from Integration layer: POSSIBLE\n");
280       break;
281     }
282   }
283 }
284
285 void PinchGestureProcessor::AddGestureDetector(PinchGestureDetector* gestureDetector, Scene& scene)
286 {
287   bool createRecognizer(mPinchGestureDetectors.empty());
288
289   mPinchGestureDetectors.push_back(gestureDetector);
290
291   if(createRecognizer)
292   {
293     Size size          = scene.GetSize();
294     mGestureRecognizer = new PinchGestureRecognizer(*this, Vector2(size.width, size.height), scene.GetDpi(), mMinimumPinchDistance, mMinimumTouchEvents, mMinimumTouchEventsAfterStart);
295   }
296 }
297
298 void PinchGestureProcessor::RemoveGestureDetector(PinchGestureDetector* gestureDetector)
299 {
300   if(!mCurrentPinchEmitters.empty())
301   {
302     // Check if the removed detector was one that is currently being pinched and remove it from emitters.
303     GestureDetectorContainer::iterator endIter = std::remove(mCurrentPinchEmitters.begin(), mCurrentPinchEmitters.end(), gestureDetector);
304     mCurrentPinchEmitters.erase(endIter, mCurrentPinchEmitters.end());
305
306     // If we no longer have any emitters, then we should clear mCurrentGesturedActor as well
307     if(mCurrentPinchEmitters.empty())
308     {
309       ResetActor();
310     }
311   }
312
313   // Find the detector...
314   PinchGestureDetectorContainer::iterator endIter = std::remove(mPinchGestureDetectors.begin(), mPinchGestureDetectors.end(), gestureDetector);
315   DALI_ASSERT_DEBUG(endIter != mPinchGestureDetectors.end());
316
317   // ...and remove it
318   mPinchGestureDetectors.erase(endIter, mPinchGestureDetectors.end());
319
320   if(mPinchGestureDetectors.empty())
321   {
322     mGestureRecognizer = nullptr;
323   }
324 }
325
326 void PinchGestureProcessor::GestureDetectorUpdated(PinchGestureDetector* gestureDetector)
327 {
328   // Nothing to do as PinchGestureDetector does not have any specific parameters.
329 }
330
331 void PinchGestureProcessor::OnGesturedActorStageDisconnection()
332 {
333   mCurrentPinchEmitters.clear();
334 }
335
336 bool PinchGestureProcessor::CheckGestureDetector(GestureDetector* detector, Actor* actor)
337 {
338   // No special case required for pinch.
339   return true;
340 }
341
342 void PinchGestureProcessor::EmitGestureSignal(Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates)
343 {
344   DALI_ASSERT_DEBUG(mCurrentPinchEvent);
345
346   EmitPinchSignal(actor, gestureDetectors, *mCurrentPinchEvent, actorCoordinates);
347
348   if(actor->OnScene())
349   {
350     mCurrentPinchEmitters = gestureDetectors;
351     SetActor(actor);
352   }
353 }
354
355 } // namespace Internal
356
357 } // namespace Dali