|
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: AknStylusPopUpMenu physics handler |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <stdlib.h> |
|
20 #include <aknphysics.h> |
|
21 #include <aknphysicsobserveriface.h> |
|
22 #include <AknUtils.h> |
|
23 |
|
24 #include <touchfeedback.h> |
|
25 #include "aknstyluspopupmenuphysicshandler.h" |
|
26 #include "aknstyluspopupmenucontent.h" |
|
27 |
|
28 const TInt KNoItemSelected( -1 ); |
|
29 |
|
30 |
|
31 |
|
32 // ======== MEMBER FUNCTIONS ======== |
|
33 |
|
34 // --------------------------------------------------------------------------- |
|
35 // CAknStylusPopUpMenuPhysicsHandler::NewL |
|
36 // --------------------------------------------------------------------------- |
|
37 // |
|
38 CAknStylusPopUpMenuPhysicsHandler* CAknStylusPopUpMenuPhysicsHandler::NewL( |
|
39 CAknStylusPopUpMenuContent* aPopUpMenuContent ) |
|
40 { |
|
41 CAknStylusPopUpMenuPhysicsHandler* self = |
|
42 CAknStylusPopUpMenuPhysicsHandler::NewLC( aPopUpMenuContent ); |
|
43 CleanupStack::Pop( self ); |
|
44 return self; |
|
45 } |
|
46 |
|
47 |
|
48 // --------------------------------------------------------------------------- |
|
49 // CAknStylusPopUpMenuPhysicsHandler::NewLC |
|
50 // --------------------------------------------------------------------------- |
|
51 // |
|
52 CAknStylusPopUpMenuPhysicsHandler* CAknStylusPopUpMenuPhysicsHandler::NewLC( |
|
53 CAknStylusPopUpMenuContent* aPopUpMenuContent ) |
|
54 { |
|
55 CAknStylusPopUpMenuPhysicsHandler* self |
|
56 = new ( ELeave ) CAknStylusPopUpMenuPhysicsHandler( aPopUpMenuContent ); |
|
57 CleanupStack::PushL( self ); |
|
58 self->ConstructL(); |
|
59 return self; |
|
60 } |
|
61 |
|
62 |
|
63 // --------------------------------------------------------------------------- |
|
64 // CAknStylusPopUpMenuPhysicsHandler::~CAknStylusPopUpMenuPhysicsHandler |
|
65 // --------------------------------------------------------------------------- |
|
66 // |
|
67 CAknStylusPopUpMenuPhysicsHandler::~CAknStylusPopUpMenuPhysicsHandler() |
|
68 { |
|
69 delete iPhysics; |
|
70 } |
|
71 |
|
72 |
|
73 // --------------------------------------------------------------------------- |
|
74 // CAknStylusPopUpMenuPhysicsHandler::HandlePointerEventL |
|
75 // --------------------------------------------------------------------------- |
|
76 // |
|
77 void CAknStylusPopUpMenuPhysicsHandler::HandlePointerEventL( const TPointerEvent& aPointerEvent ) |
|
78 { |
|
79 |
|
80 // EButton1Down |
|
81 if ( aPointerEvent.iType == TPointerEvent::EButton1Down ) |
|
82 { |
|
83 |
|
84 TBool wasScrolling = ( iPhysics->OngoingPhysicsAction() != |
|
85 CAknPhysics::EAknPhysicsActionNone ); |
|
86 |
|
87 iPosition = aPointerEvent.iPosition; |
|
88 iStartPosition = aPointerEvent.iPosition; |
|
89 |
|
90 StopPhysics(); |
|
91 iStartTime.HomeTime(); |
|
92 iPanningActivated = EFalse; |
|
93 iPressedDown = ETrue; |
|
94 |
|
95 |
|
96 // if pointer event (EButton1Down) inside items rect, launch higlight timer |
|
97 // if content was not flicking, set also item to be selected |
|
98 |
|
99 TPoint adjustedPosition( aPointerEvent.iPosition + TPoint(0, Offset() ) ); |
|
100 TInt item = iPopUpMenuContent->ContainingItem( adjustedPosition ); |
|
101 if ( item != KNoItemSelected ) |
|
102 { |
|
103 if ( !wasScrolling ) |
|
104 { |
|
105 SetHighlight( item ); |
|
106 CCoeEnv::Static()->WsSession().Finish(); |
|
107 } |
|
108 |
|
109 MTouchFeedback* feedback = MTouchFeedback::Instance(); |
|
110 if ( feedback ) |
|
111 { |
|
112 feedback->InstantFeedback( NULL, |
|
113 ETouchFeedbackList, |
|
114 aPointerEvent ); |
|
115 } |
|
116 } |
|
117 } |
|
118 |
|
119 // EDrag |
|
120 |
|
121 if ( aPointerEvent.iType == TPointerEvent::EDrag ) |
|
122 { |
|
123 TInt distanceFromStart( iStartPosition.iY - aPointerEvent.iPosition.iY ); |
|
124 |
|
125 if ( !iPanningActivated && Abs( distanceFromStart ) > DragThreshold() ) |
|
126 { |
|
127 iPanningActivated = ETrue; |
|
128 iPosition = aPointerEvent.iPosition; |
|
129 |
|
130 // clear highlight |
|
131 iPopUpMenuContent->SetHighlight( KNoItemSelected ); |
|
132 } |
|
133 |
|
134 if ( iPanningActivated ) |
|
135 { |
|
136 TInt deltaY( iPosition.iY - aPointerEvent.iPosition.iY ); |
|
137 iPosition = aPointerEvent.iPosition; |
|
138 iPhysics->RegisterPanningPosition( TPoint( 0, deltaY ) ); |
|
139 } |
|
140 } |
|
141 |
|
142 // EButton1Up |
|
143 |
|
144 if ( aPointerEvent.iType == TPointerEvent::EButton1Up ) |
|
145 { |
|
146 iPressedDown = EFalse; |
|
147 TPoint adjustedPosition( aPointerEvent.iPosition + TPoint(0, Offset() ) ); |
|
148 TInt item = iPopUpMenuContent->ContainingItem( adjustedPosition ); |
|
149 if ( item != KNoItemSelected ) |
|
150 { |
|
151 MTouchFeedback* feedback = MTouchFeedback::Instance(); |
|
152 if ( feedback ) |
|
153 { |
|
154 feedback->InstantFeedback( NULL, |
|
155 ETouchFeedbackList, |
|
156 ETouchFeedbackVibra, |
|
157 aPointerEvent ); |
|
158 } |
|
159 } |
|
160 |
|
161 TPoint distance( 0, iStartPosition.iY - aPointerEvent.iPosition.iY ); |
|
162 if ( iPhysics->StartPhysics( distance, iStartTime ) ) |
|
163 { |
|
164 return; |
|
165 } |
|
166 |
|
167 // select item |
|
168 if ( !iPanningActivated && |
|
169 iPopUpMenuContent->CurrentItem() != KNoItemSelected ) |
|
170 { |
|
171 if ( iViewRect.Contains( aPointerEvent.iPosition ) ) |
|
172 { |
|
173 iPopUpMenuContent->SelectItem( |
|
174 iPopUpMenuContent->CurrentItem() ); |
|
175 } |
|
176 else |
|
177 { |
|
178 // Clear highlight |
|
179 SetHighlight( KNoItemSelected ); |
|
180 } |
|
181 } |
|
182 } |
|
183 } |
|
184 |
|
185 // --------------------------------------------------------------------------- |
|
186 // CAknStylusPopUpMenuPhysicsHandler::SetViewRect |
|
187 // --------------------------------------------------------------------------- |
|
188 // |
|
189 void CAknStylusPopUpMenuPhysicsHandler::SetViewRect( const TRect& aRect ) |
|
190 { |
|
191 iViewRect = aRect; |
|
192 } |
|
193 |
|
194 // --------------------------------------------------------------------------- |
|
195 // CAknStylusPopUpMenuPhysicsHandler::SetItemHeight |
|
196 // --------------------------------------------------------------------------- |
|
197 // |
|
198 void CAknStylusPopUpMenuPhysicsHandler::SetItemHeight( TInt aItemHeight ) |
|
199 { |
|
200 iItemHeight = aItemHeight; |
|
201 } |
|
202 |
|
203 // --------------------------------------------------------------------------- |
|
204 // CAknStylusPopUpMenuPhysicsHandler::SetWorldHeight |
|
205 // --------------------------------------------------------------------------- |
|
206 // |
|
207 void CAknStylusPopUpMenuPhysicsHandler::SetWorldHeight( TInt aWorldHeight ) |
|
208 { |
|
209 iWorldHeight = aWorldHeight; |
|
210 } |
|
211 |
|
212 // --------------------------------------------------------------------------- |
|
213 // CAknStylusPopUpMenuPhysicsHandler::Offset |
|
214 // --------------------------------------------------------------------------- |
|
215 // |
|
216 TInt CAknStylusPopUpMenuPhysicsHandler::Offset() const |
|
217 { |
|
218 if ( iItemHeight == 0 ) |
|
219 { |
|
220 return 0; |
|
221 } |
|
222 |
|
223 if ( iScrollIndex < 0 ) |
|
224 { |
|
225 return iScrollIndex; |
|
226 } |
|
227 |
|
228 if ( iScrollIndex >= iWorldHeight - iViewRect.Height() ) |
|
229 { |
|
230 return iScrollIndex - ( iWorldHeight - iViewRect.Height() ); |
|
231 } |
|
232 |
|
233 return iScrollIndex % iItemHeight; |
|
234 } |
|
235 |
|
236 |
|
237 // --------------------------------------------------------------------------- |
|
238 // CAknStylusPopUpMenuPhysicsHandler::InitPhysicsL |
|
239 // --------------------------------------------------------------------------- |
|
240 // |
|
241 void CAknStylusPopUpMenuPhysicsHandler::InitPhysicsL() |
|
242 { |
|
243 TSize worldSize( iViewRect.Width(), iWorldHeight ); |
|
244 TSize viewSize( iViewRect.Width(), iViewRect.Height() ); |
|
245 |
|
246 iPhysics->InitPhysicsL( worldSize, viewSize, EFalse ); |
|
247 iPhysics->UpdateViewWindowControl( iPopUpMenuContent ); |
|
248 } |
|
249 |
|
250 |
|
251 |
|
252 // --------------------------------------------------------------------------- |
|
253 // CAknStylusPopUpMenuPhysicsHandler::HandleScrollEvent |
|
254 // --------------------------------------------------------------------------- |
|
255 // |
|
256 void CAknStylusPopUpMenuPhysicsHandler::HandleScrollEvent( TInt aNewScrollIndex ) |
|
257 { |
|
258 iScrollIndex = aNewScrollIndex; |
|
259 ScrollView(); |
|
260 } |
|
261 |
|
262 |
|
263 // --------------------------------------------------------------------------- |
|
264 // CAknStylusPopUpMenuPhysicsHandler::Reset |
|
265 // --------------------------------------------------------------------------- |
|
266 // |
|
267 void CAknStylusPopUpMenuPhysicsHandler::Reset() |
|
268 { |
|
269 iPrevOffset = -1; |
|
270 iPrevTopmostItem = -1; |
|
271 iScrollIndex = 0; |
|
272 } |
|
273 |
|
274 |
|
275 // --------------------------------------------------------------------------- |
|
276 // CAknStylusPopUpMenuPhysicsHandler::ViewPositionChanged |
|
277 // --------------------------------------------------------------------------- |
|
278 // |
|
279 void CAknStylusPopUpMenuPhysicsHandler::ViewPositionChanged( |
|
280 const TPoint& aNewPosition, |
|
281 TBool aDrawNow, |
|
282 TUint /*aFlags*/ ) |
|
283 { |
|
284 if ( !iPopUpMenuContent->IsVisible() ) |
|
285 { |
|
286 return; |
|
287 } |
|
288 iScrollIndex = aNewPosition.iY - iViewRect.Height() / 2; |
|
289 |
|
290 ScrollView( aDrawNow ); |
|
291 } |
|
292 |
|
293 |
|
294 // --------------------------------------------------------------------------- |
|
295 // CAknStylusPopUpMenuPhysicsHandler::PhysicEmulationEnded |
|
296 // --------------------------------------------------------------------------- |
|
297 // |
|
298 void CAknStylusPopUpMenuPhysicsHandler::PhysicEmulationEnded() |
|
299 { |
|
300 } |
|
301 |
|
302 |
|
303 // --------------------------------------------------------------------------- |
|
304 // CAknStylusPopUpMenuPhysicsHandler::ViewPosition |
|
305 // --------------------------------------------------------------------------- |
|
306 // |
|
307 TPoint CAknStylusPopUpMenuPhysicsHandler::ViewPosition() const |
|
308 { |
|
309 return TPoint( iViewRect.Width() / 2, iScrollIndex + iViewRect.Height() / 2 ); |
|
310 } |
|
311 |
|
312 // --------------------------------------------------------------------------- |
|
313 // CAknStylusPopUpMenuPhysicsHandler::CAknStylusPopUpMenuPhysicsHandler |
|
314 // --------------------------------------------------------------------------- |
|
315 // |
|
316 CAknStylusPopUpMenuPhysicsHandler::CAknStylusPopUpMenuPhysicsHandler( |
|
317 CAknStylusPopUpMenuContent* aPopUpMenuContent ) |
|
318 : |
|
319 iPopUpMenuContent( aPopUpMenuContent ), |
|
320 iPhysics( NULL ), |
|
321 iPrevOffset( -1 ), |
|
322 iPrevTopmostItem( -1 ), |
|
323 iOffsetItemCount( 0 ) |
|
324 { |
|
325 } |
|
326 |
|
327 |
|
328 // --------------------------------------------------------------------------- |
|
329 // CAknStylusPopUpMenuPhysicsHandler::ConstructL |
|
330 // --------------------------------------------------------------------------- |
|
331 // |
|
332 void CAknStylusPopUpMenuPhysicsHandler::ConstructL() |
|
333 { |
|
334 iPhysics = CAknPhysics::NewL( *this, iPopUpMenuContent ); |
|
335 } |
|
336 |
|
337 |
|
338 // --------------------------------------------------------------------------- |
|
339 // CAknStylusPopUpMenuPhysicsHandler::DragThreshold |
|
340 // --------------------------------------------------------------------------- |
|
341 // |
|
342 TInt CAknStylusPopUpMenuPhysicsHandler::DragThreshold() |
|
343 { |
|
344 return iPhysics->DragThreshold(); |
|
345 } |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 // --------------------------------------------------------------------------- |
|
351 // CAknStylusPopUpMenuPhysicsHandler::SetHighlight |
|
352 // --------------------------------------------------------------------------- |
|
353 // |
|
354 void CAknStylusPopUpMenuPhysicsHandler::SetHighlight( TInt aItem ) |
|
355 { |
|
356 iPopUpMenuContent->SetHighlight( aItem ); |
|
357 } |
|
358 |
|
359 // --------------------------------------------------------------------------- |
|
360 // CAknStylusPopUpMenuPhysicsHandler::ScrollView |
|
361 // --------------------------------------------------------------------------- |
|
362 // |
|
363 void CAknStylusPopUpMenuPhysicsHandler::ScrollView( TBool aDrawNow ) |
|
364 { |
|
365 if ( iItemHeight != 0 ) |
|
366 { |
|
367 TInt scrollIndex( Min( iScrollIndex, iWorldHeight - iViewRect.Height() ) ); |
|
368 TInt topmostItem( scrollIndex / iItemHeight ); |
|
369 |
|
370 // Update view based on topmostItem and offset. |
|
371 // Offset is retrieved from Physics handler by PopupMenuContent |
|
372 // inside this function call. |
|
373 |
|
374 // Update only if topmost item or offset changed |
|
375 if ( aDrawNow && |
|
376 ( topmostItem != iPrevTopmostItem || Offset() != iPrevOffset ) ) |
|
377 { |
|
378 iPopUpMenuContent->UpdateView( topmostItem ); |
|
379 } |
|
380 |
|
381 iPrevOffset = Offset(); |
|
382 iPrevTopmostItem = topmostItem; |
|
383 |
|
384 //when appear or dispear a tiem,a feedback was given. |
|
385 if ( abs( iPrevOffset ) <= iViewRect.Height() |
|
386 && ( iPrevOffset/iItemHeight ) != iOffsetItemCount ) |
|
387 { |
|
388 if ( iPhysics ) |
|
389 { |
|
390 TTouchFeedbackType feedbackType = ETouchFeedbackVibra; |
|
391 switch( iPhysics->OngoingPhysicsAction() ) |
|
392 { |
|
393 case CAknPhysics::EAknPhysicsActionDragging: |
|
394 { |
|
395 feedbackType = static_cast<TTouchFeedbackType> |
|
396 ( ETouchFeedbackVibra | ETouchFeedbackAudio ); |
|
397 } |
|
398 case CAknPhysics::EAknPhysicsActionFlicking: |
|
399 case CAknPhysics::EAknPhysicsActionBouncing: |
|
400 { |
|
401 MTouchFeedback* feedback = MTouchFeedback::Instance(); |
|
402 if ( feedback ) |
|
403 { |
|
404 feedback->InstantFeedback( iPopUpMenuContent, |
|
405 ETouchFeedbackSensitiveList, |
|
406 feedbackType, |
|
407 TPointerEvent() ); |
|
408 } |
|
409 break; |
|
410 } |
|
411 default: |
|
412 break; |
|
413 } |
|
414 } |
|
415 } |
|
416 iOffsetItemCount = iPrevOffset/iItemHeight; |
|
417 } |
|
418 } |
|
419 |
|
420 // --------------------------------------------------------------------------- |
|
421 // CAknStylusPopUpMenuPhysicsHandler::StopPhysics |
|
422 // --------------------------------------------------------------------------- |
|
423 // |
|
424 void CAknStylusPopUpMenuPhysicsHandler::StopPhysics() |
|
425 { |
|
426 iPhysics->StopPhysics(); |
|
427 iPhysics->ResetFriction(); |
|
428 } |