TextBox.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #include "TextBox.h"
  2. #include "Game.h"
  3. namespace gameplay
  4. {
  5. static bool space(char c) {
  6. return isspace(c);
  7. }
  8. TextBox::TextBox() : _lastKeypress(0), _fontSize(0), _caretImage(NULL), _passwordChar('*'), _inputMode(TEXT), _ctrlPressed(false)
  9. {
  10. }
  11. TextBox::~TextBox()
  12. {
  13. }
  14. TextBox* TextBox::create(const char* id, Theme::Style* style)
  15. {
  16. GP_ASSERT(style);
  17. TextBox* textBox = new TextBox();
  18. if (id)
  19. textBox->_id = id;
  20. textBox->setStyle(style);
  21. return textBox;
  22. }
  23. TextBox* TextBox::create(Theme::Style* style, Properties* properties)
  24. {
  25. TextBox* textBox = new TextBox();
  26. textBox->initialize(style, properties);
  27. return textBox;
  28. }
  29. void TextBox::initialize(Theme::Style* style, Properties* properties)
  30. {
  31. GP_ASSERT(properties);
  32. Label::initialize(style, properties);
  33. _inputMode = getInputMode(properties->getString("inputMode"));
  34. }
  35. int TextBox::getLastKeypress()
  36. {
  37. return _lastKeypress;
  38. }
  39. void TextBox::addListener(Control::Listener* listener, int eventFlags)
  40. {
  41. if ((eventFlags & Control::Listener::VALUE_CHANGED) == Control::Listener::VALUE_CHANGED)
  42. {
  43. GP_ERROR("VALUE_CHANGED event is not applicable to TextBox.");
  44. }
  45. Control::addListener(listener, eventFlags);
  46. }
  47. bool TextBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  48. {
  49. switch (evt)
  50. {
  51. case Touch::TOUCH_PRESS:
  52. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  53. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  54. {
  55. _contactIndex = (int) contactIndex;
  56. if (_state == NORMAL)
  57. Game::getInstance()->displayKeyboard(true);
  58. setCaretLocation(x, y);
  59. _state = ACTIVE;
  60. _dirty = true;
  61. }
  62. else
  63. {
  64. _contactIndex = INVALID_CONTACT_INDEX;
  65. _state = NORMAL;
  66. Game::getInstance()->displayKeyboard(false);
  67. _dirty = true;
  68. return false;
  69. }
  70. break;
  71. case Touch::TOUCH_MOVE:
  72. if (_state == ACTIVE &&
  73. x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  74. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  75. {
  76. setCaretLocation(x, y);
  77. _dirty = true;
  78. }
  79. break;
  80. case Touch::TOUCH_RELEASE:
  81. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  82. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  83. {
  84. setCaretLocation(x, y);
  85. _state = FOCUS;
  86. }
  87. else
  88. {
  89. _state = NORMAL;
  90. Game::getInstance()->displayKeyboard(false);
  91. }
  92. _contactIndex = INVALID_CONTACT_INDEX;
  93. _dirty = true;
  94. break;
  95. }
  96. return _consumeInputEvents;
  97. }
  98. bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
  99. {
  100. switch (evt)
  101. {
  102. case Keyboard::KEY_PRESS:
  103. {
  104. switch (key)
  105. {
  106. case Keyboard::KEY_CTRL:
  107. {
  108. _ctrlPressed = true;
  109. break;
  110. }
  111. case Keyboard::KEY_HOME:
  112. {
  113. Font* font = getFont(_state);
  114. GP_ASSERT(font);
  115. unsigned int fontSize = getFontSize(_state);
  116. Font::Justify textAlignment = getTextAlignment(_state);
  117. bool rightToLeft = getTextRightToLeft(_state);
  118. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, 0,
  119. textAlignment, true, rightToLeft);
  120. _dirty = true;
  121. break;
  122. }
  123. case Keyboard::KEY_END:
  124. {
  125. Font* font = getFont(_state);
  126. GP_ASSERT(font);
  127. unsigned int fontSize = getFontSize(_state);
  128. Font::Justify textAlignment = getTextAlignment(_state);
  129. bool rightToLeft = getTextRightToLeft(_state);
  130. const std::string displayedText = getDisplayedText();
  131. font->getLocationAtIndex(displayedText.c_str(), _textBounds, fontSize, &_caretLocation, displayedText.size(),
  132. textAlignment, true, rightToLeft);
  133. _dirty = true;
  134. break;
  135. }
  136. case Keyboard::KEY_DELETE:
  137. {
  138. Font* font = getFont(_state);
  139. GP_ASSERT(font);
  140. unsigned int fontSize = getFontSize(_state);
  141. Font::Justify textAlignment = getTextAlignment(_state);
  142. bool rightToLeft = getTextRightToLeft(_state);
  143. int textIndex = font->getIndexAtLocation(getDisplayedText().c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  144. textAlignment, true, rightToLeft);
  145. _text.erase(textIndex, 1);
  146. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  147. textAlignment, true, rightToLeft);
  148. _dirty = true;
  149. notifyListeners(Control::Listener::TEXT_CHANGED);
  150. break;
  151. }
  152. case Keyboard::KEY_TAB:
  153. {
  154. // Allow tab to move the focus forward.
  155. return false;
  156. break;
  157. }
  158. case Keyboard::KEY_LEFT_ARROW:
  159. {
  160. const std::string displayedText = getDisplayedText();
  161. Font* font = getFont(_state);
  162. GP_ASSERT(font);
  163. unsigned int fontSize = getFontSize(_state);
  164. Font::Justify textAlignment = getTextAlignment(_state);
  165. bool rightToLeft = getTextRightToLeft(_state);
  166. int textIndex = font->getIndexAtLocation(displayedText.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  167. textAlignment, true, rightToLeft);
  168. if (_ctrlPressed)
  169. {
  170. std::string::const_reverse_iterator it = std::find_if(displayedText.rend() - (textIndex - 1), displayedText.rend(), space);
  171. textIndex = std::distance(displayedText.begin(), it.base());
  172. }
  173. else
  174. {
  175. --textIndex;
  176. }
  177. font->getLocationAtIndex(displayedText.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  178. textAlignment, true, rightToLeft);
  179. _dirty = true;
  180. break;
  181. }
  182. case Keyboard::KEY_RIGHT_ARROW:
  183. {
  184. const std::string displayedText = getDisplayedText();
  185. Font* font = getFont(_state);
  186. GP_ASSERT(font);
  187. unsigned int fontSize = getFontSize(_state);
  188. Font::Justify textAlignment = getTextAlignment(_state);
  189. bool rightToLeft = getTextRightToLeft(_state);
  190. int textIndex = font->getIndexAtLocation(displayedText.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  191. textAlignment, true, rightToLeft);
  192. if (_ctrlPressed)
  193. {
  194. std::string::const_iterator it = std::find_if(displayedText.begin() + (textIndex + 1), displayedText.end(), space);
  195. textIndex = std::distance(displayedText.begin(), it);
  196. }
  197. else
  198. {
  199. ++textIndex;
  200. }
  201. font->getLocationAtIndex(displayedText.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  202. textAlignment, true, rightToLeft);
  203. _dirty = true;
  204. break;
  205. }
  206. case Keyboard::KEY_UP_ARROW:
  207. {
  208. Font* font = getFont(_state);
  209. GP_ASSERT(font);
  210. unsigned int fontSize = getFontSize(_state);
  211. Font::Justify textAlignment = getTextAlignment(_state);
  212. bool rightToLeft = getTextRightToLeft(_state);
  213. _prevCaretLocation.set(_caretLocation);
  214. _caretLocation.y -= fontSize;
  215. int textIndex = font->getIndexAtLocation(getDisplayedText().c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  216. textAlignment, true, rightToLeft);
  217. if (textIndex == -1)
  218. {
  219. _caretLocation.set(_prevCaretLocation);
  220. }
  221. _dirty = true;
  222. break;
  223. }
  224. case Keyboard::KEY_DOWN_ARROW:
  225. {
  226. Font* font = getFont(_state);
  227. GP_ASSERT(font);
  228. unsigned int fontSize = getFontSize(_state);
  229. Font::Justify textAlignment = getTextAlignment(_state);
  230. bool rightToLeft = getTextRightToLeft(_state);
  231. _prevCaretLocation.set(_caretLocation);
  232. _caretLocation.y += fontSize;
  233. int textIndex = font->getIndexAtLocation(getDisplayedText().c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  234. textAlignment, true, rightToLeft);
  235. if (textIndex == -1)
  236. {
  237. _caretLocation.set(_prevCaretLocation);
  238. }
  239. _dirty = true;
  240. break;
  241. }
  242. }
  243. break;
  244. }
  245. case Keyboard::KEY_CHAR:
  246. {
  247. Font* font = getFont(_state);
  248. GP_ASSERT(font);
  249. unsigned int fontSize = getFontSize(_state);
  250. Font::Justify textAlignment = getTextAlignment(_state);
  251. bool rightToLeft = getTextRightToLeft(_state);
  252. int textIndex = font->getIndexAtLocation(getDisplayedText().c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  253. textAlignment, true, rightToLeft);
  254. if (textIndex == -1)
  255. {
  256. textIndex = 0;
  257. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, 0,
  258. textAlignment, true, rightToLeft);
  259. }
  260. switch (key)
  261. {
  262. case Keyboard::KEY_BACKSPACE:
  263. {
  264. if (textIndex > 0)
  265. {
  266. --textIndex;
  267. _text.erase(textIndex, 1);
  268. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  269. textAlignment, true, rightToLeft);
  270. _dirty = true;
  271. }
  272. break;
  273. }
  274. case Keyboard::KEY_RETURN:
  275. // TODO: Handle line-break insertion correctly.
  276. break;
  277. case Keyboard::KEY_ESCAPE:
  278. break;
  279. case Keyboard::KEY_TAB:
  280. // Allow tab to move the focus forward.
  281. return false;
  282. break;
  283. default:
  284. {
  285. // Insert character into string.
  286. _text.insert(textIndex, 1, (char)key);
  287. // Get new location of caret.
  288. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
  289. textAlignment, true, rightToLeft);
  290. if (key == ' ')
  291. {
  292. // If a space was entered, check that caret is still within bounds.
  293. if (_caretLocation.x >= _textBounds.x + _textBounds.width ||
  294. _caretLocation.y >= _textBounds.y + _textBounds.height)
  295. {
  296. // If not, undo the character insertion.
  297. _text.erase(textIndex, 1);
  298. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  299. textAlignment, true, rightToLeft);
  300. // No need to check again.
  301. break;
  302. }
  303. }
  304. // Always check that the text still fits within the clip region.
  305. Rectangle textBounds;
  306. font->measureText(getDisplayedText().c_str(), _textBounds, fontSize, &textBounds, textAlignment, true, true);
  307. if (textBounds.x < _textBounds.x || textBounds.y < _textBounds.y ||
  308. textBounds.width >= _textBounds.width || textBounds.height >= _textBounds.height)
  309. {
  310. // If not, undo the character insertion.
  311. _text.erase(textIndex, 1);
  312. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  313. textAlignment, true, rightToLeft);
  314. // TextBox is not dirty.
  315. break;
  316. }
  317. _dirty = true;
  318. break;
  319. }
  320. break;
  321. }
  322. notifyListeners(Control::Listener::TEXT_CHANGED);
  323. break;
  324. }
  325. case Keyboard::KEY_RELEASE:
  326. switch (key)
  327. {
  328. case Keyboard::KEY_CTRL:
  329. {
  330. _ctrlPressed = false;
  331. break;
  332. }
  333. }
  334. }
  335. _lastKeypress = key;
  336. return _consumeInputEvents;
  337. }
  338. void TextBox::update(const Control* container, const Vector2& offset)
  339. {
  340. Label::update(container, offset);
  341. _fontSize = getFontSize(_state);
  342. _caretImage = getImage("textCaret", _state);
  343. }
  344. void TextBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  345. {
  346. if (_caretImage && (_state == ACTIVE || hasFocus()))
  347. {
  348. // Draw the cursor at its current location.
  349. const Rectangle& region = _caretImage->getRegion();
  350. if (!region.isEmpty())
  351. {
  352. GP_ASSERT(spriteBatch);
  353. const Theme::UVs uvs = _caretImage->getUVs();
  354. Vector4 color = _caretImage->getColor();
  355. color.w *= _opacity;
  356. float caretWidth = region.width * _fontSize / region.height;
  357. spriteBatch->draw(_caretLocation.x - caretWidth * 0.5f, _caretLocation.y, caretWidth, _fontSize, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  358. }
  359. }
  360. _dirty = false;
  361. }
  362. void TextBox::setCaretLocation(int x, int y)
  363. {
  364. // Get index into string and cursor location from the latest touch location.
  365. _prevCaretLocation.set(_caretLocation);
  366. _caretLocation.set(x + _absoluteBounds.x,
  367. y + _absoluteBounds.y);
  368. Font* font = getFont(_state);
  369. unsigned int fontSize = getFontSize(_state);
  370. Font::Justify textAlignment = getTextAlignment(_state);
  371. bool rightToLeft = getTextRightToLeft(_state);
  372. const std::string displayedText = getDisplayedText();
  373. int index = font->getIndexAtLocation(displayedText.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  374. textAlignment, true, rightToLeft);
  375. if (index == -1)
  376. {
  377. // Attempt to find the nearest valid caret location.
  378. Rectangle textBounds;
  379. font->measureText(displayedText.c_str(), _textBounds, fontSize, &textBounds, textAlignment, true, true);
  380. if (_caretLocation.x > textBounds.x + textBounds.width &&
  381. _caretLocation.y > textBounds.y + textBounds.height)
  382. {
  383. font->getLocationAtIndex(displayedText.c_str(), _textBounds, fontSize, &_caretLocation, (unsigned int)_text.length(),
  384. textAlignment, true, rightToLeft);
  385. return;
  386. }
  387. if (_caretLocation.x < textBounds.x)
  388. {
  389. _caretLocation.x = textBounds.x;
  390. }
  391. else if (_caretLocation.x > textBounds.x + textBounds.width)
  392. {
  393. _caretLocation.x = textBounds.x + textBounds.width;
  394. }
  395. if (_caretLocation.y < textBounds.y)
  396. {
  397. _caretLocation.y = textBounds.y;
  398. }
  399. else if (_caretLocation.y > textBounds.y + textBounds.height)
  400. {
  401. Font* font = getFont(_state);
  402. GP_ASSERT(font);
  403. unsigned int fontSize = getFontSize(_state);
  404. _caretLocation.y = textBounds.y + textBounds.height - fontSize;
  405. }
  406. index = font->getIndexAtLocation(displayedText.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  407. textAlignment, true, rightToLeft);
  408. if (index == -1)
  409. {
  410. // We failed to find a valid location; just put the caret back to where it was.
  411. _caretLocation.set(_prevCaretLocation);
  412. }
  413. }
  414. }
  415. const char* TextBox::getType() const
  416. {
  417. return "textBox";
  418. }
  419. void TextBox::setPasswordChar(char character)
  420. {
  421. _passwordChar = character;
  422. }
  423. char TextBox::getPasswordChar() const
  424. {
  425. return _passwordChar;
  426. }
  427. void TextBox::setInputMode(InputMode inputMode)
  428. {
  429. _inputMode = inputMode;
  430. }
  431. TextBox::InputMode TextBox::getInputMode() const
  432. {
  433. return _inputMode;
  434. }
  435. void TextBox::drawText(const Rectangle& clip)
  436. {
  437. if (_text.size() <= 0)
  438. return;
  439. // Draw the text.
  440. if (_font)
  441. {
  442. const std::string displayedText = getDisplayedText();
  443. _font->start();
  444. _font->drawText(displayedText.c_str(), _textBounds, _textColor, getFontSize(_state), getTextAlignment(_state), true, getTextRightToLeft(_state), &_viewportClipBounds);
  445. _font->finish();
  446. }
  447. }
  448. TextBox::InputMode TextBox::getInputMode(const char* inputMode)
  449. {
  450. if (!inputMode)
  451. {
  452. return TextBox::TEXT;
  453. }
  454. if (strcmp(inputMode, "TEXT") == 0)
  455. {
  456. return TextBox::TEXT;
  457. }
  458. else if (strcmp(inputMode, "PASSWORD") == 0)
  459. {
  460. return TextBox::PASSWORD;
  461. }
  462. else
  463. {
  464. GP_ERROR("Failed to get corresponding textbox inputmode for unsupported value '%s'.", inputMode);
  465. }
  466. // Default.
  467. return TextBox::TEXT;
  468. }
  469. std::string TextBox::getDisplayedText() const
  470. {
  471. std::string displayedText;
  472. switch (_inputMode) {
  473. case PASSWORD:
  474. displayedText.insert((size_t)0, _text.length(), _passwordChar);
  475. break;
  476. case TEXT:
  477. default:
  478. displayedText = _text;
  479. break;
  480. }
  481. return displayedText;
  482. }
  483. }