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 "tap-gesture-detector.h"
24 #include <dali/public-api/events/touch-point.h>
25 #include <dali/public-api/math/vector2.h>
27 #include <dali/integration-api/events/gesture-requests.h>
28 #include <dali/integration-api/events/tap-gesture-event.h>
29 #include <dali/integration-api/events/touch-event-integ.h>
30 #include <base/core-event-interface.h>
45 // TODO: Set these according to DPI
46 const float MAXIMUM_MOTION_ALLOWED = 20.0f;
47 const unsigned long MAXIMUM_TIME_ALLOWED = 300u;
48 } // unnamed namespace
50 TapGestureDetector::TapGestureDetector(CoreEventInterface& coreEventInterface, Vector2 screenSize, const Integration::TapGestureRequest& request)
51 : GestureDetector(screenSize, Gesture::Tap),
52 mCoreEventInterface(coreEventInterface),
54 mMinimumTapsRequired(request.minTaps),
55 mMaximumTapsRequired(request.maxTaps),
60 mTimer = Dali::Timer::New(MAXIMUM_TIME_ALLOWED);
61 mTimer.TickSignal().Connect( mTimerSlot, &TapGestureDetector::TimerCallback );
64 TapGestureDetector::~TapGestureDetector()
68 void TapGestureDetector::SendEvent(const Integration::TouchEvent& event)
70 if (event.GetPointCount() == 1)
72 const TouchPoint& point = event.points[0];
73 TouchPoint::State pointState = point.state;
79 if (pointState == TouchPoint::Down)
81 mTouchPosition.x = point.screen.x;
82 mTouchPosition.y = point.screen.y;
83 mTouchTime = event.time;
86 EmitGesture( Gesture::Possible, mTouchTime );
93 Vector2 distanceDelta(abs(mTouchPosition.x - point.screen.x),
94 abs(mTouchPosition.y - point.screen.y));
96 unsigned long timeDelta = abs(event.time - mTouchTime);
98 if (distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
99 distanceDelta.y > MAXIMUM_MOTION_ALLOWED ||
100 timeDelta > MAXIMUM_TIME_ALLOWED)
102 // We may have already registered some taps so try emitting the gesture
103 EmitGesture( mTapsRegistered ? Gesture::Started : Gesture::Cancelled, event.time );
104 mState = (pointState == TouchPoint::Motion) ? Failed : Clear;
108 if (mState == Touched && pointState == TouchPoint::Up)
112 if (mTapsRegistered < mMaximumTapsRequired)
114 // Only emit gesture after timer expires if asked for multiple taps.
120 EmitGesture(Gesture::Started, event.time);
130 if (pointState == TouchPoint::Down)
134 Vector2 distanceDelta(abs(mTouchPosition.x - point.screen.x),
135 abs(mTouchPosition.y - point.screen.y));
137 // Check if subsequent tap is in a different position, if not then emit the previous tap
138 // count gesture (if required),
139 if (distanceDelta.x > MAXIMUM_MOTION_ALLOWED ||
140 distanceDelta.y > MAXIMUM_MOTION_ALLOWED)
142 EmitGesture(Gesture::Started, event.time);
143 mTouchPosition.x = point.screen.x;
144 mTouchPosition.y = point.screen.y;
147 mTouchTime = event.time;
156 if (pointState == TouchPoint::Up)
172 // We have entered a multi-touch event so emit registered gestures if required.
173 EmitGesture(Gesture::Started, event.time);
177 void TapGestureDetector::Update(const Integration::GestureRequest& request)
179 const Integration::TapGestureRequest& tap = static_cast<const Integration::TapGestureRequest&>(request);
181 mMinimumTapsRequired = tap.minTaps;
182 mMaximumTapsRequired = tap.maxTaps;
185 bool TapGestureDetector::TimerCallback()
187 EmitGesture( ( mTapsRegistered >= mMinimumTapsRequired ? Gesture::Started : Gesture::Cancelled ), mTouchTime + MAXIMUM_TIME_ALLOWED);
192 void TapGestureDetector::EmitGesture( Gesture::State state, unsigned int time )
194 if ( (state == Gesture::Possible) ||
195 (state == Gesture::Cancelled) ||
196 (mTapsRegistered >= mMinimumTapsRequired && mTapsRegistered <= mMaximumTapsRequired) )
198 Integration::TapGestureEvent event( state );
199 event.numberOfTaps = mTapsRegistered;
200 event.point = mTouchPosition;
203 mCoreEventInterface.QueueCoreEvent(event);
208 } // namespace Adaptor
210 } // namespace Internal