2 * Copyright (c) 2020 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/hit-test-algorithm-impl.h>
27 #include <dali/internal/event/events/actor-gesture-data.h>
28 #include <dali/internal/event/events/ray-test.h>
29 #include <dali/internal/event/render-tasks/render-task-impl.h>
41 * Functor to check whether an actor requires a particular gesture or not
43 struct GestureHitTestCheck : public HitTestAlgorithm::HitTestInterface
45 GestureHitTestCheck( GestureType::Value type )
50 bool IsActorHittable( Actor* actor ) override
52 return actor->IsGestureRequired( mType ) && // Does the Application or derived actor type require the gesture?
53 actor->IsHittable(); // Is actor sensitive, visible and on the scene?
56 bool DescendActorHierarchy( Actor* actor ) override
58 return actor->IsVisible() && // Actor is visible, if not visible then none of its children are visible.
59 actor->IsSensitive(); // Actor is sensitive, if insensitive none of its children should be hittable either.
62 bool DoesLayerConsumeHit( Layer* layer ) override
64 return layer->IsTouchConsumed();
67 GestureType::Value mType;
70 } // unnamed namespace
73 GestureProcessor::GestureProcessor( GestureType::Value type )
74 : mGestureRecognizer(),
75 mNeedsUpdate( false ),
77 mCurrentGesturedActor( nullptr ),
78 mGesturedActorDisconnected( false )
82 GestureProcessor::~GestureProcessor()
87 void GestureProcessor::ProcessTouch( Scene& scene, const Integration::TouchEvent& event )
89 if( mGestureRecognizer )
91 mGestureRecognizer->SendEvent(scene, event);
95 void GestureProcessor::GetGesturedActor( Actor*& actor, GestureDetectorContainer& gestureDetectors )
99 // We may be checking a parent so ensure the parent requires this gesture (and do not unintentionally create the gesture data for the parent)
100 if ( actor->IsGestureRequired( mType ) )
102 // Retrieve the actor's detectors and check if they satisfy current gesture
103 const GestureDetectorContainer& connectedDetectors( actor->GetGestureData().GetGestureDetectorContainer( mType ) );
104 const GestureDetectorContainer::const_iterator endIter( connectedDetectors.end() );
105 for ( GestureDetectorContainer::const_iterator iter = connectedDetectors.begin(); iter != endIter; ++iter )
107 GestureDetector* current(*iter);
109 // Check deriving class for whether the current gesture satisfies the gesture detector's parameters.
110 if ( CheckGestureDetector( current, actor ) )
112 gestureDetectors.push_back(current);
116 // The hit actor or one of the parents is a gestured actor, break out.
117 if ( !gestureDetectors.empty() )
123 // No match, we should now check the hit actor's parent.
124 actor = actor->GetParent();
128 void GestureProcessor::ProcessAndEmit( HitTestAlgorithm::Results& hitTestResults )
130 if ( hitTestResults.actor )
132 Actor* hitTestActor( &GetImplementation( hitTestResults.actor ) );
133 Actor* actor( hitTestActor );
138 GestureDetectorContainer gestureDetectors;
139 GetGesturedActor( actor, gestureDetectors );
141 if ( actor && !gestureDetectors.empty() )
143 // We have a match but check if the hit point is within the gestured actor's bounds.
144 // If it is not then continue up the actor hierarchy.
146 if ( actor == hitTestActor )
148 // Our gesture detector's attached actor WAS the hit actor so we can can emit the signal.
149 EmitGestureSignal( actor, gestureDetectors, hitTestResults.actorCoordinates );
150 break; // We have found AND emitted a signal on the gestured actor, break out.
154 if ( actor->IsHittable() )
156 const Vector3 size( actor->GetCurrentSize() );
158 if ( ( size.x > 0.0f ) && ( size.y > 0.0f ) )
160 // Ensure tap is within the actor's area
161 if ( rayTest.SphereTest( *actor, hitTestResults.rayOrigin, hitTestResults.rayDirection ) ) // Quick check
163 Vector2 hitPointLocal;
164 float distance( 0.0f );
165 if( rayTest.ActorTest( *actor, hitTestResults.rayOrigin, hitTestResults.rayDirection, hitPointLocal, distance ) )
167 // One of the parents was the gestured actor so we can emit the signal for that actor.
168 EmitGestureSignal( actor, gestureDetectors, hitPointLocal );
169 break; // We have found AND emitted a signal on the gestured actor, break out.
177 // Continue up hierarchy to see if any of the parents require this gesture.
180 actor = actor->GetParent();
186 bool GestureProcessor::HitTest( Scene& scene, Vector2 screenCoordinates, HitTestAlgorithm::Results& hitTestResults )
188 GestureHitTestCheck hitCheck( mType );
189 HitTestAlgorithm::HitTest( scene.GetSize(), scene.GetRenderTaskList(), scene.GetLayerList(), screenCoordinates, hitTestResults, hitCheck );
190 return hitTestResults.renderTask && hitTestResults.actor;
193 void GestureProcessor::SetActor( Actor* actor )
195 if ( actor && actor != mCurrentGesturedActor )
199 mCurrentGesturedActor = actor;
200 mCurrentGesturedActor->AddObserver( *this );
202 mGesturedActorDisconnected = false;
205 void GestureProcessor::ResetActor()
207 if ( mCurrentGesturedActor )
209 mCurrentGesturedActor->RemoveObserver( *this );
210 mCurrentGesturedActor = nullptr;
211 mGesturedActorDisconnected = false;
215 Actor* GestureProcessor::GetCurrentGesturedActor()
217 return mGesturedActorDisconnected ? nullptr : mCurrentGesturedActor;
220 void GestureProcessor::SceneObjectRemoved(Object& object)
222 if ( mCurrentGesturedActor == &object &&
223 !mGesturedActorDisconnected )
225 // Inform deriving classes.
226 OnGesturedActorStageDisconnection();
228 // do not call object.RemoveObserver here, object is currently iterating through observers... you wouldnt want to upset object now would you?
229 mGesturedActorDisconnected = true;
233 void GestureProcessor::ObjectDestroyed(Object& object)
235 if ( mCurrentGesturedActor == &object )
237 // Inform deriving classes.
238 OnGesturedActorStageDisconnection();
240 mCurrentGesturedActor = nullptr;
244 } // namespace Internal