2 * Copyright (c) 2022 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/gesture-processor.h>
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>
38 * Functor to check whether an actor requires a particular gesture or not
40 struct GestureHitTestCheck : public HitTestAlgorithm::HitTestInterface
42 GestureHitTestCheck(GestureType::Value type)
47 bool IsActorHittable(Actor* actor) override
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?
53 bool DescendActorHierarchy(Actor* actor) override
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.
59 bool DoesLayerConsumeHit(Layer* layer) override
61 return layer->IsTouchConsumed();
64 bool ActorRequiresHitResultCheck(Actor* actor, Integration::Point point, Vector2 hitPointLocal, uint32_t timeStamp) override
66 return actor->EmitHitTestResultSignal(point, hitPointLocal, timeStamp);
69 GestureType::Value mType;
72 } // unnamed namespace
74 GestureProcessor::GestureProcessor(GestureType::Value type)
75 : mGestureRecognizer(),
78 mCurrentGesturedActor(nullptr),
81 mGesturedActorDisconnected(false)
85 GestureProcessor::~GestureProcessor()
90 void GestureProcessor::ProcessTouch(Scene& scene, const Integration::TouchEvent& event)
92 if(mGestureRecognizer)
94 if(!event.points.empty())
96 mPoint = event.points[0];
97 mEventTime = event.time;
99 mGestureRecognizer->SendEvent(scene, event);
103 void GestureProcessor::GetGesturedActor(Actor*& actor, GestureDetectorContainer& gestureDetectors)
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))
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)
115 GestureDetector* current(*iter);
117 // Check deriving class for whether the current gesture satisfies the gesture detector's parameters.
118 if(CheckGestureDetector(current, actor))
120 gestureDetectors.push_back(current);
124 // The hit actor or one of the parents is a gestured actor, break out.
125 if(!gestureDetectors.empty())
131 // No match, we should now check the hit actor's parent.
132 actor = actor->GetParent();
136 void GestureProcessor::ProcessAndEmit(HitTestAlgorithm::Results& hitTestResults)
138 if(hitTestResults.actor)
140 Actor* hitTestActor(&GetImplementation(hitTestResults.actor));
141 Actor* actor(hitTestActor);
146 GestureDetectorContainer gestureDetectors;
147 GetGesturedActor(actor, gestureDetectors);
149 if(actor && !gestureDetectors.empty())
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.
154 if(actor == hitTestActor)
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())
161 break; // We have found AND emitted a signal on the gestured actor, break out.
163 actor->SetNeedGesturePropagation(false);
167 if(actor->IsHittable())
169 const Vector3 size(actor->GetCurrentSize());
171 if((size.x > 0.0f) && (size.y > 0.0f))
173 // Ensure tap is within the actor's area
174 if(rayTest.SphereTest(*actor, hitTestResults.rayOrigin, hitTestResults.rayDirection)) // Quick check
176 Vector2 hitPointLocal;
177 float distance(0.0f);
178 if(rayTest.ActorTest(*actor, hitTestResults.rayOrigin, hitTestResults.rayDirection, hitPointLocal, distance))
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())
185 break; // We have found AND emitted a signal on the gestured actor, break out.
187 actor->SetNeedGesturePropagation(false);
195 // Continue up hierarchy to see if any of the parents require this gesture.
198 actor = actor->GetParent();
204 bool GestureProcessor::HitTest(Scene& scene, Vector2 screenCoordinates, HitTestAlgorithm::Results& hitTestResults)
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;
213 void GestureProcessor::SetActor(Actor* actor)
215 if(actor && actor != mCurrentGesturedActor)
219 mCurrentGesturedActor = actor;
220 mCurrentGesturedActor->AddObserver(*this);
222 mGesturedActorDisconnected = false;
225 void GestureProcessor::ResetActor()
227 if(mCurrentGesturedActor)
229 mCurrentGesturedActor->RemoveObserver(*this);
230 mCurrentGesturedActor = nullptr;
231 mGesturedActorDisconnected = false;
235 Actor* GestureProcessor::GetCurrentGesturedActor()
237 return mGesturedActorDisconnected ? nullptr : mCurrentGesturedActor;
240 void GestureProcessor::SceneObjectRemoved(Object& object)
242 if(mCurrentGesturedActor == &object &&
243 !mGesturedActorDisconnected)
245 // Inform deriving classes.
246 OnGesturedActorStageDisconnection();
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;
253 void GestureProcessor::ObjectDestroyed(Object& object)
255 if(mCurrentGesturedActor == &object)
257 // Inform deriving classes.
258 OnGesturedActorStageDisconnection();
260 mCurrentGesturedActor = nullptr;
264 } // namespace Internal