bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
buildUI();
addListeners();
return true;
}
void HelloWorld::buildUI(){
aTF=TextFieldTTF::textFieldWithPlaceHolder("Value","Courier",16);
aTF->setPosition(100,300);
addChild(aTF);
auto addLabel=Label::create();
addLabel->setString("+");
addChild(addLabel);
addLabel->setPosition(aTF->getPositionX()+50,aTF->getPositionY());
bTF=TextFieldTTF::textFieldWithPlaceHolder("Value","Courier",16);
bTF->setPosition(addLabel->getPositionX()+50,aTF->getPositionY());
addChild(bTF);
auto equalLabel=Label::create();
equalLabel->setString("=");
addChild(equalLabel);
equalLabel->setPosition(bTF->getPositionX()+50,aTF->getPositionY());
resultLabel=Label::create();
addChild(resultLabel);
resultLabel->setPosition(equalLabel->getPositionX()+50,aTF->getPositionY());
addBtn=Label::create();
addBtn->setString("Add");
addBtn->setSystemFontSize(16);
addChild(addBtn);
addBtn->setPosition(aTF->getPositionX(),aTF->getPositionY()-50);
}
void HelloWorld::addListeners(){
auto director=Director::getInstance();
auto handler=[=](Touch *t,Event *e){
auto target=e->getCurrentTarget();
if (target->getBoundingBox().containsPoint(t->getLocation())) {
if (aTF==target) {
aTF->attachWithIME();
}else if(bTF==target){
bTF->attachWithIME();
}else if (target==addBtn){
int a=__String::create(aTF->getString())->intValue();
int b=__String::create(bTF->getString())->intValue();
resultLabel->setString(StringUtils::format("%d",a+b));
}
}
return false;
};
auto addListenerToTarget=[director,handler](Node *target){
auto l=EventListenerTouchOneByOne::create();
l->onTouchBegan=handler;
director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(l,target);
};
addListenerToTarget(addBtn);
addListenerToTarget(aTF);
addListenerToTarget(bTF);
// auto addListener=EventListenerTouchOneByOne::create();
// addListener->onTouchBegan=handler;
// director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(addListener,addBtn);
//
// auto aTfClickListener=EventListenerTouchOneByOne::create();
// aTfClickListener->onTouchBegan=handler;
// director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(aTfClickListener,aTF);
//
// auto bTfClickListener=EventListenerTouchOneByOne::create();
// bTfClickListener->onTouchBegan=handler;
// director->getEventDispatcher()->addEventListenerWithSceneGraphPriority(bTfClickListener,bTF);
}