Dali 3D User Interface Engine
gesture-manager.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 
18 // CLASS HEADER
19 #include "gesture-manager.h"
20 
21 // EXTERNAL INCLUDES
23 
24 // INTERNAL INCLUDES
25 #include <events/gesture-detector.h>
26 #include <events/long-press-gesture-detector.h>
27 #include <events/pan-gesture-detector.h>
28 #include <events/pinch-gesture-detector.h>
29 #include <events/tap-gesture-detector.h>
31 
32 namespace Dali
33 {
34 
35 namespace Internal
36 {
37 
38 namespace Adaptor
39 {
40 
41 namespace
42 {
43 
44 #if defined(DEBUG_ENABLED)
45 Integration::Log::Filter* gLogFilter = Integration::Log::Filter::New( Debug::NoLogging, false, "LOG_GESTURE_MANAGER" );
46 
50 const char * GetGestureTypeString( Gesture::Type type )
51 {
52  static const char * const pinch( "Pinch" );
53  static const char * const pan( "Pan" );
54  static const char * const tap( "tap" );
55  static const char * const longPress( "LongPress" );
56  static const char * const invalid( "Invalid" );
57 
58  const char * retVal( NULL );
59 
60  switch ( type )
61  {
62  case Gesture::LongPress:
63  {
64  retVal = longPress;
65  break;
66  }
67 
68  case Gesture::Pan:
69  {
70  retVal = pan;
71  break;
72  }
73 
74  case Gesture::Pinch:
75  {
76  retVal = pinch;
77  break;
78  }
79 
80  case Gesture::Tap:
81  {
82  retVal = tap;
83  break;
84  }
85 
86  default:
87  retVal = invalid;
88  break;
89  }
90 
91  return retVal;
92 };
93 #endif // DEBUG_ENABLED
94 
95 const float MINIMUM_DISTANCE_DELTA_DIVISOR = 85.0f;
96 } // unnamed namespace
97 
98 GestureManager::GestureManager(CoreEventInterface& coreEventInterface, Vector2 screenSize,CallbackManager* callbackManager, EnvironmentOptions& environmentOptions)
99 : mCoreEventInterface( coreEventInterface ),
100  mScreenSize( screenSize ),
101  mCallbackManager( callbackManager ),
102  mEnvironmentOptions( environmentOptions ),
103  mMinimumDistanceDelta(-1.0f),
104  mRunning( true ) // This allows gestures to be created before Adaptor::Start() is called e.g. by Indicator
105 {
106  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Creating GestureManager\n" );
107 }
108 
110 {
111  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Destroying GestureManager\n" );
112 }
113 
114 void GestureManager::SendEvent(const Integration::TouchEvent& event)
115 {
116  if (mRunning)
117  {
118  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SendEvent: START\n" );
119 
120  // gestures can be added / deleted during SendEvent so we make a copy of the container.
121  // the gestures are reference counted, so unused gesture detectors will be deleted when
122  // the local variable detectors goes out of scope.
124 
125  // Send the event to all gesture detectors.
126  for ( GestureDetectorContainer::iterator iter = detectors.begin(), endIter = detectors.end(); iter != endIter; ++iter )
127  {
128  (*iter)->SendEvent(event);
129  }
130  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SendEvent: END\n" );
131  }
132 }
133 
135 {
136  if (mRunning)
137  {
138  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Stop\n" );
139 
140  mGestureDetectors.clear();
141  mRunning = false;
142  }
143 }
144 
146 {
147  mMinimumDistanceDelta = distance;
148  for( GestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
149  {
150  if ( ( *iter )->GetType() == Gesture::Pinch )
151  {
152  DALI_LOG_INFO( gLogFilter, Debug::General, "Set Minimum Pinch Distance: %f\n", distance );
153  PinchGestureDetector* gestureDetector = static_cast<PinchGestureDetector*>(iter->Get());
154  gestureDetector->SetMinimumPinchDistance(distance);
155  break;
156  }
157  }
158 }
159 
160 void GestureManager::Register(const Integration::GestureRequest& request)
161 {
162  if (mRunning)
163  {
164  DALI_LOG_INFO( gLogFilter, Debug::General, "Creating %s Detector\n", GetGestureTypeString( request.type ) );
165 
166  switch (request.type)
167  {
168  case Gesture::LongPress:
169  {
170  GestureDetectorPtr gesture = new LongPressGestureDetector(mCoreEventInterface, mScreenSize, static_cast<const Integration::LongPressGestureRequest&>(request));
171  mGestureDetectors.push_back(gesture);
172  break;
173  }
174 
175  case Gesture::Pan:
176  {
177  GestureDetectorPtr gesture = new PanGestureDetector(mCoreEventInterface, mScreenSize, static_cast<const Integration::PanGestureRequest&>(request), mEnvironmentOptions);
178  mGestureDetectors.push_back(gesture);
179  break;
180  }
181 
182  case Gesture::Pinch:
183  {
186  mGestureDetectors.push_back(gesture);
187  break;
188  }
189 
190  case Gesture::Tap:
191  {
192  GestureDetectorPtr gesture = new TapGestureDetector(mCoreEventInterface, mScreenSize, static_cast<const Integration::TapGestureRequest&>(request));
193  mGestureDetectors.push_back(gesture);
194  break;
195  }
196 
197  default:
198  DALI_ASSERT_DEBUG(false);
199  break;
200  }
201  }
202 }
203 
204 void GestureManager::Unregister(const Integration::GestureRequest& request)
205 {
206  if ( mRunning )
207  {
208  DALI_LOG_INFO( gLogFilter, Debug::General, "Unregister: %s\n", GetGestureTypeString( request.type ) );
209 
210  DeleteGestureDetector( request.type );
211 
212  }
213 }
214 
215 void GestureManager::Update(const Integration::GestureRequest& request)
216 {
217  for( GestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter < endIter; ++iter )
218  {
219  if ( (*iter)->GetType() == request.type )
220  {
221  DALI_LOG_INFO( gLogFilter, Debug::General, "Update: %s\n", GetGestureTypeString( request.type ) );
222  (*iter)->Update( request );
223  break;
224  }
225  }
226 }
227 
229 {
230  for( GestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
231  {
232  if ( ( *iter )->GetType() == type )
233  {
234  DALI_LOG_INFO( gLogFilter, Debug::General, "DeleteGestureDetector: %s\n", GetGestureTypeString( type ) );
235  mGestureDetectors.erase( iter );
236  break;
237  }
238  }
239 }
240 
241 } // namespace Adaptor
242 
243 } // namespace Internal
244 
245 } // namespace Dali
Dali Docs Home
Read more about Dali