#include "REDEditBox.h" void REDEditBoxDelegate::editBoxTextChanged(cocos2d::ui::EditBox *editBox, const std::string &text) { if (_textChangeEvent) { _textChangeEvent(editBox); } } void REDEditBoxDelegate::editBoxReturn(cocos2d::ui::EditBox *editBox) { if (_returnEvent) { _returnEvent(editBox); } } void REDEditBoxDelegate::setTextChangeEvent(ccEditBoxCallback textChangeEvent) { _textChangeEvent = textChangeEvent; } void REDEditBoxDelegate::setReturnEvent(ccEditBoxCallback returnEvent) { _returnEvent = returnEvent; } bool REDEditBox::init() { return REDEditBox::initWithSizeAndBackgroundSprite(Size(100, 30), ""); } bool REDEditBox::initWithSizeAndBackgroundSprite(const Size &size, const std::string &pNormal9SpriteBg, TextureResType texType) { if (Widget::init()) { _editBoxImpl = __createSystemEditBox(this); _editBoxImpl->initWithSize(size); _editBoxImpl->setInputMode(EditBox::InputMode::ANY); if (texType == Widget::TextureResType::LOCAL) { _backgroundSprite = cocos2d::ui::Scale9Sprite::create(pNormal9SpriteBg); } else { _backgroundSprite = cocos2d::ui::Scale9Sprite::createWithSpriteFrameName(pNormal9SpriteBg); } this->setContentSize(size); this->setPosition(Vec2(0, 0)); if(_backgroundSprite != nullptr) { _backgroundSprite->setPosition(Vec2(_contentSize.width/2, _contentSize.height/2)); _backgroundSprite->setContentSize(size); this->addProtectedChild(_backgroundSprite); } this->setTouchEnabled(true); this->addTouchEventListener(CC_CALLBACK_2(EditBox::touchDownAction, this)); return true; } return false; } void REDEditBox::loadBackgroundTexture(const std::string &fileName, TextureResType texType) { this->removeProtectedChild(_backgroundSprite); if (fileName.empty()) { _backgroundSprite->resetRender(); } else { switch (texType) { case TextureResType::LOCAL: if (_backgroundSprite != nullptr) { _backgroundSprite->setTexture(fileName); } else { _backgroundSprite = cocos2d::ui::Scale9Sprite::create(fileName); } break; case TextureResType::PLIST: if (_backgroundSprite != nullptr) { _backgroundSprite->setSpriteFrame(fileName); } else { _backgroundSprite = cocos2d::ui::Scale9Sprite::createWithSpriteFrameName(fileName); } break; default: break; } } _backgroundSprite->setPosition(Vec2(_contentSize.width/2, _contentSize.height/2)); _backgroundSprite->setContentSize(_contentSize); this->addProtectedChild(_backgroundSprite); } void REDEditBox::setInsetLeft(float leftInset) { _backgroundSprite->setInsetLeft(leftInset); } float REDEditBox::getInsetLeft() const { return _backgroundSprite->getInsetLeft(); } void REDEditBox::setInsetTop(float topInset) { _backgroundSprite->setInsetTop(topInset); } float REDEditBox::getInsetTop() const { return _backgroundSprite->getInsetTop(); } void REDEditBox::setInsetRight(float rightInset) { _backgroundSprite->setInsetRight(rightInset); } float REDEditBox::getInsetRight() const { return _backgroundSprite->getInsetRight(); } void REDEditBox::setInsetBottom(float bottomInset) { _backgroundSprite->setInsetBottom(bottomInset); } float REDEditBox::getInsetBottom() const { return _backgroundSprite->getInsetBottom(); }