2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FUi_TouchPanningGestureDetector.cpp
19 * @brief This is the implementation file for %_TouchPanningGestureDetector class
22 * This file contains the implementation of %_TouchPanningGestureDetector class.
25 #include <FBaseUtilMath.h>
26 #include <FBaseColIEnumeratorT.h>
27 #include <FUiITouchPanningGestureEventListener.h>
28 #include "FUi_TouchPanningGestureDetector.h"
29 #include "FUi_ITouchPanningGestureEventListener.h"
30 #include "FUi_TouchManager.h"
31 #include "FUi_TouchPanningGestureDetectorImpl.h"
33 using namespace Tizen::Base::Utility;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Graphics;
40 const int DEFAULT_TOUCH_COUNT = 1;
41 const int MIN_MOVEALLOWANCE = 20;
42 const int POLLING_PANNING = 16;
45 namespace Tizen { namespace Ui
47 _TouchPanningGestureDetector::_TouchPanningGestureDetector(void)
48 : __touchCount(DEFAULT_TOUCH_COUNT)
50 , __maxPointId(DEFAULT_TOUCH_COUNT-1)
53 result r = GetLastResult();
54 SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, GetErrorMessage(r));
56 __pTimer = new (std::nothrow)Timer;
57 SysTryReturnVoidResult(NID_UI, __pTimer, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
59 r = __pTimer->Construct(*this);
60 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
62 SetDetectorType(_TOUCH_GESTURE_DETECTOR_TYPE_PANNING);
70 _TouchPanningGestureDetector::~_TouchPanningGestureDetector(void)
77 _TouchPanningGestureDetector::SetTouchCount(int count)
79 SysTryReturnResult(NID_UI, count > 0, E_INVALID_ARG, "[E_INVALID_ARG] Argument is less than 0");
82 __maxPointId = __touchCount-1;
84 if (__touchCount > DEFAULT_TOUCH_COUNT)
86 _Control* pControl = GetControl();
87 if (pControl && !pControl->IsMultiTouchEnabled())
89 pControl->SetMultiTouchEnabled(true);
97 _TouchPanningGestureDetector::GetTouchCount(void) const
103 _TouchPanningGestureDetector::GetTouchInfoListN(const _Control* pControl) const
105 if (GetDetectorState() != _TOUCH_GESTURE_DETECTOR_STATE_STARTED
106 && GetDetectorState() != _TOUCH_GESTURE_DETECTOR_STATE_CHANGED
107 && GetDetectorState() != _TOUCH_GESTURE_DETECTOR_STATE_FINISHED)
112 Point controlPos(0, 0);
113 if (pControl != null)
115 controlPos.x = pControl->GetAbsoluteBounds().x;
116 controlPos.y = pControl->GetAbsoluteBounds().y;
119 _TouchManager* pTouchManager = _TouchManager::GetInstance();
120 SysTryReturn(NID_UI, pTouchManager != null, null, E_SYSTEM, "[E_SYSTEM] _TouchManager does not exist.");
122 IListT<_FingerInfo*>* pFingerInfoList = pTouchManager->GetMultiFingerInfoListN();
123 SysTryReturn(NID_UI, pFingerInfoList, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
125 LinkedListT<_TouchInfo*>* pReturnList = new (std::nothrow) LinkedListT<_TouchInfo*>;
126 SysTryCatch(NID_UI, pReturnList, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
128 for (int i = 0; i < pFingerInfoList->GetCount(); i++)
130 _FingerInfo* pFingerInfo = null;
131 pFingerInfoList->GetAt(i, pFingerInfo);
133 if (pFingerInfo == null)
138 if (pFingerInfo->GetStatus() != _TOUCH_FOCUS_OUT)
140 _TouchInfo* pTouchInfo = new (std::nothrow) _TouchInfo;
141 SysTryCatch(NID_UI, pTouchInfo, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.\n");
142 Point point(pFingerInfo->GetScreenPoint().x - controlPos.x, pFingerInfo->GetScreenPoint().y - controlPos.y);
144 pTouchInfo->SetTouchInfo(pFingerInfo->GetPointId(), pFingerInfo->GetStatus(), point, false, 0);
145 pReturnList->Add(pTouchInfo);
147 if (pReturnList->GetCount() == __touchCount)
154 delete pFingerInfoList;
161 IEnumeratorT<_TouchInfo*>* pEnumerator = pReturnList->GetEnumeratorN();
164 while(pEnumerator->MoveNext() == E_SUCCESS)
166 _TouchInfo* pTouchInfo = null;
167 pEnumerator->GetCurrent(pTouchInfo);
168 if (pTouchInfo == null)
177 pReturnList->RemoveAll();
183 delete pFingerInfoList;
184 pFingerInfoList = null;
191 _TouchPanningGestureDetector::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
193 if (touchinfo.GetPointId() > __maxPointId)
198 if (__fingerCount < 0)
204 if ( __fingerCount < __touchCount)
206 SetDetectorState(_TOUCH_GESTURE_DETECTOR_STATE_READY);
208 else if (__touchCount == __fingerCount)
210 SetDetectorState(_TOUCH_GESTURE_DETECTOR_STATE_READY);
211 SetGestureStart(true);
212 SetDetectorState(_TOUCH_GESTURE_DETECTOR_STATE_STARTED);
213 __pTimer->Start(POLLING_PANNING);
220 _TouchPanningGestureDetector::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
222 if (touchinfo.GetPointId() > __maxPointId)
227 _TouchGestureDetectorState state = GetDetectorState();
228 if (state == _TOUCH_GESTURE_DETECTOR_STATE_STARTED || state == _TOUCH_GESTURE_DETECTOR_STATE_SUCCESS_AND_WAIT || state == _TOUCH_GESTURE_DETECTOR_STATE_CHANGED)
230 if (__touchCount > DEFAULT_TOUCH_COUNT)
232 _TouchManager* pTouchManager = _TouchManager::GetInstance();
233 SysTryReturn(NID_UI, pTouchManager != null, null, E_SYSTEM, "[E_SYSTEM] _TouchManager does not found.");
235 IListT<_FingerInfo*>* pFingerInfoList = pTouchManager->GetMultiFingerInfoListN();
236 bool movedAll = false;
239 NONE = 0x0, RIGHT = 0x1, UP = 0x2, LEFT = 0x4, DOWN = 0x8
242 int directionList[__touchCount];
246 for(int i=0; i < __touchCount; i++)
248 _FingerInfo* pFingerInfo = null;
249 pFingerInfoList->GetAt(i, pFingerInfo);
250 directionList[i] = NONE;
254 FloatPoint currentPoint = pFingerInfo->GetPoint();
255 FloatPoint controlPosition(0,0);
256 _Control* pControl = GetControl();
259 controlPosition.x = pControl->GetAbsoluteBoundsF().x;
260 controlPosition.y = pControl->GetAbsoluteBoundsF().y;
262 FloatPoint startPoint = pFingerInfo->GetStartPoint();
263 startPoint.x = startPoint.x - controlPosition.x;
264 startPoint.y = startPoint.y - controlPosition.y;
266 if (Math::Abs(startPoint.x - currentPoint.x) >= MIN_MOVEALLOWANCE || Math::Abs(startPoint.y - currentPoint.y) >= MIN_MOVEALLOWANCE)
270 if (currentPoint.x - startPoint.x > 0)
272 directionList[i] |= RIGHT;
274 else if (currentPoint.x - startPoint.x < 0)
276 directionList[i] |= LEFT;
279 if (currentPoint.y - startPoint.y > 0)
281 directionList[i] |= DOWN;
283 else if (currentPoint.y - startPoint.y < 0)
285 directionList[i] |= UP;
294 delete pFingerInfoList;
299 bool sameDirection = true;
300 for(int i=0; i<__touchCount-1; i++)
302 if (directionList[i] != directionList[i+1])
304 sameDirection = false;
311 SetDetectorState(_TOUCH_GESTURE_DETECTOR_STATE_CHANGED);
312 __pTimer->Start(POLLING_PANNING);
318 SetDetectorState(_TOUCH_GESTURE_DETECTOR_STATE_CHANGED);
319 __pTimer->Start(POLLING_PANNING);
326 _TouchPanningGestureDetector::OnTimerExpired(Timer& timer)
328 SetDetectorStateChanged();
330 __pTimer->Start(POLLING_PANNING);
334 _TouchPanningGestureDetector::SetDetectorStateChanged(void)
336 if ( GetDetectorState() == _TOUCH_GESTURE_DETECTOR_STATE_CHANGED )
338 SetDetectorState(_TOUCH_GESTURE_DETECTOR_STATE_CHANGED);
343 _TouchPanningGestureDetector::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
345 if (touchinfo.GetPointId() > __maxPointId)
351 _TouchGestureDetectorState state = GetDetectorState();
354 if (IsGestureStarted())
356 SetGestureStart(false);
357 if ( state == _TOUCH_GESTURE_DETECTOR_STATE_STARTED || state == _TOUCH_GESTURE_DETECTOR_STATE_CHANGED || state == _TOUCH_GESTURE_DETECTOR_STATE_SUCCESS_AND_WAIT)
359 SetDetectorState(_TOUCH_GESTURE_DETECTOR_STATE_FINISHED);
367 _TouchPanningGestureDetector::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
375 _TouchPanningGestureDetector::OnTouchGestureDetectorAdded(void)
377 __touchCount = DEFAULT_TOUCH_COUNT;
379 __maxPointId = DEFAULT_TOUCH_COUNT-1;
383 _TouchPanningGestureDetector::OnTouchGestureDetectorRemoved(void)