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/render-tasks/render-task-impl.h>
40 * Functor to check whether an actor requires a particular gesture or not
42 struct GestureHitTestCheck : public HitTestAlgorithm::HitTestInterface
44 GestureHitTestCheck( GestureType::Value type )
49 bool IsActorHittable( Actor* actor ) override
51 return actor->IsGestureRequired( mType ) && // Does the Application or derived actor type require the gesture?
52 actor->IsHittable(); // Is actor sensitive, visible and on the scene?
55 bool DescendActorHierarchy( Actor* actor ) override
57 return actor->IsVisible() && // Actor is visible, if not visible then none of its children are visible.
58 actor->IsSensitive(); // Actor is sensitive, if insensitive none of its children should be hittable either.
61 bool DoesLayerConsumeHit( Layer* layer ) override
63 return layer->IsTouchConsumed();
66 GestureType::Value mType;
69 } // unnamed namespace
72 GestureProcessor::GestureProcessor( GestureType::Value type )
73 : mGestureRecognizer(),
74 mNeedsUpdate( false ),
76 mCurrentGesturedActor( nullptr ),
77 mGesturedActorDisconnected( false )
81 GestureProcessor::~GestureProcessor()
86 void GestureProcessor::ProcessTouch( Scene& scene, const Integration::TouchEvent& event )
88 if( mGestureRecognizer )
90 mGestureRecognizer->SendEvent(scene, event);
94 void GestureProcessor::GetGesturedActor( Actor*& actor, GestureDetectorContainer& gestureDetectors )
98 // We may be checking a parent so ensure the parent requires this gesture (and do not unintentionally create the gesture data for the parent)
99 if ( actor->IsGestureRequired( mType ) )
101 // Retrieve the actor's detectors and check if they satisfy current gesture
102 const GestureDetectorContainer& connectedDetectors( actor->GetGestureData().GetGestureDetectorContainer( mType ) );
103 const GestureDetectorContainer::const_iterator endIter( connectedDetectors.end() );
104 for ( GestureDetectorContainer::const_iterator iter = connectedDetectors.begin(); iter != endIter; ++iter )
106 GestureDetector* current(*iter);
108 // Check deriving class for whether the current gesture satisfies the gesture detector's parameters.
109 if ( CheckGestureDetector( current, actor ) )
111 gestureDetectors.push_back(current);
115 // The hit actor or one of the parents is a gestured actor, break out.
116 if ( !gestureDetectors.empty() )
122 // No match, we should now check the hit actor's parent.
123 actor = actor->GetParent();
127 void GestureProcessor::ProcessAndEmit( HitTestAlgorithm::Results& hitTestResults )
129 if ( hitTestResults.actor )
131 Actor* hitTestActor( &GetImplementation( hitTestResults.actor ) );
132 Actor* actor( hitTestActor );
136 GestureDetectorContainer gestureDetectors;
137 GetGesturedActor( actor, gestureDetectors );
139 if ( actor && !gestureDetectors.empty() )
141 // We have a match but check if the hit point is within the gestured actor's bounds.
142 // If it is not then continue up the actor hierarchy.
144 if ( actor == hitTestActor )
146 // Our gesture detector's attached actor WAS the hit actor so we can can emit the signal.
147 EmitGestureSignal( actor, gestureDetectors, hitTestResults.actorCoordinates );
148 break; // We have found AND emitted a signal on the gestured actor, break out.
152 if ( actor->IsHittable() )
154 const Vector3 size( actor->GetCurrentSize() );
156 if ( ( size.x > 0.0f ) && ( size.y > 0.0f ) )
158 // Ensure tap is within the actor's area
159 if ( actor->RaySphereTest( hitTestResults.rayOrigin, hitTestResults.rayDirection ) ) // Quick check
161 Vector2 hitPointLocal;
162 float distance( 0.0f );
163 if( actor->RayActorTest( hitTestResults.rayOrigin, hitTestResults.rayDirection, hitPointLocal, distance ) )
165 // One of the parents was the gestured actor so we can emit the signal for that actor.
166 EmitGestureSignal( actor, gestureDetectors, hitPointLocal );
167 break; // We have found AND emitted a signal on the gestured actor, break out.
175 // Continue up hierarchy to see if any of the parents require this gesture.
178 actor = actor->GetParent();
184 bool GestureProcessor::HitTest( Scene& scene, Vector2 screenCoordinates, HitTestAlgorithm::Results& hitTestResults )
186 GestureHitTestCheck hitCheck( mType );
187 HitTestAlgorithm::HitTest( scene.GetSize(), scene.GetRenderTaskList(), scene.GetLayerList(), screenCoordinates, hitTestResults, hitCheck );
188 return hitTestResults.renderTask && hitTestResults.actor;
191 void GestureProcessor::SetActor( Actor* actor )
193 if ( actor && actor != mCurrentGesturedActor )
197 mCurrentGesturedActor = actor;
198 mCurrentGesturedActor->AddObserver( *this );
200 mGesturedActorDisconnected = false;
203 void GestureProcessor::ResetActor()
205 if ( mCurrentGesturedActor )
207 mCurrentGesturedActor->RemoveObserver( *this );
208 mCurrentGesturedActor = nullptr;
209 mGesturedActorDisconnected = false;
213 Actor* GestureProcessor::GetCurrentGesturedActor()
215 return mGesturedActorDisconnected ? nullptr : mCurrentGesturedActor;
218 void GestureProcessor::SceneObjectRemoved(Object& object)
220 if ( mCurrentGesturedActor == &object &&
221 !mGesturedActorDisconnected )
223 // Inform deriving classes.
224 OnGesturedActorStageDisconnection();
226 // do not call object.RemoveObserver here, object is currently iterating through observers... you wouldnt want to upset object now would you?
227 mGesturedActorDisconnected = true;
231 void GestureProcessor::ObjectDestroyed(Object& object)
233 if ( mCurrentGesturedActor == &object )
235 // Inform deriving classes.
236 OnGesturedActorStageDisconnection();
238 mCurrentGesturedActor = nullptr;
242 } // namespace Internal