[Solved] Handle two different TextFields

[Solved] Handle two different TextFields
0.0 0

#1

Hi.
I trying to handle two different TextFields to enter text, but touch handle always at Email field.
I dont understand what is wrong, help me please.
cocos2dx 3.8.1

header

#include "cocos2d.h"
#include "DialogBase.h"

USING_NS_CC;
using namespace ui;

class DialogAuth : public DialogBase, public TextFieldDelegate {
public:
    bool init();
    CREATE_FUNC(DialogAuth);
private:
    virtual bool onTextFieldAttachWithIME(TextFieldTTF *sender);
    virtual bool onTextFieldDetachWithIME(TextFieldTTF *sender);
    virtual bool onTextFieldInsertText(TextFieldTTF *sender, const char *text, size_t nLen);
    virtual bool onTextFieldDeleteBackward(TextFieldTTF *sender, const char *delText, size_t nLen);
    virtual bool onVisit(TextFieldTTF *sender, Renderer *renderer, Mat4 const &transform, uint32_t flags);
    bool onTextFieldTouchBegan(cocos2d::Touch* touch, cocos2d::Event *event);
};

cpp

#include "DialogAuth.h"

bool DialogAuth::init() {
    if(!DialogBase::init()) {
        return false;
    }
    
    auto lblEmail = Label::createWithTTF("Email: ", GameFont, 40);
    lblEmail->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
    lblEmail->setPosition(Vec2(this->getDialogRect().origin.x + this->getDialogRect().size.width / 2 - 10,
                               this->getDialogRect().origin.y + this->getDialogRect().size.height /2 + 100));
    this->addChild(lblEmail);
    
    auto tfEmail = TextFieldTTF::textFieldWithPlaceHolder("<enter here>", GameFont, 40);
    tfEmail->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
    tfEmail->setPosition(lblEmail->getPosition().x + 20, lblEmail->getPosition().y);
    tfEmail->setColorSpaceHolder(Color3B::WHITE);
    tfEmail->setDelegate(this);
    this->addChild(tfEmail);

    auto lblPassword = Label::createWithTTF("Password: ", GameFont, 40);
    lblPassword->setAnchorPoint(Vec2::ANCHOR_MIDDLE_RIGHT);
    lblPassword->setPosition(Vec2(lblEmail->getPosition().x, lblEmail->getPosition().y - 100));
    this->addChild(lblPassword);
    
    auto tfPassword = TextFieldTTF::textFieldWithPlaceHolder("<enter here>", GameFont, 40);
    tfPassword->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
    tfPassword->setPosition(lblPassword->getPosition().x + 20, lblPassword->getPosition().y);
    tfPassword->setColorSpaceHolder(Color3B::WHITE);
    tfPassword->setDelegate(this);
    this->addChild(tfPassword);

    auto listenerEmail = EventListenerTouchOneByOne::create();
    listenerEmail->onTouchBegan = (cocos2d::EventListenerTouchOneByOne::ccTouchBeganCallback)CC_CALLBACK_2(DialogAuth::onTextFieldTouchBegan, this);
    this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerEmail, tfEmail);

    auto listenerPassword = EventListenerTouchOneByOne::create();
    listenerPassword->onTouchBegan = (cocos2d::EventListenerTouchOneByOne::ccTouchBeganCallback)CC_CALLBACK_2(DialogAuth::onTextFieldTouchBegan, this);
    this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerPassword, tfPassword);
    
    this->addCancelButton(LanguageManager::getString("DialogAuthBackButton"));
    this->addDoneButton(LanguageManager::getString("DialogAuthNextButton"), CC_CALLBACK_1(DialogAuth::onMenuCallback, this));
    this->addCustomButton(LanguageManager::getString("DialogAuthRegisterButton"),
                          Vec2(this->getDialogRect().origin.x + this->getDialogRect().size.width/2, this->getDialogRect().origin.y + 20),
                          DialogBaseButtons::btnDoneTag+1);
    
    return true;
}

bool DialogAuth::onTextFieldAttachWithIME(TextFieldTTF *sender) {
    return TextFieldDelegate::onTextFieldAttachWithIME(sender);
}

bool DialogAuth::onTextFieldDetachWithIME(TextFieldTTF *sender) {
    return TextFieldDelegate::onTextFieldDetachWithIME(sender);
}

bool DialogAuth::onTextFieldInsertText(TextFieldTTF *sender, const char *text, size_t nLen) {
    return TextFieldDelegate::onTextFieldInsertText(sender, text, nLen);
}

bool DialogAuth::onTextFieldDeleteBackward(TextFieldTTF *sender, const char *delText, size_t nLen) {
    return TextFieldDelegate::onTextFieldDeleteBackward(sender, delText, nLen);
}

bool DialogAuth::onVisit(TextFieldTTF *sender, Renderer *renderer, const Mat4 &transform, uint32_t flags) {
    return TextFieldDelegate::onVisit(sender, renderer, transform, flags);
}

bool DialogAuth::onTextFieldTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) {
    if(auto textField = dynamic_cast<TextFieldTTF*>(event->getCurrentTarget())) {
        textField->attachWithIME();
        return true;
    } else {
        return false;
    }
}

#2

anyone can help?


#3

i am replaced TextField with EditBox, i got all my needs.


#4

Hey,

I was wondering, how did you handle the attachWithIME part using EditBox, when user uses the “Next” key on the virtual keyboard?


#5

@rcpadilha
hey, i don’t now about Next button, but here is all my project code