UIEditBox.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2012 James Chen
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "ui/UIEditBox/UIEditBox.h"
  22. #include "ui/UIEditBox/UIEditBoxImpl.h"
  23. NS_CC_BEGIN
  24. namespace ui {
  25. static const float CHECK_EDITBOX_POSITION_INTERVAL = 0.1f;
  26. EditBox::EditBox(void)
  27. : _editBoxImpl(nullptr)
  28. , _delegate(nullptr)
  29. , _backgroundSprite(nullptr)
  30. #if CC_ENABLE_SCRIPT_BINDING
  31. , _scriptEditBoxHandler(0)
  32. #endif
  33. {
  34. }
  35. EditBox::~EditBox(void)
  36. {
  37. CC_SAFE_DELETE(_editBoxImpl);
  38. #if CC_ENABLE_SCRIPT_BINDING
  39. unregisterScriptEditBoxHandler();
  40. #endif
  41. }
  42. void EditBox::touchDownAction(Ref* /*sender*/, TouchEventType controlEvent)
  43. {
  44. if (controlEvent == Widget::TouchEventType::ENDED) {
  45. _editBoxImpl->openKeyboard();
  46. }
  47. }
  48. EditBox* EditBox::create(const Size& size,
  49. const std::string& normalSprite,
  50. TextureResType texType /*= TextureResType::LOCAL*/)
  51. {
  52. EditBox* pRet = new (std::nothrow) EditBox();
  53. if (pRet != nullptr && pRet->initWithSizeAndBackgroundSprite(size, normalSprite, texType))
  54. {
  55. pRet->autorelease();
  56. }
  57. else
  58. {
  59. CC_SAFE_DELETE(pRet);
  60. }
  61. return pRet;
  62. }
  63. EditBox* EditBox::create(const cocos2d::Size &size, cocos2d::ui::Scale9Sprite *normalSprite, ui::Scale9Sprite* /*pressedSprite*/, Scale9Sprite* /*disabledSprite*/)
  64. {
  65. EditBox* pRet = new (std::nothrow) EditBox();
  66. if (pRet != nullptr && pRet->initWithSizeAndBackgroundSprite(size, normalSprite))
  67. {
  68. pRet->autorelease();
  69. }
  70. else
  71. {
  72. CC_SAFE_DELETE(pRet);
  73. }
  74. return pRet;
  75. }
  76. bool EditBox::initWithSizeAndBackgroundSprite(const cocos2d::Size &size, cocos2d::ui::Scale9Sprite *pNormal9SpriteBg)
  77. {
  78. if (Widget::init())
  79. {
  80. _editBoxImpl = __createSystemEditBox(this);
  81. _editBoxImpl->initWithSize(size);
  82. _editBoxImpl->setInputMode(EditBox::InputMode::ANY);
  83. _backgroundSprite = pNormal9SpriteBg;
  84. this->setContentSize(size);
  85. this->setPosition(Vec2(0, 0));
  86. _backgroundSprite->setPosition(Vec2(_contentSize.width/2, _contentSize.height/2));
  87. _backgroundSprite->setContentSize(size);
  88. this->addProtectedChild(_backgroundSprite);
  89. this->setTouchEnabled(true);
  90. this->addTouchEventListener(CC_CALLBACK_2(EditBox::touchDownAction, this));
  91. return true;
  92. }
  93. return false;
  94. }
  95. bool EditBox::initWithSizeAndBackgroundSprite(const Size& size,
  96. const std::string& pNormal9SpriteBg,
  97. TextureResType texType)
  98. {
  99. if (Widget::init())
  100. {
  101. _editBoxImpl = __createSystemEditBox(this);
  102. _editBoxImpl->initWithSize(size);
  103. _editBoxImpl->setInputMode(EditBox::InputMode::ANY);
  104. if (texType == Widget::TextureResType::LOCAL)
  105. {
  106. _backgroundSprite = Scale9Sprite::create(pNormal9SpriteBg);
  107. }
  108. else
  109. {
  110. _backgroundSprite = Scale9Sprite::createWithSpriteFrameName(pNormal9SpriteBg);
  111. }
  112. this->setContentSize(size);
  113. this->setPosition(Vec2(0, 0));
  114. _backgroundSprite->setPosition(Vec2(_contentSize.width/2, _contentSize.height/2));
  115. _backgroundSprite->setContentSize(size);
  116. this->addProtectedChild(_backgroundSprite);
  117. this->setTouchEnabled(true);
  118. this->addTouchEventListener(CC_CALLBACK_2(EditBox::touchDownAction, this));
  119. return true;
  120. }
  121. return false;
  122. }
  123. void EditBox::setDelegate(EditBoxDelegate* pDelegate)
  124. {
  125. _delegate = pDelegate;
  126. if (_editBoxImpl != nullptr)
  127. {
  128. _editBoxImpl->setDelegate(pDelegate);
  129. }
  130. }
  131. EditBoxDelegate* EditBox::getDelegate()
  132. {
  133. return _delegate;
  134. }
  135. void EditBox::setText(const char* pText)
  136. {
  137. if (pText != nullptr)
  138. {
  139. if (_editBoxImpl != nullptr)
  140. {
  141. _editBoxImpl->setText(pText);
  142. }
  143. }
  144. }
  145. const char* EditBox::getText(void) const
  146. {
  147. if (_editBoxImpl != nullptr)
  148. {
  149. const char* pText = _editBoxImpl->getText();
  150. if(pText != nullptr)
  151. return pText;
  152. }
  153. return "";
  154. }
  155. void EditBox::setFont(const char* pFontName, int fontSize)
  156. {
  157. CCASSERT(pFontName != nullptr, "fontName can't be nullptr");
  158. if (pFontName != nullptr)
  159. {
  160. if (_editBoxImpl != nullptr)
  161. {
  162. _editBoxImpl->setFont(pFontName, fontSize);
  163. }
  164. }
  165. }
  166. void EditBox::setFontName(const char* pFontName)
  167. {
  168. CCASSERT(pFontName != nullptr, "fontName can't be nullptr");
  169. if (_editBoxImpl != nullptr)
  170. {
  171. _editBoxImpl->setFont(pFontName, _editBoxImpl->getFontSize());
  172. }
  173. }
  174. const char* EditBox::getFontName(void) const
  175. {
  176. if (_editBoxImpl != nullptr)
  177. {
  178. return _editBoxImpl->getFontName();
  179. }
  180. return "";
  181. }
  182. void EditBox::setFontSize(int fontSize)
  183. {
  184. if (_editBoxImpl != nullptr)
  185. {
  186. _editBoxImpl->setFont(_editBoxImpl->getFontName(), fontSize);
  187. }
  188. }
  189. int EditBox::getFontSize() const
  190. {
  191. if (_editBoxImpl != nullptr)
  192. {
  193. return _editBoxImpl->getFontSize();
  194. }
  195. return -1;
  196. }
  197. void EditBox::setFontColor(const Color3B& color)
  198. {
  199. setFontColor(Color4B(color));
  200. }
  201. void EditBox::setFontColor(const Color4B& color)
  202. {
  203. if (_editBoxImpl != nullptr)
  204. {
  205. _editBoxImpl->setFontColor(color);
  206. }
  207. }
  208. const Color4B& EditBox::getFontColor() const
  209. {
  210. if (_editBoxImpl != nullptr)
  211. {
  212. return _editBoxImpl->getFontColor();
  213. }
  214. return Color4B::WHITE;
  215. }
  216. void EditBox::setPlaceholderFont(const char* pFontName, int fontSize)
  217. {
  218. CCASSERT(pFontName != nullptr, "fontName can't be nullptr");
  219. if (pFontName != nullptr)
  220. {
  221. if (_editBoxImpl != nullptr)
  222. {
  223. _editBoxImpl->setPlaceholderFont(pFontName, fontSize);
  224. }
  225. }
  226. }
  227. void EditBox::setPlaceholderFontName(const char* pFontName)
  228. {
  229. CCASSERT(pFontName != nullptr, "fontName can't be nullptr");
  230. if (_editBoxImpl != nullptr)
  231. {
  232. _editBoxImpl->setPlaceholderFont(pFontName, _editBoxImpl->getPlaceholderFontSize());
  233. }
  234. }
  235. const char* EditBox::getPlaceholderFontName() const
  236. {
  237. if (_editBoxImpl != nullptr)
  238. {
  239. return _editBoxImpl->getPlaceholderFontName();
  240. }
  241. return "";
  242. }
  243. void EditBox::setPlaceholderFontSize(int fontSize)
  244. {
  245. if (_editBoxImpl != nullptr)
  246. {
  247. _editBoxImpl->setPlaceholderFont(_editBoxImpl->getPlaceholderFontName(), fontSize);
  248. }
  249. }
  250. int EditBox::getPlaceholderFontSize() const
  251. {
  252. if (_editBoxImpl != nullptr)
  253. {
  254. return _editBoxImpl->getPlaceholderFontSize();
  255. }
  256. return -1;
  257. }
  258. void EditBox::setPlaceholderFontColor(const Color3B& color)
  259. {
  260. setPlaceholderFontColor(Color4B(color));
  261. }
  262. void EditBox::setPlaceholderFontColor(const Color4B& color)
  263. {
  264. if (_editBoxImpl != nullptr)
  265. {
  266. _editBoxImpl->setPlaceholderFontColor(color);
  267. }
  268. }
  269. const Color4B& EditBox::getPlaceholderFontColor() const
  270. {
  271. if (_editBoxImpl != nullptr)
  272. {
  273. return _editBoxImpl->getPlaceholderFontColor();
  274. }
  275. return Color4B::GRAY;
  276. }
  277. void EditBox::setPlaceHolder(const char* pText)
  278. {
  279. if (pText != nullptr)
  280. {
  281. if (_editBoxImpl != nullptr)
  282. {
  283. _editBoxImpl->setPlaceHolder(pText);
  284. }
  285. }
  286. }
  287. const char* EditBox::getPlaceHolder(void) const
  288. {
  289. if (_editBoxImpl != nullptr)
  290. {
  291. return _editBoxImpl->getPlaceHolder();
  292. }
  293. return "";
  294. }
  295. void EditBox::setInputMode(EditBox::InputMode inputMode)
  296. {
  297. if (_editBoxImpl != nullptr)
  298. {
  299. _editBoxImpl->setInputMode(inputMode);
  300. }
  301. }
  302. EditBox::InputMode EditBox::getInputMode() const
  303. {
  304. if (_editBoxImpl != nullptr)
  305. {
  306. return _editBoxImpl->getInputMode();
  307. }
  308. return InputMode::SINGLE_LINE;
  309. }
  310. void EditBox::setMaxLength(int maxLength)
  311. {
  312. if (_editBoxImpl != nullptr)
  313. {
  314. _editBoxImpl->setMaxLength(maxLength);
  315. }
  316. }
  317. int EditBox::getMaxLength()
  318. {
  319. if (_editBoxImpl != nullptr)
  320. {
  321. return _editBoxImpl->getMaxLength();
  322. }
  323. return -1;
  324. }
  325. void EditBox::setInputFlag(EditBox::InputFlag inputFlag)
  326. {
  327. if (_editBoxImpl != nullptr)
  328. {
  329. _editBoxImpl->setInputFlag(inputFlag);
  330. }
  331. }
  332. EditBox::InputFlag EditBox::getInputFlag() const
  333. {
  334. if (_editBoxImpl != nullptr)
  335. {
  336. return _editBoxImpl->getInputFlag();
  337. }
  338. return InputFlag::LOWERCASE_ALL_CHARACTERS;
  339. }
  340. void EditBox::setReturnType(EditBox::KeyboardReturnType returnType)
  341. {
  342. if (_editBoxImpl != nullptr)
  343. {
  344. _editBoxImpl->setReturnType(returnType);
  345. }
  346. }
  347. EditBox::KeyboardReturnType EditBox::getReturnType() const
  348. {
  349. if (_editBoxImpl != nullptr)
  350. {
  351. return _editBoxImpl->getReturnType();
  352. }
  353. return KeyboardReturnType::DEFAULT;
  354. }
  355. void EditBox::setTextHorizontalAlignment(TextHAlignment alignment)
  356. {
  357. if (_editBoxImpl != nullptr)
  358. {
  359. _editBoxImpl->setTextHorizontalAlignment(alignment);
  360. }
  361. }
  362. TextHAlignment EditBox::getTextHorizontalAlignment() const
  363. {
  364. if (_editBoxImpl != nullptr)
  365. {
  366. return _editBoxImpl->getTextHorizontalAlignment();
  367. }
  368. return TextHAlignment::LEFT;
  369. }
  370. /* override function */
  371. void EditBox::setPosition(const Vec2& pos)
  372. {
  373. Widget::setPosition(pos);
  374. if (_editBoxImpl != nullptr)
  375. {
  376. _editBoxImpl->setPosition(pos);
  377. }
  378. }
  379. void EditBox::setVisible(bool visible)
  380. {
  381. Widget::setVisible(visible);
  382. if (_editBoxImpl != nullptr)
  383. {
  384. _editBoxImpl->setVisible(visible);
  385. }
  386. }
  387. void EditBox::setContentSize(const Size& size)
  388. {
  389. Widget::setContentSize(size);
  390. if (_editBoxImpl != nullptr)
  391. {
  392. _editBoxImpl->setContentSize(size);
  393. }
  394. }
  395. void EditBox::adaptRenderers()
  396. {
  397. if (_contentSizeDirty)
  398. {
  399. _backgroundSprite->setContentSize(_contentSize);
  400. _backgroundSprite->setPosition(Vec2(_contentSize.width/2, _contentSize.height/2));
  401. }
  402. }
  403. void EditBox::setAnchorPoint(const Vec2& anchorPoint)
  404. {
  405. Widget::setAnchorPoint(anchorPoint);
  406. if (_editBoxImpl != nullptr)
  407. {
  408. _editBoxImpl->setAnchorPoint(anchorPoint);
  409. }
  410. }
  411. std::string EditBox::getDescription() const
  412. {
  413. return "EditBox";
  414. }
  415. void EditBox::draw(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
  416. {
  417. Widget::draw(renderer, parentTransform, parentFlags);
  418. if (_editBoxImpl != nullptr)
  419. {
  420. _editBoxImpl->draw(renderer, parentTransform, parentFlags & FLAGS_TRANSFORM_DIRTY);
  421. }
  422. }
  423. void EditBox::onEnter(void)
  424. {
  425. #if CC_ENABLE_SCRIPT_BINDING
  426. if (_scriptType == kScriptTypeJavascript)
  427. {
  428. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
  429. return;
  430. }
  431. #endif
  432. Widget::onEnter();
  433. if (_editBoxImpl != nullptr)
  434. {
  435. _editBoxImpl->onEnter();
  436. }
  437. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  438. this->schedule(CC_SCHEDULE_SELECTOR(EditBox::updatePosition), CHECK_EDITBOX_POSITION_INTERVAL);
  439. #endif
  440. }
  441. void EditBox::updatePosition(float dt)
  442. {
  443. if (nullptr != _editBoxImpl) {
  444. _editBoxImpl->updatePosition(dt);
  445. }
  446. }
  447. void EditBox::onExit(void)
  448. {
  449. #if CC_ENABLE_SCRIPT_BINDING
  450. if (_scriptType == kScriptTypeJavascript)
  451. {
  452. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExit))
  453. return;
  454. }
  455. #endif
  456. Widget::onExit();
  457. if (_editBoxImpl != nullptr)
  458. {
  459. // remove system edit control
  460. _editBoxImpl->closeKeyboard();
  461. }
  462. }
  463. static Rect getRect(Node * pNode)
  464. {
  465. Size contentSize = pNode->getContentSize();
  466. Rect rect = Rect(0, 0, contentSize.width, contentSize.height);
  467. return RectApplyTransform(rect, pNode->getNodeToWorldTransform());
  468. }
  469. void EditBox::keyboardWillShow(IMEKeyboardNotificationInfo& info)
  470. {
  471. // CCLOG("CCEditBox::keyboardWillShow");
  472. Rect rectTracked = getRect(this);
  473. // some adjustment for margin between the keyboard and the edit box.
  474. rectTracked.origin.y -= 4;
  475. // if the keyboard area doesn't intersect with the tracking node area, nothing needs to be done.
  476. if (!rectTracked.intersectsRect(info.end))
  477. {
  478. CCLOG("needn't to adjust view layout.");
  479. return;
  480. }
  481. // assume keyboard at the bottom of screen, calculate the vertical adjustment.
  482. _adjustHeight = info.end.getMaxY() - rectTracked.getMinY();
  483. // CCLOG("CCEditBox:needAdjustVerticalPosition(%f)", _adjustHeight);
  484. if (_editBoxImpl != nullptr)
  485. {
  486. _editBoxImpl->doAnimationWhenKeyboardMove(info.duration, _adjustHeight);
  487. }
  488. }
  489. void EditBox::keyboardDidShow(IMEKeyboardNotificationInfo& /*info*/)
  490. {
  491. }
  492. void EditBox::keyboardWillHide(IMEKeyboardNotificationInfo& info)
  493. {
  494. // CCLOG("CCEditBox::keyboardWillHide");
  495. if (_editBoxImpl != nullptr)
  496. {
  497. _editBoxImpl->doAnimationWhenKeyboardMove(info.duration, -_adjustHeight);
  498. }
  499. }
  500. void EditBox::keyboardDidHide(IMEKeyboardNotificationInfo& /*info*/)
  501. {
  502. }
  503. #if CC_ENABLE_SCRIPT_BINDING
  504. void EditBox::registerScriptEditBoxHandler(int handler)
  505. {
  506. unregisterScriptEditBoxHandler();
  507. _scriptEditBoxHandler = handler;
  508. }
  509. void EditBox::unregisterScriptEditBoxHandler(void)
  510. {
  511. if (0 != _scriptEditBoxHandler)
  512. {
  513. ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptHandler(_scriptEditBoxHandler);
  514. _scriptEditBoxHandler = 0;
  515. }
  516. }
  517. #endif
  518. }
  519. NS_CC_END