/*
* ============================================================================
* Name : tactileexampleappview.cpp
* Part of : Tactile Example
* Interface : N/A
* Description : Implementation of view class
* Version :
*
* Copyright (c) 2008 Nokia Corporation.
* This material, including documentation and any related
* computer programs, is protected by copyright controlled by
* Nokia Corporation.
* ============================================================================
*/
// INCLUDE FILES
#include <aknbutton.h>
#include <stringloader.h>
#include <touchfeedback.h>
#include <tactileexample.rsg>
#include "tactileexampleappview.h"
#include "tactileexamplesquare.h"
#include "tactileexamplecircle.h"
//ENUMERATION
enum TPointerToComponentView
{
ESquare = 0,
ECircle,
EButton
};
//CONSTANTS
const TInt KNumberOfComponents = 3;
const TInt KMovementGrid = 10; // in pixels
const TInt KButtonHeight = 4; // divisions of screen height
const TInt KObjSize = 4; // divisions of screen height or width
// ============================ MEMBER FUNCTIONS =============================
// ---------------------------------------------------------------------------
// Two-phased constructor.
// ---------------------------------------------------------------------------
//
CTactileExampleAppView* CTactileExampleAppView::NewL( const TRect& aRect )
{
CTactileExampleAppView* self = CTactileExampleAppView::NewLC( aRect );
CleanupStack::Pop( self );
return self;
}
// ---------------------------------------------------------------------------
// Two-phased constructor.
// ---------------------------------------------------------------------------
//
CTactileExampleAppView* CTactileExampleAppView::NewLC( const TRect& aRect )
{
CTactileExampleAppView* self = new ( ELeave ) CTactileExampleAppView;
CleanupStack::PushL( self );
self->ConstructL( aRect );
return self;
}
// ---------------------------------------------------------------------------
// Frees reserved resources
// ---------------------------------------------------------------------------
//
CTactileExampleAppView::~CTactileExampleAppView()
{
delete iButton;
delete iSquare;
delete iCircle;
}
// ---------------------------------------------------------------------------
// From class CCoeControl.
// return nbr of controls inside this container.
// ---------------------------------------------------------------------------
//
TInt CTactileExampleAppView::CountComponentControls() const
{
return KNumberOfComponents;
}
// ---------------------------------------------------------------------------
// From class CCoeControl.
// return pointer of controls inside this container
// ---------------------------------------------------------------------------
//
CCoeControl* CTactileExampleAppView::ComponentControl(TInt aIndex) const
{
switch( aIndex )
{
case EButton:
return iButton;
case ESquare:
return iSquare;
case ECircle:
return iCircle;
default:
return NULL;
}
}
// ---------------------------------------------------------------------------
// From class CCoeControl.
// Draw this CTactileExampleAppView to the screen.
// ---------------------------------------------------------------------------
//
void CTactileExampleAppView::Draw( const TRect& aRect ) const
{
// Get the standard graphics context
CWindowGc& gc = SystemGc();
gc.SetPenStyle( CGraphicsContext::ENullPen );
gc.SetBrushColor( KRgbWhite );
gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
// Clears the area
gc.Clear( aRect );
}
// ---------------------------------------------------------------------------
// From class CCoeControl.
// Called by framework when the view size is changed.
// Set objects to default positions
// ---------------------------------------------------------------------------
//
void CTactileExampleAppView::SizeChanged()
{
// Gets the control's extent
TRect rect( Rect() );
// Set button's extent
TRect buttonRect = TRect( rect.iTl.iX,
rect.iBr.iY - rect.Height() / KButtonHeight,
rect.iBr.iX, rect.iBr.iY );
iButton->SetRect( buttonRect );
// Set area where dragging is possible
iDragArea.SetRect( rect.iTl.iX, rect.iTl.iY, rect.iBr.iX, buttonRect.iTl.iY );
// Update object dimension
ObjectDimension();
}
// ---------------------------------------------------------------------------
// From class CCoeControl.
// Moves objects and plays feedback during dragging
// ---------------------------------------------------------------------------
//
void CTactileExampleAppView::HandlePointerEventL( const TPointerEvent& aPointerEvent )
{
TPoint point = aPointerEvent.iPosition;
if ( iDragArea.Contains( point ))
{
if ( aPointerEvent.iType == TPointerEvent::EButton1Down &&
iSquare->Rect().Center() != iCircle->Rect().Center() )
{
if ( iCircle->HitRegionContains( point, *iCircle ))
{
// dragging of circle started
iDragCircle = ETrue;
iDraggingOffset = iCircle->Position() - point;
}
else if ( iSquare->HitRegionContains( point, *iSquare ))
{
// dragging of square started
iDragSquare = ETrue;
iDraggingOffset = iSquare->Position() - point;
}
else
{
// Cancel dragging just in case
iDragCircle = EFalse;
iDragSquare = EFalse;
}
}
if ( aPointerEvent.iType == TPointerEvent::EDrag )
{
if( iDragCircle )
{
Move( iCircle, point + iDraggingOffset );
}
else if( iDragSquare )
{
Move( iSquare, point + iDraggingOffset );
}
if( iSquare->Rect().Center() == iCircle->Rect().Center() )
{
// Objects are in end positions -> cancel dragging
iDragCircle = EFalse;
iDragSquare = EFalse;
// and disable all feedbacks
iSquare->EnableFeedback( EFalse );
iCircle->EnableFeedback( EFalse );
}
}
}
if ( aPointerEvent.iType == TPointerEvent::EButton1Up )
{
// possible dragging is cancelled if button is released
iDragCircle = EFalse;
iDragSquare = EFalse;
}
CCoeControl::HandlePointerEventL( aPointerEvent );
}
// ---------------------------------------------------------------------------
// From class CCoeControl.
// Enables feedback when layout is changed
// ---------------------------------------------------------------------------
//
void CTactileExampleAppView::HandleResourceChange( TInt aType )
{
CCoeControl::HandleResourceChange( aType );
if ( aType == KEikDynamicLayoutVariantSwitch )
{
// Set circle and square to default positions
SetObjects();
// enable feedback again since it might have been disabled
// if square & circle are in end positions
iCircle->EnableFeedback( ETrue );
iSquare->EnableFeedback( ETrue );
// ohjects are made visible/hidden depending
// is touch supported for new layout
iSquare->MakeVisible( AknLayoutUtils::PenEnabled() );
iCircle->MakeVisible( AknLay