Update contrib.
1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // Anim DLL to deal with handwriting
20 #define DEFAUlT_LINE_WIDTH 4
21 #define DEFAUlT_MASK_WIDTH_FACTOR 3
22 #define DEFAUlT_END_POINT_FACTOR 2
23 #define BLACK TRgb::Gray2(0)
24 #define WHITE TRgb::Gray2(1)
26 #define leavescan_needs_to_see_some_brackets_here { }
27 EXPORT_C CAnimDll *CreateCAnimDllL()
29 return(new(ELeave) CHandWritingAnimDll());
33 /*CHandWritingAnimDll*/
35 CAnim *CHandWritingAnimDll::CreateInstanceL(TInt /*aType*/)
37 return new(ELeave) CHandWritingAnim();
43 CHandWritingAnim::~CHandWritingAnim()
45 iFunctions->GetRawEvents(EFalse);
47 delete iMaskBitmapDevice;
50 void CHandWritingAnim::Activate()
52 if (iState==EHwStateDeactive)
54 iState=EHwStateInactive;
55 iFunctions->GetRawEvents(ETrue);
59 void CHandWritingAnim::Deactivate()
61 if (iState!=EHwStateDeactive)
63 iState=EHwStateDeactive;
64 iFunctions->GetRawEvents(EFalse);
65 iSpriteFunctions->Activate(EFalse);
70 void CHandWritingAnim::SpriteChangeL(TBool aUsingSeparateMask)
72 if (aUsingSeparateMask)
74 TSpriteMember *spriteMember=iSpriteFunctions->GetSpriteMember(0);
75 iMaskBitmapDevice=CFbsBitmapDevice::NewL(spriteMember->iMaskBitmap); //If this leaves the error value will be returned to the client side
79 delete iMaskBitmapDevice;
80 iMaskBitmapDevice=NULL;
81 iDrawData.iLineColor=BLACK; //Must use black ink when there is no mask
82 iDrawData.iInitialBitmapColor=WHITE; //Must have white background when there is no mask
84 iIsMask=aUsingSeparateMask;
87 void CHandWritingAnim::SetDrawData(THandwritingDrawData *aDrawData)
92 iDrawData.iLineColor=BLACK; //Must use black ink when there is no mask
93 iDrawData.iInitialBitmapColor=WHITE; //Must have white background when there is no mask
97 TBool CHandWritingAnim::HandlePointerDown(TPoint aPoint)
99 if (iState==EHwStateWaitingMove)
101 iCurrentDrawPoint=aPoint;
102 if (iState==EHwStateInactive)
104 iState=EHwStateWaitingMove;
108 iState=EHwStateDrawing;
114 TBool CHandWritingAnim::HandlePointerMove(TPoint aPoint)
118 case EHwStateWaitingMove:
120 TPoint moved=aPoint-iCurrentDrawPoint;
121 if (Abs(moved.iX)<5 && Abs(moved.iY)<5) //Need to do something with these constants
123 iSpriteFunctions->Activate(ETrue);
125 iState=EHwStateDrawing;
127 case EHwStateDrawing:
137 TBool CHandWritingAnim::HandlePointerUp(TPoint aPoint)
139 if (iState==EHwStateInactive)
141 else if (iState==EHwStateWaitingMove)
143 TPoint moved=aPoint-iCurrentDrawPoint;
144 if (Abs(moved.iX)<5 && Abs(moved.iY)<5) //Need to do something with these constants
146 SendEatenDownEvent();
149 iSpriteFunctions->Activate(ETrue);
155 iState=EHwStateWaitingStroke;
160 void CHandWritingAnim::DrawPoint()
162 iSpriteGc->Activate(iBitmapDevice);
163 iSpriteGc->SetPenSize(TSize(iDrawData.iEndPontWidth,iDrawData.iEndPontWidth));
164 iSpriteGc->SetPenColor(iDrawData.iLineColor);
165 iSpriteGc->Plot(iCurrentDrawPoint);
166 if (iMaskBitmapDevice)
168 iSpriteGc->Activate(iMaskBitmapDevice);
169 iSpriteGc->SetPenSize(TSize(iDrawData.iMaskLineWidth,iDrawData.iMaskLineWidth));
170 iSpriteGc->SetPenColor(BLACK); //Mask must be drawn in black
171 iSpriteGc->Plot(iCurrentDrawPoint);
173 iPointStore->AddPoint(iCurrentDrawPoint);
176 void CHandWritingAnim::DrawLine(TPoint aEndPoint)
178 iSpriteGc->Activate(iBitmapDevice);
179 iSpriteGc->SetPenSize(TSize(iDrawData.iLineWidth,iDrawData.iLineWidth));
180 iSpriteGc->SetPenColor(iDrawData.iLineColor);
181 iSpriteGc->MoveTo(iCurrentDrawPoint);
182 iSpriteGc->DrawLineTo(aEndPoint);
183 if (iMaskBitmapDevice)
185 iSpriteGc->Activate(iMaskBitmapDevice);
186 iSpriteGc->SetPenSize(TSize(iDrawData.iMaskLineWidth,iDrawData.iMaskLineWidth));
187 iSpriteGc->SetPenColor(BLACK); //Mask must be drawn in black
188 iSpriteGc->MoveTo(iCurrentDrawPoint);
189 iSpriteGc->DrawLineTo(aEndPoint);
191 iCurrentDrawPoint=aEndPoint;
192 iPointStore->AddPoint(aEndPoint);
195 void CHandWritingAnim::UpdateSprite()
198 iSpriteGc->RectDrawnTo(drawTo);
199 iSpriteFunctions->UpdateMember(0,drawTo,EFalse);
202 void CHandWritingAnim::StartTimer()
204 iFunctions->SetNextInterval(2);
207 void CHandWritingAnim::SendEatenDownEvent()
210 rawEvent.Set(TRawEvent::EButton1Down,iCurrentDrawPoint.iX,iCurrentDrawPoint.iY);
211 iFunctions->PostRawEvent(rawEvent);
212 iState=EHwStateInactive;
215 void CHandWritingAnim::CharacterFinished()
217 iState=EHwStateInactive;
218 iLastGeneratedCharacter=iPointStore->GetChar();
219 /*TRawEvent rawEvent;
220 rawEvent.Set(TRawEvent::EKeyDown,iLastGeneratedCharacter);
221 iFunctions->PostKeyEvent(rawEvent);*/
223 keyEvent.iCode=keyEvent.iScanCode=iLastGeneratedCharacter;
224 keyEvent.iModifiers=keyEvent.iRepeats=0;
225 iFunctions->PostKeyEvent(keyEvent);
226 iPointStore->ClearPoints();
227 iSpriteFunctions->Activate(EFalse);
231 void CHandWritingAnim::ClearSprite()
233 iSpriteGc->Activate(iBitmapDevice);
234 iSpriteGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
235 iSpriteGc->SetBrushColor(iDrawData.iInitialBitmapColor);
237 if (iMaskBitmapDevice)
239 iSpriteGc->Activate(iMaskBitmapDevice);
240 iSpriteGc->SetBrushColor(WHITE); //Mask must be cleared in white
244 iSpriteGc->RectDrawnTo(drawnTo); //Clear the drawnTo rect.
247 TBool CHandWritingAnim::OfferRawEvent(const TRawEvent &aRawEvent)
249 if (iState==EHwStateDeactive)
251 switch (aRawEvent.Type())
253 case TRawEvent::EButton1Down:
255 iDownTime.HomeTime();
256 return HandlePointerDown(aRawEvent.Pos());
258 case TRawEvent::EPointerMove:
259 return HandlePointerMove(aRawEvent.Pos());
260 case TRawEvent::EButton1Up:
261 return HandlePointerUp(aRawEvent.Pos());
267 void CHandWritingAnim::ConstructL(TAny *)
269 TSpriteMember *spriteMember=iSpriteFunctions->GetSpriteMember(0);
270 iIsMask=(spriteMember->iBitmap->Handle() != spriteMember->iMaskBitmap->Handle());
271 iBitmapDevice=CFbsBitmapDevice::NewL(spriteMember->iBitmap);
273 iMaskBitmapDevice=CFbsBitmapDevice::NewL(spriteMember->iMaskBitmap);
274 iState=EHwStateDeactive;
275 iSpriteGc=CFbsBitGc::NewL();
277 iDrawData.iLineColor=BLACK;
278 iDrawData.iInitialBitmapColor=WHITE;
279 iDrawData.iLineWidth=DEFAUlT_LINE_WIDTH;
280 iDrawData.iMaskLineWidth=DEFAUlT_MASK_WIDTH_FACTOR*DEFAUlT_LINE_WIDTH;
281 iDrawData.iEndPontWidth=DEFAUlT_END_POINT_FACTOR*DEFAUlT_LINE_WIDTH;
282 iSpriteFunctions->SizeChangedL();
283 iPointStore=new(ELeave) CPointStore();
284 iPointStore->ConstructL();
287 void CHandWritingAnim::Animate(TDateTime* /*aDateTime*/)
289 iFunctions->SetInterval(0);
290 if (iState==EHwStateWaitingMove)
291 SendEatenDownEvent();
292 else if (iState==EHwStateWaitingStroke)
296 void CHandWritingAnim::Redraw()
300 void CHandWritingAnim::Command(TInt aOpcode,TAny *aParams)
307 case EHwOpDeactivate:
310 case EHwOpSetDrawData:;
311 SetDrawData(STATIC_CAST(THandwritingDrawData*,aParams));
318 void CHandWritingAnim::FocusChanged(TBool )
322 TInt CHandWritingAnim::CommandReplyL(TInt aOpcode,TAny *aParams)
326 case EHwOpSpriteMask:
327 SpriteChangeL(*STATIC_CAST(TBool*,aParams));
329 case EHwOpGetLastChar:
330 return iLastGeneratedCharacter;
340 CPointStore::CPointStore()
343 void CPointStore::ConstructL()
345 iPoints=new(ELeave) CArrayFixFlat<TPoint>(16);
346 iPoints->ResizeL(256);
349 void CPointStore::AddPoint(TPoint aPoint)
352 (*iPoints)[iNumPoints++]=aPoint;
355 TInt CPointStore::GetChar()
357 TPoint oldPoint=(*iPoints)[0];
359 TPoint totalPoint=oldPoint;
360 TInt xInc=0,xDec=0,yInc=0,yDec=0;
361 TInt yState=0,xState=0;
363 for (ii=1;ii<iNumPoints;++ii)
365 newPoint=(*iPoints)[ii];
366 totalPoint+=newPoint;
367 if (newPoint.iX>oldPoint.iX)
369 if (newPoint.iX<oldPoint.iX)
371 if (newPoint.iY>oldPoint.iY)
373 if (newPoint.iY<oldPoint.iY)
377 newPoint-=(*iPoints)[0];
380 else if (yInc>10*yDec)
384 else if (xInc>10*xDec)
386 if (xState!=0 && yState!=0)
388 if (Abs(newPoint.iY)<Abs(newPoint.iX))
394 return xState>0 ? EKeyRightArrow:EKeyLeftArrow;
396 return yState>0 ? EKeyDownArrow:EKeyUpArrow;
399 TInt type=(totalPoint.iY/10)%10;
407 return firstChar+((totalPoint.iX/10)%numChars);
410 void CPointStore::ClearPoints()