uiacceltk/hitchcock/Client/src/alftexturegroup.cpp
changeset 0 15bf7259bb7c
child 3 d8a3531bc6b8
equal deleted inserted replaced
-1:000000000000 0:15bf7259bb7c
       
     1 /*
       
     2 * Copyright (c) 2006 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:   Group of textures.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "alf/alftexturegroup.h"
       
    22 #include "alf/alfenv.h"
       
    23 #include "alf/alftexture.h"
       
    24 
       
    25 
       
    26 NONSHARABLE_CLASS( CAlfInternalTextureLoadObserver ) : public CBase, public MAlfTextureLoadingCompletedObserver
       
    27     {
       
    28 public:
       
    29 
       
    30     /**
       
    31      * Static factory method. 
       
    32      * @param aEnv Environment
       
    33      */
       
    34     static CAlfInternalTextureLoadObserver* NewL(CAlfTextureGroup& aTextureGroup);
       
    35 
       
    36     /**
       
    37      * Static factory method. 
       
    38      * @param aEnv Environment
       
    39      */
       
    40     static CAlfInternalTextureLoadObserver* NewLC(CAlfTextureGroup& aTextureGroup);
       
    41 
       
    42    /**
       
    43      * Destructor
       
    44      */
       
    45     virtual ~CAlfInternalTextureLoadObserver();
       
    46 
       
    47    /**
       
    48      * From MAlfTextureLoadingCompletedObserver
       
    49      */
       
    50     void TextureLoadingCompleted(CAlfTexture& aTexture,
       
    51                                 TInt aTextureId,
       
    52                                 TInt aErrorCode);
       
    53 
       
    54 private:
       
    55 
       
    56    /**
       
    57      * Constructor.
       
    58      */
       
    59     CAlfInternalTextureLoadObserver();
       
    60     
       
    61    /**
       
    62      * Constructor.
       
    63      */
       
    64     void ConstructL(CAlfTextureGroup& aTextureGroup);
       
    65 
       
    66 private:
       
    67         
       
    68     CAlfTextureGroup* iTextureGroup; // Not owned   
       
    69     };
       
    70 
       
    71 // Private structure
       
    72 struct CAlfTextureGroup::TPrivateData
       
    73     {
       
    74     /** Environment */
       
    75     CAlfEnv* iEnv;                	
       
    76 
       
    77     /** Queue of texture group loading observers. */
       
    78     RPointerArray<MAlfTextureGroupLoadingCompletedObserver> iLoadObserverQueue;
       
    79 
       
    80     /** Queue of textures of this group. Textures are not owned by this class */
       
    81     RPointerArray<CAlfTexture> iTextures;
       
    82     
       
    83     /** Error */ 
       
    84     TInt iError;
       
    85     
       
    86     /** Flag to enable/disable observing notifications */
       
    87     TBool iObservingNotificationsEnabled;
       
    88     
       
    89     /** Observer for texture laodings */
       
    90     CAlfInternalTextureLoadObserver* iTextureLoadObserver;
       
    91     };
       
    92 
       
    93 
       
    94 
       
    95 
       
    96 // ======== MEMBER FUNCTIONS ========
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // Constructor
       
   100 // ---------------------------------------------------------------------------
       
   101 //
       
   102 EXPORT_C CAlfTextureGroup::CAlfTextureGroup() 
       
   103 	{
       
   104 		
       
   105 	}
       
   106 	
       
   107 // ---------------------------------------------------------------------------
       
   108 // Destructor
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 EXPORT_C CAlfTextureGroup::~CAlfTextureGroup()
       
   112 	{
       
   113     while (Count())
       
   114         {
       
   115         RemoveTexture(Texture(Count()-1));
       
   116         }
       
   117 
       
   118 	if ( iData )
       
   119 	    {
       
   120 	    iData->iLoadObserverQueue.Close();	
       
   121 	    iData->iTextures.Close();		    
       
   122 	    
       
   123 	    delete iData->iTextureLoadObserver;
       
   124 	    iData->iTextureLoadObserver = NULL;
       
   125 	    }	
       
   126 
       
   127 	delete iData;
       
   128 	}
       
   129 // ---------------------------------------------------------------------------
       
   130 // NewL
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 EXPORT_C CAlfTextureGroup* CAlfTextureGroup::NewL(CAlfEnv& aEnv)
       
   134 	{
       
   135 	CAlfTextureGroup* self = CAlfTextureGroup::NewLC(aEnv);        
       
   136     CleanupStack::Pop( self );
       
   137     return self;		
       
   138 	}
       
   139 	
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // NewLC
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 EXPORT_C CAlfTextureGroup* CAlfTextureGroup::NewLC(CAlfEnv& aEnv)
       
   146 	{
       
   147     CAlfTextureGroup* self = new( ELeave ) CAlfTextureGroup();
       
   148     CleanupStack::PushL( self );
       
   149     self->ConstructL(aEnv);
       
   150     return self;	
       
   151 	}
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 // 2nd phase constructor
       
   155 // ---------------------------------------------------------------------------
       
   156 //
       
   157 EXPORT_C void CAlfTextureGroup::ConstructL(CAlfEnv& aEnv)
       
   158 	{
       
   159     iData = new (ELeave) TPrivateData;    
       
   160     // Zero all data
       
   161     iData->iEnv = &aEnv;
       
   162     iData->iError = KErrNone;
       
   163     iData->iObservingNotificationsEnabled = ETrue;
       
   164     iData->iTextureLoadObserver = NULL;
       
   165     
       
   166     iData->iTextureLoadObserver = CAlfInternalTextureLoadObserver::NewL(*this);    
       
   167 	}
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // 
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 EXPORT_C void CAlfTextureGroup::AddTextureL(CAlfTexture& aTexture)
       
   174     {
       
   175     if (!IsTextureInGroup(aTexture))
       
   176         {            
       
   177         TBool addLoadObserver = ETrue;
       
   178         for(TInt i = 0; i < Count(); i++)
       
   179           	{
       
   180         	if (iData->iTextures[i]->TextureManager() == aTexture.TextureManager())
       
   181         		{
       
   182                 addLoadObserver = EFalse;
       
   183         		break;
       
   184         		}								                       
       
   185             }
       
   186 
       
   187         iData->iTextures.Append(&aTexture);
       
   188 
       
   189         if (addLoadObserver)
       
   190             {
       
   191             RegisterAsTextureLoadObserverL(aTexture.TextureManager());                        
       
   192             }
       
   193         }            
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // 
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 EXPORT_C void CAlfTextureGroup::RemoveTexture(CAlfTexture& aTexture)
       
   201     {
       
   202     if (IsTextureInGroup(aTexture))
       
   203         {            
       
   204         for(TInt i = 0; i < Count(); i++)
       
   205           	{
       
   206         	if (iData->iTextures[i] == &aTexture)
       
   207         		{
       
   208         		iData->iTextures.Remove(i);
       
   209         		iData->iTextures.Compress();
       
   210         		break;
       
   211         		}								                       
       
   212             }                
       
   213 
       
   214         TBool removeLoadObserver = ETrue;
       
   215         for(TInt i = 0; i < Count(); i++)
       
   216           	{
       
   217         	if (iData->iTextures[i]->TextureManager() == aTexture.TextureManager())
       
   218         		{
       
   219                 removeLoadObserver = EFalse;
       
   220         		break;
       
   221         		}								                       
       
   222             }
       
   223         
       
   224         if (removeLoadObserver)
       
   225             {
       
   226             UnRegisterAsTextureLoadObserver(aTexture.TextureManager());    
       
   227             }
       
   228         }
       
   229     }
       
   230 
       
   231 // ---------------------------------------------------------------------------
       
   232 // 
       
   233 // ---------------------------------------------------------------------------
       
   234 //
       
   235 EXPORT_C TBool CAlfTextureGroup::IsLoadingCompleted() const
       
   236     {
       
   237     for (TInt i=0; i<Count();i++)
       
   238         {
       
   239         if ((Texture(i).Size() == TSize(0,0)) || !Texture(i).HasContent())
       
   240             {
       
   241             return EFalse;    
       
   242             }
       
   243         }
       
   244     
       
   245     if (Count() == 0)
       
   246         {
       
   247         return EFalse;            
       
   248         }
       
   249         
       
   250     return ETrue;   
       
   251     }
       
   252 
       
   253 // ---------------------------------------------------------------------------
       
   254 // 
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 EXPORT_C TInt CAlfTextureGroup::Count() const
       
   258     {
       
   259     return iData->iTextures.Count();    
       
   260     }
       
   261     
       
   262 // ---------------------------------------------------------------------------
       
   263 // 
       
   264 // ---------------------------------------------------------------------------
       
   265 //
       
   266 EXPORT_C CAlfTexture& CAlfTextureGroup::Texture( TInt aIndex ) const
       
   267     {
       
   268     return *iData->iTextures[aIndex];    
       
   269     }
       
   270 
       
   271 // ---------------------------------------------------------------------------
       
   272 // 
       
   273 // ---------------------------------------------------------------------------
       
   274 //
       
   275 EXPORT_C void CAlfTextureGroup::AddLoadObserverL(MAlfTextureGroupLoadingCompletedObserver& aObserver)
       
   276     {
       
   277     iData->iLoadObserverQueue.Append(&aObserver);            
       
   278     }
       
   279 
       
   280 // ---------------------------------------------------------------------------
       
   281 // 
       
   282 // ---------------------------------------------------------------------------
       
   283 //
       
   284 EXPORT_C void CAlfTextureGroup::RemoveLoadObserver(MAlfTextureGroupLoadingCompletedObserver& aObserver)
       
   285     {
       
   286     TInt count = iData->iLoadObserverQueue.Count();
       
   287     for(TInt i = 0; i < count; i++)
       
   288       	{
       
   289     	if (iData->iLoadObserverQueue[i] == &aObserver)
       
   290     		{
       
   291     		iData->iLoadObserverQueue.Remove(i);
       
   292     		iData->iLoadObserverQueue.Compress();
       
   293     		break;
       
   294     		}								                       
       
   295         }                
       
   296     }
       
   297 
       
   298 // ---------------------------------------------------------------------------
       
   299 // 
       
   300 // ---------------------------------------------------------------------------
       
   301 //
       
   302 EXPORT_C void CAlfTextureGroup::EnableLoadObservers(TBool aEnable)
       
   303     {
       
   304     iData->iObservingNotificationsEnabled = aEnable;    
       
   305     }
       
   306 
       
   307 
       
   308 // ---------------------------------------------------------------------------
       
   309 // 
       
   310 // ---------------------------------------------------------------------------
       
   311 //
       
   312 void CAlfTextureGroup::NotifyTextureAvailability(CAlfTexture& aTexture,
       
   313                                 TInt aErrorCode)
       
   314     {
       
   315     if (IsTextureInGroup(aTexture))
       
   316         {
       
   317         if (aErrorCode != KErrNone)
       
   318             {
       
   319             iData->iError = aErrorCode;    
       
   320             }
       
   321         
       
   322         if (IsLoadingCompleted() && iData->iObservingNotificationsEnabled)
       
   323             {
       
   324             NotifyObservers();    
       
   325             }            
       
   326         }            
       
   327     }
       
   328       
       
   329 // ---------------------------------------------------------------------------
       
   330 // 
       
   331 // ---------------------------------------------------------------------------
       
   332 //
       
   333 TBool CAlfTextureGroup::IsTextureInGroup(CAlfTexture& aTexture) const
       
   334     {
       
   335     TInt count = iData->iTextures.Count();
       
   336     for(TInt i = 0; i < Count(); i++)
       
   337       	{
       
   338     	if (iData->iTextures[i] == &aTexture)
       
   339     		{
       
   340     		return ETrue;
       
   341     		}								                       
       
   342         }                
       
   343     return EFalse;    
       
   344     }
       
   345     
       
   346 
       
   347 // ---------------------------------------------------------------------------
       
   348 // 
       
   349 // ---------------------------------------------------------------------------
       
   350 //
       
   351 void CAlfTextureGroup::NotifyObservers()
       
   352     {
       
   353     TInt count = iData->iLoadObserverQueue.Count();
       
   354     for(TInt i = 0; i < count; i++)
       
   355       	{
       
   356     	iData->iLoadObserverQueue[i]->TextureGroupLoadingCompleted(*this,iData->iError);
       
   357         }                        
       
   358     }
       
   359     
       
   360 // ---------------------------------------------------------------------------
       
   361 // 
       
   362 // ---------------------------------------------------------------------------
       
   363 //
       
   364 void CAlfTextureGroup::RegisterAsTextureLoadObserverL(CAlfTextureManager* aManager)
       
   365     {
       
   366     if (aManager)
       
   367         {
       
   368         aManager->AddLoadObserverL(iData->iTextureLoadObserver);    
       
   369         }        
       
   370     }
       
   371     
       
   372 // ---------------------------------------------------------------------------
       
   373 // 
       
   374 // ---------------------------------------------------------------------------
       
   375 //
       
   376 void CAlfTextureGroup::UnRegisterAsTextureLoadObserver(CAlfTextureManager* aManager)
       
   377     {
       
   378     if (aManager)
       
   379         {
       
   380         aManager->RemoveLoadObserver(iData->iTextureLoadObserver);    
       
   381         }                
       
   382     }
       
   383     
       
   384 
       
   385 
       
   386 // ---------------------------------------------------------------------------
       
   387 // 
       
   388 // ---------------------------------------------------------------------------
       
   389 //
       
   390 CAlfInternalTextureLoadObserver* CAlfInternalTextureLoadObserver::NewL(CAlfTextureGroup& aTextureGroup)
       
   391     {
       
   392 	CAlfInternalTextureLoadObserver* self = CAlfInternalTextureLoadObserver::NewLC(aTextureGroup);        
       
   393     CleanupStack::Pop( self );
       
   394     return self;		        
       
   395     }
       
   396 
       
   397 // ---------------------------------------------------------------------------
       
   398 // 
       
   399 // ---------------------------------------------------------------------------
       
   400 //
       
   401 CAlfInternalTextureLoadObserver* CAlfInternalTextureLoadObserver::NewLC(CAlfTextureGroup& aTextureGroup)
       
   402     {
       
   403     CAlfInternalTextureLoadObserver* self = new( ELeave ) CAlfInternalTextureLoadObserver();
       
   404     CleanupStack::PushL( self );
       
   405     self->ConstructL(aTextureGroup);
       
   406     return self;	            
       
   407     }
       
   408 
       
   409 // ---------------------------------------------------------------------------
       
   410 // 
       
   411 // ---------------------------------------------------------------------------
       
   412 //
       
   413 CAlfInternalTextureLoadObserver::~CAlfInternalTextureLoadObserver()
       
   414     {        
       
   415     }
       
   416 
       
   417 // ---------------------------------------------------------------------------
       
   418 // 
       
   419 // ---------------------------------------------------------------------------
       
   420 //
       
   421 CAlfInternalTextureLoadObserver::CAlfInternalTextureLoadObserver()
       
   422     {        
       
   423     }
       
   424     
       
   425 // ---------------------------------------------------------------------------
       
   426 // 
       
   427 // ---------------------------------------------------------------------------
       
   428 //
       
   429 void CAlfInternalTextureLoadObserver::ConstructL(CAlfTextureGroup& aTextureGroup)
       
   430     {
       
   431     iTextureGroup = &aTextureGroup;    
       
   432     }
       
   433 
       
   434 // ---------------------------------------------------------------------------
       
   435 // 
       
   436 // ---------------------------------------------------------------------------
       
   437 //
       
   438 void CAlfInternalTextureLoadObserver::TextureLoadingCompleted(CAlfTexture& aTexture,
       
   439                                 TInt /*aTextureId*/,
       
   440                                 TInt aErrorCode)
       
   441     {
       
   442     iTextureGroup->NotifyTextureAvailability(aTexture, aErrorCode);        
       
   443     }
       
   444