00001 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). 00002 // All rights reserved. 00003 // This component and the accompanying materials are made available 00004 // under the terms of "Eclipse Public License v1.0" 00005 // which accompanies this distribution, and is available 00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". 00007 // 00008 // Initial Contributors: 00009 // Nokia Corporation - initial contribution. 00010 // 00011 // Contributors: 00012 // 00013 // Description: 00014 // This file contains implementations of derived class 00015 // functions for client and windows. 00016 // PROJECT: BitmapSprite 00017 // CREATE A SPRITE USING BITMAPS LOADED FROM FILE 00018 // PROJECT OVERVIEW 00019 // Creates a sprite using bitmaps loaded from file. 00020 // A hatched background has been drawn on the window to illustrate 00021 // the fact that the sprite does not overlay a rectangular area, like 00022 // window, but only the area specified by the bitmap mask. 00023 // The animation of the sprite does not cause a redraw event for the 00024 // window beneath it. 00025 // The sprite can be moved around the screen using the arrow keys. 00026 // Functions introduced in this project include: 00027 // RWsSprite::Construct() 00028 // RWsSpriteBase::AppendMember() 00029 // RWsSpriteBase::Activate() 00030 // RWsSpriteBase::Close() 00031 // STRUCTURE 00032 // To create a sprite, an application must set up sprite members to 00033 // contain bitmaps, and add the sprite members to the sprite. The 00034 // bitmaps can either be read in from files, or created using 00035 // off-screen bitmaps which can be drawn to via a graphics context 00036 // in a similar way to a window. This example project uses the former 00037 // method. 00038 // 00039 00040 #include <eikenv.h> 00041 #include <w32std.h> 00042 #include "Base.h" 00043 #include "BitmapSprite.h" 00044 00045 #include <bitmapsprite.mbg> 00046 00047 CSprite::CSprite(CWsClient *aClient) : iClient(aClient), iSpritePos(80, 75) 00048 { 00049 } 00050 00051 CSprite::~CSprite() 00052 { 00053 for (TInt i=0; i<8; i++) 00054 { 00055 delete iSpriteMember[i].iBitmap; 00056 delete iSpriteMember[i].iMaskBitmap; 00057 } 00058 iSprite.Close(); 00059 } 00060 00061 void CSprite::UpdatePos(TPoint aAdjust) 00062 { 00063 iSpritePos += aAdjust; 00064 iSprite.SetPosition(iSpritePos); 00065 } 00066 00067 void CSprite::ConstructL(CWindow* aWindow) 00068 { 00069 CEikonEnv* eikenv=CEikonEnv::Static(); 00070 iSprite = RWsSprite(iClient->iWs); 00071 User::LeaveIfError(iSprite.Construct(aWindow->Window(), iSpritePos, 0)); 00072 // Initialize sprite members 00073 _LIT(KStar,"*"); // "*" means "the <app-name>.mbm file in the app directory" 00074 for (TInt i=0; i<8; i++) 00075 { 00076 iSpriteMember[i].iInvertMask = EFalse; 00077 iSpriteMember[i].iOffset = TPoint (0,0); 00078 iSpriteMember[i].iInterval = TTimeIntervalMicroSeconds32 (200000); 00079 iSpriteMember[i].iBitmap = eikenv->CreateBitmapL(KStar, i); 00080 if (i%2 == 0) 00081 { 00082 iSpriteMember[i].iMaskBitmap=eikenv->CreateBitmapL(KStar, EMbmBitmapspriteMil1mask); 00083 } 00084 else 00085 { 00086 iSpriteMember[i].iMaskBitmap=eikenv->CreateBitmapL(KStar, EMbmBitmapspriteMil2mask); 00087 } 00088 User::LeaveIfError(iSprite.AppendMember(iSpriteMember[i])); 00089 } 00090 00091 // Activate the sprite 00092 User::LeaveIfError(iSprite.Activate()); 00093 } 00094 00095 00097 // CMainWindow implementation 00099 00100 00101 /****************************************************************************\ 00102 | Function: Constructor/Destructor for CMainWindow 00103 | Input: aClient Client application that owns the window 00104 \****************************************************************************/ 00105 CMainWindow::CMainWindow (CWsClient* aClient) 00106 : CWindow (aClient) 00107 { 00108 } 00109 00110 00111 CMainWindow::~CMainWindow () 00112 { 00113 iWindow.Close(); 00114 } 00115 00116 /****************************************************************************\ 00117 | Function: CMainWindow::Draw 00118 | Purpose: Redraws the contents of CMainWindow within a given 00119 | rectangle. 00120 | Input: aRect Rectangle that needs redrawing 00121 | Output: None 00122 \****************************************************************************/ 00123 00124 void CMainWindow::Draw(const TRect& aRect) 00125 { 00126 // Draw a hatched pattern to illustrate that sprite is drawn only 00127 // where its bitmap mask is 0 00128 CWindowGc* gc=SystemGc(); // get a gc 00129 gc->SetClippingRect(aRect); // clip outside this rect 00130 gc->Clear(aRect); // clear 00131 gc->SetPenStyle(CGraphicsContext::ESolidPen); 00132 gc->SetPenColor(TRgb::Gray4(2)); 00133 TSize size = Window().Size(); 00134 TInt width = size.iWidth; 00135 TInt height = size.iHeight; 00136 TInt numHoriz=height/5; 00137 TInt numVert=width/10; 00138 for (TInt i=numHoriz; i>0; i--) 00139 { 00140 gc->DrawLine (TPoint(0,height/numHoriz*i), TPoint(width, height/numHoriz*i)); 00141 } 00142 for (TInt j=numVert; j>0; j--) 00143 { 00144 gc->DrawLine (TPoint(width/numVert*j, 0), TPoint(width/numVert*j, height)); 00145 } 00146 } 00147 00148 00149 /****************************************************************************\ 00150 | Function: CMainWindow::HandlePointerEvent 00151 | Purpose: Handles pointer events for CMainWindow. 00152 | Input: aPointerEvent The pointer event! 00153 | Output: None 00154 \****************************************************************************/ 00155 00156 void CMainWindow::HandlePointerEvent (TPointerEvent& /*aPointerEvent*/) 00157 { 00158 } 00159 00160 00162 // CExampleWsClient implementation 00164 00165 CExampleWsClient* CExampleWsClient::NewL(const TRect& aRect) 00166 { 00167 // make new client 00168 CExampleWsClient* client=new (ELeave) CExampleWsClient(aRect); 00169 CleanupStack::PushL(client); // push, just in case 00170 client->ConstructL(); // construct and run 00171 CleanupStack::Pop(); 00172 return client; 00173 } 00174 00175 /****************************************************************************\ 00176 | Function: Constructor/Destructor for CExampleWsClient 00177 | Destructor deletes everything that was allocated by 00178 | ConstructMainWindowL() 00179 \****************************************************************************/ 00180 00181 CExampleWsClient::CExampleWsClient(const TRect& aRect) 00182 :iRect(aRect) 00183 { 00184 } 00185 00186 CExampleWsClient::~CExampleWsClient () 00187 { 00188 delete iMainWindow; 00189 delete iSprite; 00190 } 00191 00192 00193 /****************************************************************************\ 00194 | Function: CExampleWsClient::ConstructMainWindowL() 00195 | Called by base class's ConstructL 00196 | Purpose: Allocates and creates all the windows owned by this client 00197 | (See list of windows in CExampleWsCLient declaration). 00198 \****************************************************************************/ 00199 00200 void CExampleWsClient::ConstructMainWindowL() 00201 { 00202 iMainWindow=new (ELeave) CMainWindow(this); 00203 iMainWindow->ConstructL(iRect, TRgb (255,255,255)); 00204 iSprite=new (ELeave) CSprite (this); 00205 iSprite->ConstructL(iMainWindow); 00206 } 00207 00208 00209 /****************************************************************************\ 00210 | Function: CExampleWsClient::RunL() 00211 | Called by active scheduler when an even occurs 00212 | Purpose: Processes events according to their type 00213 | For key events: calls HandleKeyEventL() (global to client) 00214 | For pointer event: calls HandlePointerEvent() for window 00215 | event occurred in. 00216 \****************************************************************************/ 00217 void CExampleWsClient::RunL() 00218 { 00219 // get the event 00220 iWs.GetEvent(iWsEvent); 00221 TInt eventType=iWsEvent.Type(); 00222 // take action on it 00223 switch (eventType) 00224 { 00225 // events global within window group 00226 case EEventNull: 00227 break; 00228 case EEventKey: 00229 { 00230 TKeyEvent& keyEvent=*iWsEvent.Key(); // get key event 00231 HandleKeyEventL (keyEvent); 00232 break; 00233 } 00234 case EEventModifiersChanged: 00235 break; 00236 case EEventKeyUp: 00237 case EEventKeyDown: 00238 case EEventFocusLost: 00239 case EEventFocusGained: 00240 case EEventSwitchOn: 00241 case EEventPassword: 00242 case EEventWindowGroupsChanged: 00243 case EEventErrorMessage: 00244 break; 00245 // events local to specific windows 00246 case EEventPointer: 00247 { 00248 CWindow* window=(CWindow*)(iWsEvent.Handle()); // get window 00249 TPointerEvent& pointerEvent=*iWsEvent.Pointer(); 00250 window->HandlePointerEvent (pointerEvent); 00251 break; 00252 } 00253 case EEventPointerExit: 00254 case EEventPointerEnter: 00255 break; 00256 case EEventPointerBufferReady: 00257 { 00258 break; 00259 } 00260 case EEventDragDrop: 00261 break; 00262 default: 00263 break; 00264 } 00265 IssueRequest(); // maintain outstanding request 00266 } 00267 00268 /****************************************************************************\ 00269 | Function: CExampleWsClient::HandleKeyEventL() 00270 | Purpose: Processes key events for CExampleWsClient 00271 | Gets the key code from the key event. Exits on 'Escape' 00272 \****************************************************************************/ 00273 void CExampleWsClient::HandleKeyEventL (TKeyEvent& aKeyEvent) 00274 { 00275 TUint code = aKeyEvent.iCode; 00276 switch (code) 00277 { 00278 case EKeyLeftArrow: 00279 iSprite->UpdatePos(TPoint(-2,0)); 00280 break; 00281 case EKeyRightArrow: 00282 iSprite->UpdatePos(TPoint(2,0)); 00283 break; 00284 case EKeyUpArrow: 00285 iSprite->UpdatePos(TPoint(0,-2)); 00286 break; 00287 case EKeyDownArrow: 00288 iSprite->UpdatePos(TPoint(0,2)); 00289 break; 00290 } 00291 } 00292
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.