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 break; // We have found AND emitted a signal on the gestured actor, break out.
150 if(actor->IsHittable())
152 const Vector3 size(actor->GetCurrentSize());
154 if((size.x > 0.0f) && (size.y > 0.0f))
156 // Ensure tap is within the actor's area
157 if(rayTest.SphereTest(*actor, hitTestResults.rayOrigin, hitTestResults.rayDirection)) // Quick check
159 Vector2 hitPointLocal;
160 float distance(0.0f);
161 if(rayTest.ActorTest(*actor, hitTestResults.rayOrigin, hitTestResults.rayDirection, hitPointLocal, distance))
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.
173 // Continue up hierarchy to see if any of the parents require this gesture.
176 actor = actor->GetParent();
182 bool GestureProcessor::HitTest(Scene& scene, Vector2 screenCoordinates, HitTestAlgorithm::Results& hitTestResults)
184 GestureHitTestCheck hitCheck(mType);
185 HitTestAlgorithm::HitTest(scene.GetSize(), scene.GetRenderTaskList(), scene.GetLayerList(), screenCoordinates, hitTestResults, hitCheck);
186 return hitTestResults.renderTask && hitTestResults.actor;
189 void GestureProcessor::SetActor(Actor* actor)
191 if(actor && actor != mCurrentGesturedActor)
195 mCurrentGesturedActor = actor;
196 mCurrentGesturedActor->AddObserver(*this);
198 mGesturedActorDisconnected = false;
201 void GestureProcessor::ResetActor()
203 if(mCurrentGesturedActor)
205 mCurrentGesturedActor->RemoveObserver(*this);
206 mCurrentGesturedActor = nullptr;
207 mGesturedActorDisconnected = false;
211 Actor* GestureProcessor::GetCurrentGesturedActor()
213 return mGesturedActorDisconnected ? nullptr : mCurrentGesturedActor;
216 void GestureProcessor::SceneObjectRemoved(Object& object)
218 if(mCurrentGesturedActor == &object &&
219 !mGesturedActorDisconnected)
221 // Inform deriving classes.
222 OnGesturedActorStageDisconnection();
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;
229 void GestureProcessor::ObjectDestroyed(Object& object)
231 if(mCurrentGesturedActor == &object)
233 // Inform deriving classes.
234 OnGesturedActorStageDisconnection();
236 mCurrentGesturedActor = nullptr;
240 } // namespace Internal