2 * Copyright (c) 2018 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-detector-impl.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/event/events/actor-gesture-data.h>
27 #include <dali/internal/event/events/gesture-event-processor.h>
28 #include <dali/internal/event/common/thread-local-storage.h>
29 #include <dali/internal/event/common/stage-impl.h>
37 GestureDetector::GestureDetector(Gesture::Type type, const SceneGraph::PropertyOwner* sceneObject )
38 : Object( sceneObject ),
40 mGestureEventProcessor( ThreadLocalStorage::Get().GetGestureEventProcessor() )
44 GestureDetector::~GestureDetector()
46 if ( !mAttachedActors.empty() )
48 for ( GestureDetectorActorContainer::iterator iter = mAttachedActors.begin(), endIter = mAttachedActors.end(); iter != endIter; ++iter )
50 Actor* actor( *iter );
51 actor->RemoveObserver( *this );
52 actor->GetGestureData().RemoveGestureDetector( *this );
55 mAttachedActors.clear();
57 // Guard to allow handle destruction after Core has been destroyed
58 if ( Stage::IsInstalled() )
60 mGestureEventProcessor.RemoveGestureDetector( this );
65 void GestureDetector::Attach(Actor& actor)
67 if ( !IsAttached(actor) )
69 // Register with EventProcessor if first actor being added
70 if ( mAttachedActors.empty() )
72 mGestureEventProcessor.AddGestureDetector(this);
75 mAttachedActors.push_back(&actor);
77 // We need to observe the actor's destruction
78 actor.AddObserver(*this);
80 // Add the detector to the actor (so the actor knows it requires this gesture when going through hit-test algorithm)
81 actor.GetGestureData().AddGestureDetector( *this );
83 // Notification for derived classes
88 void GestureDetector::Detach(Actor& actor)
90 if ( !mAttachedActors.empty() )
92 GestureDetectorActorContainer::iterator match = find(mAttachedActors.begin(), mAttachedActors.end(), &actor);
94 if ( match != mAttachedActors.end() )
96 // We no longer need to observe the actor's destruction
97 actor.RemoveObserver(*this);
99 // Remove detector from actor-gesture-data
100 actor.GetGestureData().RemoveGestureDetector( *this );
102 mAttachedActors.erase(match);
104 // Notification for derived classes
105 OnActorDetach(actor);
107 // Unregister from gesture event processor if we do not have any actors
108 if ( mAttachedActors.empty() )
110 // Guard to allow handle destruction after Core has been destroyed
111 if( Stage::IsInstalled() )
113 mGestureEventProcessor.RemoveGestureDetector(this);
120 void GestureDetector::DetachAll()
122 if ( !mAttachedActors.empty() )
124 GestureDetectorActorContainer attachedActors(mAttachedActors);
126 // Clear mAttachedActors before we call OnActorDetach in case derived classes call a method which manipulates mAttachedActors.
127 mAttachedActors.clear();
129 for ( GestureDetectorActorContainer::iterator iter = attachedActors.begin(), endIter = attachedActors.end(); iter != endIter; ++iter )
133 // We no longer need to observe the actor's destruction
134 actor->RemoveObserver(*this);
136 // Remove detector from actor-gesture-data
137 actor->GetGestureData().RemoveGestureDetector( *this );
139 // Notification for derived classes
140 OnActorDetach(*actor);
143 // Guard to allow handle destruction after Core has been destroyed
144 if ( Stage::IsInstalled() )
146 // Unregister from gesture event processor
147 mGestureEventProcessor.RemoveGestureDetector(this);
152 size_t GestureDetector::GetAttachedActorCount() const
154 return mAttachedActors.size();
157 Dali::Actor GestureDetector::GetAttachedActor(size_t index) const
161 if( index < mAttachedActors.size() )
163 actor = Dali::Actor( mAttachedActors[index] );
169 bool GestureDetector::IsAttached(Actor& actor) const
171 return find(mAttachedActors.begin(), mAttachedActors.end(), &actor) != mAttachedActors.end();
174 void GestureDetector::ObjectDestroyed(Object& object)
176 if ( !mAttachedActors.empty() )
178 GestureDetectorActorContainer::iterator match = find(mAttachedActors.begin(), mAttachedActors.end(), &object);
180 if ( match != mAttachedActors.end() )
182 mAttachedActors.erase(match);
184 // Notification for derived classes
185 OnActorDestroyed(object);
187 // Unregister from gesture event processor if we do not have any actors
188 if ( mAttachedActors.empty() )
190 // Guard to allow handle destruction after Core has been destroyed
191 if ( Stage::IsInstalled() )
193 mGestureEventProcessor.RemoveGestureDetector(this);
200 } // namespace Internal