|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================ 00003 * Name : tactileexampleappview.cpp 00004 * Part of : Tactile Example 00005 * Interface : N/A 00006 * Description : Implementation of view class 00007 * Version : 00008 * 00009 * Copyright (c) 2008 Nokia Corporation. 00010 * This material, including documentation and any related 00011 * computer programs, is protected by copyright controlled by 00012 * Nokia Corporation. 00013 * ============================================================================ 00014 */ 00015 00016 // INCLUDE FILES 00017 #include <aknbutton.h> 00018 #include <stringloader.h> 00019 #include <touchfeedback.h> 00020 #include <tactileexample.rsg> 00021 #include "tactileexampleappview.h" 00022 #include "tactileexamplesquare.h" 00023 #include "tactileexamplecircle.h" 00024 00025 00026 //ENUMERATION 00027 enum TPointerToComponentView 00028 { 00029 ESquare = 0, 00030 ECircle, 00031 EButton 00032 }; 00033 00034 00035 //CONSTANTS 00036 const TInt KNumberOfComponents = 3; 00037 const TInt KMovementGrid = 10; // in pixels 00038 const TInt KButtonHeight = 4; // divisions of screen height 00039 const TInt KObjSize = 4; // divisions of screen height or width 00040 00041 00042 // ============================ MEMBER FUNCTIONS ============================= 00043 00044 00045 // --------------------------------------------------------------------------- 00046 // Two-phased constructor. 00047 // --------------------------------------------------------------------------- 00048 // 00049 CTactileExampleAppView* CTactileExampleAppView::NewL( const TRect& aRect ) 00050 { 00051 CTactileExampleAppView* self = CTactileExampleAppView::NewLC( aRect ); 00052 CleanupStack::Pop( self ); 00053 return self; 00054 } 00055 00056 00057 // --------------------------------------------------------------------------- 00058 // Two-phased constructor. 00059 // --------------------------------------------------------------------------- 00060 // 00061 CTactileExampleAppView* CTactileExampleAppView::NewLC( const TRect& aRect ) 00062 { 00063 CTactileExampleAppView* self = new ( ELeave ) CTactileExampleAppView; 00064 CleanupStack::PushL( self ); 00065 self->ConstructL( aRect ); 00066 return self; 00067 } 00068 00069 00070 // --------------------------------------------------------------------------- 00071 // Frees reserved resources 00072 // --------------------------------------------------------------------------- 00073 // 00074 CTactileExampleAppView::~CTactileExampleAppView() 00075 { 00076 delete iButton; 00077 delete iSquare; 00078 delete iCircle; 00079 } 00080 00081 00082 // --------------------------------------------------------------------------- 00083 // From class CCoeControl. 00084 // return nbr of controls inside this container. 00085 // --------------------------------------------------------------------------- 00086 // 00087 TInt CTactileExampleAppView::CountComponentControls() const 00088 { 00089 return KNumberOfComponents; 00090 } 00091 00092 00093 // --------------------------------------------------------------------------- 00094 // From class CCoeControl. 00095 // return pointer of controls inside this container 00096 // --------------------------------------------------------------------------- 00097 // 00098 CCoeControl* CTactileExampleAppView::ComponentControl(TInt aIndex) const 00099 { 00100 switch( aIndex ) 00101 { 00102 case EButton: 00103 return iButton; 00104 case ESquare: 00105 return iSquare; 00106 case ECircle: 00107 return iCircle; 00108 default: 00109 return NULL; 00110 } 00111 } 00112 00113 00114 // --------------------------------------------------------------------------- 00115 // From class CCoeControl. 00116 // Draw this CTactileExampleAppView to the screen. 00117 // --------------------------------------------------------------------------- 00118 // 00119 void CTactileExampleAppView::Draw( const TRect& aRect ) const 00120 { 00121 // Get the standard graphics context 00122 CWindowGc& gc = SystemGc(); 00123 00124 gc.SetPenStyle( CGraphicsContext::ENullPen ); 00125 gc.SetBrushColor( KRgbWhite ); 00126 gc.SetBrushStyle( CGraphicsContext::ESolidBrush ); 00127 00128 // Clears the area 00129 gc.Clear( aRect ); 00130 } 00131 00132 00133 // --------------------------------------------------------------------------- 00134 // From class CCoeControl. 00135 // Called by framework when the view size is changed. 00136 // Set objects to default positions 00137 // --------------------------------------------------------------------------- 00138 // 00139 void CTactileExampleAppView::SizeChanged() 00140 { 00141 // Gets the control's extent 00142 TRect rect( Rect() ); 00143 00144 // Set button's extent 00145 TRect buttonRect = TRect( rect.iTl.iX, 00146 rect.iBr.iY - rect.Height() / KButtonHeight, 00147 rect.iBr.iX, rect.iBr.iY ); 00148 iButton->SetRect( buttonRect ); 00149 00150 // Set area where dragging is possible 00151 iDragArea.SetRect( rect.iTl.iX, rect.iTl.iY, rect.iBr.iX, buttonRect.iTl.iY ); 00152 00153 // Update object dimension 00154 ObjectDimension(); 00155 } 00156 00157 00158 // --------------------------------------------------------------------------- 00159 // From class CCoeControl. 00160 // Moves objects and plays feedback during dragging 00161 // --------------------------------------------------------------------------- 00162 // 00163 void CTactileExampleAppView::HandlePointerEventL( const TPointerEvent& aPointerEvent ) 00164 { 00165 TPoint point = aPointerEvent.iPosition; 00166 00167 if ( iDragArea.Contains( point )) 00168 { 00169 if ( aPointerEvent.iType == TPointerEvent::EButton1Down && 00170 iSquare->Rect().Center() != iCircle->Rect().Center() ) 00171 { 00172 if ( iCircle->HitRegionContains( point, *iCircle )) 00173 { 00174 // dragging of circle started 00175 iDragCircle = ETrue; 00176 iDraggingOffset = iCircle->Position() - point; 00177 } 00178 else if ( iSquare->HitRegionContains( point, *iSquare )) 00179 { 00180 // dragging of square started 00181 iDragSquare = ETrue; 00182 iDraggingOffset = iSquare->Position() - point; 00183 } 00184 else 00185 { 00186 // Cancel dragging just in case 00187 iDragCircle = EFalse; 00188 iDragSquare = EFalse; 00189 } 00190 } 00191 00192 if ( aPointerEvent.iType == TPointerEvent::EDrag ) 00193 { 00194 if( iDragCircle ) 00195 { 00196 Move( iCircle, point + iDraggingOffset ); 00197 } 00198 else if( iDragSquare ) 00199 { 00200 Move( iSquare, point + iDraggingOffset ); 00201 } 00202 00203 if( iSquare->Rect().Center() == iCircle->Rect().Center() ) 00204 { 00205 // Objects are in end positions -> cancel dragging 00206 iDragCircle = EFalse; 00207 iDragSquare = EFalse; 00208 00209 // and disable all feedbacks 00210 iSquare->EnableFeedback( EFalse ); 00211 iCircle->EnableFeedback( EFalse ); 00212 } 00213 } 00214 } 00215 00216 if ( aPointerEvent.iType == TPointerEvent::EButton1Up ) 00217 { 00218 // possible dragging is cancelled if button is released 00219 iDragCircle = EFalse; 00220 iDragSquare = EFalse; 00221 } 00222 00223 CCoeControl::HandlePointerEventL( aPointerEvent ); 00224 } 00225 00226 00227 // --------------------------------------------------------------------------- 00228 // From class CCoeControl. 00229 // Enables feedback when layout is changed 00230 // --------------------------------------------------------------------------- 00231 // 00232 void CTactileExampleAppView::HandleResourceChange( TInt aType ) 00233 { 00234 CCoeControl::HandleResourceChange( aType ); 00235 if ( aType == KEikDynamicLayoutVariantSwitch ) 00236 { 00237 // Set circle and square to default positions 00238 SetObjects(); 00239 00240 // enable feedback again since it might have been disabled 00241 // if square & circle are in end positions 00242 iCircle->EnableFeedback( ETrue ); 00243 iSquare->EnableFeedback( ETrue ); 00244 00245 // ohjects are made visible/hidden depending 00246 // is touch supported for new layout 00247 iSquare->MakeVisible( AknLayoutUtils::PenEnabled() ); 00248 iCircle->MakeVisible( AknLayoutUtils::PenEnabled() ); 00249 } 00250 } 00251 00252 00253 // --------------------------------------------------------------------------- 00254 // From class MCoeControlObserver 00255 // Checks if button is pressed and returns objects to default positions 00256 // --------------------------------------------------------------------------- 00257 // 00258 void CTactileExampleAppView::HandleControlEventL( 00259 CCoeControl* aControl, TCoeEvent aEventType ) 00260 { 00261 if ( aControl == iButton ) 00262 { 00263 if ( aEventType == EEventStateChanged ) 00264 { 00265 // Button press resets object positions 00266 SetObjects(); 00267 DrawDeferred(); 00268 00269 // enable feedback again since it might have been disabled 00270 // if square & circle are in end positions 00271 iCircle->EnableFeedback( ETrue ); 00272 iSquare->EnableFeedback( ETrue ); 00273 } 00274 } 00275 } 00276 00277 00278 // --------------------------------------------------------------------------- 00279 // C++ default constructor can NOT contain any code, that might leave. 00280 // --------------------------------------------------------------------------- 00281 // 00282 CTactileExampleAppView::CTactileExampleAppView() 00283 { 00284 // No implementation required 00285 } 00286 00287 00288 // --------------------------------------------------------------------------- 00289 // Symbian 2nd phase constructor can leave. 00290 // --------------------------------------------------------------------------- 00291 // 00292 void CTactileExampleAppView::ConstructL( const TRect& aRect ) 00293 { 00294 // Create a window for this application view 00295 CreateWindowL(); 00296 00297 // Load a string from the resource file to be used as button label 00298 HBufC* textResource = StringLoader::LoadLC( R_TACT_BUTTON_TEXT ); 00299 00300 // Create button 00301 iButton = CAknButton::NewL( NULL, NULL, NULL, NULL, 00302 *textResource, KNullDesC, KAknButtonSizeFitText, 0 ); 00303 User::LeaveIfNull( iButton ); 00304 iButton->SetContainerWindowL( *this ); 00305 iButton->MakeVisible( ETrue ); 00306 iButton->SetObserver( this ); 00307 00308 // Update object dimesion 00309 ObjectDimension(); 00310 00311 // Create square 00312 iSquare = CTactileExampleSquare::NewL( TRect( TSize( iObjectDimension, 00313 iObjectDimension )), this ); 00314 iSquare->SetContainerWindowL( *this ); 00315 iSquare->MakeVisible( AknLayoutUtils::PenEnabled() ); 00316 00317 // Create circle 00318 iCircle = CTactileExampleCircle::NewL( TRect( TSize( iObjectDimension, 00319 iObjectDimension )), this ); 00320 iCircle->SetContainerWindowL( *this ); 00321 iCircle->MakeVisible( AknLayoutUtils::PenEnabled() ); 00322 00323 // Pop HBuf from CleanUpStack and Destroy it. 00324 CleanupStack::PopAndDestroy( textResource ); 00325 00326 00327 // Set the windows size 00328 SetRect( aRect ); 00329 00330 // Set objects to default positions 00331 SetObjects(); 00332 00333 // Get a pointer to touch feedback instance 00334 iFeedback = MTouchFeedback::Instance(); 00335 User::LeaveIfNull( iFeedback ); 00336 00337 EnableDragEvents(); 00338 00339 // Activate the window, which makes it ready to be drawn 00340 ActivateL(); 00341 } 00342 00343 00344 // --------------------------------------------------------------------------- 00345 // Sets the circle and square objects to default position 00346 // --------------------------------------------------------------------------- 00347 // 00348 void CTactileExampleAppView::SetObjects() 00349 { 00350 TPoint squarePos = TPoint( iDragArea.iBr.iX - iObjectDimension, 00351 iDragArea.iBr.iY - iObjectDimension ); 00352 00353 // modify coordinates to match the grid 00354 squarePos.iX = squarePos.iX - squarePos.iX % KMovementGrid; 00355 squarePos.iY = squarePos.iY - squarePos.iY % KMovementGrid; 00356 00357 iSquare->SetExtent( squarePos, TSize( iObjectDimension, iObjectDimension )); 00358 iCircle->SetExtent( TPoint( iDragArea.iTl.iX, iDragArea.iTl.iY ), 00359 TSize( iObjectDimension, iObjectDimension )); 00360 } 00361 00362 00363 // --------------------------------------------------------------------------- 00364 // Adjusts aPoint to match the grid and moves the control if needed 00365 // --------------------------------------------------------------------------- 00366 // 00367 void CTactileExampleAppView::Move( CCoeControl* aControl, TPoint aPoint ) 00368 { 00369 TPoint newPosition; 00370 00371 // adjust aPoint to match the grid 00372 newPosition.iX = aPoint.iX - aPoint.iX % KMovementGrid; 00373 newPosition.iY = aPoint.iY - aPoint.iY % KMovementGrid; 00374 00375 if ( newPosition != aControl->Position() ) 00376 { 00377 // Feedback is given as early as possible 00378 iFeedback->InstantFeedback( aControl, ETouchFeedbackSensitive ); 00379 00380 // Define the changed area 00381 TRect drawRect = TRect( newPosition, aControl->Size() ); 00382 drawRect.BoundingRect( aControl->Rect() ); 00383 00384 // set control to new position 00385 aControl->SetPosition( newPosition ); 00386 00387 // draw only changed area 00388 DrawNow( drawRect ); 00389 } 00390 } 00391 00392 00393 // --------------------------------------------------------------------------- 00394 // Calculates objects dimesion related to screen size. 00395 // --------------------------------------------------------------------------- 00396 // 00397 void CTactileExampleAppView::ObjectDimension() 00398 { 00399 TPixelsAndRotation ScreenSize; 00400 iCoeEnv->ScreenDevice()->GetDefaultScreenSizeAndRotation( ScreenSize ); 00401 TInt side = ScreenSize.iPixelSize.iWidth < ScreenSize.iPixelSize.iHeight ? 00402 ScreenSize.iPixelSize.iWidth / KObjSize : ScreenSize.iPixelSize.iHeight / KObjSize; 00403 00404 // size is adjusted to match the grid 00405 iObjectDimension = ( side - side % ( KMovementGrid * 2 )); 00406 } 00407 // End of File
|
© Nokia 2009 |