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 <boost/bind.hpp>
24 #include <dali/integration-api/debug.h>
27 #include <events/gesture-detector.h>
28 #include <events/long-press-gesture-detector.h>
29 #include <events/pan-gesture-detector.h>
30 #include <events/pinch-gesture-detector.h>
31 #include <events/tap-gesture-detector.h>
32 #include <base/core-event-interface.h>
46 #if defined(DEBUG_ENABLED)
47 Integration::Log::Filter* gLogFilter = Integration::Log::Filter::New( Debug::NoLogging, false, "LOG_GESTURE_MANAGER" );
50 * Helper method to return the string representation of a gesture type.
52 const char * GetGestureTypeString( Gesture::Type type )
54 static const char * const pinch( "Pinch" );
55 static const char * const pan( "Pan" );
56 static const char * const tap( "tap" );
57 static const char * const longPress( "LongPress" );
58 static const char * const invalid( "Invalid" );
60 const char * retVal( NULL );
64 case Gesture::LongPress:
95 #endif // DEBUG_ENABLED
97 const float MINIMUM_DISTANCE_DELTA_DIVISOR = 85.0f;
98 } // unnamed namespace
100 GestureManager::GestureManager(CoreEventInterface& coreEventInterface, Vector2 screenSize,CallbackManager* callbackManager, EnvironmentOptions& environmentOptions)
101 : mCoreEventInterface( coreEventInterface ),
102 mScreenSize( screenSize ),
103 mCallbackManager( callbackManager ),
104 mEnvironmentOptions( environmentOptions ),
105 mMinimumDistanceDelta(-1.0f),
106 mRunning( true ) // This allows gestures to be created before Adaptor::Start() is called e.g. by Indicator
108 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Creating GestureManager\n" );
111 GestureManager::~GestureManager()
113 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Destroying GestureManager\n" );
116 void GestureManager::SendEvent(const Integration::TouchEvent& event)
120 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SendEvent: START\n" );
122 // gestures can be added / deleted during SendEvent so we make a copy of the container.
123 // the gestures are reference counted, so unused gesture detectors will be deleted when
124 // the local variable detectors goes out of scope.
125 GestureDetectorContainer detectors( mGestureDetectors );
127 // Send the event to all gesture detectors.
128 for ( GestureDetectorContainer::iterator iter = detectors.begin(), endIter = detectors.end(); iter != endIter; ++iter )
130 (*iter)->SendEvent(event);
132 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "SendEvent: END\n" );
136 void GestureManager::Stop()
140 DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Stop\n" );
142 mGestureDetectors.clear();
147 void GestureManager::SetMinimumPinchDistance(float distance)
149 mMinimumDistanceDelta = distance;
150 for( GestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
152 if ( ( *iter )->GetType() == Gesture::Pinch )
154 DALI_LOG_INFO( gLogFilter, Debug::General, "Set Minimum Pinch Distance: %f\n", distance );
155 PinchGestureDetector* gestureDetector = static_cast<PinchGestureDetector*>(iter->Get());
156 gestureDetector->SetMinimumPinchDistance(distance);
162 void GestureManager::Register(const Integration::GestureRequest& request)
166 DALI_LOG_INFO( gLogFilter, Debug::General, "Creating %s Detector\n", GetGestureTypeString( request.type ) );
168 switch (request.type)
170 case Gesture::LongPress:
172 GestureDetectorPtr gesture = new LongPressGestureDetector(mCoreEventInterface, mScreenSize, static_cast<const Integration::LongPressGestureRequest&>(request));
173 mGestureDetectors.push_back(gesture);
179 GestureDetectorPtr gesture = new PanGestureDetector(mCoreEventInterface, mScreenSize, static_cast<const Integration::PanGestureRequest&>(request), mEnvironmentOptions);
180 mGestureDetectors.push_back(gesture);
186 float minPinchDistance = mMinimumDistanceDelta >= 0.0f ? mMinimumDistanceDelta : (mScreenSize.height / MINIMUM_DISTANCE_DELTA_DIVISOR);
187 GestureDetectorPtr gesture = new PinchGestureDetector(mCoreEventInterface, mScreenSize, minPinchDistance);
188 mGestureDetectors.push_back(gesture);
194 GestureDetectorPtr gesture = new TapGestureDetector(mCoreEventInterface, mScreenSize, static_cast<const Integration::TapGestureRequest&>(request));
195 mGestureDetectors.push_back(gesture);
200 DALI_ASSERT_DEBUG(false);
206 void GestureManager::Unregister(const Integration::GestureRequest& request)
210 DALI_LOG_INFO( gLogFilter, Debug::General, "Unregister: %s\n", GetGestureTypeString( request.type ) );
212 DeleteGestureDetector( request.type );
217 void GestureManager::Update(const Integration::GestureRequest& request)
219 for( GestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter < endIter; ++iter )
221 if ( (*iter)->GetType() == request.type )
223 DALI_LOG_INFO( gLogFilter, Debug::General, "Update: %s\n", GetGestureTypeString( request.type ) );
224 (*iter)->Update( request );
230 void GestureManager::DeleteGestureDetector( Gesture::Type type )
232 for( GestureDetectorContainer::iterator iter = mGestureDetectors.begin(), endIter = mGestureDetectors.end(); iter != endIter; ++iter )
234 if ( ( *iter )->GetType() == type )
236 DALI_LOG_INFO( gLogFilter, Debug::General, "DeleteGestureDetector: %s\n", GetGestureTypeString( type ) );
237 mGestureDetectors.erase( iter );
243 } // namespace Adaptor
245 } // namespace Internal