Skip to content

Instantly share code, notes, and snippets.

@tks2shimizu
Created July 10, 2013 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tks2shimizu/d0238caf278756d065c2 to your computer and use it in GitHub Desktop.
Save tks2shimizu/d0238caf278756d065c2 to your computer and use it in GitHub Desktop.
テキスト入力(1) w/ delegate
#include "HelloWorldScene.h"
USING_NS_CC;
USING_NS_CC_EXT;
CCScene* HelloWorld::scene()
{
CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !CCLayer::init() )
{
return false;
}
setTouchEnabled(true);
setTouchMode(kCCTouchesOneByOne);
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
m_textField = CCTextFieldTTF::textFieldWithPlaceHolder("文字を入力",
"Thonburi",
32);
m_textField->setPosition(ccp(winSize.width / 2, winSize.height * 2 / 3));
m_textField->setDelegate(this);
this->addChild(m_textField);
return true;
}
bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
return true;
}
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
CCPoint point = convertTouchToNodeSpace(pTouch);
CCRect rect = m_textField->boundingBox();
if (rect.containsPoint(point)) {
//テキストフィールドがタップされたら、キーボードを表示する
m_textField->attachWithIME();
}
}
bool HelloWorld::onTextFieldAttachWithIME(CCTextFieldTTF * sender)
{
CCLog("onTextFieldAttachWithIME");
return false;
}
bool HelloWorld::onTextFieldDetachWithIME(CCTextFieldTTF * sender)
{
CCLog("onTextFieldDetachWithIME");
return false;
}
bool HelloWorld::onTextFieldInsertText(CCTextFieldTTF * sender, const char * text, int nLen)
{
CCLog("onTextFieldInsertText %s, %d", text, nLen);
return false;
}
bool HelloWorld::onTextFieldDeleteBackward(CCTextFieldTTF * sender, const char * delText, int nLen)
{
CCLog("onTextFieldDeleteBackward %s, %d", delText, nLen);
return false;
}
bool HelloWorld::onDraw(CCTextFieldTTF * sender)
{
return false;
}
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "CCControlExtensions.h"
class HelloWorld : public cocos2d::CCLayer,
public cocos2d::CCTextFieldDelegate
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
CREATE_FUNC(HelloWorld);
// CCLayer
virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual bool onTextFieldAttachWithIME(cocos2d::CCTextFieldTTF * sender);
virtual bool onTextFieldDetachWithIME(cocos2d::CCTextFieldTTF * sender);
virtual bool onTextFieldInsertText(cocos2d::CCTextFieldTTF * sender, const char * text, int nLen);
virtual bool onTextFieldDeleteBackward(cocos2d::CCTextFieldTTF * sender, const char * delText, int nLen);
virtual bool onDraw(cocos2d::CCTextFieldTTF * sender);
protected:
cocos2d::CCTextFieldTTF * m_textField;
};
#endif // __HELLOWORLD_SCENE_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment