UITextField.cpp 20 KB

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