这个测试比较少,就2个方面测试。先看UML图。
一、TextFieldTTFDefaultTest类他继承自KeyboardNotificationLayer类。KeyboardNotificationLayer先设置setTouchEnable(true), 然后重载了registerWithTouchDispatchar
注册CCTouchDispachar来让Layer处理Touch事件。
KeyboardNotificationLayer::KeyboardNotificationLayer() : m_pTrackNode(0) { setTouchEnabled(true); }
void KeyboardNotificationLayer::registerWithTouchDispatcher() { CCDirector* pDirector = CCDirector::sharedDirector(); pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false); }
CCArray * children = getChildren(); CCNode * node = 0; int count = children->count(); for (int i = 0; i < count; i++) { node = (CCNode*)children->ObjectAtIndex(i); ........................ }另外这个类重载了ccTouchBegan、ccTouchEnded2个方法,ccTouchEnded调用需函数onClickTrackNode做各种的处理。
然后子类在onClickTrackNode里面获取跟踪的CCTextFieldTTF指针,CCTextFieldTTF 的指针pTextField->attachWithIME();
二、先看看这个类的方法
void callbackRemoveNodeWhenDidAction(CCNode * pNode); // KeyboardNotificationLayer virtual std::string subtitle(); virtual void onClickTrackNode(bool bClicked); // CCLayer virtual void onEnter(); virtual void onExit(); // CCTextFieldDelegate virtual bool onTextFieldAttachWithIME(CCTextFieldTTF * pSender); virtual bool onTextFieldDetachWithIME(CCTextFieldTTF * pSender); virtual bool onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen); virtual bool onTextFieldDeleteBackward(CCTextFieldTTF * pSender, const char * delText, int nLen); virtual bool onDraw(CCTextFieldTTF * pSender);
ccTouchEnded里面调用需函数OnClickTrackNode,然后在里面attch/WithIME,attchWithIME里面调用attachDelegateWithIME,attachDelegateWithIME里面调用
canAttachWithIME,canAttachWithIME里面调用子类的onTextFieldAttachWithIME,当前代码的里面的onTextFieldAttachWithIME仅是执行淡入淡出的动作。
离开的时候调用onTextFieldDetachWithIME,里面仅是执行stopAction和设置一下透明度setOpacity。
输入字符的时候执行onTextFieldInsertText动作,删除字符的时执行候onTextFieldDeleteBackward里面的动作。onDraw直接返回。