UIEditBoxImpl-common.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2012 James Chen
  4. Copyright (c) 2013-2015 zilongshanren
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "ui/UIEditBox/UIEditBoxImpl-common.h"
  24. #define kLabelZOrder 9999
  25. #include "ui/UIEditBox/UIEditBox.h"
  26. #include "base/CCDirector.h"
  27. #include "2d/CCLabel.h"
  28. #include "ui/UIHelper.h"
  29. static const int CC_EDIT_BOX_PADDING = 5;
  30. static cocos2d::Size applyPadding(const cocos2d::Size& sizeToCorrect) {
  31. return cocos2d::Size(sizeToCorrect.width - CC_EDIT_BOX_PADDING * 2, sizeToCorrect.height);
  32. }
  33. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  34. #define PASSWORD_CHAR "*"
  35. #else
  36. #define PASSWORD_CHAR "\u25CF"
  37. #endif
  38. NS_CC_BEGIN
  39. namespace ui {
  40. EditBoxImplCommon::EditBoxImplCommon(EditBox* pEditText)
  41. : EditBoxImpl(pEditText)
  42. , _label(nullptr)
  43. , _labelPlaceHolder(nullptr)
  44. , _editBoxInputMode(EditBox::InputMode::SINGLE_LINE)
  45. , _editBoxInputFlag(EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS)
  46. , _keyboardReturnType(EditBox::KeyboardReturnType::DEFAULT)
  47. , _fontSize(-1)
  48. , _placeholderFontSize(-1)
  49. , _colText(Color3B::WHITE)
  50. , _colPlaceHolder(Color3B::GRAY)
  51. , _maxLength(-1)
  52. , _alignment(TextHAlignment::LEFT)
  53. , _editingMode(false)
  54. {
  55. }
  56. EditBoxImplCommon::~EditBoxImplCommon()
  57. {
  58. }
  59. bool EditBoxImplCommon::initWithSize(const Size& size)
  60. {
  61. do
  62. {
  63. Rect rect = Rect(0, 0, size.width, size.height);
  64. this->createNativeControl(rect);
  65. initInactiveLabels(size);
  66. setContentSize(size);
  67. return true;
  68. }while (0);
  69. return false;
  70. }
  71. void EditBoxImplCommon::initInactiveLabels(const Size& size)
  72. {
  73. const char* pDefaultFontName = this->getNativeDefaultFontName();
  74. _label = Label::create();
  75. _label->setAnchorPoint(Vec2(0,1));
  76. _label->setOverflow(Label::Overflow::CLAMP);
  77. _label->setVisible(false);
  78. _editBox->addChild(_label, kLabelZOrder);
  79. _labelPlaceHolder = Label::create();
  80. _labelPlaceHolder->setAnchorPoint(Vec2(0, 1.0f));
  81. _labelPlaceHolder->setTextColor(Color4B::GRAY);
  82. _labelPlaceHolder->enableWrap(false);
  83. _editBox->addChild(_labelPlaceHolder, kLabelZOrder);
  84. setFont(pDefaultFontName, size.height*2/3);
  85. setPlaceholderFont(pDefaultFontName, size.height*2/3);
  86. }
  87. void EditBoxImplCommon::placeInactiveLabels(const Size& size)
  88. {
  89. _label->setDimensions(size.width, size.height);
  90. auto placeholderSize = _labelPlaceHolder->getContentSize();
  91. if(_editBoxInputMode == EditBox::InputMode::ANY){
  92. _label->setPosition(Vec2(CC_EDIT_BOX_PADDING, size.height - CC_EDIT_BOX_PADDING));
  93. _label->setVerticalAlignment(TextVAlignment::TOP);
  94. _label->enableWrap(true);
  95. _labelPlaceHolder->setPosition(Vec2(CC_EDIT_BOX_PADDING,
  96. size.height - CC_EDIT_BOX_PADDING));
  97. _labelPlaceHolder->setVerticalAlignment(TextVAlignment::TOP);
  98. }
  99. else {
  100. _label->enableWrap(false);
  101. _label->setPosition(Vec2(CC_EDIT_BOX_PADDING, size.height));
  102. _label->setVerticalAlignment(TextVAlignment::CENTER);
  103. _labelPlaceHolder->setPosition(Vec2(CC_EDIT_BOX_PADDING,
  104. (size.height + placeholderSize.height) / 2));
  105. _labelPlaceHolder->setVerticalAlignment(TextVAlignment::CENTER);
  106. }
  107. }
  108. void EditBoxImplCommon::setInactiveText(const char* pText)
  109. {
  110. if(EditBox::InputFlag::PASSWORD == _editBoxInputFlag)
  111. {
  112. std::string passwordString;
  113. for(size_t i = 0; i < strlen(pText); ++i)
  114. passwordString.append(PASSWORD_CHAR);
  115. _label->setString(passwordString);
  116. }
  117. else
  118. {
  119. _label->setString(pText);
  120. }
  121. // Clip the text width to fit to the text box
  122. const auto maxSize = applyPadding(_editBox->getContentSize());
  123. Size labelSize = _label->getContentSize();
  124. if(labelSize.width > maxSize.width || labelSize.height > maxSize.height)
  125. {
  126. _label->setDimensions(maxSize.width, maxSize.height);
  127. }
  128. }
  129. void EditBoxImplCommon::setFont(const char* pFontName, int fontSize)
  130. {
  131. _fontName = pFontName;
  132. _fontSize = fontSize;
  133. this->setNativeFont(pFontName, fontSize * _label->getNodeToWorldAffineTransform().a);
  134. if (!_fontName.empty())
  135. {
  136. _label->setSystemFontName(pFontName);
  137. }
  138. if (fontSize > 0)
  139. {
  140. _label->setSystemFontSize(fontSize);
  141. }
  142. }
  143. void EditBoxImplCommon::setFontColor(const Color4B& color)
  144. {
  145. _colText = color;
  146. this->setNativeFontColor(color);
  147. _label->setTextColor(color);
  148. }
  149. void EditBoxImplCommon::setPlaceholderFont(const char* pFontName, int fontSize)
  150. {
  151. _placeholderFontName = pFontName;
  152. _placeholderFontSize = fontSize;
  153. this->setNativePlaceholderFont(pFontName, fontSize * _labelPlaceHolder->getNodeToWorldAffineTransform().a);
  154. if (!_placeholderFontName.empty())
  155. {
  156. _labelPlaceHolder->setSystemFontName(pFontName);
  157. }
  158. if (fontSize > 0)
  159. {
  160. _labelPlaceHolder->setSystemFontSize(fontSize);
  161. }
  162. }
  163. void EditBoxImplCommon::setPlaceholderFontColor(const Color4B &color)
  164. {
  165. _colPlaceHolder = color;
  166. this->setNativePlaceholderFontColor(color);
  167. _labelPlaceHolder->setTextColor(color);
  168. }
  169. void EditBoxImplCommon::setInputMode(EditBox::InputMode inputMode)
  170. {
  171. _editBoxInputMode = inputMode;
  172. this->setNativeInputMode(inputMode);
  173. this->placeInactiveLabels(applyPadding(_editBox->getContentSize()));
  174. }
  175. void EditBoxImplCommon::setMaxLength(int maxLength)
  176. {
  177. _maxLength = maxLength;
  178. this->setNativeMaxLength(maxLength);
  179. }
  180. void EditBoxImplCommon::setTextHorizontalAlignment(cocos2d::TextHAlignment alignment)
  181. {
  182. _alignment = alignment;
  183. this->setNativeTextHorizontalAlignment(alignment);
  184. refreshLabelAlignment();
  185. }
  186. void EditBoxImplCommon::setInputFlag(EditBox::InputFlag inputFlag)
  187. {
  188. _editBoxInputFlag = inputFlag;
  189. this->setNativeInputFlag(inputFlag);
  190. }
  191. void EditBoxImplCommon::setReturnType(EditBox::KeyboardReturnType returnType)
  192. {
  193. _keyboardReturnType = returnType;
  194. this->setNativeReturnType(returnType);
  195. }
  196. void EditBoxImplCommon::refreshInactiveText()
  197. {
  198. setInactiveText(_text.c_str());
  199. refreshLabelAlignment();
  200. if (!_editingMode) {
  201. if (_text.size() == 0) {
  202. _label->setVisible(false);
  203. _labelPlaceHolder->setVisible(true);
  204. } else {
  205. _label->setVisible(true);
  206. _labelPlaceHolder->setVisible(false);
  207. }
  208. }
  209. }
  210. void EditBoxImplCommon::refreshLabelAlignment()
  211. {
  212. _label->setHorizontalAlignment(_alignment);
  213. _labelPlaceHolder->setHorizontalAlignment(_alignment);
  214. }
  215. void EditBoxImplCommon::setText(const char* text)
  216. {
  217. if (nullptr != text) {
  218. this->setNativeText(text);
  219. _text = text;
  220. refreshInactiveText();
  221. }
  222. }
  223. void EditBoxImplCommon::setPlaceHolder(const char* pText)
  224. {
  225. if (pText != NULL)
  226. {
  227. _placeHolder = pText;
  228. this->setNativePlaceHolder(pText);
  229. _labelPlaceHolder->setString(_placeHolder);
  230. }
  231. }
  232. void EditBoxImplCommon::setVisible(bool visible)
  233. {
  234. if(visible) {
  235. refreshInactiveText();
  236. } else {
  237. this->setNativeVisible(visible);
  238. _label->setVisible(visible);
  239. _labelPlaceHolder->setVisible(visible);
  240. }
  241. }
  242. void EditBoxImplCommon::setContentSize(const Size& size)
  243. {
  244. _contentSize = applyPadding(size);
  245. CCLOG("[Edit text] content size = (%f, %f)", _contentSize.width, _contentSize.height);
  246. placeInactiveLabels(_contentSize);
  247. }
  248. void EditBoxImplCommon::draw(Renderer* /*renderer*/, const Mat4& /*transform*/, uint32_t flags)
  249. {
  250. if(flags)
  251. {
  252. auto rect = ui::Helper::convertBoundingBoxToScreen(_editBox);
  253. this->updateNativeFrame(rect);
  254. }
  255. }
  256. void EditBoxImplCommon::onEnter(void)
  257. {
  258. const char* pText = getText();
  259. if (pText) {
  260. setInactiveText(pText);
  261. }
  262. }
  263. void EditBoxImplCommon::openKeyboard()
  264. {
  265. _label->setVisible(false);
  266. _labelPlaceHolder->setVisible(false);
  267. _editingMode = true;
  268. this->setNativeVisible(true);
  269. this->nativeOpenKeyboard();
  270. }
  271. void EditBoxImplCommon::closeKeyboard()
  272. {
  273. this->nativeCloseKeyboard();
  274. _editingMode = false;
  275. }
  276. void EditBoxImplCommon::onEndEditing(const std::string& /*text*/)
  277. {
  278. _editingMode = false;
  279. this->setNativeVisible(false);
  280. refreshInactiveText();
  281. }
  282. void EditBoxImplCommon::editBoxEditingDidBegin()
  283. {
  284. // LOGD("textFieldShouldBeginEditing...");
  285. cocos2d::ui::EditBoxDelegate *pDelegate = _editBox->getDelegate();
  286. if (pDelegate != nullptr)
  287. {
  288. pDelegate->editBoxEditingDidBegin(_editBox);
  289. }
  290. #if CC_ENABLE_SCRIPT_BINDING
  291. if (NULL != _editBox && 0 != _editBox->getScriptEditBoxHandler())
  292. {
  293. cocos2d::CommonScriptData data(_editBox->getScriptEditBoxHandler(), "began", _editBox);
  294. cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
  295. cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  296. }
  297. #endif
  298. }
  299. void EditBoxImplCommon::editBoxEditingDidEnd(const std::string& text, EditBoxDelegate::EditBoxEndAction action)
  300. {
  301. // LOGD("textFieldShouldEndEditing...");
  302. _text = text;
  303. cocos2d::ui::EditBoxDelegate *pDelegate = _editBox->getDelegate();
  304. if (pDelegate != nullptr)
  305. {
  306. pDelegate->editBoxEditingDidEndWithAction(_editBox, action);
  307. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  308. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  309. #elif _MSC_VER >= 1400 //vs 2005 or higher
  310. #pragma warning (push)
  311. #pragma warning (disable: 4996)
  312. #endif
  313. pDelegate->editBoxEditingDidEnd(_editBox);
  314. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  315. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  316. #elif _MSC_VER >= 1400 //vs 2005 or higher
  317. #pragma warning (pop)
  318. #endif
  319. pDelegate->editBoxReturn(_editBox);
  320. }
  321. #if CC_ENABLE_SCRIPT_BINDING
  322. if (_editBox != nullptr && 0 != _editBox->getScriptEditBoxHandler())
  323. {
  324. cocos2d::CommonScriptData data(_editBox->getScriptEditBoxHandler(), "ended", _editBox);
  325. cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
  326. cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  327. memset(data.eventName, 0, sizeof(data.eventName));
  328. strncpy(data.eventName, "return", sizeof(data.eventName));
  329. event.data = (void *)&data;
  330. cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  331. }
  332. #endif
  333. if (_editBox != nullptr)
  334. {
  335. this->onEndEditing(_text);
  336. }
  337. }
  338. void EditBoxImplCommon::editBoxEditingChanged(const std::string& text)
  339. {
  340. // LOGD("editBoxTextChanged...");
  341. cocos2d::ui::EditBoxDelegate *pDelegate = _editBox->getDelegate();
  342. _text = text;
  343. if (pDelegate != nullptr)
  344. {
  345. pDelegate->editBoxTextChanged(_editBox, text);
  346. }
  347. #if CC_ENABLE_SCRIPT_BINDING
  348. if (NULL != _editBox && 0 != _editBox->getScriptEditBoxHandler())
  349. {
  350. cocos2d::CommonScriptData data(_editBox->getScriptEditBoxHandler(), "changed", _editBox);
  351. cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
  352. cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  353. }
  354. #endif
  355. }
  356. }
  357. NS_CC_END