UIEditBoxImpl-common.cpp 11 KB

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