UITextField.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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/UITextField.h"
  22. #include "platform/CCFileUtils.h"
  23. #include "ui/UIHelper.h"
  24. #include "base/ccUTF8.h"
  25. #include "2d/CCCamera.h"
  26. NS_CC_BEGIN
  27. namespace ui {
  28. UICCTextField * UICCTextField::create()
  29. {
  30. UICCTextField *ret = new (std::nothrow) UICCTextField();
  31. if(ret)
  32. ret->autorelease();
  33. return ret;
  34. }
  35. UICCTextField::UICCTextField()
  36. : _maxLengthEnabled(false)
  37. , _maxLength(0)
  38. , _attachWithIME(false)
  39. , _detachWithIME(false)
  40. , _insertText(false)
  41. , _deleteBackward(false)
  42. {
  43. }
  44. UICCTextField::~UICCTextField()
  45. {
  46. }
  47. UICCTextField * UICCTextField::create(const std::string& placeholder, const std::string& fontName, float fontSize)
  48. {
  49. UICCTextField *pRet = new (std::nothrow) UICCTextField();
  50. if(pRet && pRet->initWithPlaceHolder("", fontName, fontSize))
  51. {
  52. pRet->autorelease();
  53. if (!placeholder.empty())
  54. {
  55. pRet->setPlaceHolder(placeholder);
  56. }
  57. return pRet;
  58. }
  59. CC_SAFE_DELETE(pRet);
  60. return nullptr;
  61. }
  62. void UICCTextField::onEnter()
  63. {
  64. TextFieldTTF::onEnter();
  65. TextFieldTTF::setDelegate(this);
  66. }
  67. bool UICCTextField::onTextFieldAttachWithIME(TextFieldTTF* /*pSender*/)
  68. {
  69. setAttachWithIME(true);
  70. return false;
  71. }
  72. bool UICCTextField::onTextFieldInsertText(TextFieldTTF* /*pSender*/, const char *text, size_t nLen)
  73. {
  74. if (nLen == 1 && strcmp(text, "\n") == 0)
  75. {
  76. return false;
  77. }
  78. setInsertText(true);
  79. if (_maxLengthEnabled)
  80. {
  81. if (static_cast<int>(TextFieldTTF::getCharCount()) >= _maxLength)
  82. {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. bool UICCTextField::onTextFieldDeleteBackward(TextFieldTTF* /*pSender*/, const char* /*delText*/, size_t /*nLen*/)
  89. {
  90. setDeleteBackward(true);
  91. return false;
  92. }
  93. bool UICCTextField::onTextFieldDetachWithIME(TextFieldTTF* /*pSender*/)
  94. {
  95. setDetachWithIME(true);
  96. return false;
  97. }
  98. void UICCTextField::insertText(const char* text, size_t len)
  99. {
  100. std::string input_text = text;
  101. if (strcmp(text, "\n") != 0)
  102. {
  103. if (_maxLengthEnabled)
  104. {
  105. long text_count = StringUtils::getCharacterCountInUTF8String(getString());
  106. if (text_count >= _maxLength)
  107. {
  108. // password
  109. if (this->isSecureTextEntry())
  110. {
  111. setPasswordText(getString());
  112. }
  113. return;
  114. }
  115. long input_count = StringUtils::getCharacterCountInUTF8String(text);
  116. long total = text_count + input_count;
  117. if (total > _maxLength)
  118. {
  119. long length = _maxLength - text_count;
  120. input_text = Helper::getSubStringOfUTF8String(input_text, 0, length);
  121. len = input_text.length();
  122. }
  123. }
  124. }
  125. TextFieldTTF::insertText(input_text.c_str(), len);
  126. }
  127. void UICCTextField::openIME()
  128. {
  129. TextFieldTTF::attachWithIME();
  130. }
  131. void UICCTextField::closeIME()
  132. {
  133. TextFieldTTF::detachWithIME();
  134. }
  135. void UICCTextField::setMaxLengthEnabled(bool enable)
  136. {
  137. _maxLengthEnabled = enable;
  138. }
  139. bool UICCTextField::isMaxLengthEnabled()const
  140. {
  141. return _maxLengthEnabled;
  142. }
  143. void UICCTextField::setMaxLength(int length)
  144. {
  145. _maxLength = length;
  146. }
  147. int UICCTextField::getMaxLength()const
  148. {
  149. return _maxLength;
  150. }
  151. std::size_t UICCTextField::getCharCount()const
  152. {
  153. return TextFieldTTF::getCharCount();
  154. }
  155. void UICCTextField::setPasswordEnabled(bool enable)
  156. {
  157. this->setSecureTextEntry(enable);
  158. }
  159. bool UICCTextField::isPasswordEnabled()const
  160. {
  161. return this->isSecureTextEntry();
  162. }
  163. void UICCTextField::setPasswordStyleText(const std::string& styleText)
  164. {
  165. this->setPasswordTextStyle(styleText);
  166. }
  167. void UICCTextField::setPasswordText(const std::string& text)
  168. {
  169. std::string tempStr = "";
  170. long text_count = StringUtils::getCharacterCountInUTF8String(text);
  171. long max = text_count;
  172. if (_maxLengthEnabled)
  173. {
  174. if (text_count > _maxLength)
  175. {
  176. max = _maxLength;
  177. }
  178. }
  179. for (int i = 0; i < max; ++i)
  180. {
  181. tempStr.append(_passwordStyleText);
  182. }
  183. Label::setString(tempStr);
  184. }
  185. void UICCTextField::setAttachWithIME(bool attach)
  186. {
  187. _attachWithIME = attach;
  188. }
  189. bool UICCTextField::getAttachWithIME()const
  190. {
  191. return _attachWithIME;
  192. }
  193. void UICCTextField::setDetachWithIME(bool detach)
  194. {
  195. _detachWithIME = detach;
  196. }
  197. bool UICCTextField::getDetachWithIME()const
  198. {
  199. return _detachWithIME;
  200. }
  201. void UICCTextField::setInsertText(bool insert)
  202. {
  203. _insertText = insert;
  204. }
  205. bool UICCTextField::getInsertText()const
  206. {
  207. return _insertText;
  208. }
  209. void UICCTextField::setDeleteBackward(bool deleteBackward)
  210. {
  211. _deleteBackward = deleteBackward;
  212. }
  213. bool UICCTextField::getDeleteBackward()const
  214. {
  215. return _deleteBackward;
  216. }
  217. static const int TEXTFIELD_RENDERER_Z = (-1);
  218. IMPLEMENT_CLASS_GUI_INFO(TextField)
  219. TextField::TextField():
  220. _textFieldRenderer(nullptr),
  221. _touchWidth(0.0f),
  222. _touchHeight(0.0f),
  223. _useTouchArea(false),
  224. _textFieldEventListener(nullptr),
  225. _textFieldEventSelector(nullptr),
  226. _eventCallback(nullptr),
  227. _textFieldRendererAdaptDirty(true),
  228. _fontName("Thonburi"),
  229. _fontSize(10),
  230. _fontType(FontType::SYSTEM)
  231. {
  232. }
  233. TextField::~TextField()
  234. {
  235. _textFieldEventListener = nullptr;
  236. _textFieldEventSelector = nullptr;
  237. }
  238. TextField* TextField::create()
  239. {
  240. TextField* widget = new (std::nothrow) TextField();
  241. if (widget && widget->init())
  242. {
  243. widget->autorelease();
  244. return widget;
  245. }
  246. CC_SAFE_DELETE(widget);
  247. return nullptr;
  248. }
  249. TextField* TextField::create(const std::string &placeholder, const std::string &fontName, int fontSize)
  250. {
  251. TextField* widget = new (std::nothrow) TextField();
  252. if (widget && widget->init())
  253. {
  254. widget->setFontName(fontName);
  255. widget->setFontSize(fontSize);
  256. widget->setPlaceHolder(placeholder);
  257. widget->autorelease();
  258. return widget;
  259. }
  260. CC_SAFE_DELETE(widget);
  261. return nullptr;
  262. }
  263. bool TextField::init()
  264. {
  265. if (Widget::init())
  266. {
  267. setTouchEnabled(true);
  268. return true;
  269. }
  270. return false;
  271. }
  272. void TextField::onEnter()
  273. {
  274. #if CC_ENABLE_SCRIPT_BINDING
  275. if (_scriptType == kScriptTypeJavascript)
  276. {
  277. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
  278. return;
  279. }
  280. #endif
  281. Widget::onEnter();
  282. scheduleUpdate();
  283. }
  284. void TextField::initRenderer()
  285. {
  286. _textFieldRenderer = UICCTextField::create();
  287. addProtectedChild(_textFieldRenderer, TEXTFIELD_RENDERER_Z, -1);
  288. }
  289. void TextField::setTouchSize(const Size &size)
  290. {
  291. _touchWidth = size.width;
  292. _touchHeight = size.height;
  293. }
  294. void TextField::setTouchAreaEnabled(bool enable)
  295. {
  296. _useTouchArea = enable;
  297. }
  298. bool TextField::hitTest(const Vec2 &pt, const Camera* camera, Vec3* /*p*/) const
  299. {
  300. if (false == _useTouchArea)
  301. {
  302. return Widget::hitTest(pt, camera, nullptr);
  303. }
  304. auto size = getContentSize();
  305. auto anch = getAnchorPoint();
  306. Rect rect((size.width - _touchWidth) * anch.x, (size.height - _touchHeight) * anch.y, _touchWidth, _touchHeight);
  307. return isScreenPointInRect(pt, camera, getWorldToNodeTransform(), rect, nullptr);
  308. }
  309. Size TextField::getTouchSize()const
  310. {
  311. return Size(_touchWidth, _touchHeight);
  312. }
  313. void TextField::setString(const std::string& text)
  314. {
  315. std::string strText(text);
  316. if (isMaxLengthEnabled())
  317. {
  318. int max = _textFieldRenderer->getMaxLength();
  319. long text_count = StringUtils::getCharacterCountInUTF8String(text);
  320. if (text_count > max)
  321. {
  322. strText = Helper::getSubStringOfUTF8String(strText, 0, max);
  323. }
  324. }
  325. if (isPasswordEnabled())
  326. {
  327. _textFieldRenderer->setPasswordText(strText);
  328. _textFieldRenderer->setString("");
  329. _textFieldRenderer->insertText(strText.c_str(), strText.size());
  330. }
  331. else
  332. {
  333. _textFieldRenderer->setString(strText);
  334. }
  335. _textFieldRendererAdaptDirty = true;
  336. updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
  337. }
  338. void TextField::setPlaceHolder(const std::string& value)
  339. {
  340. _textFieldRenderer->setPlaceHolder(value);
  341. _textFieldRendererAdaptDirty = true;
  342. updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
  343. }
  344. const std::string& TextField::getPlaceHolder()const
  345. {
  346. return _textFieldRenderer->getPlaceHolder();
  347. }
  348. const Color4B& TextField::getPlaceHolderColor()const
  349. {
  350. return _textFieldRenderer->getColorSpaceHolder();
  351. }
  352. void TextField::setPlaceHolderColor(const cocos2d::Color3B &color)
  353. {
  354. _textFieldRenderer->setColorSpaceHolder(color);
  355. }
  356. void TextField::setPlaceHolderColor(const cocos2d::Color4B &color)
  357. {
  358. _textFieldRenderer->setColorSpaceHolder(color);
  359. }
  360. const Color4B& TextField::getTextColor()const
  361. {
  362. return _textFieldRenderer->getTextColor();
  363. }
  364. void TextField::setTextColor(const cocos2d::Color4B &textColor)
  365. {
  366. _textFieldRenderer->setTextColor(textColor);
  367. }
  368. void TextField::setFontSize(int size)
  369. {
  370. if (_fontType == FontType::SYSTEM)
  371. {
  372. _textFieldRenderer->setSystemFontSize(size);
  373. }
  374. else if (_fontType == FontType::BMFONT) {
  375. _textFieldRenderer->setBMFontSize(size);
  376. }
  377. else
  378. {
  379. TTFConfig config = _textFieldRenderer->getTTFConfig();
  380. config.fontSize = size;
  381. _textFieldRenderer->setTTFConfig(config);
  382. }
  383. _fontSize = size;
  384. _textFieldRendererAdaptDirty = true;
  385. updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
  386. }
  387. int TextField::getFontSize()const
  388. {
  389. return _fontSize;
  390. }
  391. void TextField::setFontName(const std::string& name)
  392. {
  393. if(FileUtils::getInstance()->isFileExist(name))
  394. {
  395. std::string lcName = name;
  396. std::transform(lcName.begin(), lcName.end(), lcName.begin(), ::tolower);
  397. if(lcName.substr(lcName.length() - 4) == ".fnt") {
  398. _textFieldRenderer->setBMFontFilePath(name);
  399. _fontType = FontType::BMFONT;
  400. }
  401. else {
  402. TTFConfig config = _textFieldRenderer->getTTFConfig();
  403. config.fontFilePath = name;
  404. config.fontSize = _fontSize;
  405. _textFieldRenderer->setTTFConfig(config);
  406. _fontType = FontType::TTF;
  407. }
  408. }
  409. else
  410. {
  411. _textFieldRenderer->setSystemFontName(name);
  412. if (_fontType == FontType::TTF)
  413. {
  414. _textFieldRenderer->requestSystemFontRefresh();
  415. }
  416. _fontType = FontType::SYSTEM;
  417. }
  418. _fontName = name;
  419. _textFieldRendererAdaptDirty = true;
  420. updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
  421. }
  422. const std::string& TextField::getFontName()const
  423. {
  424. return _fontName;
  425. }
  426. void TextField::didNotSelectSelf()
  427. {
  428. _textFieldRenderer->detachWithIME();
  429. }
  430. const std::string& TextField::getString()const
  431. {
  432. return _textFieldRenderer->getString();
  433. }
  434. int TextField::getStringLength() const {
  435. return _textFieldRenderer->getStringLength();
  436. }
  437. bool TextField::onTouchBegan(Touch *touch, Event *unusedEvent)
  438. {
  439. bool pass = Widget::onTouchBegan(touch, unusedEvent);
  440. if (_hitted)
  441. {
  442. if (isFocusEnabled())
  443. {
  444. requestFocus();
  445. }
  446. _textFieldRenderer->attachWithIME();
  447. }
  448. else
  449. {
  450. this->didNotSelectSelf();
  451. }
  452. return pass;
  453. }
  454. void TextField::setMaxLengthEnabled(bool enable)
  455. {
  456. _textFieldRenderer->setMaxLengthEnabled(enable);
  457. }
  458. bool TextField::isMaxLengthEnabled()const
  459. {
  460. return _textFieldRenderer->isMaxLengthEnabled();
  461. }
  462. void TextField::setMaxLength(int length)
  463. {
  464. _textFieldRenderer->setMaxLength(length);
  465. setString(getString());
  466. }
  467. int TextField::getMaxLength()const
  468. {
  469. return _textFieldRenderer->getMaxLength();
  470. }
  471. void TextField::setPasswordEnabled(bool enable)
  472. {
  473. _textFieldRenderer->setPasswordEnabled(enable);
  474. }
  475. bool TextField::isPasswordEnabled()const
  476. {
  477. return _textFieldRenderer->isPasswordEnabled();
  478. }
  479. void TextField::setPasswordStyleText(const char *styleText)
  480. {
  481. _textFieldRenderer->setPasswordStyleText(styleText);
  482. setString(getString());
  483. }
  484. const char* TextField::getPasswordStyleText()const
  485. {
  486. return _textFieldRenderer->getPasswordTextStyle().c_str();
  487. }
  488. void TextField::update(float /*dt*/)
  489. {
  490. if (getDetachWithIME())
  491. {
  492. detachWithIMEEvent();
  493. setDetachWithIME(false);
  494. }
  495. if (getAttachWithIME())
  496. {
  497. attachWithIMEEvent();
  498. setAttachWithIME(false);
  499. }
  500. if (getDeleteBackward())
  501. {
  502. _textFieldRendererAdaptDirty = true;
  503. updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
  504. deleteBackwardEvent();
  505. setDeleteBackward(false);
  506. }
  507. if (getInsertText())
  508. {
  509. //we update the content size first such that when user call getContentSize() in event callback won't be wrong
  510. _textFieldRendererAdaptDirty = true;
  511. updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
  512. insertTextEvent();
  513. setInsertText(false);
  514. }
  515. }
  516. bool TextField::getAttachWithIME()const
  517. {
  518. return _textFieldRenderer->getAttachWithIME();
  519. }
  520. void TextField::setAttachWithIME(bool attach)
  521. {
  522. _textFieldRenderer->setAttachWithIME(attach);
  523. }
  524. bool TextField::getDetachWithIME()const
  525. {
  526. return _textFieldRenderer->getDetachWithIME();
  527. }
  528. void TextField::setDetachWithIME(bool detach)
  529. {
  530. _textFieldRenderer->setDetachWithIME(detach);
  531. }
  532. bool TextField::getInsertText()const
  533. {
  534. return _textFieldRenderer->getInsertText();
  535. }
  536. void TextField::setInsertText(bool insertText)
  537. {
  538. _textFieldRenderer->setInsertText(insertText);
  539. }
  540. bool TextField::getDeleteBackward()const
  541. {
  542. return _textFieldRenderer->getDeleteBackward();
  543. }
  544. void TextField::setDeleteBackward(bool deleteBackward)
  545. {
  546. _textFieldRenderer->setDeleteBackward(deleteBackward);
  547. }
  548. void TextField::attachWithIMEEvent()
  549. {
  550. this->retain();
  551. if (_textFieldEventListener && _textFieldEventSelector)
  552. {
  553. (_textFieldEventListener->*_textFieldEventSelector)(this, TEXTFIELD_EVENT_ATTACH_WITH_IME);
  554. }
  555. if (_eventCallback) {
  556. _eventCallback(this, EventType::ATTACH_WITH_IME);
  557. }
  558. if (_ccEventCallback)
  559. {
  560. _ccEventCallback(this, static_cast<int>(EventType::ATTACH_WITH_IME));
  561. }
  562. this->release();
  563. }
  564. void TextField::detachWithIMEEvent()
  565. {
  566. this->retain();
  567. if (_textFieldEventListener && _textFieldEventSelector)
  568. {
  569. (_textFieldEventListener->*_textFieldEventSelector)(this, TEXTFIELD_EVENT_DETACH_WITH_IME);
  570. }
  571. if (_eventCallback)
  572. {
  573. _eventCallback(this, EventType::DETACH_WITH_IME);
  574. }
  575. if (_ccEventCallback)
  576. {
  577. _ccEventCallback(this, static_cast<int>(EventType::DETACH_WITH_IME));
  578. }
  579. this->release();
  580. }
  581. void TextField::insertTextEvent()
  582. {
  583. this->retain();
  584. if (_textFieldEventListener && _textFieldEventSelector)
  585. {
  586. (_textFieldEventListener->*_textFieldEventSelector)(this, TEXTFIELD_EVENT_INSERT_TEXT);
  587. }
  588. if (_eventCallback)
  589. {
  590. _eventCallback(this, EventType::INSERT_TEXT);
  591. }
  592. if (_ccEventCallback)
  593. {
  594. _ccEventCallback(this, static_cast<int>(EventType::INSERT_TEXT));
  595. }
  596. this->release();
  597. }
  598. void TextField::deleteBackwardEvent()
  599. {
  600. this->retain();
  601. if (_textFieldEventListener && _textFieldEventSelector)
  602. {
  603. (_textFieldEventListener->*_textFieldEventSelector)(this, TEXTFIELD_EVENT_DELETE_BACKWARD);
  604. }
  605. if (_eventCallback)
  606. {
  607. _eventCallback(this, EventType::DELETE_BACKWARD);
  608. }
  609. if (_ccEventCallback)
  610. {
  611. _ccEventCallback(this, static_cast<int>(EventType::DELETE_BACKWARD));
  612. }
  613. this->release();
  614. }
  615. void TextField::addEventListenerTextField(Ref *target, SEL_TextFieldEvent selector)
  616. {
  617. _textFieldEventListener = target;
  618. _textFieldEventSelector = selector;
  619. }
  620. void TextField::addEventListener(const ccTextFieldCallback& callback)
  621. {
  622. _eventCallback = callback;
  623. }
  624. void TextField::onSizeChanged()
  625. {
  626. Widget::onSizeChanged();
  627. _textFieldRendererAdaptDirty = true;
  628. }
  629. void TextField::adaptRenderers()
  630. {
  631. if (_textFieldRendererAdaptDirty)
  632. {
  633. textfieldRendererScaleChangedWithSize();
  634. _textFieldRendererAdaptDirty = false;
  635. }
  636. }
  637. void TextField::textfieldRendererScaleChangedWithSize()
  638. {
  639. if (!_ignoreSize)
  640. {
  641. _textFieldRenderer->setDimensions(_contentSize.width, _contentSize.height);
  642. }
  643. _textFieldRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
  644. }
  645. Size TextField::getAutoRenderSize()
  646. {
  647. Size virtualSize = _textFieldRenderer->getContentSize();
  648. if (!_ignoreSize)
  649. {
  650. _textFieldRenderer->setDimensions(0, 0);
  651. virtualSize = _textFieldRenderer->getContentSize();
  652. _textFieldRenderer->setDimensions(_contentSize.width, _contentSize.height);
  653. }
  654. return virtualSize;
  655. }
  656. Size TextField::getVirtualRendererSize() const
  657. {
  658. return _textFieldRenderer->getContentSize();
  659. }
  660. Node* TextField::getVirtualRenderer()
  661. {
  662. return _textFieldRenderer;
  663. }
  664. std::string TextField::getDescription() const
  665. {
  666. return "TextField";
  667. }
  668. void TextField::attachWithIME()
  669. {
  670. _textFieldRenderer->attachWithIME();
  671. }
  672. Widget* TextField::createCloneInstance()
  673. {
  674. return TextField::create();
  675. }
  676. void TextField::copySpecialProperties(Widget *widget)
  677. {
  678. TextField* textField = dynamic_cast<TextField*>(widget);
  679. if (textField)
  680. {
  681. setString(textField->_textFieldRenderer->getString());
  682. setPlaceHolder(textField->getString());
  683. setFontSize(textField->_fontSize);
  684. setFontName(textField->_fontName);
  685. setMaxLengthEnabled(textField->isMaxLengthEnabled());
  686. setMaxLength(textField->getMaxLength());
  687. setPasswordEnabled(textField->isPasswordEnabled());
  688. setPasswordStyleText(textField->getPasswordStyleText());
  689. setAttachWithIME(textField->getAttachWithIME());
  690. setDetachWithIME(textField->getDetachWithIME());
  691. setInsertText(textField->getInsertText());
  692. setDeleteBackward(textField->getDeleteBackward());
  693. _eventCallback = textField->_eventCallback;
  694. _ccEventCallback = textField->_ccEventCallback;
  695. _textFieldEventListener = textField->_textFieldEventListener;
  696. _textFieldEventSelector = textField->_textFieldEventSelector;
  697. }
  698. }
  699. void TextField::setTextAreaSize(const Size &size)
  700. {
  701. this->setContentSize(size);
  702. }
  703. void TextField::setTextHorizontalAlignment(TextHAlignment alignment)
  704. {
  705. _textFieldRenderer->setHorizontalAlignment(alignment);
  706. }
  707. TextHAlignment TextField::getTextHorizontalAlignment() const
  708. {
  709. return _textFieldRenderer->getHorizontalAlignment();
  710. }
  711. void TextField::setTextVerticalAlignment(TextVAlignment alignment)
  712. {
  713. _textFieldRenderer->setVerticalAlignment(alignment);
  714. }
  715. TextVAlignment TextField::getTextVerticalAlignment() const
  716. {
  717. return _textFieldRenderer->getVerticalAlignment();
  718. }
  719. void TextField::setCursorEnabled(bool enabled)
  720. {
  721. _textFieldRenderer->setCursorEnabled(enabled);
  722. }
  723. void TextField::setCursorChar(char cursor)
  724. {
  725. _textFieldRenderer->setCursorChar(cursor);
  726. }
  727. void TextField::setCursorPosition(std::size_t cursorPosition)
  728. {
  729. _textFieldRenderer->setCursorPosition(cursorPosition);
  730. }
  731. void TextField::setCursorFromPoint(const Vec2 &point, const Camera* camera)
  732. {
  733. _textFieldRenderer->setCursorFromPoint(point, camera);
  734. }
  735. }
  736. NS_CC_END