examples/Graphics/WS/VectorSprite/VectorSprite.cpp

00001 /*
00002 Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 #include <w32std.h>
00032 #include "Base.h"
00033 #include "VectorSprite.h"
00034 
00035 CSprite::CSprite(CWsClient *aClient) : iClient(aClient)
00036         {
00037         }
00038 
00039 CSprite::~CSprite()
00040         {
00041         for (TInt i=0; i<4; i++)
00042                 {
00043                 delete iSpriteMember[i].iBitmap;
00044                 delete iSpriteMember[i].iMaskBitmap;
00045                 }
00046         iSprite.Close();
00047         }
00048 
00049 void CSprite::ConstructL(CWindow* aWindow)
00050         {
00051         iSprite = RWsSprite(iClient->iWs);
00052         User::LeaveIfError(iSprite.Construct(aWindow->Window(), TPoint(100,100), 0));
00053         for (TInt spriteNum = 0; spriteNum < 4; spriteNum++)
00054                 {
00055                 CreateBitmapL(iSpriteMember[spriteNum].iBitmap, spriteNum, EFalse);
00056                 CreateBitmapL(iSpriteMember[spriteNum].iMaskBitmap, spriteNum, ETrue);
00057                 iSpriteMember[spriteNum].iInvertMask = EFalse;
00058                 iSpriteMember[spriteNum].iOffset = TPoint (0,0);
00059                 iSpriteMember[spriteNum].iInterval = TTimeIntervalMicroSeconds32 (500000);
00060                 iSpriteMember[spriteNum].iDrawMode = CGraphicsContext::EDrawModePEN;
00061                 User::LeaveIfError(iSprite.AppendMember(iSpriteMember[spriteNum]));
00062                 }
00063         User::LeaveIfError(iSprite.Activate());
00064         iSprite.UpdateMember(3);
00065         }
00066 
00067 void CSprite::CreateBitmapL(CFbsBitmap* &aBitmap, TInt aSpriteNum, TBool aDoMask)
00068         {
00069     // device and context for drawing to the off-screen bitmap
00070     CFbsBitmapDevice* bitmapDevice;
00071     CGraphicsContext* bitmapContext;
00072     
00073     // create bitmap
00074         // aBitmap is effectively member data of CSprite, and this function is only
00075         // called from CSprite's constructor. This means we can be sure aBitmap has 
00076         // not been new'ed before (if it had, we'd need to do a delete here and set
00077         // aBitmap to 0.
00078     aBitmap = new (ELeave) CFbsBitmap();
00079     aBitmap->Create(TSize(50,50),EGray4);
00080     // create a device and gc for it
00081     bitmapDevice = CFbsBitmapDevice::NewL(aBitmap);
00082     bitmapDevice->CreateContext(bitmapContext);
00083         // Set up pen color etc.
00084         bitmapContext->SetBrushColor(aDoMask? TRgb::Gray4(0) : TRgb::Gray4(2));
00085         bitmapContext->SetBrushStyle(CGraphicsContext::ESolidBrush);
00086         bitmapContext->SetPenStyle(CGraphicsContext::ENullPen);
00087         bitmapContext->DrawRect(TRect(TSize(50,50)));
00088         bitmapContext->SetPenStyle(CGraphicsContext::ESolidPen);
00089         bitmapContext->SetPenSize(TSize(4,4));
00090         bitmapContext->SetPenColor(aDoMask? TRgb::Gray4(3) : TRgb::Gray4(0));
00091     // draw to it
00092         switch (aSpriteNum)
00093                 {
00094         case 0:
00095             bitmapContext->DrawLine (TPoint(10,10), TPoint(40,40));
00096                 break;
00097         case 1:
00098                 bitmapContext->DrawLine (TPoint(25,10), TPoint(25,40));
00099                 break;
00100         case 2:
00101                 bitmapContext->DrawLine (TPoint(40,10), TPoint(10,40));
00102                 break;
00103         case 3:
00104                 bitmapContext->DrawLine (TPoint(10,25), TPoint(40,25));
00105                 break;
00106                 }
00107     // delete the context and device
00108     delete bitmapContext;
00109     delete bitmapDevice;
00110         }
00111 
00112 /****************************************************************************\
00113 |       Function:       Constructor/Destructor for CMainWindow
00114 |       Input:          aClient         Client application that owns the window
00115 \****************************************************************************/
00116 CMainWindow::CMainWindow (CWsClient* aClient)
00117 : CWindow (aClient)
00118         {
00119         }
00120 
00121 
00122 CMainWindow::~CMainWindow ()
00123         {
00124         iWindow.Close();
00125         }
00126 
00127 /****************************************************************************\
00128 |       Function:       CMainWindow::Draw
00129 |       Purpose:        Redraws the contents of CMainWindow within a given
00130 |                               rectangle.
00131 |       Input:          aRect   Rectangle that needs redrawing
00132 |       Output:         None
00133 \****************************************************************************/
00134 
00135 void CMainWindow::Draw(const TRect& aRect)
00136         {
00137         CWindowGc* gc=SystemGc(); // get a gc
00138         gc->SetClippingRect(aRect); // clip outside this rect
00139         gc->Clear(aRect); // clear
00140         gc->SetPenStyle(CGraphicsContext::ESolidPen);
00141         gc->SetPenColor(TRgb::Gray4(2));
00142         TSize size = Window().Size();
00143         TInt width = size.iWidth;
00144         TInt height = size.iHeight;
00145         TInt numHoriz=height/5;
00146         TInt numVert=width/10;
00147         for (TInt i=numHoriz; i>0; i--)
00148                 {
00149                 gc->DrawLine (TPoint(0,height/numHoriz*i), TPoint(width, height/numHoriz*i));
00150                 }
00151         for (TInt j=numVert; j>0; j--)
00152                 {
00153                 gc->DrawLine (TPoint(width/numVert*j, 0), TPoint(width/numVert*j, height));
00154                 }
00155         }
00156 
00157 
00158 /****************************************************************************\
00159 |       Function:       CMainWindow::HandlePointerEvent
00160 |       Purpose:        Handles pointer events for CMainWindow.
00161 |       Input:          aPointerEvent   The pointer event!
00162 |       Output:         None
00163 \****************************************************************************/
00164 
00165 void CMainWindow::HandlePointerEvent (TPointerEvent& aPointerEvent)
00166         {       
00167         switch (aPointerEvent.iType)
00168                 {
00169         case TPointerEvent::EButton1Down:
00170                 break;
00171         case TPointerEvent::EButton1Up:
00172                 break;
00173         case TPointerEvent::EButton3Down:
00174                 break;
00175         default:
00176                 break;
00177                 }
00178         }
00179 
00180 
00182 //                                       CExampleWsClient implementation
00184 
00185 CExampleWsClient* CExampleWsClient::NewL(const TRect& aRect)
00186         {
00187         // make new client
00188         CExampleWsClient* client=new (ELeave) CExampleWsClient(aRect); 
00189         CleanupStack::PushL(client); // push, just in case
00190         client->ConstructL(); // construct and run
00191         CleanupStack::Pop();
00192         return client;
00193         }
00194 
00195 /****************************************************************************\
00196 |       Function:       Constructor/Destructor for CExampleWsClient
00197 |                               Destructor deletes everything that was allocated by
00198 |                               ConstructMainWindowL()
00199 \****************************************************************************/
00200 
00201 CExampleWsClient::CExampleWsClient(const TRect& aRect)
00202 :iRect(aRect)
00203         {
00204         }
00205 
00206 CExampleWsClient::~CExampleWsClient ()
00207         {
00208         delete iMainWindow;
00209         delete iSprite;
00210         }
00211 
00212 
00213 /****************************************************************************\
00214 |       Function:       CExampleWsClient::ConstructMainWindowL()
00215 |                               Called by base class's ConstructL
00216 |       Purpose:        Allocates and creates all the windows owned by this client
00217 |                               (See list of windows in CExampleWsCLient declaration).
00218 \****************************************************************************/
00219 
00220 void CExampleWsClient::ConstructMainWindowL()
00221         {
00222         iMainWindow=new (ELeave) CMainWindow(this);
00223         iMainWindow->ConstructL(iRect, TRgb (255,255,255));
00224         iSprite=new (ELeave) CSprite (this);
00225         iSprite->ConstructL(iMainWindow);
00226         }
00227 
00228 
00229 /****************************************************************************\
00230 |       Function:       CExampleWsClient::RunL()
00231 |                               Called by active scheduler when an even occurs
00232 |       Purpose:        Processes events according to their type
00233 |                               For key events: calls HandleKeyEventL() (global to client)
00234 |                               For pointer event: calls HandlePointerEvent() for window
00235 |                                  event occurred in.
00236 \****************************************************************************/
00237 void CExampleWsClient::RunL()
00238         {
00239         // get the event
00240         iWs.GetEvent(iWsEvent);
00241         TInt eventType=iWsEvent.Type();
00242         // take action on it
00243         switch (eventType)
00244                 {
00245         // events global within window group
00246         case EEventNull:
00247                 break;
00248         case EEventKey:
00249                 {
00250                 TKeyEvent& keyEvent=*iWsEvent.Key(); // get key event
00251                 HandleKeyEventL (keyEvent);
00252                 break;
00253                 }
00254         case EEventModifiersChanged:
00255                 break;
00256         case EEventKeyUp:
00257         case EEventKeyDown:
00258         case EEventFocusLost:
00259         case EEventFocusGained:
00260         case EEventSwitchOn:
00261         case EEventPassword:
00262         case EEventWindowGroupsChanged:
00263         case EEventErrorMessage:
00264                 break;
00265         // events local to specific windows
00266         case EEventPointer:
00267                 {
00268                 CWindow* window=(CWindow*)(iWsEvent.Handle()); // get window
00269                 TPointerEvent& pointerEvent=*iWsEvent.Pointer();
00270                 window->HandlePointerEvent (pointerEvent);
00271                 break;
00272                 }
00273         case EEventPointerExit:
00274         case EEventPointerEnter:
00275                 break;
00276         case EEventPointerBufferReady:
00277                 {
00278                 break;
00279                 }
00280         case EEventDragDrop:
00281                 break;
00282         default:
00283                 break;
00284                 }
00285         IssueRequest(); // maintain outstanding request
00286         }
00287 
00288 /****************************************************************************\
00289 |       Function:       CExampleWsClient::HandleKeyEventL()
00290 |       Purpose:        Processes key events for CExampleWsClient
00291 \****************************************************************************/
00292 void CExampleWsClient::HandleKeyEventL (TKeyEvent& /*aKeyEvent*/)
00293         {
00294         }

Generated by  doxygen 1.6.2