UIEditBoxImpl-tizen.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2012 James Chen
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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-tizen.h"
  23. #if (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN)
  24. #include <Evas.h>
  25. #include <Elementary.h>
  26. #include <efl_extension.h>
  27. #include "ui/UIEditBox/UIEditBox.h"
  28. #include "2d/CCLabel.h"
  29. #include "base/ccUTF8.h"
  30. #include "platform/tizen/CCApplication-tizen.h"
  31. #include "base/CCDirector.h"
  32. NS_CC_BEGIN
  33. namespace ui {
  34. EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
  35. {
  36. return new EditBoxImplTizen(pEditBox);
  37. }
  38. EditBoxImplTizen::EditBoxImplTizen(EditBox* pEditText)
  39. : EditBoxImpl(pEditText)
  40. , _label(nullptr)
  41. , _labelPlaceHolder(nullptr)
  42. , _editBoxInputMode(EditBox::InputMode::SINGLE_LINE)
  43. , _editBoxInputFlag(EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS)
  44. , _keyboardReturnType(EditBox::KeyboardReturnType::DEFAULT)
  45. , _alignment(TextHAlignment::LEFT)
  46. , _fontSize(-1)
  47. , _placeholderFontSize(-1)
  48. , _colText(Color3B::WHITE)
  49. , _colPlaceHolder(Color3B::GRAY)
  50. , _maxLength(-1)
  51. {
  52. }
  53. EditBoxImplTizen::~EditBoxImplTizen()
  54. {
  55. }
  56. void EditBoxImplTizen::doAnimationWhenKeyboardMove(float duration, float distance)
  57. { // don't need to be implemented on tizen platform.
  58. }
  59. static const int CC_EDIT_BOX_PADDING = 5;
  60. bool EditBoxImplTizen::initWithSize(const Size& size)
  61. {
  62. int fontSize = size.height-12;
  63. _label = Label::create();
  64. _label->setSystemFontSize(size.height-12);
  65. // align the text vertically center
  66. _label->setAnchorPoint(Vec2(0, 0.5f));
  67. _label->setPosition(Vec2(CC_EDIT_BOX_PADDING, size.height / 2.0f));
  68. _label->setTextColor(_colText);
  69. _editBox->addChild(_label);
  70. _labelPlaceHolder = Label::create();
  71. _labelPlaceHolder->setSystemFontSize(size.height-12);
  72. // align the text vertically center
  73. _labelPlaceHolder->setAnchorPoint(Vec2(0, 0.5f));
  74. _labelPlaceHolder->setPosition(CC_EDIT_BOX_PADDING, size.height / 2.0f);
  75. _labelPlaceHolder->setVisible(false);
  76. _labelPlaceHolder->setTextColor(_colPlaceHolder);
  77. _editBox->addChild(_labelPlaceHolder);
  78. _editSize = size;
  79. return true;
  80. }
  81. void EditBoxImplTizen::setFont(const char* pFontName, int fontSize)
  82. {
  83. _fontName = pFontName;
  84. _fontSize = fontSize;
  85. if(_label != NULL) {
  86. _label->setSystemFontName(pFontName);
  87. _label->setSystemFontSize(fontSize);
  88. }
  89. if(_labelPlaceHolder != NULL) {
  90. _labelPlaceHolder->setSystemFontName(pFontName);
  91. _labelPlaceHolder->setSystemFontSize(fontSize);
  92. }
  93. }
  94. void EditBoxImplTizen::setFontColor(const Color4B& color)
  95. {
  96. _colText = color;
  97. _label->setTextColor(color);
  98. }
  99. void EditBoxImplTizen::setPlaceholderFont(const char* pFontName, int fontSize)
  100. {
  101. _placeholderFontName = pFontName;
  102. _placeholderFontSize = fontSize;
  103. if(_labelPlaceHolder != NULL) {
  104. _labelPlaceHolder->setSystemFontName(pFontName);
  105. _labelPlaceHolder->setSystemFontSize(fontSize);
  106. }
  107. }
  108. void EditBoxImplTizen::setPlaceholderFontColor(const Color4B& color)
  109. {
  110. _colPlaceHolder = color;
  111. _labelPlaceHolder->setTextColor(color);
  112. }
  113. void EditBoxImplTizen::setInputMode(EditBox::InputMode inputMode)
  114. {
  115. _editBoxInputMode = inputMode;
  116. }
  117. void EditBoxImplTizen::setMaxLength(int maxLength)
  118. {
  119. _maxLength = maxLength;
  120. }
  121. int EditBoxImplTizen::getMaxLength()
  122. {
  123. return _maxLength;
  124. }
  125. void EditBoxImplTizen::setInputFlag(EditBox::InputFlag inputFlag)
  126. {
  127. _editBoxInputFlag = inputFlag;
  128. }
  129. void EditBoxImplTizen::setReturnType(EditBox::KeyboardReturnType returnType)
  130. {
  131. _keyboardReturnType = returnType;
  132. }
  133. bool EditBoxImplTizen::isEditing()
  134. {
  135. return false;
  136. }
  137. void EditBoxImplTizen::setText(const char* pText)
  138. {
  139. if (pText != NULL)
  140. {
  141. _text = pText;
  142. if (_text.length() > 0)
  143. {
  144. _labelPlaceHolder->setVisible(false);
  145. std::string strToShow;
  146. if (EditBox::InputFlag::PASSWORD == _editBoxInputFlag)
  147. {
  148. long length = cc_utf8_strlen(_text.c_str(), -1);
  149. for (long i = 0; i < length; i++)
  150. {
  151. strToShow.append("*");
  152. }
  153. }
  154. else
  155. {
  156. strToShow = _text;
  157. }
  158. _label->setString(strToShow.c_str());
  159. // Clip the text width to fit to the text box
  160. float fMaxWidth = _editSize.width - CC_EDIT_BOX_PADDING * 2;
  161. auto labelSize = _label->getContentSize();
  162. if(labelSize.width > fMaxWidth) {
  163. _label->setDimensions(fMaxWidth,labelSize.height);
  164. }
  165. }
  166. else
  167. {
  168. _labelPlaceHolder->setVisible(true);
  169. _label->setString("");
  170. }
  171. }
  172. }
  173. const char* EditBoxImplTizen::getText(void)
  174. {
  175. return _text.c_str();
  176. }
  177. void EditBoxImplTizen::setPlaceHolder(const char* pText)
  178. {
  179. if (pText != NULL)
  180. {
  181. _placeHolder = pText;
  182. if (_placeHolder.length() > 0 && _text.length() == 0)
  183. {
  184. _labelPlaceHolder->setVisible(true);
  185. }
  186. _labelPlaceHolder->setString(_placeHolder.c_str());
  187. }
  188. }
  189. const char* EditBoxImplTizen::getPlaceHolder(void)
  190. {
  191. return _placeHolder.c_str();
  192. }
  193. void EditBoxImplTizen::setPosition(const Vec2& pos)
  194. { // don't need to be implemented on tizen platform.
  195. }
  196. void EditBoxImplTizen::setVisible(bool visible)
  197. { // don't need to be implemented on tizen platform.
  198. }
  199. void EditBoxImplTizen::setContentSize(const Size& size)
  200. { // don't need to be implemented on tizen platform.
  201. }
  202. void EditBoxImplTizen::setAnchorPoint(const Vec2& anchorPoint)
  203. { // don't need to be implemented on tizen platform.
  204. }
  205. void EditBoxImplTizen::visit(void)
  206. { // don't need to be implemented on tizen platform.
  207. }
  208. void EditBoxImplTizen::onEnter(void)
  209. { // don't need to be implemented on tizen platform.
  210. }
  211. static void editBoxCallbackFunc(const char* pText, void* ctx)
  212. {
  213. EditBoxImplTizen* thiz = (EditBoxImplTizen*)ctx;
  214. thiz->setText(pText);
  215. if (thiz->getDelegate() != NULL)
  216. {
  217. thiz->getDelegate()->editBoxTextChanged(thiz->getEditBox(), thiz->getText());
  218. thiz->getDelegate()->editBoxEditingDidEnd(thiz->getEditBox());
  219. thiz->getDelegate()->editBoxReturn(thiz->getEditBox());
  220. }
  221. #if CC_ENABLE_SCRIPT_BINDING
  222. EditBox* pEditBox = thiz->getEditBox();
  223. if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
  224. {
  225. CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
  226. ScriptEvent event(kCommonEvent,(void*)&data);
  227. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  228. memset(data.eventName, 0, sizeof(data.eventName));
  229. strncpy(data.eventName, "ended", sizeof(data.eventName));
  230. event.data = (void*)&data;
  231. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  232. memset(data.eventName, 0, sizeof(data.eventName));
  233. strncpy(data.eventName, "return", sizeof(data.eventName));
  234. event.data = (void*)&data;
  235. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  236. }
  237. #endif
  238. }
  239. static Evas_Object * s_keypadWin = nullptr;
  240. static void entry_back_cb(void *data, Evas_Object *obj, void *event_info)
  241. {
  242. evas_object_del(s_keypadWin);
  243. s_keypadWin = nullptr;
  244. }
  245. static void entry_activated_cb(void *data, Evas_Object *obj, void *event_info)
  246. {
  247. char *commit_str = (char *)event_info;
  248. const char* text = elm_entry_entry_get(obj);
  249. editBoxCallbackFunc(text, data);
  250. elm_entry_input_panel_hide(obj);
  251. evas_object_del(s_keypadWin);
  252. s_keypadWin = nullptr;
  253. }
  254. void EditBoxImplTizen::openKeyboard()
  255. {
  256. if (s_keypadWin)
  257. {
  258. return;
  259. }
  260. if (_delegate != NULL)
  261. {
  262. _delegate->editBoxEditingDidBegin(_editBox);
  263. }
  264. #if CC_ENABLE_SCRIPT_BINDING
  265. EditBox* pEditBox = this->getEditBox();
  266. if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
  267. {
  268. CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
  269. ScriptEvent event(cocos2d::kCommonEvent,(void*)&data);
  270. ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
  271. }
  272. #endif
  273. Evas_Object* parent = Application::getInstance()->_win;
  274. GLView* glView = Director::getInstance()->getOpenGLView();
  275. Size frameSize = glView->getFrameSize();
  276. s_keypadWin = elm_win_add(parent, "cocos2d-x", ELM_WIN_BASIC);
  277. elm_win_autodel_set(s_keypadWin, EINA_TRUE);
  278. elm_win_conformant_set(s_keypadWin, EINA_TRUE);
  279. elm_win_alpha_set(s_keypadWin, EINA_TRUE);
  280. evas_object_show(s_keypadWin);
  281. eext_object_event_callback_add(s_keypadWin, EEXT_CALLBACK_BACK, entry_back_cb, this);
  282. int rots[2];
  283. rots[0] = (int)(elm_win_rotation_get(parent));
  284. rots[1] = rots[0] + 180 % 360;
  285. elm_win_wm_rotation_available_rotations_set(s_keypadWin, rots, 2);
  286. Evas_Object* bgFull = elm_bg_add(s_keypadWin);
  287. evas_object_color_set(bgFull, 0, 0, 0, 0xa0);
  288. evas_object_resize(bgFull, frameSize.width, frameSize.height);
  289. evas_object_show(bgFull);
  290. int height = frameSize.height / 10;
  291. Evas_Object* rectangle = elm_bg_add(bgFull);
  292. evas_object_resize(rectangle, frameSize.width, height);
  293. evas_object_move(rectangle, 0, height);
  294. evas_object_color_set(rectangle, 0xff, 0xff, 0xff, 0xff);
  295. evas_object_show(rectangle);
  296. Evas_Object* title = elm_entry_add(rectangle);
  297. evas_object_resize(title, frameSize.width, height);
  298. auto text = _placeHolder.c_str();
  299. auto richText = (char*)malloc(strlen(text) + 50);
  300. sprintf(richText,"<color=#ffffff>%s</>", text);
  301. elm_entry_entry_set(title, richText);
  302. elm_entry_editable_set(title, EINA_FALSE);
  303. //elm_entry_drag_disabled_set(title, EINA_TRUE);
  304. //elm_entry_drop_disabled_set(title, EINA_TRUE);
  305. evas_object_show(title);
  306. free(richText);
  307. Evas_Object* entry = elm_entry_add(bgFull);
  308. elm_object_focus_set(entry, EINA_TRUE);
  309. evas_object_resize(entry, frameSize.width, height);
  310. evas_object_move(entry, 0, height);
  311. elm_entry_single_line_set(entry, EINA_TRUE);
  312. elm_entry_line_wrap_set(entry, ELM_WRAP_MIXED);
  313. elm_entry_entry_set(entry, _text.c_str());
  314. evas_object_show(entry);
  315. elm_object_focus_set(entry, EINA_TRUE);
  316. eext_entry_selection_back_event_allow_set(entry, EINA_TRUE);
  317. Elm_Entry_Filter_Limit_Size limit_size = { 0, };
  318. limit_size.max_char_count = _maxLength;
  319. elm_entry_markup_filter_append(entry, elm_entry_filter_limit_size, &limit_size);
  320. elm_entry_input_panel_return_key_type_set(entry, ELM_INPUT_PANEL_RETURN_KEY_TYPE_DONE);
  321. elm_entry_prediction_allow_set(entry, EINA_FALSE);
  322. evas_object_smart_callback_add(entry, "activated", entry_activated_cb, this);
  323. switch(_editBoxInputFlag)
  324. {
  325. case EditBox::InputFlag::PASSWORD:
  326. elm_entry_password_set(entry, EINA_TRUE);
  327. elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_EMAIL);
  328. break;
  329. case EditBox::InputFlag::SENSITIVE:
  330. elm_entry_input_hint_set(entry, ELM_INPUT_HINT_SENSITIVE_DATA);
  331. break;
  332. case EditBox::InputFlag::INITIAL_CAPS_WORD:
  333. elm_entry_autocapital_type_set(entry, ELM_AUTOCAPITAL_TYPE_WORD);
  334. break;
  335. case EditBox::InputFlag::INITIAL_CAPS_SENTENCE:
  336. elm_entry_autocapital_type_set(entry, ELM_AUTOCAPITAL_TYPE_SENTENCE);
  337. break;
  338. case EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS:
  339. elm_entry_autocapital_type_set(entry, ELM_AUTOCAPITAL_TYPE_ALLCHARACTER);
  340. break;
  341. }
  342. switch(_editBoxInputMode)
  343. {
  344. case EditBox::InputMode::ANY:
  345. break;
  346. case EditBox::InputMode::EMAIL_ADDRESS:
  347. elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_EMAIL);
  348. break;
  349. case EditBox::InputMode::NUMERIC:
  350. elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_NUMBER);
  351. break;
  352. case EditBox::InputMode::PHONE_NUMBER:
  353. elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_PHONENUMBER);
  354. break;
  355. case EditBox::InputMode::URL:
  356. elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_URL);
  357. break;
  358. case EditBox::InputMode::DECIMAL:
  359. elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_HEX); //fixme?
  360. break;
  361. case EditBox::InputMode::SINGLE_LINE:
  362. break;
  363. }
  364. }
  365. void EditBoxImplTizen::closeKeyboard()
  366. {
  367. }
  368. }
  369. NS_CC_END
  370. #endif /* #if (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN) */