2 * Copyright (c) 2021 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 GestureType::Value mType;
67 } // unnamed namespace
69 GestureProcessor::GestureProcessor(GestureType::Value type)
70 : mGestureRecognizer(),
73 mCurrentGesturedActor(nullptr),
74 mGesturedActorDisconnected(false)
78 GestureProcessor::~GestureProcessor()
83 void GestureProcessor::ProcessTouch(Scene& scene, const Integration::TouchEvent& event)
85 if(mGestureRecognizer)
87 mGestureRecognizer->SendEvent(scene, event);
91 void GestureProcessor::GetGesturedActor(Actor*& actor, GestureDetectorContainer& gestureDetectors)
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))
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)
103 GestureDetector* current(*iter);
105 // Check deriving class for whether the current gesture satisfies the gesture detector's parameters.
106 if(CheckGestureDetector(current, actor))
108 gestureDetectors.push_back(current);
112 // The hit actor or one of the parents is a gestured actor, break out.
113 if(!gestureDetectors.empty())
119 // No match, we should now check the hit actor's parent.
120 actor = actor->GetParent();
124 void GestureProcessor::ProcessAndEmit(HitTestAlgorithm::Results& hitTestResults)
126 if(hitTestResults.actor)
128 Actor* hitTestActor(&GetImplementation(hitTestResults.actor));
129 Actor* actor(hitTestActor);
134 GestureDetectorContainer gestureDetectors;
135 GetGesturedActor(actor, gestureDetectors);
137 if(actor && !gestureDetectors.empty())
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.
142 if(actor == hitTestActor)
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 // If NeedGesturePropagation is true, it passes the gesture to the parent.
147 if(!actor->NeedGesturePropagation())
149 break; // We have found AND emitted a signal on the gestured actor, break out.
151 actor->SetNeedGesturePropagation(false);
155 if(actor->IsHittable())
157 const Vector3 size(actor->GetCurrentSize());
159 if((size.x > 0.0f) && (size.y > 0.0f))
161 // Ensure tap is within the actor's area
162 if(rayTest.SphereTest(*actor, hitTestResults.rayOrigin, hitTestResults.rayDirection)) // Quick check
164 Vector2 hitPointLocal;
165 float distance(0.0f);
166 if(rayTest.ActorTest(*actor, hitTestResults.rayOrigin, hitTestResults.rayDirection, hitPointLocal, distance))
168 // One of the parents was the gestured actor so we can emit the signal for that actor.
169 EmitGestureSignal(actor, gestureDetectors, hitPointLocal);
170 // If NeedGesturePropagation is true, it passes the gesture to the parent.
171 if(!actor->NeedGesturePropagation())
173 break; // We have found AND emitted a signal on the gestured actor, break out.
175 actor->SetNeedGesturePropagation(false);
183 // Continue up hierarchy to see if any of the parents require this gesture.
186 actor = actor->GetParent();
192 bool GestureProcessor::HitTest(Scene& scene, Vector2 screenCoordinates, HitTestAlgorithm::Results& hitTestResults)
194 GestureHitTestCheck hitCheck(mType);
195 HitTestAlgorithm::HitTest(scene.GetSize(), scene.GetRenderTaskList(), scene.GetLayerList(), screenCoordinates, hitTestResults, hitCheck);
196 return hitTestResults.renderTask && hitTestResults.actor;
199 void GestureProcessor::SetActor(Actor* actor)
201 if(actor && actor != mCurrentGesturedActor)
205 mCurrentGesturedActor = actor;
206 mCurrentGesturedActor->AddObserver(*this);
208 mGesturedActorDisconnected = false;
211 void GestureProcessor::ResetActor()
213 if(mCurrentGesturedActor)
215 mCurrentGesturedActor->RemoveObserver(*this);
216 mCurrentGesturedActor = nullptr;
217 mGesturedActorDisconnected = false;
221 Actor* GestureProcessor::GetCurrentGesturedActor()
223 return mGesturedActorDisconnected ? nullptr : mCurrentGesturedActor;
226 void GestureProcessor::SceneObjectRemoved(Object& object)
228 if(mCurrentGesturedActor == &object &&
229 !mGesturedActorDisconnected)
231 // Inform deriving classes.
232 OnGesturedActorStageDisconnection();
234 // do not call object.RemoveObserver here, object is currently iterating through observers... you wouldnt want to upset object now would you?
235 mGesturedActorDisconnected = true;
239 void GestureProcessor::ObjectDestroyed(Object& object)
241 if(mCurrentGesturedActor == &object)
243 // Inform deriving classes.
244 OnGesturedActorStageDisconnection();
246 mCurrentGesturedActor = nullptr;
250 } // namespace Internal