AppSrc/CDrawGrid.cpp
changeset 3 93fff7023be8
equal deleted inserted replaced
2:e1e28b0273b0 3:93fff7023be8
       
     1 /*
       
     2 * Copyright (c) 2009 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: Juha Kauppinen, Mika Hokkanen
       
    13 * 
       
    14 * Description: Photo Browser
       
    15 *
       
    16 */
       
    17 
       
    18 #include "CDrawGrid.h"
       
    19 #include "TextureLoader.h"
       
    20 #include "DrawUtility.h"
       
    21 #include "ImagicConsts.h"
       
    22 
       
    23 
       
    24 const TInt  KDrawLimit = 65;
       
    25 const float KInitialZoomGrid = -4.1;//zoom value for grid when application starts drawing grid
       
    26 
       
    27 const TReal KMaxAngleLandscape = 45;//max tilt angle when moving in grid
       
    28 const TReal KMaxAnglePortrait = 10;//max tilt angle when moving in grid
       
    29 //const TReal KAngle2Start128Loading = 1;
       
    30 const float KTargetZCoord = -1.5;
       
    31 const float KZoomInMaxGrid = -3.5;//max possible zoom value 
       
    32 const float KZoomOutMaxGrid = -6;//min possible zoom value
       
    33 #ifdef DOUBLETAP_ZOOMGRID
       
    34 const TInt  KDoubleTapZoomGrid1 = KInitialZoomGrid;
       
    35 const TInt  KDoubleTapZoomGrid2 = KZoomOutMaxGrid * 7 / 10;  // 70% of max zoom out
       
    36 #endif
       
    37 
       
    38 CDrawGrid::CDrawGrid(CImagicContainerBrowser* aContainer, TInt& aCurrentIndex):
       
    39     iContainer(aContainer)//,
       
    40     //iCurrentIndex(aCurrentIndex)
       
    41     {
       
    42     // No implementation required
       
    43     }
       
    44 
       
    45 CDrawGrid::~CDrawGrid()
       
    46     {
       
    47     }
       
    48 
       
    49 CDrawGrid* CDrawGrid::NewLC(CImagicContainerBrowser* aContainer, TInt& aCurrentIndex)
       
    50     {
       
    51     CDrawGrid* self = new (ELeave) CDrawGrid(aContainer,aCurrentIndex);
       
    52     CleanupStack::PushL(self);
       
    53     self->ConstructL(aContainer,aCurrentIndex);
       
    54     return self;
       
    55     }
       
    56 
       
    57 CDrawGrid* CDrawGrid::NewL(CImagicContainerBrowser* aContainer, TInt& aCurrentIndex)
       
    58     {
       
    59     CDrawGrid* self = CDrawGrid::NewLC(aContainer,aCurrentIndex);
       
    60     CleanupStack::Pop(); // self;
       
    61     return self;
       
    62     }
       
    63 
       
    64 void CDrawGrid::ConstructL(CImagicContainerBrowser* aContainer, TInt& aCurrentIndex)
       
    65     {
       
    66     //iContainer = aContainer;
       
    67     }
       
    68 
       
    69 
       
    70 TReal CDrawGrid::GetCurrentGridTilt()
       
    71     {
       
    72     return iPerspectiveCurrent;
       
    73     }
       
    74 
       
    75 TReal CDrawGrid::GetGridZoom()
       
    76     {
       
    77     return iDrawGridZoom;
       
    78     }
       
    79 
       
    80 TGridXY CDrawGrid::GetGridTargetXY()
       
    81     {
       
    82     return iDrawGridTargetXY;
       
    83     }
       
    84 
       
    85 void CDrawGrid::SetGridTargetXY(TGridXY aValue)
       
    86     {
       
    87     iDrawGridTargetXY = aValue;
       
    88     }
       
    89 
       
    90 TGridXY CDrawGrid::GetGridXY()
       
    91     {
       
    92     return iDrawGridXY;
       
    93     }
       
    94 
       
    95 void CDrawGrid::SetGridXY(TGridXY aValue)
       
    96     {
       
    97     iDrawGridXY = aValue;
       
    98     }
       
    99 
       
   100 void CDrawGrid::KeyPressed()
       
   101     {
       
   102     iMenuAlpha = 1;
       
   103     iKeyTimer = 0;
       
   104     iKeyTimer2 = 10;
       
   105     }
       
   106 
       
   107 void CDrawGrid::KeyReleased()
       
   108     {
       
   109     iMenuAlpha = 0.99;
       
   110     iKeyTimer = 0;
       
   111     iKeyTimer2 = 10;
       
   112     }
       
   113 
       
   114 void CDrawGrid::KeyEvent()
       
   115     {
       
   116     //iMenuAlpha = 1;
       
   117     }
       
   118 
       
   119 /*----------------------------------------------------------------------*/
       
   120 // Set perspective projection
       
   121 //
       
   122 void CDrawGrid::SetPrespective(const TSize &aSize)
       
   123     {
       
   124     DP0_IMAGIC(_L("CDrawGrid::SetPrespective"));
       
   125     // Calculate aspect ratio
       
   126     GLfloat aspectRatio = (GLfloat)(aSize.iWidth) / (GLfloat)(aSize.iHeight);
       
   127 
       
   128     // Calculate prespective values
       
   129     const float near = 0.001;
       
   130     const float far = 100.0;
       
   131     const float top = 0.414*near;
       
   132     const float bottom = -top;
       
   133     const float left = aspectRatio * bottom;
       
   134     const float right = aspectRatio * top;
       
   135     
       
   136     // Set perspective
       
   137     glLoadIdentity();
       
   138     glFrustumf(left,right, bottom,top, near,far);
       
   139     
       
   140     float tmp = iContainer->GetDisplayRotAngle();
       
   141     iContainer->Interpolate(tmp, iContainer->GetDisplayRotTargetAngle(), 0.2);
       
   142     iContainer->SetDisplayRotAngle(tmp);
       
   143     glRotatef(iContainer->GetDisplayRotAngle(), 0,0,1);
       
   144     }
       
   145 
       
   146 void CDrawGrid::UpdateImageCoordinates(const TInt aFirstIndex)
       
   147     {
       
   148     const TReal KMinY = -(CImagicContainerBrowser::KGridSizeY - 1) * CImagicContainerBrowser::KSpacingY;
       
   149     TReal y2 = KMinY;
       
   150     TReal x2 = -CImagicContainerBrowser::KSpacingX;
       
   151     
       
   152     CImageData* prevImageData = iContainer->iIEngine->GetImageData(aFirstIndex - 1);
       
   153     if (prevImageData)
       
   154         {
       
   155         x2 = prevImageData->iGridData.iX;
       
   156         y2 = prevImageData->iGridData.iY;
       
   157         }
       
   158 
       
   159     for(TInt i = aFirstIndex; i<iContainer->iIEngine->GetTotalNumOfImages(); i++)
       
   160         {
       
   161         if ((y2 -= iContainer->KSpacingY) < KMinY)
       
   162             {
       
   163             y2 = 0;
       
   164             x2 += CImagicContainerBrowser::KSpacingX;
       
   165             }
       
   166 
       
   167         CImageData* imageData = iContainer->iIEngine->GetImageData(i);        
       
   168 #ifdef GAP_BETWEEN_FOLDERS
       
   169         TGridMode gridMode = iContainer->iIEngine->GetImageList().GetGridMode();
       
   170         if (gridMode)   
       
   171             {
       
   172             // Make small gap between folders
       
   173             CImageData* prevImageData = iContainer->iIEngine->GetImageData(i - 1);            
       
   174             if (prevImageData != NULL)
       
   175                 {
       
   176                 TBool gap = EFalse; 
       
   177                 if (gridMode == EGridModeFolder)
       
   178                     {
       
   179                     TFileName path, prevPath;
       
   180                     imageData->GetPath(path);
       
   181                     prevImageData->GetPath(prevPath);
       
   182                     gap = (path != prevPath);
       
   183                     }
       
   184                 else
       
   185                     {
       
   186                     gap = (imageData->iPersonId != prevImageData->iPersonId);
       
   187                     }
       
   188                     
       
   189                 if (gap)
       
   190                     {
       
   191                     x2 += CImagicContainerBrowser::KSpacingX / 2;
       
   192                     if (y2 < 0)
       
   193                         {
       
   194                         x2 += CImagicContainerBrowser::KSpacingX;
       
   195                         y2 = 0;
       
   196                         }
       
   197                     }
       
   198                 }
       
   199             }
       
   200 #endif // GAP_BETWEEN_FOLDERS            
       
   201         imageData->iGridData.iX = x2;
       
   202         imageData->iGridData.iY = y2;
       
   203         }
       
   204     }
       
   205 
       
   206 void CDrawGrid::HandleKeys()
       
   207     {
       
   208     iKeyTimer2--;
       
   209     if(iKeyTimer2 < 0)
       
   210         iKeyTimer2 = 0;
       
   211     
       
   212     if(iKeyTimer == 0 || iKeyTimer2 == 0)
       
   213         {
       
   214         iKeyTimer = 3;
       
   215         
       
   216         CKeyData keyData = iContainer->GetKeyData();
       
   217         
       
   218         // Calculate new index from movement keys
       
   219         iContainer->SetCurrentIndex(keyData.iY + iContainer->GetCurrentIndex());
       
   220 
       
   221         // Get next image in same row
       
   222         for (TInt i = 0;i < Abs(keyData.iX);i++)
       
   223             {
       
   224             TInt index = iContainer->GetCurrentIndex();
       
   225             CImageData* currentImageData = iContainer->iIEngine->GetImageData(index);
       
   226             while (currentImageData)
       
   227                 {
       
   228                 index += keyData.iX > 0 ? 1 : -1;
       
   229                 CImageData* imageData = iContainer->iIEngine->GetImageData(index);
       
   230                 if (imageData == NULL)
       
   231                     {
       
   232                     currentImageData = NULL;
       
   233                     break;
       
   234                     }
       
   235                 
       
   236                 // Next image is found
       
   237                 if (Abs(imageData->iGridData.iY - currentImageData->iGridData.iY) <
       
   238                         CImagicContainerBrowser::KSpacingY / 2)
       
   239                     {
       
   240                     iContainer->SetCurrentIndex(index);
       
   241                     currentImageData = NULL;
       
   242                     break;
       
   243                     }
       
   244                 }
       
   245             }
       
   246         
       
   247         //iContainer->SetCurrentIndex(keyData.iX * CImagicContainerBrowser::KGridSizeY + iContainer->GetCurrentIndex());
       
   248 		
       
   249         //We have to zero key data after reading it
       
   250         keyData.iX = 0;
       
   251         keyData.iY = 0;
       
   252         }
       
   253     else
       
   254         {
       
   255         iKeyTimer--;
       
   256         }
       
   257     }
       
   258     
       
   259 /*----------------------------------------------------------------------*/
       
   260 // Initializes Grid view
       
   261 //
       
   262 void CDrawGrid::InitDrawGrid()
       
   263     {
       
   264     DP0_IMAGIC(_L("CDrawGrid::InitDrawGrid"));
       
   265     
       
   266     iKeyTimer = 0;
       
   267     
       
   268     iPerspectiveCurrent = 0;//current value of perspective when moving on grid by tilting while grid
       
   269     //iPerspectiveTarget = 0;//target value of perspective
       
   270     iDrawGridTargetZoom = KInitialZoomGrid;//Set target zooming value when draving Grid
       
   271     
       
   272     iGridMovingSpeed = 0.05;
       
   273     iGridZoomSpeed = 0.1;
       
   274     iGridZoomStep = 0.3;
       
   275     iDrawGridZoom = KInitialZoomGrid;//Set target zooming value when draving Grid
       
   276     
       
   277     CImageData* imageData = iContainer->iIEngine->GetImageData(iContainer->GetCurrentIndex());
       
   278     if(imageData)
       
   279         imageData->iGridData.iScale = 6.5;
       
   280     
       
   281     // set scale a little bigger than initial target value to show animation in opening grid
       
   282 //    CImageData* imageData = iIEngine->GetImageData(iCurrentIndex, iImagicAppUi->GetUIDrawMode());
       
   283 //    if (imageData) imageData->iGridData.iScale += 0.5;
       
   284 
       
   285     // set zooming factor a little bigger than initial target value to show animation in opening grid
       
   286 //    iDrawGridZoom = iDrawGridZoom + 0.5;
       
   287     }
       
   288 
       
   289 /*----------------------------------------------------------------------*/
       
   290 // Draws grid
       
   291 //
       
   292 void CDrawGrid::DrawGridL(const TSize &aSize)
       
   293     {
       
   294     DP0_IMAGIC(_L("CDrawGrid::DrawGrid"));
       
   295     
       
   296     SetPrespective(aSize);// Setup perspective
       
   297     
       
   298     // If user hasn't press anything, stay in beginning of the grid
       
   299     if(!iContainer->IsUserInputGiven())
       
   300         {
       
   301         TInt index = -1;  
       
   302         TReal maxX = CImagicContainerBrowser::KSpacingX; 
       
   303         if(iContainer->GetScreenSize().iHeight > 240)
       
   304             maxX = CImagicContainerBrowser::KSpacingX * 2;
       
   305         
       
   306         // Select image which closest to ideal position
       
   307         for(TInt i = 0;i < iContainer->iIEngine->GetTotalNumOfImages();i++)
       
   308             {
       
   309             CImageData* imageData = iContainer->iIEngine->GetImageData(i);
       
   310             if (imageData->iGridData.iX > maxX) 
       
   311                 break;
       
   312             if (imageData->iGridData.iY > -CImagicContainerBrowser::KSpacingY * 2)
       
   313                 index = i; 
       
   314             }
       
   315         
       
   316         if (index >= 0)
       
   317             iContainer->SetCurrentIndex(index);
       
   318         }
       
   319     
       
   320     
       
   321 #ifdef MOMENTUM_MOVE
       
   322     // Move grid x and y automatically when flick
       
   323     // FindImageInScreen() has to be called here, before SetPerspective() to get 
       
   324     // OpenGL matrix before initialised in it.
       
   325     if(iContainer->iMomentumMove)
       
   326         {
       
   327         // so move automatically. Target is set in HandleGestureEnd(), when 
       
   328         // user releases touch.
       
   329 
       
   330         // Slowing down when it gets closer than the size of 10% of speed
       
   331         // TODO: use definition. not 0.5
       
   332         float gapX = (iContainer->iMomentumSpeedX)? (iDrawGridTargetXY.iX - iDrawGridXY.iX) / (0.5 * iContainer->iMomentumSpeedX): 0.0f;
       
   333         float gapY = (iContainer->iMomentumSpeedY)? (iDrawGridTargetXY.iY - iDrawGridXY.iY) / (0.5 * iContainer->iMomentumSpeedY): 0.0f;
       
   334         iContainer->CheckLimits(gapX, -1.0, 1.0); // max speed doesn't go beyond user's move
       
   335         iContainer->CheckLimits(gapY, -1.0, 1.0);
       
   336 
       
   337         float spdX = gapX * iContainer->iMomentumSpeedX; // speed/1sec. faster with bigger gap
       
   338         float spdY = gapY * iContainer->iMomentumSpeedY;
       
   339         iDrawGridXY.iX += spdX * iContainer->iTimeDiff; // movement = (open gl pixels)/sec * (elapsed time from last draw) 
       
   340         iDrawGridXY.iY += spdY * iContainer->iTimeDiff;
       
   341 
       
   342         DP4_IMAGIC(_L("spdX=%6.4f, gapX=%6.4f, iMomentumSpeedX=%6.4f, iTimeDiff=%6.4f"), spdX, gapX, iContainer->iMomentumSpeedX, iContainer->iTimeDiff);
       
   343         DP3_IMAGIC(_L("spdY=%6.4f, gapY=%6.4f, iMomentumSpeedY=%6.4f"), spdY, gapY, iContainer->iMomentumSpeedY);
       
   344         DP2_IMAGIC(_L("iDrawGridTargetX=%6.4f <=== iDrawGridX=%6.4f"), iDrawGridTargetXY.iX, iDrawGridXY.iX);
       
   345         DP2_IMAGIC(_L("iDrawGridTargetY=%6.4f <=== iDrawGridY=%6.4f"), iDrawGridTargetXY.iY, iDrawGridXY.iY);
       
   346         }
       
   347 
       
   348     // Pick up the picture in the center of the screen as current selected one.
       
   349     // Also does on dragging if not in the mode of no key event simulation on drag 
       
   350 #ifdef HOLD_SELECTION_ONDRAG
       
   351     if(iContainer->iMomentumMove || iContainer->iHoldSelection)
       
   352 #else
       
   353     if(iMomentumMove)
       
   354 #endif
       
   355         {
       
   356         TInt id;
       
   357         FloatCoords coord;
       
   358         coord.iX = iDrawGridXY.iX;
       
   359         coord.iY = iDrawGridXY.iY;
       
   360 #ifdef FLICK_ONLY_IN_X_IN_GRID
       
   361         if (iContainer->FindNearestImageInOGl(coord, id))
       
   362             {
       
   363             if (iContainer->iMomentumMove)
       
   364                 {
       
   365                 // Change iCurrentIndex only when x coord is changed
       
   366                 TInt cX = iContainer->GetCurrentIndex() / CImagicContainerBrowser::KGridSizeY;
       
   367                 TInt tX = id / CImagicContainerBrowser::KGridSizeY;
       
   368                 if (cX != tX) 
       
   369                     //iCurrentIndex = id;
       
   370                     iContainer->SetCurrentIndex(id);
       
   371                 }
       
   372             else
       
   373                 //iCurrentIndex = id;
       
   374                 iContainer->SetCurrentIndex(id);
       
   375             }
       
   376 #else
       
   377         if (FindNearestImageInOGl(coord, id)) 
       
   378             //iCurrentIndex = id;
       
   379             iContainer->SetCurrentIndex(id);
       
   380 #endif
       
   381         }
       
   382 
       
   383     if(iContainer->iMomentumMove)
       
   384         {
       
   385         // Stop momentum move when it gets close to target or exceeds 
       
   386         if ((!iContainer->iMomentumSpeedX || Abs(iDrawGridTargetXY.iX - iDrawGridXY.iX) < 0.15) && // 0.15 is just a guess.
       
   387             (!iContainer->iMomentumSpeedY || Abs(iDrawGridTargetXY.iY - iDrawGridXY.iY) < 0.15))
       
   388             {
       
   389             iContainer->iMomentumMove = EFalse;
       
   390             }
       
   391         }
       
   392 #endif
       
   393     
       
   394     
       
   395     
       
   396 #ifdef ZOOM_WHILE_ROTATING
       
   397     if(Abs(iDrawGridZoom-KZoomOutMaxGrid) < 1)
       
   398         iDrawGridTargetZoom = KInitialZoomGrid;
       
   399 #endif
       
   400 #ifdef ENABLE_GRID_ZOOM
       
   401     Interpolate(iDrawGridZoom, iDrawGridTargetZoom, iGridZoomSpeed);
       
   402 #endif
       
   403     
       
   404 #ifdef SCREEN_ROTATION_ON
       
   405     Interpolate(iDisplayRotation, iDisplayRotationTarget, 0.005);
       
   406 #endif
       
   407     
       
   408     //Handle moving keys-------------------------------->
       
   409     HandleKeys();
       
   410     //Handle Rotation keys, this is for single currently selected picture
       
   411     iContainer->HandleRotationKeys();
       
   412     
       
   413     //Check that current index is in grid area
       
   414     /*TInt index = iContainer->GetCurrentIndex();
       
   415     CheckIndexLimits(index);
       
   416     iContainer->SetCurrentIndex(index);*/
       
   417 
       
   418     // Update AppUI class about selected picture index
       
   419     if(iContainer->GetPrevIndex() != iContainer->GetCurrentIndex())
       
   420         {
       
   421         iContainer->iImagicAppUi->SetImageIndex(iContainer->GetCurrentIndex());
       
   422         iContainer->iDynamicLoadingOn = ETrue;
       
   423 
       
   424 //#define DISPLAY_DATE
       
   425 #ifdef DISPLAY_DATE
       
   426         DisplayDate();
       
   427 #endif
       
   428         //Update previous Grid index..
       
   429         iContainer->SetPrevIndex(iContainer->GetCurrentIndex());        
       
   430         }
       
   431     
       
   432 #ifdef MOMENTUM_MOVE
       
   433     if(!iContainer->iMomentumMove) 
       
   434         {
       
   435 #endif
       
   436         //Calculate new target X and Y positions on Grid
       
   437 
       
   438     CImageData* imageData = iContainer->iIEngine->GetImageData(iContainer->GetCurrentIndex());
       
   439     if (imageData)
       
   440         {
       
   441         iDrawGridTargetXY.iX = imageData->iGridData.iX;
       
   442         iDrawGridTargetXY.iY = -imageData->iGridData.iY;
       
   443         }
       
   444 
       
   445 #ifdef HOLD_SELECTION_ONDRAG
       
   446         if (iContainer->iHoldSelection == EFalse) 
       
   447             {
       
   448 #endif
       
   449             //and interpolate between current and target Y and X
       
   450             iContainer->Interpolate(iDrawGridXY.iX, iDrawGridTargetXY.iX, iGridMovingSpeed);
       
   451             iContainer->Interpolate(iDrawGridXY.iY, iDrawGridTargetXY.iY, iGridMovingSpeed);
       
   452     
       
   453 #ifdef HOLD_SELECTION_ONDRAG
       
   454             }
       
   455 #endif
       
   456 #ifdef MOMENTUM_MOVE
       
   457         }
       
   458 #endif
       
   459     
       
   460     // Zooming
       
   461     //Calculate new target zooming value for Grid
       
   462 //unno temp iGridZoomSpeed = 20 * iTimeDiff;
       
   463 #ifdef ENABLE_GRID_ZOOM
       
   464     iDrawGridTargetZoom += keyData.iZoomIn * iGridZoomStep;
       
   465     iDrawGridTargetZoom -= keyData.iZoomOut * iGridZoomStep;
       
   466     CheckLimits(iDrawGridTargetZoom, KZoomOutMaxGrid, KZoomInMaxGrid);
       
   467 
       
   468     keyData.iZoom = 0;//we have to reset key data after using it
       
   469     keyData.iZoomIn = keyData.iZoomOut = EFalse;
       
   470 #endif
       
   471     
       
   472     //Handle Rotation keys, this is for single currently selected picture
       
   473     //iContainer->HandleRotationKeys();
       
   474     
       
   475     
       
   476     // Draw images ---------------------------------->
       
   477     
       
   478     //Tilt Grid(camera) into moving direction
       
   479     float perspectiveDiff = iDrawGridTargetXY.iX-iDrawGridXY.iX;
       
   480     perspectiveDiff *= 10;
       
   481     
       
   482     //we stop moving of the grid littele bit earlier than we are in real zero
       
   483 #ifdef HOLD_SELECTION_ONDRAG
       
   484     if(iContainer->iHoldSelection == EFalse) {
       
   485 #endif
       
   486     iTargetPerspective = perspectiveDiff;
       
   487 #ifdef HOLD_SELECTION_ONDRAG
       
   488     }
       
   489 #endif
       
   490 
       
   491     
       
   492     //Speed up tilting when getting closer to target
       
   493     if(iPerspectiveCurrent-iTargetPerspective != 0 && 
       
   494        (Abs(iTargetPerspective) < Abs(iPerspectiveCurrent)) && 
       
   495        Abs(iPerspectiveCurrent) < 20)
       
   496         {
       
   497         iContainer->Interpolate(iPerspectiveCurrent,iTargetPerspective, Abs((KMaxAngleLandscape/iPerspectiveCurrent)/40));
       
   498         }
       
   499     else
       
   500         {
       
   501         iContainer->Interpolate(iPerspectiveCurrent,iTargetPerspective, 0.04);
       
   502         }
       
   503         
       
   504     
       
   505 #ifdef _ACCELEROMETER_SUPPORTED_
       
   506 //#ifndef _S60_5x_ACCELEROMETER_
       
   507     if(iContainer->iDeviceOrientation == EOrientationDisplayLeftUp)//Landscape
       
   508 /*#else if _S60_5x_ACCELEROMETER_
       
   509     if(iContainer->iDeviceOrientation == (TImagicDeviceOrientation)TSensrvOrientationData::EOrientationDisplayRightUp)///this is wrong
       
   510 #endif*/
       
   511 #else
       
   512     if(ETrue/*iDeviceOrientation*/)//if no accelerometer, use always landscape
       
   513 #endif
       
   514         iContainer->CheckLimits(iPerspectiveCurrent, -KMaxAngleLandscape, KMaxAngleLandscape);  
       
   515     else
       
   516         iContainer->CheckLimits(iPerspectiveCurrent, -KMaxAnglePortrait, KMaxAnglePortrait);
       
   517     
       
   518     glRotatef(iPerspectiveCurrent, 0,1,0);//make perspective when moving on grid
       
   519     
       
   520     
       
   521     
       
   522     //Go to grid position ----------->
       
   523     //Center picture grid and set limit of pictures to be drawn
       
   524     float centerOffset=2.9;//bigger value -> less movement in Y-axis
       
   525     
       
   526 #ifdef MOMENTUM_MOVE
       
   527     glTranslatef(-iDrawGridXY.iX-iDrawGridZoom*iPerspectiveCurrent/KMaxAngleLandscape,
       
   528             CImagicContainerBrowser::KSpacingY+(iDrawGridXY.iY-CImagicContainerBrowser::KSpacingY)/centerOffset, iDrawGridZoom);
       
   529 #else
       
   530     glTranslatef(-iDrawGridX, KSpacingY+(iDrawGridY-KSpacingY)/centerOffset, iDrawGridZoom);
       
   531 #endif
       
   532     
       
   533     //OpenGl Vertex data
       
   534     GLfixed vertices[8];
       
   535     glVertexPointer( 2, GL_FIXED, 0, vertices );
       
   536     
       
   537     //Bind into a null picture
       
   538     //iContainer->iCurrentBindedIndex = iContainer->iLoadingTextureIndex;
       
   539     //iContainer->iLoadingTextureIndex = 0;
       
   540     //glBindTexture( GL_TEXTURE_2D, iContainer->iLoadingTextureIndex);
       
   541     glBindTexture( GL_TEXTURE_2D, 0);
       
   542     
       
   543        
       
   544     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
       
   545     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
       
   546    	
       
   547     // Set gray color for pictures that are not loaded yet
       
   548     glColor4f(0.3,0.3,0.3, 1);
       
   549     
       
   550     TInt drawMax = iContainer->GetCurrentIndex()+KDrawLimit;
       
   551     TInt drawMin = iContainer->GetCurrentIndex()-KDrawLimit;
       
   552 
       
   553     CImageData* currentImageData = NULL;
       
   554     TGridMode gridMode = iContainer->iIEngine->GetImageList().GetGridMode();
       
   555     if (gridMode != EGridModeTime)      
       
   556         currentImageData = iContainer->iIEngine->GetImageData(iContainer->GetCurrentIndex());
       
   557     
       
   558     //This loop draws all the textures in the grid ------------------------------->
       
   559     for(TInt i2=0; i2<iContainer->iIEngine->GetTotalNumOfImages(); i2++)
       
   560         {
       
   561         TInt i = (i2 + iContainer->GetCurrentIndex() + 1) % iContainer->iIEngine->GetTotalNumOfImages();
       
   562         CImageData* imageData = iContainer->iIEngine->GetImageData(i);
       
   563         
       
   564         //If we are in same picture index, ie. not moving, draw both sides equally
       
   565         if(drawMax > iContainer->iIEngine->GetTotalNumOfImages()-1)
       
   566             drawMax = iContainer->iIEngine->GetTotalNumOfImages()-1;
       
   567         if(drawMin < 0)
       
   568             drawMin = 0;
       
   569             
       
   570         //Do not draw all images, just enough to fill the screen
       
   571         if((Abs(imageData->iGridData.iRotationAngle-imageData->iGridData.iTargetRotationAngle) > 0.1) || //Draw all all images in grid if rotated
       
   572            (i>=drawMin && i<=drawMax))
       
   573             {
       
   574             // Calculate current coordinates
       
   575             //TInt x = i/iContainer->iGridSizeY;
       
   576             //TInt y = -(i%iContainer->iGridSizeY);// Y axis is inverted
       
   577             float z=0;
       
   578             //float scale=1;
       
   579             iScaleTarget = 1;
       
   580             
       
   581             
       
   582             //Highlight by scaling up selected picture on Grid
       
   583             if(iContainer->GetCurrentIndex()==i)
       
   584                 {
       
   585                 z=0.3;
       
   586                 iScaleTarget = 1.5;
       
   587                 }
       
   588 
       
   589             TBool dim = EFalse; //(currentImageData && !currentImageData->IsSamePath(*imageData));            
       
   590             //BubbleEffect(x, y, z);
       
   591             
       
   592             //Zoom out the grid if "camera target" is far away from current position >>>
       
   593             //if (Abs(iDrawGridX-iDrawGridTargetX) > 10*KSpacingX)
       
   594 #ifdef MOMENTUM_MOVE
       
   595             float absDeltaX = Abs(iDrawGridXY.iX-iDrawGridTargetXY.iX);
       
   596             if((!iContainer->iMomentumMove && absDeltaX > 2*CImagicContainerBrowser::KSpacingX) ||
       
   597                ( iContainer->iMomentumMove && absDeltaX > 5*CImagicContainerBrowser::KSpacingX))
       
   598 #else
       
   599             if(Abs(iDrawGridX-iDrawGridTargetX) > 2*KSpacingX)
       
   600 #endif
       
   601                 {
       
   602                 z=KTargetZCoord;
       
   603                 iContainer->Interpolate(imageData->iGridData.iZ, z, 0.05);
       
   604                 }    
       
   605             else
       
   606                 {
       
   607                 iContainer->Interpolate(imageData->iGridData.iZ, z, 0.05);
       
   608                 }
       
   609             
       
   610             iContainer->Interpolate(imageData->iGridData.iScale, iScaleTarget, 0.23);
       
   611             //Zoom out the grid if "camera target" is far away from current position <<<
       
   612             
       
   613             //Rotate picture in Grid if needed
       
   614             iContainer->HandleRotation(imageData->iGridData.iRotationAngle, imageData->iGridData.iTargetRotationAngle);
       
   615             
       
   616             //Calculate new picture vertices to fix picture aspect ratio
       
   617             iContainer->SetPictureVertices(imageData, vertices);
       
   618             
       
   619             //Move camera to picture position and rotate and scale it
       
   620             glPushMatrix();    
       
   621             glTranslatef(imageData->iGridData.iX, imageData->iGridData.iY, imageData->iGridData.iZ);// - (dim ? 1.0 : 0));
       
   622             glRotatef(imageData->iGridData.iRotationAngle, 0,0,1);
       
   623             TReal scale = imageData->iGridData.iScale;
       
   624             glScalef(scale, scale, scale);
       
   625             
       
   626 #ifdef SHADOW_PHOTOS
       
   627             // Draw shadow behind image
       
   628             glPushMatrix();
       
   629             glBindTexture( GL_TEXTURE_2D, iContainer->iShadowTextureIndex);
       
   630             glTranslatef(0, 0, -0.15);
       
   631             glScalef(1.2, 1.2, 1);
       
   632             glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
       
   633             glPopMatrix();
       
   634 #endif                     
       
   635             
       
   636 #ifdef DRAW_FRAME
       
   637             // Draw frame around selected image
       
   638             if (iContainer->GetCurrentIndex()==i)
       
   639                 DrawFrame(i);
       
   640 #endif
       
   641                     
       
   642             // Calculate color for not loaded picture, more far from selection-> darker box
       
   643             float color = 
       
   644 #ifdef EMPTY_IMAGE_AS_WIREFRAME
       
   645                 0.05 + 0.2/
       
   646 #else
       
   647                 1.0/
       
   648 #endif                
       
   649                 (Abs(iDrawGridXY.iX - imageData->iGridData.iX)/2 + .2);
       
   650             if (color > 0.5)
       
   651                 color = 0.5;
       
   652             glColor4f(color,color,color, 1);
       
   653             
       
   654             //Bind to a new picture only if needed, ie. we really have new picture
       
   655             if(imageData->iGridData.iGlLQ128TextIndex != iLastGridTexture ||
       
   656                imageData->iGridData.iGlLQ32TextIndex != iLastGridTexture)
       
   657                 {
       
   658                 if(imageData->iGridData.iGlLQ128TextIndex)
       
   659                     iLastGridTexture = imageData->iGridData.iGlLQ128TextIndex;
       
   660                 else if(imageData->iGridData.iGlLQ32TextIndex)
       
   661                     iLastGridTexture = imageData->iGridData.iGlLQ32TextIndex;
       
   662                 else
       
   663                     iLastGridTexture = iContainer->iLoadingTextureIndex;
       
   664                 
       
   665                 iContainer->iCurrentBindedIndex = iLastGridTexture;
       
   666                 glBindTexture( GL_TEXTURE_2D, iLastGridTexture);
       
   667                 
       
   668                 // Picture is loaded, draw it with white color
       
   669                 if (iLastGridTexture)  
       
   670                     {
       
   671                     // Draw as solid black frame
       
   672                     if (dim)
       
   673                         glColor4f(0.4,0.4,0.4, 1);
       
   674                     else
       
   675                         glColor4f(1,1,1, 1);
       
   676                     }
       
   677                 }
       
   678             
       
   679             // Draw image
       
   680 #ifdef EMPTY_IMAGE_AS_WIREFRAME            
       
   681             if (iLastGridTexture == 0 && iContainer->GetCurrentIndex() != i)
       
   682                 {
       
   683                 GLfixed p;
       
   684                 p = vertices[6]; vertices[6] = vertices[4]; vertices[4] = p;
       
   685                 p = vertices[7]; vertices[7] = vertices[5]; vertices[5] = p;
       
   686                 glLineWidth(1.8f);  // TODO: depend on resolution
       
   687                 glDrawArrays(GL_LINE_LOOP,0,4);
       
   688                 p = vertices[6]; vertices[6] = vertices[4]; vertices[4] = p;
       
   689                 p = vertices[7]; vertices[7] = vertices[5]; vertices[5] = p;
       
   690                 }
       
   691             else
       
   692                 {
       
   693                 // Draw as solid black frame
       
   694                 if (iLastGridTexture == 0)
       
   695                     glColor4f(0,0,0, 1);
       
   696 #endif                
       
   697                 glDrawArrays(GL_TRIANGLE_STRIP,0,4);
       
   698 #ifdef EMPTY_IMAGE_AS_WIREFRAME                
       
   699                 }
       
   700 #endif            
       
   701            
       
   702             glPopMatrix();
       
   703             }//if
       
   704         
       
   705         //EGLint err = eglGetError();
       
   706         EGLint err = glGetError();
       
   707         //if(err != EGL_SUCCESS)
       
   708         while(err != GL_NO_ERROR)
       
   709             {
       
   710             CImageData* data = iContainer->iIEngine->GetImageData(i);
       
   711             //Delete all textures for this index just in case
       
   712             if(data->iGridData.iGlLQ128TextIndex)
       
   713                 glDeleteTextures(1, &data->iGridData.iGlLQ128TextIndex);data->iGridData.iGlLQ32TextIndex = 0;
       
   714             if(data->iGridData.iGlLQ32TextIndex)
       
   715                 glDeleteTextures(1, &data->iGridData.iGlLQ32TextIndex);data->iGridData.iGlLQ128TextIndex = 0;
       
   716             if(data->iGridData.iGlHQ512TextIndex)
       
   717                 glDeleteTextures(1, &data->iGridData.iGlHQ512TextIndex);data->iGridData.iGlHQ512TextIndex = 0;
       
   718             if(data->iGridData.iGlSuperHQTextIndex)
       
   719                 glDeleteTextures(1, &data->iGridData.iGlSuperHQTextIndex);data->iGridData.iGlSuperHQTextIndex = 0;
       
   720             
       
   721             err = eglGetError();
       
   722             }
       
   723                         
       
   724         }//for
       
   725     
       
   726 #if 0
       
   727     if(iMenuAlpha < 1)
       
   728         {
       
   729         iMenuAlpha-=0.15;
       
   730         if(iMenuAlpha < 0)
       
   731             iMenuAlpha = 0;
       
   732         }
       
   733 
       
   734     iContainer->iDrawUtility->DrawIcon2(iContainer->Size(), iContainer->iMenuTextureIndex, iMenuAlpha);
       
   735 #endif
       
   736     
       
   737     iContainer->DynamicLoadingL();
       
   738     }
       
   739 
       
   740 
       
   741 TBool CDrawGrid::IsDrawingNeededGrid()
       
   742     {
       
   743     
       
   744     CImageData* imageData = iContainer->iIEngine->GetImageData(iContainer->GetCurrentIndex());
       
   745     
       
   746     iContainer->SetMinMagFilterLinear(EFalse);
       
   747 #if 0
       
   748     if(iMenuAlpha != 0)
       
   749         {
       
   750         return ETrue;
       
   751         }
       
   752 #endif
       
   753     if(Abs(iPerspectiveCurrent) < CImagicContainerBrowser::KAngle2Start128Loading && Abs(iPerspectiveCurrent) > 0.001)
       
   754         {
       
   755         //DP0_IMAGIC(_L("CDrawGrid::IsDrawingNeededGrid #0"));
       
   756         iContainer->iDynamicLoadingOn = ETrue;
       
   757         }
       
   758     
       
   759     
       
   760     //Make sure that all visible images are rotated
       
   761     //this causes too much processor load
       
   762     /*for(TInt i=0; i<=CImagicContainerBrowser::K128TNImageBuffer; i++)
       
   763         {
       
   764         for(TInt j=0; j<2; j++)
       
   765             {
       
   766             TInt index = iContainer->GetCurrentIndex() + (j ?  i : -i);
       
   767             
       
   768             CImageData* imageData = iContainer->iIEngine->GetImageData(index);
       
   769             
       
   770             if(imageData != NULL)
       
   771                 if(Abs(imageData->iGridData.iRotationAngle - imageData->iGridData.iTargetRotationAngle) > 0.01)
       
   772                     {
       
   773                     DP0_IMAGIC(_L("CDrawGrid::IsDrawingNeededGrid #0.1"));
       
   774                     return ETrue;
       
   775                     }
       
   776                 else if(imageData->iGridData.iRotationAngle != imageData->iGridData.iTargetRotationAngle)
       
   777                     {
       
   778                     DP0_IMAGIC(_L("CDrawGrid::IsDrawingNeededGrid #0.2"));
       
   779                     imageData->iGridData.iRotationAngle = imageData->iGridData.iTargetRotationAngle;
       
   780                     return ETrue;
       
   781                     }
       
   782             }
       
   783         }*/
       
   784     
       
   785     //CImageData* imageData = iContainer->iIEngine->GetImageData(iContainer->GetCurrentIndex());
       
   786     
       
   787     if(imageData)
       
   788         if(Abs(imageData->iGridData.iScale - iScaleTarget) > 0.1)
       
   789             {
       
   790             return ETrue;
       
   791             }
       
   792         else if(imageData->iGridData.iScale != iScaleTarget)
       
   793             {
       
   794             imageData->iGridData.iScale = iScaleTarget;
       
   795             return ETrue;
       
   796             }
       
   797     
       
   798     
       
   799 #ifdef ENABLE_DISPLAY_ROTATION
       
   800     if(Abs(iContainer->GetDisplayRotAngle() - iContainer->GetDisplayRotTargetAngle()) > 1)
       
   801         {
       
   802         DP0_IMAGIC(_L("CDrawGrid::IsDrawingNeededGrid #2"));
       
   803         return ETrue;
       
   804         }
       
   805     else if(iContainer->GetDisplayRotAngle() != iContainer->GetDisplayRotTargetAngle())
       
   806         {
       
   807         iContainer->SetDisplayRotAngle(iContainer->GetDisplayRotTargetAngle());
       
   808         return ETrue;
       
   809         }
       
   810 #endif
       
   811     
       
   812     if(imageData)
       
   813         if(Abs(imageData->iGridData.iRotationAngle-imageData->iGridData.iTargetRotationAngle) > 1)
       
   814             {
       
   815             DP0_IMAGIC(_L("CDrawGrid::IsDrawingNeededGrid #3"));
       
   816             return ETrue;
       
   817             }
       
   818         else if(imageData->iGridData.iRotationAngle != imageData->iGridData.iTargetRotationAngle)
       
   819             {
       
   820             imageData->iGridData.iRotationAngle = imageData->iGridData.iTargetRotationAngle;
       
   821             //iContainer->iDrawNow = ETrue;
       
   822             return ETrue;
       
   823             }
       
   824     
       
   825 #ifdef ENABLE_GRID_ZOOM
       
   826     if(Abs(iDrawGridZoom - iDrawGridTargetZoom) > 0.01)
       
   827         {
       
   828         DP0_IMAGIC(_L("CDrawGrid::IsDrawingNeededGrid #4"));
       
   829         return ETrue;
       
   830         }
       
   831 #endif
       
   832     
       
   833     if(Abs(iDrawGridXY.iX - iDrawGridTargetXY.iX) > 0.02)
       
   834         {
       
   835         DP0_IMAGIC(_L("CDrawGrid::IsDrawingNeededGrid #5"));
       
   836         return ETrue;
       
   837         }
       
   838     else if(iDrawGridXY.iX != iDrawGridTargetXY.iX)
       
   839         {
       
   840         iDrawGridXY.iX = iDrawGridTargetXY.iX;
       
   841         }
       
   842     
       
   843     if(Abs(iDrawGridXY.iY - iDrawGridTargetXY.iY) > 0.02)
       
   844         {
       
   845         DP0_IMAGIC(_L("CDrawGrid::IsDrawingNeededGrid #6"));
       
   846         return ETrue;
       
   847         }
       
   848     else if(iDrawGridXY.iY != iDrawGridTargetXY.iY)
       
   849         {
       
   850         iDrawGridXY.iY = iDrawGridTargetXY.iY;
       
   851         }
       
   852     
       
   853     if(Abs(iPerspectiveCurrent - iTargetPerspective) > 0.1)
       
   854         {
       
   855         DP0_IMAGIC(_L("CDrawGrid::IsDrawingNeededGrid #7"));
       
   856         return ETrue;
       
   857         }
       
   858     else if(iPerspectiveCurrent != iTargetPerspective)
       
   859         {
       
   860         DP0_IMAGIC(_L("CDrawGrid::IsDrawingNeededGrid #7.1"));
       
   861         iPerspectiveCurrent = iTargetPerspective;
       
   862         return ETrue;
       
   863         }
       
   864     
       
   865     if(iContainer->iDrawNow)
       
   866         {
       
   867         DP0_IMAGIC(_L("CDrawGrid::IsDrawingNeededGrid #1"));
       
   868         iContainer->iDrawNow = EFalse;
       
   869         return ETrue;
       
   870         }
       
   871     
       
   872     return EFalse;
       
   873     }
       
   874 
       
   875 
       
   876 void CDrawGrid::MovingDirection()
       
   877     {
       
   878     if(iContainer->GetPrevIndex() < iContainer->GetCurrentIndex())
       
   879         {
       
   880         iMovingRight = ETrue;
       
   881         iMovingLeft = EFalse;
       
   882         }
       
   883     else if(iContainer->GetPrevIndex() > iContainer->GetCurrentIndex())
       
   884         {
       
   885         iMovingLeft = ETrue;
       
   886         iMovingRight = EFalse;
       
   887         }
       
   888     else if(iContainer->GetPrevIndex() == iContainer->GetCurrentIndex())
       
   889         {
       
   890         iMovingRight = EFalse; 
       
   891         iMovingLeft = EFalse;
       
   892         }
       
   893     }
       
   894 
       
   895 void CDrawGrid::DisplayDate()
       
   896     {
       
   897     CImageData* imageData = iContainer->iIEngine->GetImageData(iContainer->GetCurrentIndex());
       
   898     
       
   899     if(imageData)
       
   900         {
       
   901         TDateTime dateTime = imageData->GetCreatedTime().DateTime();
       
   902         if(iPrevDateTime.Month() != dateTime.Month())
       
   903             {
       
   904             TMonth month = dateTime.Month();
       
   905             iContainer->iImagicAppUi->GetImagicUtils()->DisplayYearAndMonth(0, dateTime);
       
   906             }
       
   907         iPrevDateTime = dateTime;
       
   908         }
       
   909     }
       
   910 
       
   911 /*----------------------------------------------------------------------*/
       
   912 // Calculates widht and height with aspect ratio
       
   913 //
       
   914 void CDrawGrid::DrawFrame(TInt aIndex)
       
   915     {
       
   916     // Draw frame around selected image
       
   917     glPushMatrix();
       
   918     glDisable(GL_TEXTURE_2D);
       
   919     
       
   920     glDisable(GL_DEPTH_TEST);
       
   921     
       
   922     //Frame size
       
   923     float scale=1.09;        
       
   924 #ifdef FRAME_COLOR_CHANGE
       
   925     if(iSelectionFrameColor >= 1.0)
       
   926         iSelectionFrameColor-=0.05;
       
   927         
       
   928     if(iSelectionFrameColor <= 0.3)
       
   929         iSelectionFrameColor+= 0.05;
       
   930 
       
   931     if(iTNCreationComplete)
       
   932         glColor4f(iSelectionFrameColor,iSelectionFrameColor,iSelectionFrameColor, 1);
       
   933     else
       
   934         glColor4f(1,iSelectionFrameColor,iSelectionFrameColor, 1);
       
   935 #else
       
   936 #ifdef SHADOW_PHOTOS
       
   937     glColor4f(1,0,0, 1);        // red frame
       
   938 #else    
       
   939     glColor4f(1,1,1, 1);        // white frame
       
   940 #endif    
       
   941 #endif
       
   942     
       
   943     glTranslatef(0,0,-0.03);
       
   944     glScalef(scale,scale,scale);
       
   945     glDrawArrays(GL_TRIANGLE_STRIP,0,4);
       
   946     
       
   947     //glDisable(GL_BLEND);
       
   948     //glDisable(GL_DEPTH_TEST);
       
   949     
       
   950     glEnable(GL_TEXTURE_2D);
       
   951     glPopMatrix();
       
   952     }
       
   953 
       
   954 /*----------------------------------------------------------------------*/
       
   955 // Calculates widht and height with aspect ratio
       
   956 //
       
   957 void CDrawGrid::BubbleEffect(TInt& x, TInt& y, float& z)
       
   958     {
       
   959     if(iBubbleEffect)
       
   960         {
       
   961         // Selected image in coordinates x,y
       
   962         TInt selectedX = iContainer->GetCurrentIndex()/3 /*iGridSizeY*/;
       
   963         TInt selectedY =- (iContainer->GetCurrentIndex()%3 /*iGridSizeY*/);
       
   964                     
       
   965         // Distance to selected
       
   966         iDistanceX = selectedX-x;
       
   967         iDistanceY = selectedY-y;
       
   968         // Squared
       
   969         if(iDistanceX<0) iDistanceX*= -1;
       
   970         if(iDistanceY<0) iDistanceY*= -1;
       
   971         
       
   972         // Distance
       
   973         iDiff=iDistanceX+iDistanceY;
       
   974         // Convert distance to depth
       
   975         // http://en.wikipedia.org/wiki/Gaussian_function
       
   976         if (iDiff==0) z=5.0;
       
   977         if (iDiff==1) z=4.2;
       
   978         if (iDiff==2) z=3.4;
       
   979         if (iDiff==3) z=2.1;
       
   980         if (iDiff==4) z=1.3;
       
   981         if (iDiff==5) z=0.8;
       
   982         if (iDiff==6) z=0.4;
       
   983         if (iDiff==7) z=0.3;
       
   984         if (iDiff>7) z=0.3;
       
   985         //if (iDiff==8) z=0.1;
       
   986         //if (iDiff==9) z=0.05;
       
   987         //if (iDiff==10) z=0.01;
       
   988         //if (iDiff>10) z=0;
       
   989         }
       
   990     }
       
   991 
       
   992