os/graphics/windowing/windowserver/test/HANDANIM.CPP
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// Anim DLL to deal with handwriting
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "HANDANIM.H"
sl@0
    19
sl@0
    20
#define DEFAUlT_LINE_WIDTH 4
sl@0
    21
#define DEFAUlT_MASK_WIDTH_FACTOR 3
sl@0
    22
#define DEFAUlT_END_POINT_FACTOR 2
sl@0
    23
#define BLACK TRgb::Gray2(0)
sl@0
    24
#define WHITE TRgb::Gray2(1)
sl@0
    25
sl@0
    26
#define leavescan_needs_to_see_some_brackets_here	{	}
sl@0
    27
EXPORT_C CAnimDll *CreateCAnimDllL()
sl@0
    28
	{
sl@0
    29
	return(new(ELeave) CHandWritingAnimDll());
sl@0
    30
	}
sl@0
    31
sl@0
    32
sl@0
    33
/*CHandWritingAnimDll*/
sl@0
    34
sl@0
    35
CAnim *CHandWritingAnimDll::CreateInstanceL(TInt /*aType*/)
sl@0
    36
	{
sl@0
    37
	return new(ELeave) CHandWritingAnim();
sl@0
    38
	}
sl@0
    39
sl@0
    40
sl@0
    41
/*CHandWritingAnim*/
sl@0
    42
sl@0
    43
CHandWritingAnim::~CHandWritingAnim()
sl@0
    44
	{
sl@0
    45
	iFunctions->GetRawEvents(EFalse);
sl@0
    46
	delete iBitmapDevice;
sl@0
    47
	delete iMaskBitmapDevice;
sl@0
    48
	}
sl@0
    49
sl@0
    50
void CHandWritingAnim::Activate()
sl@0
    51
	{
sl@0
    52
	if (iState==EHwStateDeactive)
sl@0
    53
		{
sl@0
    54
		iState=EHwStateInactive;
sl@0
    55
		iFunctions->GetRawEvents(ETrue);
sl@0
    56
		}
sl@0
    57
	}
sl@0
    58
sl@0
    59
void CHandWritingAnim::Deactivate()
sl@0
    60
	{
sl@0
    61
	if (iState!=EHwStateDeactive)
sl@0
    62
		{
sl@0
    63
		iState=EHwStateDeactive;
sl@0
    64
		iFunctions->GetRawEvents(EFalse);
sl@0
    65
		iSpriteFunctions->Activate(EFalse);
sl@0
    66
		ClearSprite();
sl@0
    67
		}
sl@0
    68
	}
sl@0
    69
sl@0
    70
void CHandWritingAnim::SpriteChangeL(TBool aUsingSeparateMask)
sl@0
    71
	{
sl@0
    72
	if (aUsingSeparateMask)
sl@0
    73
		{
sl@0
    74
		TSpriteMember *spriteMember=iSpriteFunctions->GetSpriteMember(0);
sl@0
    75
		iMaskBitmapDevice=CFbsBitmapDevice::NewL(spriteMember->iMaskBitmap);		//If this leaves the error value will be returned to the client side
sl@0
    76
		}
sl@0
    77
	else
sl@0
    78
		{
sl@0
    79
		delete iMaskBitmapDevice;
sl@0
    80
		iMaskBitmapDevice=NULL;
sl@0
    81
		iDrawData.iLineColor=BLACK;		//Must use black ink when there is no mask
sl@0
    82
		iDrawData.iInitialBitmapColor=WHITE;		//Must have white background when there is no mask
sl@0
    83
		}
sl@0
    84
	iIsMask=aUsingSeparateMask;
sl@0
    85
	}
sl@0
    86
sl@0
    87
void CHandWritingAnim::SetDrawData(THandwritingDrawData *aDrawData)
sl@0
    88
	{
sl@0
    89
	iDrawData=*aDrawData;
sl@0
    90
	if (!iIsMask)
sl@0
    91
		{
sl@0
    92
		iDrawData.iLineColor=BLACK;		//Must use black ink when there is no mask
sl@0
    93
		iDrawData.iInitialBitmapColor=WHITE;		//Must have white background when there is no mask
sl@0
    94
		}
sl@0
    95
	}
sl@0
    96
sl@0
    97
TBool CHandWritingAnim::HandlePointerDown(TPoint aPoint)
sl@0
    98
	{
sl@0
    99
	if (iState==EHwStateWaitingMove)
sl@0
   100
		return EFalse;
sl@0
   101
	iCurrentDrawPoint=aPoint;
sl@0
   102
	if (iState==EHwStateInactive)
sl@0
   103
		{
sl@0
   104
		iState=EHwStateWaitingMove;
sl@0
   105
		StartTimer();
sl@0
   106
		return ETrue;
sl@0
   107
		}
sl@0
   108
	iState=EHwStateDrawing;
sl@0
   109
	DrawPoint();
sl@0
   110
	UpdateSprite();
sl@0
   111
	return ETrue;
sl@0
   112
	}
sl@0
   113
sl@0
   114
TBool CHandWritingAnim::HandlePointerMove(TPoint aPoint)
sl@0
   115
	{
sl@0
   116
	switch (iState)
sl@0
   117
		{
sl@0
   118
	case EHwStateWaitingMove:
sl@0
   119
		{
sl@0
   120
		TPoint moved=aPoint-iCurrentDrawPoint;
sl@0
   121
		if (Abs(moved.iX)<5 && Abs(moved.iY)<5)			//Need to do something with these constants
sl@0
   122
			return ETrue;
sl@0
   123
		iSpriteFunctions->Activate(ETrue);
sl@0
   124
		DrawPoint();
sl@0
   125
		iState=EHwStateDrawing;
sl@0
   126
		}
sl@0
   127
	case EHwStateDrawing:
sl@0
   128
		break;
sl@0
   129
	default:
sl@0
   130
		return EFalse;
sl@0
   131
		}
sl@0
   132
	DrawLine(aPoint);
sl@0
   133
	UpdateSprite();
sl@0
   134
	return ETrue;
sl@0
   135
	}
sl@0
   136
sl@0
   137
TBool CHandWritingAnim::HandlePointerUp(TPoint aPoint)
sl@0
   138
	{
sl@0
   139
	if (iState==EHwStateInactive)
sl@0
   140
		return EFalse;
sl@0
   141
	else if (iState==EHwStateWaitingMove)
sl@0
   142
		{
sl@0
   143
		TPoint moved=aPoint-iCurrentDrawPoint;
sl@0
   144
		if (Abs(moved.iX)<5 && Abs(moved.iY)<5)			//Need to do something with these constants
sl@0
   145
			{
sl@0
   146
			SendEatenDownEvent();
sl@0
   147
			return EFalse;
sl@0
   148
			}
sl@0
   149
		iSpriteFunctions->Activate(ETrue);
sl@0
   150
		DrawPoint();
sl@0
   151
		}
sl@0
   152
	DrawLine(aPoint);
sl@0
   153
	DrawPoint();
sl@0
   154
	UpdateSprite();
sl@0
   155
	iState=EHwStateWaitingStroke;
sl@0
   156
	StartTimer();
sl@0
   157
	return ETrue;
sl@0
   158
	}
sl@0
   159
sl@0
   160
void CHandWritingAnim::DrawPoint()
sl@0
   161
	{
sl@0
   162
	iSpriteGc->Activate(iBitmapDevice);
sl@0
   163
	iSpriteGc->SetPenSize(TSize(iDrawData.iEndPontWidth,iDrawData.iEndPontWidth));
sl@0
   164
	iSpriteGc->SetPenColor(iDrawData.iLineColor);
sl@0
   165
	iSpriteGc->Plot(iCurrentDrawPoint);
sl@0
   166
	if (iMaskBitmapDevice)
sl@0
   167
		{
sl@0
   168
		iSpriteGc->Activate(iMaskBitmapDevice);
sl@0
   169
		iSpriteGc->SetPenSize(TSize(iDrawData.iMaskLineWidth,iDrawData.iMaskLineWidth));
sl@0
   170
		iSpriteGc->SetPenColor(BLACK);		//Mask must be drawn in black
sl@0
   171
		iSpriteGc->Plot(iCurrentDrawPoint);
sl@0
   172
		}
sl@0
   173
	iPointStore->AddPoint(iCurrentDrawPoint);
sl@0
   174
	}
sl@0
   175
sl@0
   176
void CHandWritingAnim::DrawLine(TPoint aEndPoint)
sl@0
   177
	{
sl@0
   178
	iSpriteGc->Activate(iBitmapDevice);
sl@0
   179
	iSpriteGc->SetPenSize(TSize(iDrawData.iLineWidth,iDrawData.iLineWidth));
sl@0
   180
	iSpriteGc->SetPenColor(iDrawData.iLineColor);
sl@0
   181
	iSpriteGc->MoveTo(iCurrentDrawPoint);
sl@0
   182
	iSpriteGc->DrawLineTo(aEndPoint);
sl@0
   183
	if (iMaskBitmapDevice)
sl@0
   184
		{
sl@0
   185
		iSpriteGc->Activate(iMaskBitmapDevice);
sl@0
   186
		iSpriteGc->SetPenSize(TSize(iDrawData.iMaskLineWidth,iDrawData.iMaskLineWidth));
sl@0
   187
		iSpriteGc->SetPenColor(BLACK);		//Mask must be drawn in black
sl@0
   188
		iSpriteGc->MoveTo(iCurrentDrawPoint);
sl@0
   189
		iSpriteGc->DrawLineTo(aEndPoint);
sl@0
   190
		}
sl@0
   191
	iCurrentDrawPoint=aEndPoint;
sl@0
   192
	iPointStore->AddPoint(aEndPoint);
sl@0
   193
	}
sl@0
   194
sl@0
   195
void CHandWritingAnim::UpdateSprite()
sl@0
   196
	{
sl@0
   197
	TRect drawTo;
sl@0
   198
	iSpriteGc->RectDrawnTo(drawTo);
sl@0
   199
	iSpriteFunctions->UpdateMember(0,drawTo,EFalse);
sl@0
   200
	}
sl@0
   201
sl@0
   202
void CHandWritingAnim::StartTimer()
sl@0
   203
	{
sl@0
   204
	iFunctions->SetNextInterval(2);
sl@0
   205
	}
sl@0
   206
sl@0
   207
void CHandWritingAnim::SendEatenDownEvent()
sl@0
   208
	{
sl@0
   209
	TRawEvent rawEvent;
sl@0
   210
	rawEvent.Set(TRawEvent::EButton1Down,iCurrentDrawPoint.iX,iCurrentDrawPoint.iY);
sl@0
   211
	iFunctions->PostRawEvent(rawEvent);
sl@0
   212
	iState=EHwStateInactive;
sl@0
   213
	}
sl@0
   214
sl@0
   215
void CHandWritingAnim::CharacterFinished()
sl@0
   216
	{		
sl@0
   217
	iState=EHwStateInactive;
sl@0
   218
	iLastGeneratedCharacter=iPointStore->GetChar();
sl@0
   219
	/*TRawEvent rawEvent;
sl@0
   220
	rawEvent.Set(TRawEvent::EKeyDown,iLastGeneratedCharacter);
sl@0
   221
	iFunctions->PostKeyEvent(rawEvent);*/
sl@0
   222
    TKeyEvent keyEvent;
sl@0
   223
    keyEvent.iCode=keyEvent.iScanCode=iLastGeneratedCharacter;
sl@0
   224
    keyEvent.iModifiers=keyEvent.iRepeats=0;
sl@0
   225
	iFunctions->PostKeyEvent(keyEvent);
sl@0
   226
	iPointStore->ClearPoints();
sl@0
   227
	iSpriteFunctions->Activate(EFalse);
sl@0
   228
	ClearSprite();
sl@0
   229
	}
sl@0
   230
sl@0
   231
void CHandWritingAnim::ClearSprite()
sl@0
   232
	{
sl@0
   233
	iSpriteGc->Activate(iBitmapDevice);
sl@0
   234
	iSpriteGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
sl@0
   235
	iSpriteGc->SetBrushColor(iDrawData.iInitialBitmapColor);
sl@0
   236
	iSpriteGc->Clear();
sl@0
   237
	if (iMaskBitmapDevice)
sl@0
   238
		{
sl@0
   239
		iSpriteGc->Activate(iMaskBitmapDevice);
sl@0
   240
		iSpriteGc->SetBrushColor(WHITE);		//Mask must be cleared in white
sl@0
   241
		iSpriteGc->Clear();
sl@0
   242
		}
sl@0
   243
    TRect drawnTo;
sl@0
   244
	iSpriteGc->RectDrawnTo(drawnTo);		//Clear the drawnTo rect.
sl@0
   245
	}
sl@0
   246
sl@0
   247
TBool CHandWritingAnim::OfferRawEvent(const TRawEvent &aRawEvent)
sl@0
   248
	{
sl@0
   249
	if (iState==EHwStateDeactive)
sl@0
   250
		return EFalse;
sl@0
   251
	switch (aRawEvent.Type())
sl@0
   252
		{
sl@0
   253
	case TRawEvent::EButton1Down:
sl@0
   254
		{
sl@0
   255
		iDownTime.HomeTime();		
sl@0
   256
		return HandlePointerDown(aRawEvent.Pos());
sl@0
   257
		}
sl@0
   258
	case TRawEvent::EPointerMove:
sl@0
   259
		return HandlePointerMove(aRawEvent.Pos());
sl@0
   260
	case TRawEvent::EButton1Up:
sl@0
   261
		return HandlePointerUp(aRawEvent.Pos());
sl@0
   262
	default:
sl@0
   263
		return EFalse;
sl@0
   264
		}
sl@0
   265
	}
sl@0
   266
sl@0
   267
void CHandWritingAnim::ConstructL(TAny *)
sl@0
   268
	{
sl@0
   269
	TSpriteMember *spriteMember=iSpriteFunctions->GetSpriteMember(0);
sl@0
   270
	iIsMask=(spriteMember->iBitmap->Handle() != spriteMember->iMaskBitmap->Handle());
sl@0
   271
	iBitmapDevice=CFbsBitmapDevice::NewL(spriteMember->iBitmap);
sl@0
   272
	if (iIsMask)
sl@0
   273
		iMaskBitmapDevice=CFbsBitmapDevice::NewL(spriteMember->iMaskBitmap);
sl@0
   274
	iState=EHwStateDeactive;
sl@0
   275
	iSpriteGc=CFbsBitGc::NewL();
sl@0
   276
	iSpriteGc->Reset();
sl@0
   277
	iDrawData.iLineColor=BLACK;
sl@0
   278
	iDrawData.iInitialBitmapColor=WHITE;
sl@0
   279
	iDrawData.iLineWidth=DEFAUlT_LINE_WIDTH;
sl@0
   280
	iDrawData.iMaskLineWidth=DEFAUlT_MASK_WIDTH_FACTOR*DEFAUlT_LINE_WIDTH;
sl@0
   281
	iDrawData.iEndPontWidth=DEFAUlT_END_POINT_FACTOR*DEFAUlT_LINE_WIDTH;
sl@0
   282
	iSpriteFunctions->SizeChangedL();
sl@0
   283
	iPointStore=new(ELeave) CPointStore();
sl@0
   284
	iPointStore->ConstructL();
sl@0
   285
	}
sl@0
   286
sl@0
   287
void CHandWritingAnim::Animate(TDateTime* /*aDateTime*/)
sl@0
   288
	{
sl@0
   289
	iFunctions->SetInterval(0);
sl@0
   290
	if (iState==EHwStateWaitingMove)
sl@0
   291
		SendEatenDownEvent();
sl@0
   292
	else if (iState==EHwStateWaitingStroke)
sl@0
   293
		CharacterFinished();
sl@0
   294
	}
sl@0
   295
sl@0
   296
void CHandWritingAnim::Redraw()
sl@0
   297
	{
sl@0
   298
	}
sl@0
   299
sl@0
   300
void CHandWritingAnim::Command(TInt aOpcode,TAny *aParams)
sl@0
   301
	{
sl@0
   302
	switch (aOpcode)
sl@0
   303
		{
sl@0
   304
		case EHwOpActivate:
sl@0
   305
			Activate();
sl@0
   306
			break;
sl@0
   307
		case EHwOpDeactivate:
sl@0
   308
			Deactivate();
sl@0
   309
			break;
sl@0
   310
		case EHwOpSetDrawData:;
sl@0
   311
			SetDrawData(STATIC_CAST(THandwritingDrawData*,aParams));
sl@0
   312
			break;
sl@0
   313
		default:
sl@0
   314
			iFunctions->Panic();
sl@0
   315
		}
sl@0
   316
	}
sl@0
   317
sl@0
   318
void CHandWritingAnim::FocusChanged(TBool )
sl@0
   319
	{
sl@0
   320
	}
sl@0
   321
sl@0
   322
TInt CHandWritingAnim::CommandReplyL(TInt aOpcode,TAny *aParams)
sl@0
   323
	{
sl@0
   324
	switch (aOpcode)
sl@0
   325
		{
sl@0
   326
		case EHwOpSpriteMask:
sl@0
   327
			SpriteChangeL(*STATIC_CAST(TBool*,aParams));
sl@0
   328
			break;
sl@0
   329
		case EHwOpGetLastChar:
sl@0
   330
			return iLastGeneratedCharacter;
sl@0
   331
		default:
sl@0
   332
			iFunctions->Panic();
sl@0
   333
		}
sl@0
   334
	return KErrNone;
sl@0
   335
	}
sl@0
   336
sl@0
   337
sl@0
   338
/*CPointStore*/
sl@0
   339
sl@0
   340
CPointStore::CPointStore()
sl@0
   341
	{}
sl@0
   342
sl@0
   343
void CPointStore::ConstructL()
sl@0
   344
	{
sl@0
   345
	iPoints=new(ELeave) CArrayFixFlat<TPoint>(16);
sl@0
   346
	iPoints->ResizeL(256);
sl@0
   347
	}
sl@0
   348
sl@0
   349
void CPointStore::AddPoint(TPoint aPoint)
sl@0
   350
	{
sl@0
   351
	if (iNumPoints<256)
sl@0
   352
		(*iPoints)[iNumPoints++]=aPoint;
sl@0
   353
	}
sl@0
   354
sl@0
   355
TInt CPointStore::GetChar()
sl@0
   356
	{
sl@0
   357
	TPoint oldPoint=(*iPoints)[0];
sl@0
   358
	TPoint newPoint;
sl@0
   359
	TPoint totalPoint=oldPoint;
sl@0
   360
	TInt xInc=0,xDec=0,yInc=0,yDec=0;
sl@0
   361
	TInt yState=0,xState=0;
sl@0
   362
	TInt ii;
sl@0
   363
	for (ii=1;ii<iNumPoints;++ii)
sl@0
   364
		{
sl@0
   365
		newPoint=(*iPoints)[ii];
sl@0
   366
		totalPoint+=newPoint;
sl@0
   367
		if (newPoint.iX>oldPoint.iX)
sl@0
   368
			++xInc;
sl@0
   369
		if (newPoint.iX<oldPoint.iX)
sl@0
   370
			++xDec;
sl@0
   371
		if (newPoint.iY>oldPoint.iY)
sl@0
   372
			++yInc;
sl@0
   373
		if (newPoint.iY<oldPoint.iY)
sl@0
   374
			++yDec;
sl@0
   375
		oldPoint=newPoint;
sl@0
   376
		}
sl@0
   377
	newPoint-=(*iPoints)[0];
sl@0
   378
	if (10*yInc<yDec)
sl@0
   379
		yState=-1;
sl@0
   380
	else if (yInc>10*yDec)
sl@0
   381
		yState=1;
sl@0
   382
	if (10*xInc<xDec)
sl@0
   383
		xState=-1;
sl@0
   384
	else if (xInc>10*xDec)
sl@0
   385
		xState=1;
sl@0
   386
	if (xState!=0 && yState!=0)
sl@0
   387
		{
sl@0
   388
		if (Abs(newPoint.iY)<Abs(newPoint.iX))
sl@0
   389
			yState=0;
sl@0
   390
		else
sl@0
   391
			xState=0;
sl@0
   392
		}
sl@0
   393
	if (xState!=0)
sl@0
   394
		return xState>0 ? EKeyRightArrow:EKeyLeftArrow;
sl@0
   395
	if (yState!=0)
sl@0
   396
		return yState>0 ? EKeyDownArrow:EKeyUpArrow;
sl@0
   397
	TInt firstChar='a';
sl@0
   398
	TInt numChars=26;
sl@0
   399
	TInt type=(totalPoint.iY/10)%10;
sl@0
   400
	if (type>5)
sl@0
   401
		firstChar='A';
sl@0
   402
	else if (type==0)
sl@0
   403
		{
sl@0
   404
		firstChar='0';
sl@0
   405
		numChars=10;
sl@0
   406
		}
sl@0
   407
	return firstChar+((totalPoint.iX/10)%numChars);
sl@0
   408
	}
sl@0
   409
sl@0
   410
void CPointStore::ClearPoints()
sl@0
   411
	{
sl@0
   412
	iNumPoints=0;
sl@0
   413
	}