2 * Copyright (c) 2014 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 "gesture-manager.h"
22 #include <dali/integration-api/debug.h>
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>
30 #include <base/core-event-interface.h>
44 #if defined(DEBUG_ENABLED)
45 Integration::Log::Filter* gLogFilter = Integration::Log::Filter::New( Debug::NoLogging, false, "LOG_GESTURE_MANAGER" );
48 * Helper method to return the string representation of a gesture type.
50 const char * GetGestureTypeString( Gesture::Type type )
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" );
58 const char * retVal( NULL );
62 case Gesture::LongPress:
93 #endif // DEBUG_ENABLED
95 const float MINIMUM_DISTANCE_DELTA_DIVISOR = 85.0f;
96 } // unnamed namespace
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
106 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Creating GestureManager\n" );
109 GestureManager::~GestureManager()
111 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Destroying GestureManager\n" );
114 void GestureManager::SendEvent(const Integration::TouchEvent& event)
118 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SendEvent: START\n" );
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.
123 GestureDetectorContainer detectors( mGestureDetectors );
125 // Send the event to all gesture detectors.
126 for ( GestureDetectorContainer::iterator iter = detectors.begin(), endIter = detectors.end(); iter != endIter; ++iter )
128 (*iter)->SendEvent(event);
130 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SendEvent: END\n" );
134 void GestureManager::Stop()
138 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Stop\n" );
140 mGestureDetectors.clear();
145 void GestureManager::SetMinimumPinchDistance(float distance)
147 mMinimumDistanceDelta = distance;
148 for( GestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
150 if ( ( *iter )->GetType() == Gesture::Pinch )
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);
160 void GestureManager::Register(const Integration::GestureRequest& request)
164 DALI_LOG_INFO( gLogFilter, Debug::General, "Creating %s Detector\n", GetGestureTypeString( request.type ) );
166 switch (request.type)
168 case Gesture::LongPress:
170 GestureDetectorPtr gesture = new LongPressGestureDetector(mCoreEventInterface, mScreenSize, static_cast<const Integration::LongPressGestureRequest&>(request));
171 mGestureDetectors.push_back(gesture);
177 GestureDetectorPtr gesture = new PanGestureDetector(mCoreEventInterface, mScreenSize, static_cast<const Integration::PanGestureRequest&>(request), mEnvironmentOptions);
178 mGestureDetectors.push_back(gesture);
184 float minPinchDistance = mMinimumDistanceDelta >= 0.0f ? mMinimumDistanceDelta : (mScreenSize.height / MINIMUM_DISTANCE_DELTA_DIVISOR);
185 GestureDetectorPtr gesture = new PinchGestureDetector(mCoreEventInterface, mScreenSize, minPinchDistance);
186 mGestureDetectors.push_back(gesture);
192 GestureDetectorPtr gesture = new TapGestureDetector(mCoreEventInterface, mScreenSize, static_cast<const Integration::TapGestureRequest&>(request));
193 mGestureDetectors.push_back(gesture);
198 DALI_ASSERT_DEBUG(false);
204 void GestureManager::Unregister(const Integration::GestureRequest& request)
208 DALI_LOG_INFO( gLogFilter, Debug::General, "Unregister: %s\n", GetGestureTypeString( request.type ) );
210 DeleteGestureDetector( request.type );
215 void GestureManager::Update(const Integration::GestureRequest& request)
217 for( GestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter < endIter; ++iter )
219 if ( (*iter)->GetType() == request.type )
221 DALI_LOG_INFO( gLogFilter, Debug::General, "Update: %s\n", GetGestureTypeString( request.type ) );
222 (*iter)->Update( request );
228 void GestureManager::DeleteGestureDetector( Gesture::Type type )
230 for( GestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
232 if ( ( *iter )->GetType() == type )
234 DALI_LOG_INFO( gLogFilter, Debug::General, "DeleteGestureDetector: %s\n", GetGestureTypeString( type ) );
235 mGestureDetectors.erase( iter );
241 } // namespace Adaptor
243 } // namespace Internal