examples/Graphics/WS/VectorSprite/VectorSprite.cpp

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 //
00015 
00016 #include <w32std.h>
00017 #include "Base.h"
00018 #include "VectorSprite.h"
00019 
00020 CSprite::CSprite(CWsClient *aClient) : iClient(aClient)
00021         {
00022         }
00023 
00024 CSprite::~CSprite()
00025         {
00026         for (TInt i=0; i<4; i++)
00027                 {
00028                 delete iSpriteMember[i].iBitmap;
00029                 delete iSpriteMember[i].iMaskBitmap;
00030                 }
00031         iSprite.Close();
00032         }
00033 
00034 void CSprite::ConstructL(CWindow* aWindow)
00035         {
00036         iSprite = RWsSprite(iClient->iWs);
00037         User::LeaveIfError(iSprite.Construct(aWindow->Window(), TPoint(100,100), 0));
00038         for (TInt spriteNum = 0; spriteNum < 4; spriteNum++)
00039                 {
00040                 CreateBitmapL(iSpriteMember[spriteNum].iBitmap, spriteNum, EFalse);
00041                 CreateBitmapL(iSpriteMember[spriteNum].iMaskBitmap, spriteNum, ETrue);
00042                 iSpriteMember[spriteNum].iInvertMask = EFalse;
00043                 iSpriteMember[spriteNum].iOffset = TPoint (0,0);
00044                 iSpriteMember[spriteNum].iInterval = TTimeIntervalMicroSeconds32 (500000);
00045                 iSpriteMember[spriteNum].iDrawMode = CGraphicsContext::EDrawModePEN;
00046                 User::LeaveIfError(iSprite.AppendMember(iSpriteMember[spriteNum]));
00047                 }
00048         User::LeaveIfError(iSprite.Activate());
00049         iSprite.UpdateMember(3);
00050         }
00051 
00052 void CSprite::CreateBitmapL(CFbsBitmap* &aBitmap, TInt aSpriteNum, TBool aDoMask)
00053         {
00054     // device and context for drawing to the off-screen bitmap
00055     CFbsBitmapDevice* bitmapDevice;
00056     CGraphicsContext* bitmapContext;
00057     
00058     // create bitmap
00059         // aBitmap is effectively member data of CSprite, and this function is only
00060         // called from CSprite's constructor. This means we can be sure aBitmap has 
00061         // not been new'ed before (if it had, we'd need to do a delete here and set
00062         // aBitmap to 0.
00063     aBitmap = new (ELeave) CFbsBitmap();
00064     aBitmap->Create(TSize(50,50),EGray4);
00065     // create a device and gc for it
00066     bitmapDevice = CFbsBitmapDevice::NewL(aBitmap);
00067     bitmapDevice->CreateContext(bitmapContext);
00068         // Set up pen color etc.
00069         bitmapContext->SetBrushColor(aDoMask? TRgb::Gray4(0) : TRgb::Gray4(2));
00070         bitmapContext->SetBrushStyle(CGraphicsContext::ESolidBrush);
00071         bitmapContext->SetPenStyle(CGraphicsContext::ENullPen);
00072         bitmapContext->DrawRect(TRect(TSize(50,50)));
00073         bitmapContext->SetPenStyle(CGraphicsContext::ESolidPen);
00074         bitmapContext->SetPenSize(TSize(4,4));
00075         bitmapContext->SetPenColor(aDoMask? TRgb::Gray4(3) : TRgb::Gray4(0));
00076     // draw to it
00077         switch (aSpriteNum)
00078                 {
00079         case 0:
00080             bitmapContext->DrawLine (TPoint(10,10), TPoint(40,40));
00081                 break;
00082         case 1:
00083                 bitmapContext->DrawLine (TPoint(25,10), TPoint(25,40));
00084                 break;
00085         case 2:
00086                 bitmapContext->DrawLine (TPoint(40,10), TPoint(10,40));
00087                 break;
00088         case 3:
00089                 bitmapContext->DrawLine (TPoint(10,25), TPoint(40,25));
00090                 break;
00091                 }
00092     // delete the context and device
00093     delete bitmapContext;
00094     delete bitmapDevice;
00095         }
00096 
00097 /****************************************************************************\
00098 |       Function:       Constructor/Destructor for CMainWindow
00099 |       Input:          aClient         Client application that owns the window
00100 \****************************************************************************/
00101 CMainWindow::CMainWindow (CWsClient* aClient)
00102 : CWindow (aClient)
00103         {
00104         }
00105 
00106 
00107 CMainWindow::~CMainWindow ()
00108         {
00109         iWindow.Close();
00110         }
00111 
00112 /****************************************************************************\
00113 |       Function:       CMainWindow::Draw
00114 |       Purpose:        Redraws the contents of CMainWindow within a given
00115 |                               rectangle.
00116 |       Input:          aRect   Rectangle that needs redrawing
00117 |       Output:         None
00118 \****************************************************************************/
00119 
00120 void CMainWindow::Draw(const TRect& aRect)
00121         {
00122         CWindowGc* gc=SystemGc(); // get a gc
00123         gc->SetClippingRect(aRect); // clip outside this rect
00124         gc->Clear(aRect); // clear
00125         gc->SetPenStyle(CGraphicsContext::ESolidPen);
00126         gc->SetPenColor(TRgb::Gray4(2));
00127         TSize size = Window().Size();
00128         TInt width = size.iWidth;
00129         TInt height = size.iHeight;
00130         TInt numHoriz=height/5;
00131         TInt numVert=width/10;
00132         for (TInt i=numHoriz; i>0; i--)
00133                 {
00134                 gc->DrawLine (TPoint(0,height/numHoriz*i), TPoint(width, height/numHoriz*i));
00135                 }
00136         for (TInt j=numVert; j>0; j--)
00137                 {
00138                 gc->DrawLine (TPoint(width/numVert*j, 0), TPoint(width/numVert*j, height));
00139                 }
00140         }
00141 
00142 
00143 /****************************************************************************\
00144 |       Function:       CMainWindow::HandlePointerEvent
00145 |       Purpose:        Handles pointer events for CMainWindow.
00146 |       Input:          aPointerEvent   The pointer event!
00147 |       Output:         None
00148 \****************************************************************************/
00149 
00150 void CMainWindow::HandlePointerEvent (TPointerEvent& aPointerEvent)
00151         {       
00152         switch (aPointerEvent.iType)
00153                 {
00154         case TPointerEvent::EButton1Down:
00155                 break;
00156         case TPointerEvent::EButton1Up:
00157                 break;
00158         case TPointerEvent::EButton3Down:
00159                 break;
00160         default:
00161                 break;
00162                 }
00163         }
00164 
00165 
00167 //                                       CExampleWsClient implementation
00169 
00170 CExampleWsClient* CExampleWsClient::NewL(const TRect& aRect)
00171         {
00172         // make new client
00173         CExampleWsClient* client=new (ELeave) CExampleWsClient(aRect); 
00174         CleanupStack::PushL(client); // push, just in case
00175         client->ConstructL(); // construct and run
00176         CleanupStack::Pop();
00177         return client;
00178         }
00179 
00180 /****************************************************************************\
00181 |       Function:       Constructor/Destructor for CExampleWsClient
00182 |                               Destructor deletes everything that was allocated by
00183 |                               ConstructMainWindowL()
00184 \****************************************************************************/
00185 
00186 CExampleWsClient::CExampleWsClient(const TRect& aRect)
00187 :iRect(aRect)
00188         {
00189         }
00190 
00191 CExampleWsClient::~CExampleWsClient ()
00192         {
00193         delete iMainWindow;
00194         delete iSprite;
00195         }
00196 
00197 
00198 /****************************************************************************\
00199 |       Function:       CExampleWsClient::ConstructMainWindowL()
00200 |                               Called by base class's ConstructL
00201 |       Purpose:        Allocates and creates all the windows owned by this client
00202 |                               (See list of windows in CExampleWsCLient declaration).
00203 \****************************************************************************/
00204 
00205 void CExampleWsClient::ConstructMainWindowL()
00206         {
00207         iMainWindow=new (ELeave) CMainWindow(this);
00208         iMainWindow->ConstructL(iRect, TRgb (255,255,255));
00209         iSprite=new (ELeave) CSprite (this);
00210         iSprite->ConstructL(iMainWindow);
00211         }
00212 
00213 
00214 /****************************************************************************\
00215 |       Function:       CExampleWsClient::RunL()
00216 |                               Called by active scheduler when an even occurs
00217 |       Purpose:        Processes events according to their type
00218 |                               For key events: calls HandleKeyEventL() (global to client)
00219 |                               For pointer event: calls HandlePointerEvent() for window
00220 |                                  event occurred in.
00221 \****************************************************************************/
00222 void CExampleWsClient::RunL()
00223         {
00224         // get the event
00225         iWs.GetEvent(iWsEvent);
00226         TInt eventType=iWsEvent.Type();
00227         // take action on it
00228         switch (eventType)
00229                 {
00230         // events global within window group
00231         case EEventNull:
00232                 break;
00233         case EEventKey:
00234                 {
00235                 TKeyEvent& keyEvent=*iWsEvent.Key(); // get key event
00236                 HandleKeyEventL (keyEvent);
00237                 break;
00238                 }
00239         case EEventModifiersChanged:
00240                 break;
00241         case EEventKeyUp:
00242         case EEventKeyDown:
00243         case EEventFocusLost:
00244         case EEventFocusGained:
00245         case EEventSwitchOn:
00246         case EEventPassword:
00247         case EEventWindowGroupsChanged:
00248         case EEventErrorMessage:
00249                 break;
00250         // events local to specific windows
00251         case EEventPointer:
00252                 {
00253                 CWindow* window=(CWindow*)(iWsEvent.Handle()); // get window
00254                 TPointerEvent& pointerEvent=*iWsEvent.Pointer();
00255                 window->HandlePointerEvent (pointerEvent);
00256                 break;
00257                 }
00258         case EEventPointerExit:
00259         case EEventPointerEnter:
00260                 break;
00261         case EEventPointerBufferReady:
00262                 {
00263                 break;
00264                 }
00265         case EEventDragDrop:
00266                 break;
00267         default:
00268                 break;
00269                 }
00270         IssueRequest(); // maintain outstanding request
00271         }
00272 
00273 /****************************************************************************\
00274 |       Function:       CExampleWsClient::HandleKeyEventL()
00275 |       Purpose:        Processes key events for CExampleWsClient
00276 \****************************************************************************/
00277 void CExampleWsClient::HandleKeyEventL (TKeyEvent& /*aKeyEvent*/)
00278         {
00279         }

Generated by  doxygen 1.6.2