uiacceltk/hitchcock/coretoolkit/src/huidropshadow.cpp
changeset 0 15bf7259bb7c
equal deleted inserted replaced
-1:000000000000 0:15bf7259bb7c
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Drop shadow handler
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "uiacceltk/huidropshadow.h"
       
    21 #include "uiacceltk/HuiUtil.h"
       
    22 #include "uiacceltk/HuiTextureProcessor.h"
       
    23 #include "uiacceltk/HuiTextureManager.h"
       
    24 #include "uiacceltk/HuiEnv.h"
       
    25 #include <AknsUtils.h>
       
    26 #include <e32math.h>
       
    27 
       
    28 // internal flags
       
    29 enum
       
    30     {
       
    31     EHuiDropShadowChanged           = 0x01,
       
    32     EHuiDropShadowColorValid        = 0x02
       
    33     };
       
    34 
       
    35 // ======== LOCAL FUNCTIONS ========
       
    36 
       
    37 NONSHARABLE_STRUCT( CHuiDropShadow::THuiDropShadowData )
       
    38     {
       
    39     THuiDropShadowData():
       
    40         iColor(KRgbBlack), 
       
    41         iColorSkinId(), 
       
    42         iColorIndex(KErrNotFound), 
       
    43         //iColorValid(EFalse),
       
    44         //iChanged( EFalse )
       
    45         iFlags(0)
       
    46             {
       
    47             }
       
    48     
       
    49     mutable TRgb iColor;
       
    50     TAknsItemID iColorSkinId;
       
    51     TInt iColorIndex;
       
    52     //mutable TBool iColorValid;
       
    53     //TBool iChanged;
       
    54     TUint8 iFlags;
       
    55     };
       
    56 
       
    57 // ======== MEMBER FUNCTIONS ========
       
    58 
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // Default constructor
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 CHuiDropShadow::CHuiDropShadow() : 
       
    65     iOpacity(0.6f), 
       
    66     iOffset( 2.f, 2.f ),
       
    67     iOffsetUnit( EHuiUnitPixel ), // magnitude is in iTimedOffset
       
    68     iRadius( 1.5f ),
       
    69     iScale( 1.f  )
       
    70     {
       
    71     }
       
    72 
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // 2nd phase constructor
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 void CHuiDropShadow::ConstructL()
       
    79     {
       
    80     iData = new (ELeave) THuiDropShadowData;
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // Destructor
       
    85 // --------------------------------------------------------------------------
       
    86 //
       
    87 CHuiDropShadow::~CHuiDropShadow()
       
    88     {
       
    89     if ( iData )
       
    90         {
       
    91         }
       
    92     delete iData;
       
    93     }
       
    94    
       
    95 // ---------------------------------------------------------------------------
       
    96 // Sets the shadow offset in polar coorsinates
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 EXPORT_C void CHuiDropShadow::SetOffset( 
       
   100     TReal32 aAngle, 
       
   101     THuiMetric aDistance, 
       
   102     TInt aTransitionTime ) __SOFTFP
       
   103     {
       
   104     if ( iOffsetUnit != aDistance.iUnit && aTransitionTime )
       
   105         {
       
   106         // @todo: convert current timed value into new unit
       
   107         }
       
   108     
       
   109     // it is not that efficient to use floating point calculations here
       
   110     // but this is not called often.
       
   111     
       
   112     TReal x;
       
   113     Math::Cos( x, 2.0*KPi*aAngle/360.0 );
       
   114     x*=aDistance.iMagnitude;
       
   115     
       
   116     
       
   117     TReal y;
       
   118     Math::Sin( y, 2.0*KPi*aAngle/360.0  );
       
   119     y*=-1.0*aDistance.iMagnitude;
       
   120     
       
   121     iOffset.Set( THuiRealPoint(x,y), aTransitionTime );
       
   122     iOffsetUnit = aDistance.iUnit;
       
   123     }
       
   124 
       
   125 // ---------------------------------------------------------------------------
       
   126 // Sets color in RGB
       
   127 // ---------------------------------------------------------------------------
       
   128 //        
       
   129 EXPORT_C void CHuiDropShadow::SetColor(
       
   130     const TRgb& aColor, 
       
   131     TInt /*aTransitionTime*/)
       
   132     {
       
   133     iData->iColor = aColor;
       
   134     iData->iColorIndex = KErrNotFound;
       
   135     iData->iFlags |= EHuiDropShadowChanged;
       
   136     }
       
   137 
       
   138 // ---------------------------------------------------------------------------
       
   139 // Sets color in S60 skin
       
   140 // ---------------------------------------------------------------------------
       
   141 //
       
   142 EXPORT_C void CHuiDropShadow::SetColor(
       
   143     const TAknsItemID& aID,
       
   144     const TInt aIndex, 
       
   145     TInt /*aTransitionTime*/)
       
   146     {
       
   147     iData->iColorSkinId = aID;
       
   148     iData->iColorIndex = aIndex;
       
   149     iData->iFlags &= ~EHuiDropShadowColorValid;
       
   150     iData->iFlags |= EHuiDropShadowChanged;
       
   151     }
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 // Returns the current color RGB value
       
   155 // ---------------------------------------------------------------------------
       
   156 // 
       
   157 TRgb CHuiDropShadow::Color() const
       
   158     {
       
   159     if ( iData->iColorIndex != KErrNotFound && !(iData->iFlags&EHuiDropShadowColorValid) )
       
   160         {
       
   161         CHuiStatic::GetCachedColor(
       
   162                 iData->iColor, 
       
   163                 iData->iColorSkinId, 
       
   164                 iData->iColorIndex);
       
   165     
       
   166         iData->iFlags |= EHuiDropShadowColorValid; // What about skins change..
       
   167         }
       
   168     return iData->iColor;
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // Returns ETrue if the drops shadow parateters have changed.
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 TBool CHuiDropShadow::Changed() const
       
   176     {
       
   177     if ( iData->iFlags&EHuiDropShadowChanged )
       
   178         {
       
   179         return ETrue;
       
   180         }
       
   181     return iOpacity.Changed() || iOffset.Changed() || iRadius.Changed() || iScale.Changed();
       
   182     }
       
   183 
       
   184 // ---------------------------------------------------------------------------
       
   185 // Clears all changed flags
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 void CHuiDropShadow::ClearChanged()
       
   189     {
       
   190     iData->iFlags &= ~EHuiDropShadowChanged;    
       
   191     iOpacity.ClearChanged();
       
   192     iOffset.ClearChanged();
       
   193     iRadius.ClearChanged();
       
   194     iScale.ClearChanged();
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // Calculates the drawging size of the shadow.
       
   199 // ---------------------------------------------------------------------------
       
   200 //    
       
   201 THuiRealSize CHuiDropShadow::ShadowDrawingSize( 
       
   202     const TSize& aImageDrawingSize, 
       
   203     const TSize& aShadowTextureSize ) const
       
   204     {
       
   205     // Shadow texture is usually smaller than the image texture in which it is based on.
       
   206     // Also, the content of the shadow texture is shrank by the blur dimension
       
   207     // from all the sides.
       
   208     
       
   209     // round the blur dimension already because the blur filter does it as well.
       
   210     const TInt blurDimension = HUI_ROUND_FLOAT_TO_INT( 2*iRadius.Now() );
       
   211     
       
   212     // the content area of the texture is smaller than the the texture area
       
   213     THuiRealSize shadowContentSize = aShadowTextureSize;
       
   214     shadowContentSize.iWidth -= 2.f*blurDimension;
       
   215     shadowContentSize.iHeight -= 2.f*blurDimension;
       
   216     
       
   217     // check the relation between the shadow content area and the actial drawed image
       
   218     // check possible divide-by-xero case
       
   219     // The default value (1) is arbitrary. It only is there to allow prevention of divide_by_zero
       
   220     TReal32 xMultiplier = 1;
       
   221     if ( shadowContentSize.iWidth != 0 )
       
   222         {
       
   223         xMultiplier = ((TReal32)(aImageDrawingSize.iWidth)) / ((TReal32)(shadowContentSize.iWidth));
       
   224         }
       
   225      
       
   226     TReal32 yMultiplier = 1;
       
   227     if ( shadowContentSize.iHeight != 0 )
       
   228         {
       
   229         yMultiplier = ((TReal32)(aImageDrawingSize.iHeight)) / ((TReal32)(shadowContentSize.iHeight));
       
   230         }
       
   231     
       
   232     // apply the multiplier to the shadow texture
       
   233     THuiRealSize shadowDrawingSize = aShadowTextureSize;
       
   234     shadowDrawingSize.iWidth *= xMultiplier;
       
   235     shadowDrawingSize.iHeight *= yMultiplier;
       
   236    
       
   237     // apply the scale
       
   238     shadowDrawingSize.iWidth *= iScale.Now();
       
   239     shadowDrawingSize.iHeight *= iScale.Now();
       
   240     
       
   241     return shadowDrawingSize;
       
   242     }
       
   243 
       
   244 // ---------------------------------------------------------------------------
       
   245 // Returns ETrue if the shadow is visible
       
   246 // ---------------------------------------------------------------------------
       
   247 // 
       
   248 TBool CHuiDropShadow::IsShadowVisible() const
       
   249     {
       
   250     return !HuiUtil::RealCompare( iScale.Now(), 0.f ) &&
       
   251            !HuiUtil::RealCompare( iOpacity.Now(), 0.f );
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // Gets the CHuiTexture from the MHuiTexture is applicable
       
   256 // ---------------------------------------------------------------------------
       
   257 //     
       
   258 CHuiTexture* CHuiDropShadow::ShadowedTextureInstance( const MHuiTexture& aTextureIf )
       
   259     {
       
   260     CHuiTexture* resultTexture = NULL;
       
   261     
       
   262     const MHuiShadowedTexture* constShadowedTexture = aTextureIf.ShadowedTexture();
       
   263     if ( constShadowedTexture )
       
   264         {
       
   265         MHuiShadowedTexture* nonConstshadowedTexture = const_cast<MHuiShadowedTexture*>(constShadowedTexture);
       
   266         resultTexture = static_cast<CHuiTexture*>( nonConstshadowedTexture );
       
   267         }
       
   268     return resultTexture;
       
   269     }
       
   270 
       
   271 // ---------------------------------------------------------------------------
       
   272 // Returns shadow distance in pixels
       
   273 // ---------------------------------------------------------------------------
       
   274 //     
       
   275 THuiRealPoint CHuiDropShadow::DistanceInPixels( const CHuiVisual& aReferenceVisual ) const
       
   276     {
       
   277     return aReferenceVisual.MetricToPixels( 
       
   278         THuiXYMetric( 
       
   279             THuiMetric( 
       
   280                 iOffset.iX.Now(), 
       
   281                 iOffsetUnit ), 
       
   282             THuiMetric( 
       
   283                 iOffset.iY.Now(), 
       
   284                 iOffsetUnit ) 
       
   285             ) 
       
   286         );
       
   287     }
       
   288 
       
   289 // ---------------------------------------------------------------------------
       
   290 // Returns shadow area
       
   291 // ---------------------------------------------------------------------------
       
   292 // 
       
   293 THuiRealRect CHuiDropShadow::ShadowDrawingRealRect( 
       
   294         const TPoint& aImageDrawingPosition,
       
   295         const TSize& aImageDrawingSize, 
       
   296         const TSize& aShadowTextureSize,
       
   297         const CHuiVisual& aReferenceVisual  ) const
       
   298     {
       
   299     const THuiRealSize shadowDrawingSize = ShadowDrawingSize( aImageDrawingSize, aShadowTextureSize );
       
   300     THuiRealPoint shadowDrawingPos( aImageDrawingPosition );
       
   301                         
       
   302     const THuiRealPoint distanceInPixels = DistanceInPixels( aReferenceVisual );
       
   303                 
       
   304     shadowDrawingPos.iX += distanceInPixels.iX;
       
   305     shadowDrawingPos.iY += distanceInPixels.iY;
       
   306                 
       
   307     shadowDrawingPos.iX -= (shadowDrawingSize.iWidth-aImageDrawingSize.iWidth )/2.f;
       
   308     shadowDrawingPos.iY -= (shadowDrawingSize.iHeight-aImageDrawingSize.iHeight )/2.f;
       
   309             
       
   310     return THuiRealRect( shadowDrawingPos, shadowDrawingSize );
       
   311     }
       
   312 
       
   313 // ---------------------------------------------------------------------------
       
   314 // Returns shadow area
       
   315 // ---------------------------------------------------------------------------
       
   316 //
       
   317 TRect CHuiDropShadow::ShadowDrawingTRect( 
       
   318         const TPoint& aImageDrawingPosition,
       
   319         const TSize& aImageDrawingSize, 
       
   320         const TSize& aShadowTextureSize,
       
   321         const CHuiVisual& aReferenceVisual  ) const
       
   322     {
       
   323     const THuiRealRect realRect = ShadowDrawingRealRect( 
       
   324         aImageDrawingPosition, 
       
   325         aImageDrawingSize, 
       
   326         aShadowTextureSize, 
       
   327         aReferenceVisual );
       
   328         
       
   329     return TRect( HUI_ROUND_FLOAT_TO_INT( realRect.iTl.iX ),
       
   330                   HUI_ROUND_FLOAT_TO_INT( realRect.iTl.iY ),
       
   331                   HUI_ROUND_FLOAT_TO_INT( realRect.iBr.iX ),
       
   332                   HUI_ROUND_FLOAT_TO_INT( realRect.iBr.iY ) );
       
   333     }