|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Touch gesture framework utility functions. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <akntouchgesturefwevents.h> |
|
19 |
|
20 #include "akntouchgesturefwutils.h" |
|
21 #include "akntouchgesturefwdefs.h" |
|
22 |
|
23 using namespace AknTouchGestureFw; |
|
24 |
|
25 _LIT(KAknTouchPointerEventDown, "[GFW] IN P%d DOWN (%d, %d) %S"); |
|
26 _LIT(KAknTouchPointerEventDrag, "[GFW] IN P%d DRAG (%d, %d) %S"); |
|
27 _LIT(KAknTouchPointerEventUp, "[GFW] IN P%d UP (%d, %d) %S"); |
|
28 _LIT(KAknTouchPointerEventHw, "HW"); |
|
29 _LIT(KAknTouchPointerEventSimulated, "SM"); |
|
30 _LIT(KAknTouchGestureGeneric, "[GFW] OUT S:%d G:%x T:%x %%:%d"); |
|
31 _LIT(KAknTouchGesturePointer, |
|
32 "[GFW] ... %d: SP:(%d,%d) CP:(%d,%d) PP:(%d,%d) S:(%d,%d)"); |
|
33 |
|
34 // ======== MEMBER FUNCTIONS ======== |
|
35 |
|
36 // --------------------------------------------------------------------------- |
|
37 // TPointerEventData::TPointerEventData |
|
38 // --------------------------------------------------------------------------- |
|
39 // |
|
40 AknTouchGestureFw::TPointerEventData::TPointerEventData( |
|
41 TPointerEvent& aPointerEvent, |
|
42 TInt aPointerNumber, |
|
43 const TTime& aTimeStamp, |
|
44 TBool aSimulated, |
|
45 TBool aTargetedToControl ) |
|
46 : iPointerEvent( aPointerEvent ), |
|
47 iPointerNumber( aPointerNumber ), |
|
48 iTimeStamp( aTimeStamp ), |
|
49 iSimulated( aSimulated ), |
|
50 iTargetedToControl( aTargetedToControl ) |
|
51 { |
|
52 } |
|
53 |
|
54 |
|
55 // --------------------------------------------------------------------------- |
|
56 // TPointEntry::TPointEntry |
|
57 // --------------------------------------------------------------------------- |
|
58 // |
|
59 TPointEntry::TPointEntry( const TPoint& aPos, const TTime& aTime ) |
|
60 : iPos( aPos ), iTime( aTime ) |
|
61 { |
|
62 } |
|
63 |
|
64 |
|
65 // --------------------------------------------------------------------------- |
|
66 // TPointEntry::TPointEntry |
|
67 // --------------------------------------------------------------------------- |
|
68 // |
|
69 TPointEntry::TPointEntry() |
|
70 : iPos( TPoint() ), iTime( TTime() ) |
|
71 { |
|
72 } |
|
73 |
|
74 |
|
75 // --------------------------------------------------------------------------- |
|
76 // TVector::TVector |
|
77 // --------------------------------------------------------------------------- |
|
78 // |
|
79 TVector::TVector( const TPoint& aFrom, const TPoint& aTo ) |
|
80 : iX( aTo.iX - aFrom.iX ), |
|
81 iY( aTo.iY - aFrom.iY ) |
|
82 { |
|
83 } |
|
84 |
|
85 |
|
86 // --------------------------------------------------------------------------- |
|
87 // TVector::TVector |
|
88 // --------------------------------------------------------------------------- |
|
89 // |
|
90 TVector::TVector( TReal aDeltaX, TReal aDeltaY ) |
|
91 : iX( aDeltaX ), iY( aDeltaY ) |
|
92 { |
|
93 } |
|
94 |
|
95 |
|
96 // --------------------------------------------------------------------------- |
|
97 // TVector::TVector |
|
98 // --------------------------------------------------------------------------- |
|
99 // |
|
100 TVector::TVector() |
|
101 : iX( 0 ), iY( 0 ) |
|
102 { |
|
103 } |
|
104 |
|
105 |
|
106 // --------------------------------------------------------------------------- |
|
107 // TVector::Angle |
|
108 // --------------------------------------------------------------------------- |
|
109 // |
|
110 TInt TVector::Angle() const |
|
111 { |
|
112 TReal angle = 0; |
|
113 TReal length = Length(); |
|
114 if ( Abs( length ) > KEpsilon ) |
|
115 { |
|
116 Math::ACos( angle, iX / length ); |
|
117 if ( iY < 0 ) |
|
118 { |
|
119 angle = 2 * KPi - angle; |
|
120 } |
|
121 } |
|
122 |
|
123 return Degrees( angle ); |
|
124 } |
|
125 |
|
126 |
|
127 // --------------------------------------------------------------------------- |
|
128 // TVector::Length |
|
129 // --------------------------------------------------------------------------- |
|
130 // |
|
131 TReal TVector::Length() const |
|
132 { |
|
133 TReal length = 0; |
|
134 Math::Sqrt( length, iX * iX + iY * iY ); |
|
135 return length; |
|
136 } |
|
137 |
|
138 |
|
139 // --------------------------------------------------------------------------- |
|
140 // static TVector::Degrees |
|
141 // --------------------------------------------------------------------------- |
|
142 // |
|
143 TInt TVector::Degrees( TReal aRadians ) |
|
144 { |
|
145 return aRadians * 180 / KPi; |
|
146 } |
|
147 |
|
148 |
|
149 // --------------------------------------------------------------------------- |
|
150 // TThresholdArea::TThresholdArea |
|
151 // --------------------------------------------------------------------------- |
|
152 // |
|
153 TThresholdArea::TThresholdArea() |
|
154 : iInitialPosition(), iActive( EFalse ) |
|
155 { |
|
156 Reset(); |
|
157 } |
|
158 |
|
159 |
|
160 // --------------------------------------------------------------------------- |
|
161 // TThresholdArea::Reset |
|
162 // --------------------------------------------------------------------------- |
|
163 // |
|
164 void TThresholdArea::Reset() |
|
165 { |
|
166 iInitialPosition = TPoint(); |
|
167 iActive = EFalse; |
|
168 } |
|
169 |
|
170 |
|
171 // --------------------------------------------------------------------------- |
|
172 // TThresholdArea::IsActive |
|
173 // --------------------------------------------------------------------------- |
|
174 // |
|
175 TBool TThresholdArea::IsActive() const |
|
176 { |
|
177 return iActive; |
|
178 } |
|
179 |
|
180 |
|
181 // --------------------------------------------------------------------------- |
|
182 // TThresholdArea::InitialPosition |
|
183 // --------------------------------------------------------------------------- |
|
184 // |
|
185 TPoint TThresholdArea::InitialPosition() const |
|
186 { |
|
187 TPoint position; |
|
188 if ( iActive ) |
|
189 { |
|
190 position = iInitialPosition; |
|
191 } |
|
192 return position; |
|
193 } |
|
194 |
|
195 |
|
196 // --------------------------------------------------------------------------- |
|
197 // TThresholdArea::Start |
|
198 // --------------------------------------------------------------------------- |
|
199 // |
|
200 void TThresholdArea::Start( const TPoint& aInitialPosition ) |
|
201 { |
|
202 iInitialPosition = aInitialPosition; |
|
203 iActive = ETrue; |
|
204 } |
|
205 |
|
206 |
|
207 // --------------------------------------------------------------------------- |
|
208 // TThresholdArea::Check |
|
209 // --------------------------------------------------------------------------- |
|
210 // |
|
211 TBool TThresholdArea::Check( const TPoint& aNewPosition, TInt aMargin ) const |
|
212 { |
|
213 TBool crossed = EFalse; |
|
214 if ( iActive ) |
|
215 { |
|
216 TInt deltaX = aNewPosition.iX - iInitialPosition.iX; |
|
217 TInt deltaY = aNewPosition.iY - iInitialPosition.iY; |
|
218 deltaX = Abs( deltaX ); |
|
219 deltaY = Abs( deltaY ); |
|
220 crossed = deltaX > aMargin || deltaY > aMargin; |
|
221 } |
|
222 return crossed; |
|
223 } |
|
224 |
|
225 |
|
226 // --------------------------------------------------------------------------- |
|
227 // Dumps pointer event data to the debug output. |
|
228 // --------------------------------------------------------------------------- |
|
229 // |
|
230 void AknTouchGestureFwUtils::DumpPointerEvent( const TPointerEvent& aEvent, |
|
231 TBool aSimulated ) |
|
232 { |
|
233 TInt pointerNumber = 0; |
|
234 |
|
235 if ( aEvent.IsAdvancedPointerEvent() ) |
|
236 { |
|
237 pointerNumber = aEvent.AdvancedPointerEvent()->PointerNumber(); |
|
238 } |
|
239 |
|
240 const TDesC* source; |
|
241 if ( aSimulated ) |
|
242 { |
|
243 source = &KAknTouchPointerEventSimulated; |
|
244 } |
|
245 else |
|
246 { |
|
247 source = &KAknTouchPointerEventHw; |
|
248 } |
|
249 |
|
250 switch ( aEvent.iType ) |
|
251 { |
|
252 case TPointerEvent::EButton1Down: |
|
253 RDebug::Print(KAknTouchPointerEventDown, pointerNumber, |
|
254 aEvent.iPosition.iX, aEvent.iPosition.iY, source); |
|
255 break; |
|
256 |
|
257 case TPointerEvent::EDrag: |
|
258 RDebug::Print(KAknTouchPointerEventDrag, pointerNumber, |
|
259 aEvent.iPosition.iX, aEvent.iPosition.iY, source); |
|
260 break; |
|
261 |
|
262 case TPointerEvent::EButton1Up: |
|
263 RDebug::Print(KAknTouchPointerEventUp, pointerNumber, |
|
264 aEvent.iPosition.iX, aEvent.iPosition.iY, source); |
|
265 break; |
|
266 |
|
267 default: |
|
268 break; |
|
269 } |
|
270 } |
|
271 |
|
272 |
|
273 // --------------------------------------------------------------------------- |
|
274 // Dumps gesture event data to the debug output. |
|
275 // --------------------------------------------------------------------------- |
|
276 // |
|
277 void AknTouchGestureFwUtils::DumpGestureEvent( |
|
278 MAknTouchGestureFwEvent& aEvent ) |
|
279 { |
|
280 TInt movement( 0 ); |
|
281 TPoint startPos; |
|
282 TPoint currentPos; |
|
283 TPoint previousPos; |
|
284 TPoint speed; |
|
285 TAknTouchGestureFwState state( EAknTouchGestureFwOn ); |
|
286 |
|
287 switch ( aEvent.Group() ) |
|
288 { |
|
289 case EAknTouchGestureFwGroupTap: |
|
290 { |
|
291 MAknTouchGestureFwTapEvent* tap = |
|
292 static_cast<MAknTouchGestureFwTapEvent*>( &aEvent ); |
|
293 startPos = tap->Position(); |
|
294 } |
|
295 break; |
|
296 |
|
297 case EAknTouchGestureFwGroupFlick: |
|
298 { |
|
299 MAknTouchGestureFwFlickEvent* flick = |
|
300 static_cast<MAknTouchGestureFwFlickEvent*>( &aEvent ); |
|
301 startPos = flick->Position(); |
|
302 speed = flick->Speed(); |
|
303 } |
|
304 break; |
|
305 |
|
306 case EAknTouchGestureFwGroupDrag: |
|
307 { |
|
308 MAknTouchGestureFwDragEvent* drag = |
|
309 static_cast<MAknTouchGestureFwDragEvent*>( &aEvent ); |
|
310 startPos = drag->StartPosition(); |
|
311 currentPos = drag->CurrentPosition(); |
|
312 previousPos = drag->PreviousPosition(); |
|
313 state = drag->State(); |
|
314 } |
|
315 break; |
|
316 |
|
317 case EAknTouchGestureFwGroupPinch: |
|
318 { |
|
319 MAknTouchGestureFwPinchEvent* pinch = |
|
320 static_cast<MAknTouchGestureFwPinchEvent*>( &aEvent ); |
|
321 movement = pinch->Movement(); |
|
322 state = pinch->State(); |
|
323 } |
|
324 break; |
|
325 |
|
326 default: |
|
327 break; |
|
328 } |
|
329 |
|
330 RDebug::Print( KAknTouchGestureGeneric, |
|
331 state, aEvent.Group(), |
|
332 aEvent.Type(), movement ); |
|
333 RDebug::Print(KAknTouchGesturePointer, 0, |
|
334 startPos.iX, startPos.iY, currentPos.iX, currentPos.iY, |
|
335 previousPos.iX, previousPos.iY, speed.iX, speed.iY ); |
|
336 } |
|
337 |
|
338 |
|
339 // --------------------------------------------------------------------------- |
|
340 // TDirectionTracer::TDirectionTracer |
|
341 // --------------------------------------------------------------------------- |
|
342 // |
|
343 TDirectionTracer::TDirectionTracer() |
|
344 : iDirection( EDirectionNeutral ), iDirectionChanged( EFalse ), |
|
345 iChangeDirection( EDirectionNeutral ), iChangeCounter( 0 ) |
|
346 { |
|
347 } |
|
348 |
|
349 // --------------------------------------------------------------------------- |
|
350 // TDirectionTracer::Reset |
|
351 // --------------------------------------------------------------------------- |
|
352 // |
|
353 void TDirectionTracer::Reset() |
|
354 { |
|
355 iDirection = EDirectionNeutral; |
|
356 iDirectionChanged = EFalse; |
|
357 iChangeDirection = EDirectionNeutral; |
|
358 iChangeCounter = 0; |
|
359 } |
|
360 |
|
361 // --------------------------------------------------------------------------- |
|
362 // TDirectionTracer::Update |
|
363 // --------------------------------------------------------------------------- |
|
364 // |
|
365 void TDirectionTracer::Update( TInt aDelta, TInt aSensitivity ) |
|
366 { |
|
367 iDirectionChanged = EFalse; |
|
368 |
|
369 if ( !aDelta ) |
|
370 { |
|
371 return; // no change, cannot determine direction |
|
372 } |
|
373 |
|
374 if ( iDirection == EDirectionNeutral ) |
|
375 { |
|
376 TDirection direction = EDirectionNeutral; |
|
377 if ( aDelta > 0 ) |
|
378 { |
|
379 direction = EDirectionPositive; |
|
380 } |
|
381 else |
|
382 { |
|
383 direction = EDirectionNegative; |
|
384 } |
|
385 |
|
386 if ( direction == iChangeDirection ) |
|
387 { |
|
388 iChangeCounter++; |
|
389 |
|
390 if ( iChangeCounter >= aSensitivity ) |
|
391 { |
|
392 iDirection = iChangeDirection; |
|
393 iDirectionChanged = ETrue; |
|
394 iChangeCounter = 0; |
|
395 } |
|
396 } |
|
397 else |
|
398 { |
|
399 iChangeDirection = direction; |
|
400 iChangeCounter = 0; |
|
401 iChangeCounter++; |
|
402 } |
|
403 } |
|
404 else if ( iDirection == EDirectionPositive ) |
|
405 { |
|
406 // Subsequent direction changes require a couple of movement |
|
407 // to same direction |
|
408 |
|
409 if ( CheckDirectionChange( -aDelta, aSensitivity, iChangeCounter ) ) |
|
410 { |
|
411 iDirection = EDirectionNegative; |
|
412 iDirectionChanged = ETrue; |
|
413 } |
|
414 } |
|
415 else // iDirection == EDirectionNegative |
|
416 { |
|
417 // Subsequent direction changes require a couple of movement |
|
418 // to same direction |
|
419 |
|
420 if ( CheckDirectionChange( aDelta, aSensitivity, iChangeCounter ) ) |
|
421 { |
|
422 iDirection = EDirectionPositive; |
|
423 iDirectionChanged = ETrue; |
|
424 } |
|
425 } |
|
426 } |
|
427 |
|
428 |
|
429 // --------------------------------------------------------------------------- |
|
430 // TDirectionTracer::IsDirectionChanged |
|
431 // --------------------------------------------------------------------------- |
|
432 // |
|
433 TBool TDirectionTracer::IsDirectionChanged() const |
|
434 { |
|
435 return iDirectionChanged; |
|
436 } |
|
437 |
|
438 |
|
439 // --------------------------------------------------------------------------- |
|
440 // TDirectionTracer::Direction |
|
441 // --------------------------------------------------------------------------- |
|
442 // |
|
443 TDirectionTracer::TDirection TDirectionTracer::Direction() const |
|
444 { |
|
445 return iDirection; |
|
446 } |
|
447 |
|
448 |
|
449 // --------------------------------------------------------------------------- |
|
450 // TDirectionTracer::CheckDirectionChange |
|
451 // --------------------------------------------------------------------------- |
|
452 // |
|
453 TBool TDirectionTracer::CheckDirectionChange( |
|
454 TInt aDelta, TInt aSensitivity, TInt& aCounter) |
|
455 { |
|
456 TBool directionChanged = EFalse; |
|
457 |
|
458 if ( aDelta > 0 ) |
|
459 { |
|
460 aCounter++; |
|
461 } |
|
462 else |
|
463 { |
|
464 aCounter = 0; |
|
465 } |
|
466 |
|
467 if ( aCounter >= aSensitivity ) |
|
468 { |
|
469 aCounter = 0; |
|
470 directionChanged = ETrue; |
|
471 } |
|
472 |
|
473 return directionChanged; |
|
474 } |
|
475 |
|
476 // End of File |