REDEditBox.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "REDEditBox.h"
  2. void REDEditBoxDelegate::editBoxTextChanged(cocos2d::ui::EditBox *editBox, const std::string &text) {
  3. if (_textChangeEvent) {
  4. _textChangeEvent(editBox);
  5. }
  6. }
  7. void REDEditBoxDelegate::editBoxReturn(cocos2d::ui::EditBox *editBox) {
  8. if (_returnEvent) {
  9. _returnEvent(editBox);
  10. }
  11. }
  12. void REDEditBoxDelegate::setTextChangeEvent(ccEditBoxCallback textChangeEvent) {
  13. _textChangeEvent = textChangeEvent;
  14. }
  15. void REDEditBoxDelegate::setReturnEvent(ccEditBoxCallback returnEvent) {
  16. _returnEvent = returnEvent;
  17. }
  18. bool REDEditBox::init() {
  19. return REDEditBox::initWithSizeAndBackgroundSprite(Size(100, 30), "");
  20. }
  21. bool REDEditBox::initWithSizeAndBackgroundSprite(const Size &size, const std::string &pNormal9SpriteBg, TextureResType texType) {
  22. if (Widget::init())
  23. {
  24. _editBoxImpl = __createSystemEditBox(this);
  25. _editBoxImpl->initWithSize(size);
  26. _editBoxImpl->setInputMode(EditBox::InputMode::ANY);
  27. if (texType == Widget::TextureResType::LOCAL)
  28. {
  29. _backgroundSprite = cocos2d::ui::Scale9Sprite::create(pNormal9SpriteBg);
  30. }
  31. else
  32. {
  33. _backgroundSprite = cocos2d::ui::Scale9Sprite::createWithSpriteFrameName(pNormal9SpriteBg);
  34. }
  35. this->setContentSize(size);
  36. this->setPosition(Vec2(0, 0));
  37. if(_backgroundSprite != nullptr) {
  38. _backgroundSprite->setPosition(Vec2(_contentSize.width/2, _contentSize.height/2));
  39. _backgroundSprite->setContentSize(size);
  40. this->addProtectedChild(_backgroundSprite);
  41. }
  42. this->setTouchEnabled(true);
  43. this->addTouchEventListener(CC_CALLBACK_2(EditBox::touchDownAction, this));
  44. return true;
  45. }
  46. return false;
  47. }
  48. void REDEditBox::loadBackgroundTexture(const std::string &fileName, TextureResType texType) {
  49. this->removeProtectedChild(_backgroundSprite);
  50. if (fileName.empty())
  51. {
  52. _backgroundSprite->resetRender();
  53. }
  54. else
  55. {
  56. switch (texType)
  57. {
  58. case TextureResType::LOCAL:
  59. if (_backgroundSprite != nullptr) {
  60. _backgroundSprite->setTexture(fileName);
  61. }
  62. else {
  63. _backgroundSprite = cocos2d::ui::Scale9Sprite::create(fileName);
  64. }
  65. break;
  66. case TextureResType::PLIST:
  67. if (_backgroundSprite != nullptr) {
  68. _backgroundSprite->setSpriteFrame(fileName);
  69. }
  70. else {
  71. _backgroundSprite = cocos2d::ui::Scale9Sprite::createWithSpriteFrameName(fileName);
  72. }
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. _backgroundSprite->setPosition(Vec2(_contentSize.width/2, _contentSize.height/2));
  79. _backgroundSprite->setContentSize(_contentSize);
  80. this->addProtectedChild(_backgroundSprite);
  81. }
  82. void REDEditBox::setInsetLeft(float leftInset) {
  83. _backgroundSprite->setInsetLeft(leftInset);
  84. }
  85. float REDEditBox::getInsetLeft() const {
  86. return _backgroundSprite->getInsetLeft();
  87. }
  88. void REDEditBox::setInsetTop(float topInset) {
  89. _backgroundSprite->setInsetTop(topInset);
  90. }
  91. float REDEditBox::getInsetTop() const {
  92. return _backgroundSprite->getInsetTop();
  93. }
  94. void REDEditBox::setInsetRight(float rightInset) {
  95. _backgroundSprite->setInsetRight(rightInset);
  96. }
  97. float REDEditBox::getInsetRight() const {
  98. return _backgroundSprite->getInsetRight();
  99. }
  100. void REDEditBox::setInsetBottom(float bottomInset) {
  101. _backgroundSprite->setInsetBottom(bottomInset);
  102. }
  103. float REDEditBox::getInsetBottom() const {
  104. return _backgroundSprite->getInsetBottom();
  105. }