exampleapps/alfexstickers/src/AlfExStickersControl.cpp
changeset 0 15bf7259bb7c
equal deleted inserted replaced
-1:000000000000 0:15bf7259bb7c
       
     1 /*
       
     2 * Copyright (c) 2008 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:   
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 /* Includes */
       
    21 #include <eikenv.h>
       
    22 #include <e32cmn.h>
       
    23 
       
    24 //Unsorted
       
    25 #include <alf/alfenv.h> 
       
    26 #include <alf/alftexture.h>
       
    27 #include <alf/alfdisplay.h>
       
    28 #include <alf/alfevent.h>
       
    29 #include <alf/alfimageloaderutil.h>
       
    30 #include <aknnotewrappers.h> 
       
    31 #include <aknutils.h>
       
    32 #include <StringLoader.h>
       
    33 
       
    34 //Layouts
       
    35 #include <alf/alfgridlayout.h>
       
    36 #include <alf/alfviewportlayout.h>
       
    37 
       
    38 //Visuals
       
    39 #include <alf/alftextvisual.h>
       
    40 #include <alf/alfimagevisual.h>
       
    41 #include <alf/alfvisual.h>
       
    42 
       
    43 //Brushes
       
    44 #include <alf/alfborderbrush.h>
       
    45 #include <alf/alfimagebrush.h>
       
    46 #include <alf/alfdropshadowbrush.h>
       
    47 #include <alf/alfbrush.h>
       
    48 #include <alf/alfbrusharray.h>
       
    49 	
       
    50 //Own
       
    51 #include "alfexstickerscontrol.h"
       
    52 #include "alfexstickerlayout.h"
       
    53 #include <alfexstickers.rsg>
       
    54 
       
    55 //TEMP
       
    56 #include <alf/alfroster.h>
       
    57 #include <alf/alftransformation.h>
       
    58 #include <alf/alfdecklayout.h>
       
    59 
       
    60 //Literals
       
    61 
       
    62 //Constants
       
    63 const TInt KStickerBitmapCount = 4;
       
    64 
       
    65 CAlfExStickersControl* CAlfExStickersControl::NewL(CAlfEnv& aEnv, RFs* aFs)
       
    66 	{
       
    67 	CAlfExStickersControl* self = new (ELeave) CAlfExStickersControl;
       
    68 	CleanupStack::PushL(self);
       
    69 	self->ConstructL(aEnv, aFs);
       
    70 	CleanupStack::Pop(self);
       
    71 	return self;
       
    72 	}
       
    73 
       
    74 
       
    75 CAlfExStickersControl* CAlfExStickersControl::NewLC(CAlfEnv& aEnv, RFs* aFs)
       
    76     {
       
    77     CAlfExStickersControl* self = new (ELeave) CAlfExStickersControl;
       
    78     CleanupStack::PushL(self);
       
    79     self->ConstructL(aEnv, aFs);
       
    80     return self;
       
    81     }
       
    82 
       
    83 
       
    84 CAlfExStickersControl::CAlfExStickersControl(): iDraggingVisual(EFalse)
       
    85     {
       
    86     }
       
    87 
       
    88 
       
    89 void CAlfExStickersControl::ConstructL(CAlfEnv& aEnv, RFs* aFs)
       
    90     {
       
    91     //Set UI Accelerator Toolkit environment object into iEnv class variable
       
    92     iEnv = &aEnv;
       
    93     //Set handle to file server session
       
    94     iFs=aFs;
       
    95         
       
    96     //Create new control for visuals
       
    97     CAlfControl::ConstructL(*iEnv);
       
    98     
       
    99     //Get the size of the main pane, save it to iLayoutsize
       
   100     AknLayoutUtils::LayoutMetricsSize(AknLayoutUtils::EMainPane, iLayoutsize);    
       
   101 
       
   102     //Assign size for the applicaton, Screen height by Screen width*2
       
   103     iVirtualSize.iWidth  = iLayoutsize.iWidth*2;
       
   104     iVirtualSize.iHeight = iLayoutsize.iHeight;
       
   105 
       
   106     //Create a viewPort layout to act as the root layout visual.
       
   107     CreateViewportL();
       
   108     
       
   109     //Initialize the texture manager
       
   110     SetTextureManagerL();
       
   111     
       
   112     //Set image for the application background
       
   113     LoadBackgroundImageL();
       
   114     
       
   115     //Create iPlainLayout and add it to the iViewPort
       
   116     iPlainLayout = CAlfLayout::AddNewL(*this,iViewPort);
       
   117  
       
   118     //Create the example stickers
       
   119     CreateStickersL();
       
   120     
       
   121 }
       
   122     
       
   123 
       
   124 CAlfExStickersControl::~CAlfExStickersControl()
       
   125     {
       
   126     for(TInt current=0;current < iImageLoaderUtilArray.Count();current++)
       
   127         {
       
   128         delete iImageLoaderUtilArray[current];
       
   129         }
       
   130     iImageLoaderUtilArray.Close();
       
   131     }
       
   132     
       
   133 
       
   134 TBool CAlfExStickersControl::OfferEventL(const TAlfEvent& aEvent)
       
   135     {
       
   136     //petjarve:This needs to be cleaned up
       
   137     TBool visual = EFalse;
       
   138     
       
   139     //Different user input cases
       
   140     //If the event was pointer down event
       
   141     if (aEvent.IsPointerEvent() && aEvent.PointerDown())
       
   142         {
       
   143         iHasBeenDragged = EFalse;
       
   144         CAlfVisual* onVisual = aEvent.Visual();
       
   145         
       
   146         //Find out if sticker (text visual) was interacted
       
   147         for (int j = 0; j < KStickerCount; ++j) 
       
   148         	{
       
   149         	if (aEvent.Visual() == iStickerArray[j]->GetVisual())
       
   150         		{
       
   151         		iSelectedDeck = iStickerArray[j];
       
   152         		visual = ETrue;
       
   153         		}
       
   154         	}
       
   155         
       
   156         //Interaction was on a visual
       
   157         if (visual)
       
   158             {
       
   159             iOnVisual = onVisual;
       
   160             iPlainLayout->MoveVisualToFront(*iSelectedDeck);
       
   161 
       
   162             iDraggingVisual = ETrue;
       
   163             }
       
   164         
       
   165         //Interaction was on background -> move it
       
   166         else
       
   167             {
       
   168             ;
       
   169             }   
       
   170             
       
   171         iStartDisplayPoint = aEvent.PointerEvent().iPosition;
       
   172         iStartVisualPos = onVisual->Pos().ValueNow();
       
   173             
       
   174         Display()->Roster().AddPointerEventObserver(EAlfPointerEventReportDrag, *this);
       
   175                 
       
   176         return ETrue;    
       
   177         }
       
   178     
       
   179     //If the event was a "drag" event
       
   180     else if(aEvent.IsPointerEvent() && aEvent.PointerEvent().iType == TPointerEvent::EDrag)
       
   181         {
       
   182         iHasBeenDragged = ETrue;
       
   183         //If we are dragging sticker
       
   184         if (iDraggingVisual && iSelectedDeck)
       
   185             {
       
   186             iDraggedPosition = 
       
   187                 aEvent.PointerEvent().iPosition
       
   188                 - iStartDisplayPoint+iViewPos;
       
   189             
       
   190             iSelectedDeck->SetPos(iDraggedPosition, 50);
       
   191             }
       
   192 
       
   193         //Else we are dragging the background
       
   194         else
       
   195             {
       
   196             TAlfRealPoint temp = (iStartDisplayPoint-(aEvent.PointerEvent().iPosition));
       
   197 
       
   198             if(temp.iX <0) temp.iX=0;
       
   199             if(temp.iY <0) temp.iY=0;
       
   200             
       
   201             if (temp.iY  > iVirtualSize.iHeight - iLayoutsize.iHeight)
       
   202 	            {
       
   203 	            temp.iY =iVirtualSize.iHeight- iLayoutsize.iHeight ;
       
   204 	            }
       
   205 	            
       
   206 	        if (temp.iX  > iVirtualSize.iWidth - iLayoutsize.iWidth)
       
   207 	            {
       
   208 	            temp.iX=iVirtualSize.iWidth- iLayoutsize.iWidth ;
       
   209 	            }    
       
   210             
       
   211             iViewPos=temp;
       
   212             iViewPort->SetViewportPos(iViewPos, 0);   
       
   213             }    
       
   214         
       
   215         return ETrue;
       
   216         }
       
   217 
       
   218     //If the event was simple tap event, ie. not a drag
       
   219     else if (aEvent.PointerUp() && iHasBeenDragged == EFalse)
       
   220     	{
       
   221     	//petjarve: Possibly unneeded check
       
   222     	if (iStartDisplayPoint == aEvent.PointerEvent().iPosition && iSelectedDeck)
       
   223     		{
       
   224     		iSelectedDeck->ChangeStickerSizeL();
       
   225     		
       
   226 	    	return ETrue;
       
   227     		}
       
   228     	return ETrue;
       
   229     	}
       
   230     
       
   231     else if( aEvent.PointerUp() )
       
   232         {
       
   233         Display()->Roster().RemovePointerEventObserver(EAlfPointerEventReportDrag, *this);
       
   234         iDraggingVisual = EFalse;
       
   235         return ETrue;
       
   236         }
       
   237         
       
   238     return EFalse;
       
   239     }
       
   240 
       
   241 
       
   242 void CAlfExStickersControl::CreateViewportL()
       
   243 	{
       
   244 	//Create a viewPort layout to act as the root layout visual.
       
   245     iViewPort = CAlfViewportLayout::AddNewL(*this); 
       
   246 
       
   247     //Set inital viewport position value to top left corner
       
   248     iViewPos = TAlfRealPoint(0, 0);
       
   249     
       
   250     //Set Viewport sizes, both virtual and non virtual and position (top left corner)
       
   251     iViewPort->SetVirtualSize(TAlfRealSize(iVirtualSize), 0);
       
   252     iViewPort->SetViewportSize(TAlfRealSize(iLayoutsize), 0);
       
   253     iViewPort->SetViewportPos(iViewPos, 0);
       
   254     
       
   255     //Enable scrolling of the Viewport
       
   256     iViewPort->EnableScrollingL();
       
   257 
       
   258 	}
       
   259 
       
   260 
       
   261 void CAlfExStickersControl::SetTextureManagerL()
       
   262 	{
       
   263 	//First create path to our textures
       
   264     User::LeaveIfError( iFs->CreatePrivatePath(EDriveC));
       
   265 	TChar driveChar;
       
   266     iFs->DriveToChar( EDriveC,driveChar);
       
   267     TDriveName driveName;
       
   268     driveName.Append(driveChar);
       
   269     driveName.Append(KDriveDelimiter);
       
   270        
       
   271     iFs->PrivatePath(iPrivateFolderPath);
       
   272     iPrivateFolderPath.Insert(0,driveName);
       
   273 
       
   274     //Set texture manager path
       
   275     Env().TextureManager().SetImagePathL(iPrivateFolderPath);
       
   276 	}
       
   277 
       
   278 
       
   279 void CAlfExStickersControl::LoadBackgroundImageL()
       
   280 	{
       
   281     //Experimental background image for the viewport
       
   282 	//Load the board texture
       
   283 	TFileName filename;
       
   284 	filename.Append(iPrivateFolderPath);
       
   285 	filename.Append(*StringLoader::LoadLC(R_ALFEXSTICKERS_BOARD_TEXTURE));
       
   286 	CleanupStack::PopAndDestroy(1);
       
   287 	
       
   288 	//Create image loader util. This object must exist through life time of texture. 
       
   289 	CAlfImageLoaderUtil* imageLoaderUtil = new(ELeave) CAlfImageLoaderUtil;
       
   290 	iImageLoaderUtilArray.AppendL(imageLoaderUtil); 
       
   291 	
       
   292 	//Create board texture from SVG file.
       
   293 	CAlfTexture& board = CreateSVGTextureL(filename, iVirtualSize, imageLoaderUtil);
       
   294 	
       
   295     //Create new Image Visual and add it to the iViewPort
       
   296     CAlfImageVisual* image = CAlfImageVisual::AddNewL(*this, iViewPort); 
       
   297     
       
   298     //Set scaling to cover the whole visual/layout
       
   299     image->SetScaleMode(CAlfImageVisual::EScaleFitHeight);
       
   300     
       
   301     //Set board texture into image visual
       
   302     image->SetImage(TAlfImage(board));
       
   303 	}
       
   304 
       
   305 
       
   306 CAlfTexture& CAlfExStickersControl::CreateSVGTextureL(const TFileName& aFileName, 
       
   307         TSize& aSize, CAlfImageLoaderUtil* aImageLoaderUtil)
       
   308     {
       
   309     //Set size of the SVG image. Must be called before creating image loader.
       
   310     aImageLoaderUtil->SetSize(aSize, EAspectRatioNotPreserved);
       
   311     
       
   312     //Use image loader util to create SVG image loader. 
       
   313     MAlfBitmapProvider* provider = aImageLoaderUtil->CreateSVGImageLoaderL(aFileName);
       
   314             
       
   315     //Use texture manager to create the texture with given bitmap provider.
       
   316     return Env().TextureManager().CreateTextureL(KAlfAutoGeneratedTextureId, 
       
   317             provider, EAlfTextureFlagDefault);
       
   318 
       
   319     }
       
   320 
       
   321 
       
   322 void CAlfExStickersControl::CreateStickersL()
       
   323 	{
       
   324 	//Create example stickers for the application
       
   325 	for (int i = 0; i < KStickerCount; ++i) 
       
   326     	{
       
   327     	iStickerArray[i] = static_cast<CAlfExStickerLayout*>(CAlfExStickerLayout::AddNewL(*this, iPlainLayout));
       
   328     	iStickerArray[i]->SetStickerPlaceL(this);
       
   329     	iStickerArray[i]->SetStickerTextContentL();
       
   330     	iStickerArray[i]->SetStickerDropShadowBrushL();
       
   331     	LoadStickerImageL(i);
       
   332     	
       
   333     	if (i==0)
       
   334     		iStickerArray[i]->ChangeStickerSizeL();
       
   335     	}
       
   336 	}
       
   337 
       
   338 
       
   339 void CAlfExStickersControl::LoadStickerImageL(TInt aIndex)
       
   340     {
       
   341     //This makes bitmap selection to proceed sequently within cycles.
       
   342     TInt bitmapID = aIndex % KStickerBitmapCount;
       
   343             
       
   344     TFileName filename;
       
   345     filename.Append(iPrivateFolderPath);
       
   346     filename.Append(*StringLoader::LoadLC(R_ALFEXSTICKERS_STICKER_TEXTURE_BLUE + bitmapID));
       
   347     CleanupStack::PopAndDestroy(1);
       
   348     
       
   349     //Create image loader util. This object must exist through life time of texture. 
       
   350     CAlfImageLoaderUtil* imageLoaderUtil = new(ELeave) CAlfImageLoaderUtil;
       
   351     iImageLoaderUtilArray.AppendL(imageLoaderUtil);
       
   352     
       
   353     TSize stickerSize(KStickerWidth, KStickerHeight);
       
   354     
       
   355     //Create sticker texture
       
   356     CAlfTexture& stickerTexture = CreateSVGTextureL(filename, stickerSize, imageLoaderUtil);
       
   357     
       
   358     //Assign texture to image brush
       
   359     iStickerArray[aIndex]->SetStickerImageBrushL(stickerTexture);
       
   360     }
       
   361 
       
   362 
       
   363 CAlfControl* CAlfExStickersControl::GetControl()
       
   364 	{
       
   365 	//Returns control to child visuals who need it
       
   366 	return this;
       
   367 	}
       
   368