widgetmodel/alfwidgetmodel/src/alfmeshvisualattributesetter.cpp
branchRCL_3
changeset 25 4ea6f81c838a
parent 23 514d98f21c43
child 26 0e9bb658ef58
equal deleted inserted replaced
23:514d98f21c43 25:4ea6f81c838a
     1 /*
       
     2 * Copyright (c) 2007 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:   Implements attributesetters for MeshVisual.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "alf/alfattribute.h"
       
    21 #include "alf/alfmeshvisualattributesetter.h"
       
    22 #include "alf/alfattributevaluetype.h"
       
    23 #include <alf/alfvisual.h>
       
    24 #include <alf/alfattributeexception.h>
       
    25 #include <alf/alfvisualexception.h>
       
    26 #include "alf/alfattributecontainer.h"
       
    27 #include <libc/string.h>
       
    28 #include <osn/ustring.h>
       
    29 #include "alf/attrproperty.h"
       
    30 #include <uiacceltk/HuiRealPoint.h>
       
    31 
       
    32 #include <alf/alfmeshvisual.h>
       
    33 #include <alf/alftexture.h>
       
    34 #include <utf.h>
       
    35 #include <alf/alfenv.h>
       
    36 #include <alf/alfdataexception.h>
       
    37 #include <alf/alfdisplay.h>
       
    38 
       
    39 #include <alf/alfbrusharray.h>
       
    40 #include <alf/alfframebrush.h>
       
    41 
       
    42 
       
    43 #include <alf/alfeventhandler.h>
       
    44 #include <alf/alfevent.h>
       
    45 #include <osn/alfptrvector.h>
       
    46 
       
    47 
       
    48 using namespace osncore;
       
    49 
       
    50 using namespace duiuimodel::meshvisualattributes;
       
    51 using namespace duiuimodel::commonvisualattributes;
       
    52 
       
    53 namespace Alf
       
    54     {
       
    55 
       
    56 class AlfMeshVisualAttributeSetterImpl
       
    57     {
       
    58 public:
       
    59     AlfMeshVisualAttributeSetterImpl();
       
    60     ~AlfMeshVisualAttributeSetterImpl();
       
    61 public:
       
    62     //Keep track of loaded texture for imagevisuals.
       
    63     //Texture manager doesn't unload the texture from memory untill Env is deleted.
       
    64     //Hence need to unload them once attribute setter is deleted.
       
    65     AlfPtrVector<CAlfTexture> mLoadedTextures;
       
    66     //Env needed to access TextureManager while unloading textures.
       
    67     CAlfEnv* mEnv;
       
    68     };
       
    69 
       
    70 AlfMeshVisualAttributeSetterImpl::AlfMeshVisualAttributeSetterImpl()
       
    71     {
       
    72     //Do not delete textures here as they may be in use by the visuals that have not been deleted.
       
    73     mEnv = CAlfEnv::Static();
       
    74     mLoadedTextures.setAutoDelete(false);
       
    75     }
       
    76 
       
    77 AlfMeshVisualAttributeSetterImpl::~AlfMeshVisualAttributeSetterImpl()
       
    78     {
       
    79     //Unload all loaded textures created by this attributesetter to free up the memory.
       
    80     for (int i =0;i<mLoadedTextures.count();i++)
       
    81         {
       
    82         CAlfTexture* Texture = mLoadedTextures[i];
       
    83         mEnv->TextureManager().UnloadTexture(Texture->Id());
       
    84         }
       
    85     mLoadedTextures.clear();
       
    86     }
       
    87 
       
    88 // ======== MEMBER FUNCTIONS ========
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // Constructor.
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 OSN_EXPORT AlfMeshVisualAttributeSetter::AlfMeshVisualAttributeSetter()
       
    95     {
       
    96     mImpl.reset(new (EMM) AlfMeshVisualAttributeSetterImpl());
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // Destructor.
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 OSN_EXPORT AlfMeshVisualAttributeSetter::~AlfMeshVisualAttributeSetter()
       
   104     {
       
   105     }
       
   106 
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // ---------------------------------------------------------------------------
       
   110 //
       
   111 IAlfVariantType* AlfMeshVisualAttributeSetter::getData(AlfAttribute& aAttr, IAlfMap* aData)
       
   112     {
       
   113     IAlfVariantType* data = 0;
       
   114     const char* dataField = aAttr.getDataField();
       
   115     if (dataField)
       
   116         {
       
   117         data = aData->item(UString(dataField));
       
   118         }
       
   119     return data;
       
   120     }
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // ---------------------------------------------------------------------------
       
   124 //
       
   125 int AlfMeshVisualAttributeSetter::getTime(const AlfAttribute& aAttr, bool aImmediate)
       
   126     {
       
   127     int time = 0;
       
   128     if (!aImmediate)
       
   129         {
       
   130         time = aAttr.getTime();
       
   131         }
       
   132 
       
   133     return time;
       
   134     }
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // ---------------------------------------------------------------------------
       
   138 //
       
   139 
       
   140 void AlfMeshVisualAttributeSetter::handleDynamicDataAttribute(CAlfVisual & aVisual,
       
   141         AlfAttribute& aAttr, AlfAttributeContainer& aContainer, IAlfMap* aData)
       
   142     {
       
   143     if (!doHandleDynamicDataAttribute(aVisual, aAttr, aContainer, aData))
       
   144         {
       
   145         AlfCommonVisualAttributeSetter::handleDynamicDataAttribute(
       
   146             aVisual, aAttr, aContainer, aData);
       
   147         }
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------------------------
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 void AlfMeshVisualAttributeSetter::handleStaticDataAttribute(
       
   154     CAlfVisual &aVisual, AlfAttribute& aAttr,
       
   155     AlfAttributeContainer& aContainer, IAlfMap* aData)
       
   156     {
       
   157     CAlfMeshVisual* meshVisual = dynamic_cast<CAlfMeshVisual*>(&aVisual);
       
   158     if (!meshVisual)
       
   159         {
       
   160         ALF_THROW ( AlfDataException, ECommonError, "AlfMeshVisualAttributeSetter" )
       
   161         }
       
   162     if (!aData)  ALF_THROW ( AlfDataException, EInvalidVariantDataType, "AlfMeshVisualAttributeSetter" )
       
   163 
       
   164         const char* dataField = aAttr.getDataField();
       
   165     if ( !dataField )  ALF_THROW ( AlfDataException, EInvalidAttribute, "AlfMeshVisualAttributeSetter" )
       
   166 
       
   167         IAlfVariantType* data = aData->item ( UString(dataField));
       
   168 
       
   169     const char* attrName = aAttr.name();
       
   170 
       
   171     if (data)
       
   172         {
       
   173         if (!strcmp(attrName,KMeshType))
       
   174             {
       
   175             if (data->type() == IAlfVariantType::EInt)
       
   176                 {
       
   177                 int val = data->integer();
       
   178                 TRAPD(err,meshVisual->CreateMeshL(val));
       
   179                 if (err!=KErrNone)
       
   180                     {
       
   181                     ALF_THROW ( AlfDataException, err, "AlfMeshVisualAttributeSetter")
       
   182                     }
       
   183                 }
       
   184             }
       
   185         else if (!strcmp(attrName, KImagePath))
       
   186             {
       
   187             if (data->type() == IAlfVariantType::EString)
       
   188                 {
       
   189                 TPtrC8 src;
       
   190                 src.Set((TUint8*)data->string().getUtf8());
       
   191 
       
   192                 HandleImagePathAttribute( src, meshVisual, primary );
       
   193                 }
       
   194             }
       
   195         else if (!strcmp(attrName,KSecondaryImagePath))
       
   196             {
       
   197             if (data->type() == IAlfVariantType::EString)
       
   198                 {
       
   199                 TPtrC8 src;
       
   200                 src.Set((TUint8*)data->string().getUtf8());
       
   201 
       
   202                 HandleImagePathAttribute( src, meshVisual, secondary );
       
   203                 }
       
   204             }
       
   205         else if (!strcmp(attrName,KSpecularImagePath))
       
   206             {
       
   207             if (data->type() == IAlfVariantType::EString)
       
   208                 {
       
   209                 TPtrC8 src;
       
   210                 src.Set((TUint8*)data->string().getUtf8());
       
   211 
       
   212                 HandleImagePathAttribute( src, meshVisual, specular );
       
   213                 }
       
   214             }
       
   215         else
       
   216             {
       
   217             //check, if dynamic attribute(but set by user as static), if so, set attributes immediately.
       
   218             //If not, the function will call baseclass SetAttributeValue.
       
   219             if (!doHandleDynamicDataAttribute(aVisual, aAttr, aContainer, aData, true))
       
   220                 {
       
   221                 AlfCommonVisualAttributeSetter::handleStaticDataAttribute(
       
   222                     aVisual, aAttr, aContainer, aData);
       
   223                 }
       
   224             }
       
   225         }
       
   226     }
       
   227 
       
   228 // ---------------------------------------------------------------------------
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 void AlfMeshVisualAttributeSetter::handleDynamicAttribute(CAlfVisual &aVisual,
       
   232         AlfAttribute& aAttr, AlfAttributeContainer& aContainer)
       
   233     {
       
   234     if (!doHandleDynamicAttribute(aVisual, aAttr, aContainer))
       
   235         {
       
   236         AlfCommonVisualAttributeSetter::handleDynamicAttribute(
       
   237             aVisual, aAttr, aContainer);
       
   238         }
       
   239     }
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 void AlfMeshVisualAttributeSetter::handleStaticAttribute(
       
   245     CAlfVisual& aVisual, AlfAttribute& aAttr, AlfAttributeContainer& aContainer)
       
   246     {
       
   247     CAlfMeshVisual* meshVisual = dynamic_cast<CAlfMeshVisual*>(&aVisual);
       
   248     if(!meshVisual)
       
   249         {
       
   250         return;
       
   251         }
       
   252     const char* attrName = aAttr.name();
       
   253 
       
   254     // meshVisual attributes
       
   255     if (!strcmp(attrName,KMeshType))
       
   256         {
       
   257         int val = aAttr.intValue();
       
   258         TRAPD(err,meshVisual->CreateMeshL(val));
       
   259         if (err!=KErrNone)
       
   260             {
       
   261             ALF_THROW(AlfDataException, err, "AlfMeshVisualAttributeSetter");
       
   262             }
       
   263         }
       
   264     else if (!strcmp(attrName,KImagePath))
       
   265         {
       
   266         TPtrC8 src((TUint8*)aAttr.stringValue().getUtf8());        
       
   267         HandleImagePathAttribute( src, meshVisual, primary );
       
   268         }
       
   269     else if (!strcmp(attrName,KSecondaryImagePath))
       
   270         {
       
   271         TPtrC8 src((TUint8*)aAttr.stringValue().getUtf8());        
       
   272         HandleImagePathAttribute( src, meshVisual, secondary );
       
   273         }
       
   274     else if (!strcmp(attrName,KSpecularImagePath))
       
   275         {
       
   276         TPtrC8 src((TUint8*)aAttr.stringValue().getUtf8());        
       
   277         HandleImagePathAttribute( src, meshVisual, specular );
       
   278         }
       
   279     else
       
   280         {
       
   281         if (!doHandleDynamicAttribute(aVisual, aAttr, aContainer, true))
       
   282             {
       
   283             AlfCommonVisualAttributeSetter::handleStaticAttribute(aVisual, aAttr, aContainer);
       
   284             }
       
   285         }
       
   286     }
       
   287 
       
   288 
       
   289 // ---------------------------------------------------------------------------
       
   290 // ---------------------------------------------------------------------------
       
   291 //
       
   292 OSN_EXPORT void AlfMeshVisualAttributeSetter::setAttributeValue(
       
   293     CAlfVisual &aVisual,
       
   294     AlfAttributeContainer* aContainer,
       
   295     IAlfMap* aData)
       
   296     {
       
   297     CAlfMeshVisual* meshVisual = dynamic_cast<CAlfMeshVisual*>(&aVisual);
       
   298     if (!meshVisual)
       
   299         {
       
   300         ALF_THROW ( AlfVisualException, EInvalidVisual, "AlfMeshVisualAttributeSetter")
       
   301         }
       
   302 
       
   303     AlfCommonVisualAttributeSetter::setAttributeValue(
       
   304         aVisual, aContainer, aData);
       
   305     }
       
   306 
       
   307 // ---------------------------------------------------------------------------
       
   308 // ---------------------------------------------------------------------------
       
   309 //
       
   310 OSN_EXPORT TAlfCommand* AlfMeshVisualAttributeSetter::createCommand(
       
   311     CAlfVisual& /*aVisual*/, AlfAttributeContainer* /*aContainer*/,
       
   312     IAlfMap* /*aData*/, int /*aTransitionTime*/, CAlfVisual* /*aRefVisual*/)
       
   313     {
       
   314     TAlfCommand* cmd = 0;
       
   315     return cmd;
       
   316     }
       
   317 
       
   318 // ---------------------------------------------------------------------------
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 OSN_EXPORT void AlfMeshVisualAttributeSetter::createAndSendCommands(
       
   322     CAlfVisual& aVisual,
       
   323     AlfAttributeContainer* aContainer,
       
   324     CAlfVisual* aRefVisual )
       
   325     {
       
   326 
       
   327     // Call the base class implementation.It goes through its loop and checks if there are attributes
       
   328     // that it handles.
       
   329     AlfCommonVisualAttributeSetter::createAndSendCommands(
       
   330         aVisual,
       
   331         aContainer,
       
   332         aRefVisual );
       
   333     }
       
   334 
       
   335 // ---------------------------------------------------------------------------
       
   336 // ---------------------------------------------------------------------------
       
   337 //
       
   338 
       
   339 bool AlfMeshVisualAttributeSetter::doHandleDynamicDataAttribute(CAlfVisual & aVisual,
       
   340         AlfAttribute& aAttr, AlfAttributeContainer& /*aContainer*/, IAlfMap* aData, bool aImmediate)
       
   341     {
       
   342     bool handled = true;
       
   343 
       
   344     if (!aData)
       
   345         {
       
   346         ALF_THROW ( AlfDataException, EInvalidVariantDataType, "AlfMeshVisualAttributeSetter" )
       
   347         }
       
   348     else
       
   349         {
       
   350         CAlfMeshVisual* meshVisual = dynamic_cast<CAlfMeshVisual*>(&aVisual);
       
   351         if(!meshVisual)
       
   352             {
       
   353             return false;
       
   354             }
       
   355         const char* attrName = aAttr.name();
       
   356 
       
   357         if ( !strcmp( attrName, KSecondaryAlpha ) )
       
   358             {
       
   359             const IAlfVariantType* data = getData(aAttr, aData);
       
   360             if (data && data->type() == IAlfVariantType::EReal)
       
   361                 {
       
   362                 TAlfTimedValue tVal(data->real(), getTime(aAttr, aImmediate));
       
   363                 tVal.SetStyle(aAttr.getInterpolationStyle());
       
   364                 tVal.SetMappingFunctionIdentifier( aAttr.getMappingFunctionId() );
       
   365                 meshVisual->SetSecondaryAlpha( tVal );
       
   366                 }
       
   367             }
       
   368         else if ( !strcmp( attrName, KYawAngle ) )
       
   369             {
       
   370             const IAlfVariantType* data = getData(aAttr, aData);
       
   371             if (data && data->type() == IAlfVariantType::EReal)
       
   372                 {
       
   373                 TAlfTimedValue tVal(data->real(), getTime(aAttr, aImmediate));
       
   374                 tVal.SetStyle(aAttr.getInterpolationStyle());
       
   375                 tVal.SetMappingFunctionIdentifier( aAttr.getMappingFunctionId() );
       
   376                 meshVisual->SetYawAngle( tVal );
       
   377                 }
       
   378             }
       
   379         else if ( !strcmp( attrName, KPitchAngle ) )
       
   380             {
       
   381             const IAlfVariantType* data = getData(aAttr, aData);
       
   382             if (data && data->type() == IAlfVariantType::EReal)
       
   383                 {
       
   384                 TAlfTimedValue tVal(data->real(), getTime(aAttr, aImmediate));
       
   385                 tVal.SetStyle(aAttr.getInterpolationStyle());
       
   386                 tVal.SetMappingFunctionIdentifier( aAttr.getMappingFunctionId() );
       
   387                 meshVisual->SetPitchAngle( tVal );
       
   388                 }
       
   389             }
       
   390         else if ( !strcmp( attrName, KScale ) )
       
   391             {
       
   392             const IAlfVariantType* data = getData(aAttr, aData);
       
   393             if (data && data->type() == IAlfVariantType::EReal)
       
   394                 {
       
   395                 TAlfTimedValue tVal(data->real(), getTime(aAttr, aImmediate));
       
   396                 tVal.SetStyle(aAttr.getInterpolationStyle());
       
   397                 tVal.SetMappingFunctionIdentifier( aAttr.getMappingFunctionId() );
       
   398                 meshVisual->SetScale( tVal );
       
   399                 }
       
   400             }
       
   401         else
       
   402             {
       
   403             handled = false;
       
   404             }
       
   405         }
       
   406 
       
   407     return handled;
       
   408     }
       
   409 
       
   410 // -----------------------------------------------------------------------------
       
   411 // -----------------------------------------------------------------------------
       
   412 //
       
   413 bool AlfMeshVisualAttributeSetter::doHandleDynamicAttribute(
       
   414     CAlfVisual &aVisual, AlfAttribute& aAttr,
       
   415     AlfAttributeContainer& /*aContainer*/, bool aImmediate)
       
   416     {
       
   417     CAlfMeshVisual* meshVisual = dynamic_cast<CAlfMeshVisual*>(&aVisual);
       
   418     if(!meshVisual)
       
   419         {
       
   420         return false;
       
   421         }
       
   422 
       
   423     const char* attrName = aAttr.name();
       
   424     bool handled = true;
       
   425 
       
   426     // KSecondaryAlpha
       
   427     if (!strcmp(attrName, KSecondaryAlpha))
       
   428         {
       
   429         TAlfTimedValue tVal(aAttr.getSourceValue()->realValue());
       
   430         tVal.SetTarget(aAttr.getTargetValue()->realValue(),getTime(aAttr, aImmediate));
       
   431         tVal.SetStyle( aAttr.getInterpolationStyle() );
       
   432         tVal.SetMappingFunctionIdentifier( aAttr.getMappingFunctionId() );
       
   433         meshVisual->SetSecondaryAlpha( tVal );
       
   434         }
       
   435     //KYawAngle
       
   436     else if (!strcmp(attrName, KYawAngle))
       
   437         {
       
   438         TAlfTimedValue tVal((TReal32)aAttr.getSourceValue()->realValue());
       
   439         tVal.SetTarget((TReal32)aAttr.getTargetValue()->realValue(),getTime(aAttr, aImmediate));
       
   440         tVal.SetStyle( aAttr.getInterpolationStyle() );
       
   441         tVal.SetMappingFunctionIdentifier( aAttr.getMappingFunctionId() );
       
   442         meshVisual->SetYawAngle( tVal );
       
   443         }
       
   444     //KPitchAngle
       
   445     else if (!strcmp(attrName, KPitchAngle))
       
   446         {
       
   447         TAlfTimedValue tVal((TReal32)aAttr.getSourceValue()->realValue());
       
   448         tVal.SetTarget((TReal32)aAttr.getTargetValue()->realValue(),getTime(aAttr, aImmediate));
       
   449         tVal.SetStyle( aAttr.getInterpolationStyle() );
       
   450         tVal.SetMappingFunctionIdentifier( aAttr.getMappingFunctionId() );
       
   451         meshVisual->SetPitchAngle( tVal );
       
   452         }
       
   453     //KScale
       
   454     else if (!strcmp(attrName,KScale))
       
   455         {
       
   456         TAlfTimedValue tVal((TReal32)aAttr.getSourceValue()->realValue());
       
   457         tVal.SetTarget((TReal32)aAttr.getTargetValue()->realValue(),getTime(aAttr, aImmediate));
       
   458         tVal.SetStyle( aAttr.getInterpolationStyle() );
       
   459         tVal.SetMappingFunctionIdentifier( aAttr.getMappingFunctionId());
       
   460         meshVisual->SetScale( tVal );
       
   461         }
       
   462     else // Call the base class implementation also.
       
   463         //It goes through the same loop again and checks if there are attributes that it handles.
       
   464         {
       
   465         handled = false;
       
   466         }
       
   467 
       
   468     return handled;
       
   469     }
       
   470 
       
   471 // -----------------------------------------------------------------------------
       
   472 // -----------------------------------------------------------------------------
       
   473 //
       
   474 void AlfMeshVisualAttributeSetter::HandleImagePathAttribute(
       
   475     const TPtrC8& aStringValue,
       
   476     CAlfMeshVisual* aVisual,
       
   477     imageCategory aImageCategory )
       
   478     {
       
   479     auto_ptr<HBufC> DesC;
       
   480     TRAPD( err, DesC.reset(CnvUtfConverter::ConvertToUnicodeFromUtf8L(aStringValue)));
       
   481     if ( err != KErrNone )
       
   482         {
       
   483         ALF_THROW( AlfDataException, err, "AlfMeshVisualAttributeSetter" );
       
   484         }
       
   485 
       
   486     // check if the texture is already loaded
       
   487     const TInt existingTextureId = aVisual->Env().TextureManager().TextureId(*(DesC.get()));
       
   488     if ( existingTextureId != KErrNotFound )
       
   489         {
       
   490         const CAlfTexture* texture = aVisual->Env().TextureManager().Texture( existingTextureId );
       
   491 
       
   492         if ( aImageCategory == primary )
       
   493             {
       
   494             aVisual->SetImage(TAlfImage(*texture));
       
   495             }
       
   496         else if ( aImageCategory == secondary )
       
   497             {
       
   498             aVisual->SetSecondaryImage(TAlfImage(*texture));
       
   499             }
       
   500         else // specular image
       
   501             {
       
   502             aVisual->SetSpecularImage(TAlfImage(*texture));
       
   503             }
       
   504         }
       
   505     else
       
   506         {
       
   507         mImpl->mLoadedTextures.resize(mImpl->mLoadedTextures.count()+1);
       
   508 
       
   509         CAlfTexture* texture = NULL;
       
   510         TRAP( err, texture = &(aVisual->Env().TextureManager().LoadTextureL(
       
   511             *(DesC.get()), EAlfTextureFlagDefault, KAlfAutoGeneratedTextureId)));
       
   512 
       
   513         if ( err != KErrNone )
       
   514             {
       
   515             ALF_THROW( AlfDataException, err, "AlfMeshVisualAttributeSetter" );
       
   516             }
       
   517 
       
   518         // This cannot fail because the array has already been resized
       
   519         mImpl->mLoadedTextures.insert(mImpl->mLoadedTextures.count(),texture);
       
   520 
       
   521         if ( aImageCategory == primary )
       
   522             {
       
   523             aVisual->SetImage(TAlfImage(*texture));
       
   524             }
       
   525         else if ( aImageCategory == secondary )
       
   526             {
       
   527             aVisual->SetSecondaryImage(TAlfImage(*texture));
       
   528             }
       
   529         else // specular image
       
   530             {
       
   531             aVisual->SetSpecularImage(TAlfImage(*texture));
       
   532             }
       
   533         }
       
   534     }
       
   535 
       
   536 
       
   537     }// Alf