2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/events/pinch-gesture/pinch-gesture-processor.h>
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>
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;
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.
56 const GestureDetectorContainer& gestureDetectors,
57 const PinchGestureEvent& pinchEvent,
60 Internal::PinchGesturePtr pinch(new Internal::PinchGesture(pinchEvent.state));
61 pinch->SetTime(pinchEvent.time);
63 pinch->SetScale(pinchEvent.scale);
64 pinch->SetSpeed(pinchEvent.speed);
65 pinch->SetScreenCenterPoint(pinchEvent.centerPoint);
67 pinch->SetLocalCenterPoint(localCenter);
68 pinch->SetSourceType(pinchEvent.sourceType);
69 pinch->SetSourceData(pinchEvent.sourceData);
71 Dali::Actor actorHandle(actor);
72 const GestureDetectorContainer::const_iterator endIter = gestureDetectors.end();
73 for(GestureDetectorContainer::const_iterator iter = gestureDetectors.begin(); iter != endIter; ++iter)
75 static_cast<PinchGestureDetector*>(*iter)->EmitPinchGestureSignal(actorHandle, Dali::PinchGesture(pinch.Get()));
80 * Functor which checks whether the specified actor is attached to the gesture detector.
81 * It returns true if it is no longer attached. This can be used in remove_if functions.
83 struct IsNotAttachedFunctor
87 * @param[in] actor The actor to check whether it is attached.
89 IsNotAttachedFunctor(Actor* actor)
95 * Returns true if not attached, false if it is still attached.
96 * @param[in] detector The detector to check.
97 * @return true, if not attached, false otherwise.
99 bool operator()(const GestureDetector* detector) const
101 return !detector->IsAttached(*actorToCheck);
104 Actor* actorToCheck; ///< The actor to check whether it is attached or not.
107 } // unnamed namespace
109 PinchGestureProcessor::PinchGestureProcessor()
110 : GestureProcessor(GestureType::PINCH),
111 mPinchGestureDetectors(),
112 mCurrentPinchEmitters(),
113 mCurrentPinchEvent(nullptr),
114 mMinimumPinchDistance(-1.0f),
115 mMinimumTouchEvents(MINIMUM_TOUCH_EVENTS_REQUIRED),
116 mMinimumTouchEventsAfterStart(MINIMUM_TOUCH_EVENTS_REQUIRED_AFTER_START)
120 PinchGestureProcessor::~PinchGestureProcessor() = default;
122 void PinchGestureProcessor::SetMinimumPinchDistance(float value)
124 mMinimumPinchDistance = value;
126 if(mGestureRecognizer)
128 PinchGestureRecognizer* pinchRecognizer = dynamic_cast<PinchGestureRecognizer*>(mGestureRecognizer.Get());
131 pinchRecognizer->SetMinimumPinchDistance(value);
136 void PinchGestureProcessor::SetMinimumTouchEvents(uint32_t value)
138 if(value > 1u && mMinimumTouchEvents != value)
140 mMinimumTouchEvents = value;
142 if(mGestureRecognizer)
144 PinchGestureRecognizer* pinchRecognizer = dynamic_cast<PinchGestureRecognizer*>(mGestureRecognizer.Get());
147 pinchRecognizer->SetMinimumTouchEvents(value);
153 void PinchGestureProcessor::SetMinimumTouchEventsAfterStart(uint32_t value)
155 if(value > 1u && mMinimumTouchEventsAfterStart != value)
157 mMinimumTouchEventsAfterStart = value;
159 if(mGestureRecognizer)
161 PinchGestureRecognizer* pinchRecognizer = dynamic_cast<PinchGestureRecognizer*>(mGestureRecognizer.Get());
164 pinchRecognizer->SetMinimumTouchEventsAfterStart(value);
170 void PinchGestureProcessor::Process(Scene& scene, const PinchGestureEvent& pinchEvent)
172 DALI_TRACE_SCOPE(gTraceFilter, "DALI_PROCESS_PINCH_GESTURE");
173 switch(pinchEvent.state)
175 case GestureState::STARTED:
177 // The pinch gesture should only be sent to the gesture detector which first received it so that
178 // it can be told when the gesture ends as well.
180 mCurrentPinchEmitters.clear();
183 HitTestAlgorithm::Results hitTestResults;
184 if(HitTest(scene, pinchEvent.centerPoint, hitTestResults))
186 // Record the current render-task for Screen->Actor coordinate conversions
187 mCurrentRenderTask = hitTestResults.renderTask;
189 // Set mCurrentPinchEvent to use inside overridden methods called from ProcessAndEmit()
190 mCurrentPinchEvent = &pinchEvent;
191 ProcessAndEmit(hitTestResults);
192 mCurrentPinchEvent = nullptr;
197 case GestureState::CONTINUING:
198 case GestureState::FINISHED:
199 case GestureState::CANCELLED:
201 // Only send subsequent pinch gesture signals if we processed the pinch gesture when it started.
202 // Check if actor is still touchable.
204 Actor* currentGesturedActor = GetCurrentGesturedActor();
205 if(currentGesturedActor)
207 if(currentGesturedActor->IsHittable() && !mCurrentPinchEmitters.empty() && mCurrentRenderTask)
209 // Ensure actor is still attached to the emitters, if it is not then remove the emitter.
210 GestureDetectorContainer::iterator endIter = std::remove_if(mCurrentPinchEmitters.begin(), mCurrentPinchEmitters.end(), IsNotAttachedFunctor(currentGesturedActor));
211 mCurrentPinchEmitters.erase(endIter, mCurrentPinchEmitters.end());
213 if(!mCurrentPinchEmitters.empty())
216 RenderTask& renderTaskImpl(*mCurrentRenderTask.Get());
217 currentGesturedActor->ScreenToLocal(renderTaskImpl, actorCoords.x, actorCoords.y, pinchEvent.centerPoint.x, pinchEvent.centerPoint.y);
219 EmitPinchSignal(currentGesturedActor, mCurrentPinchEmitters, pinchEvent, actorCoords);
223 // If we have no current emitters then clear pinched actor as well.
227 // Clear current emitters if pinch gesture has ended or been cancelled.
228 if(pinchEvent.state == GestureState::FINISHED || pinchEvent.state == GestureState::CANCELLED)
230 mCurrentPinchEmitters.clear();
236 mCurrentPinchEmitters.clear();
243 case GestureState::CLEAR:
245 DALI_ABORT("Incorrect state received from Integration layer: CLEAR\n");
248 case GestureState::POSSIBLE:
250 DALI_ABORT("Incorrect state received from Integration layer: POSSIBLE\n");
256 void PinchGestureProcessor::AddGestureDetector(PinchGestureDetector* gestureDetector, Scene& scene)
258 bool createRecognizer(mPinchGestureDetectors.empty());
260 mPinchGestureDetectors.push_back(gestureDetector);
264 Size size = scene.GetSize();
265 mGestureRecognizer = new PinchGestureRecognizer(*this, Vector2(size.width, size.height), scene.GetDpi(), mMinimumPinchDistance, mMinimumTouchEventsAfterStart, mMinimumTouchEventsAfterStart);
269 void PinchGestureProcessor::RemoveGestureDetector(PinchGestureDetector* gestureDetector)
271 if(!mCurrentPinchEmitters.empty())
273 // Check if the removed detector was one that is currently being pinched and remove it from emitters.
274 GestureDetectorContainer::iterator endIter = std::remove(mCurrentPinchEmitters.begin(), mCurrentPinchEmitters.end(), gestureDetector);
275 mCurrentPinchEmitters.erase(endIter, mCurrentPinchEmitters.end());
277 // If we no longer have any emitters, then we should clear mCurrentGesturedActor as well
278 if(mCurrentPinchEmitters.empty())
284 // Find the detector...
285 PinchGestureDetectorContainer::iterator endIter = std::remove(mPinchGestureDetectors.begin(), mPinchGestureDetectors.end(), gestureDetector);
286 DALI_ASSERT_DEBUG(endIter != mPinchGestureDetectors.end());
289 mPinchGestureDetectors.erase(endIter, mPinchGestureDetectors.end());
291 if(mPinchGestureDetectors.empty())
293 mGestureRecognizer = nullptr;
297 void PinchGestureProcessor::GestureDetectorUpdated(PinchGestureDetector* gestureDetector)
299 // Nothing to do as PinchGestureDetector does not have any specific parameters.
302 void PinchGestureProcessor::OnGesturedActorStageDisconnection()
304 mCurrentPinchEmitters.clear();
307 bool PinchGestureProcessor::CheckGestureDetector(GestureDetector* detector, Actor* actor)
309 // No special case required for pinch.
313 void PinchGestureProcessor::EmitGestureSignal(Actor* actor, const GestureDetectorContainer& gestureDetectors, Vector2 actorCoordinates)
315 DALI_ASSERT_DEBUG(mCurrentPinchEvent);
317 EmitPinchSignal(actor, gestureDetectors, *mCurrentPinchEvent, actorCoordinates);
321 mCurrentPinchEmitters = gestureDetectors;
326 } // namespace Internal