TextBox.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. #include "TextBox.h"
  2. #include "Game.h"
  3. namespace gameplay
  4. {
  5. 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. // TODO: Move cursor to end of line.
  126. break;
  127. }
  128. case Keyboard::KEY_DELETE:
  129. {
  130. Font* font = getFont(_state);
  131. GP_ASSERT(font);
  132. unsigned int fontSize = getFontSize(_state);
  133. Font::Justify textAlignment = getTextAlignment(_state);
  134. bool rightToLeft = getTextRightToLeft(_state);
  135. int textIndex = font->getIndexAtLocation(getDisplayedText().c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  136. textAlignment, true, rightToLeft);
  137. _text.erase(textIndex, 1);
  138. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  139. textAlignment, true, rightToLeft);
  140. _dirty = true;
  141. notifyListeners(Control::Listener::TEXT_CHANGED);
  142. break;
  143. }
  144. case Keyboard::KEY_TAB:
  145. {
  146. // Allow tab to move the focus forward.
  147. return false;
  148. break;
  149. }
  150. case Keyboard::KEY_LEFT_ARROW:
  151. {
  152. const std::string displayedText = getDisplayedText();
  153. Font* font = getFont(_state);
  154. GP_ASSERT(font);
  155. unsigned int fontSize = getFontSize(_state);
  156. Font::Justify textAlignment = getTextAlignment(_state);
  157. bool rightToLeft = getTextRightToLeft(_state);
  158. int textIndex = font->getIndexAtLocation(displayedText.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  159. textAlignment, true, rightToLeft);
  160. if (_ctrlPressed)
  161. {
  162. std::string::const_reverse_iterator it = std::find_if(displayedText.rend() - (textIndex - 1), displayedText.rend(), space);
  163. textIndex = std::distance(displayedText.begin(), it.base());
  164. }
  165. else
  166. {
  167. --textIndex;
  168. }
  169. font->getLocationAtIndex(displayedText.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  170. textAlignment, true, rightToLeft);
  171. _dirty = true;
  172. break;
  173. }
  174. case Keyboard::KEY_RIGHT_ARROW:
  175. {
  176. const std::string displayedText = getDisplayedText();
  177. Font* font = getFont(_state);
  178. GP_ASSERT(font);
  179. unsigned int fontSize = getFontSize(_state);
  180. Font::Justify textAlignment = getTextAlignment(_state);
  181. bool rightToLeft = getTextRightToLeft(_state);
  182. int textIndex = font->getIndexAtLocation(displayedText.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  183. textAlignment, true, rightToLeft);
  184. if (_ctrlPressed)
  185. {
  186. std::string::const_iterator it = std::find_if(displayedText.begin() + (textIndex + 1), displayedText.end(), space);
  187. textIndex = std::distance(displayedText.begin(), it);
  188. }
  189. else
  190. {
  191. ++textIndex;
  192. }
  193. font->getLocationAtIndex(displayedText.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  194. textAlignment, true, rightToLeft);
  195. _dirty = true;
  196. break;
  197. }
  198. case Keyboard::KEY_UP_ARROW:
  199. {
  200. Font* font = getFont(_state);
  201. GP_ASSERT(font);
  202. unsigned int fontSize = getFontSize(_state);
  203. Font::Justify textAlignment = getTextAlignment(_state);
  204. bool rightToLeft = getTextRightToLeft(_state);
  205. _prevCaretLocation.set(_caretLocation);
  206. _caretLocation.y -= fontSize;
  207. int textIndex = font->getIndexAtLocation(getDisplayedText().c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  208. textAlignment, true, rightToLeft);
  209. if (textIndex == -1)
  210. {
  211. _caretLocation.set(_prevCaretLocation);
  212. }
  213. _dirty = true;
  214. break;
  215. }
  216. case Keyboard::KEY_DOWN_ARROW:
  217. {
  218. Font* font = getFont(_state);
  219. GP_ASSERT(font);
  220. unsigned int fontSize = getFontSize(_state);
  221. Font::Justify textAlignment = getTextAlignment(_state);
  222. bool rightToLeft = getTextRightToLeft(_state);
  223. _prevCaretLocation.set(_caretLocation);
  224. _caretLocation.y += fontSize;
  225. int textIndex = font->getIndexAtLocation(getDisplayedText().c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  226. textAlignment, true, rightToLeft);
  227. if (textIndex == -1)
  228. {
  229. _caretLocation.set(_prevCaretLocation);
  230. }
  231. _dirty = true;
  232. break;
  233. }
  234. }
  235. break;
  236. }
  237. case Keyboard::KEY_CHAR:
  238. {
  239. Font* font = getFont(_state);
  240. GP_ASSERT(font);
  241. unsigned int fontSize = getFontSize(_state);
  242. Font::Justify textAlignment = getTextAlignment(_state);
  243. bool rightToLeft = getTextRightToLeft(_state);
  244. int textIndex = font->getIndexAtLocation(getDisplayedText().c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  245. textAlignment, true, rightToLeft);
  246. if (textIndex == -1)
  247. {
  248. textIndex = 0;
  249. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, 0,
  250. textAlignment, true, rightToLeft);
  251. }
  252. switch (key)
  253. {
  254. case Keyboard::KEY_BACKSPACE:
  255. {
  256. if (textIndex > 0)
  257. {
  258. --textIndex;
  259. _text.erase(textIndex, 1);
  260. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  261. textAlignment, true, rightToLeft);
  262. _dirty = true;
  263. }
  264. break;
  265. }
  266. case Keyboard::KEY_RETURN:
  267. // TODO: Handle line-break insertion correctly.
  268. break;
  269. case Keyboard::KEY_ESCAPE:
  270. break;
  271. case Keyboard::KEY_TAB:
  272. // Allow tab to move the focus forward.
  273. return false;
  274. break;
  275. default:
  276. {
  277. // Insert character into string.
  278. _text.insert(textIndex, 1, (char)key);
  279. // Get new location of caret.
  280. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
  281. textAlignment, true, rightToLeft);
  282. if (key == ' ')
  283. {
  284. // If a space was entered, check that caret is still within bounds.
  285. if (_caretLocation.x >= _textBounds.x + _textBounds.width ||
  286. _caretLocation.y >= _textBounds.y + _textBounds.height)
  287. {
  288. // If not, undo the character insertion.
  289. _text.erase(textIndex, 1);
  290. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  291. textAlignment, true, rightToLeft);
  292. // No need to check again.
  293. break;
  294. }
  295. }
  296. // Always check that the text still fits within the clip region.
  297. Rectangle textBounds;
  298. font->measureText(getDisplayedText().c_str(), _textBounds, fontSize, &textBounds, textAlignment, true, true);
  299. if (textBounds.x < _textBounds.x || textBounds.y < _textBounds.y ||
  300. textBounds.width >= _textBounds.width || textBounds.height >= _textBounds.height)
  301. {
  302. // If not, undo the character insertion.
  303. _text.erase(textIndex, 1);
  304. font->getLocationAtIndex(getDisplayedText().c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  305. textAlignment, true, rightToLeft);
  306. // TextBox is not dirty.
  307. break;
  308. }
  309. _dirty = true;
  310. break;
  311. }
  312. break;
  313. }
  314. notifyListeners(Control::Listener::TEXT_CHANGED);
  315. break;
  316. }
  317. case Keyboard::KEY_RELEASE:
  318. switch (key)
  319. {
  320. case Keyboard::KEY_CTRL:
  321. {
  322. _ctrlPressed = false;
  323. break;
  324. }
  325. }
  326. }
  327. _lastKeypress = key;
  328. return _consumeInputEvents;
  329. }
  330. void TextBox::update(const Control* container, const Vector2& offset)
  331. {
  332. Label::update(container, offset);
  333. _fontSize = getFontSize(_state);
  334. _caretImage = getImage("textCaret", _state);
  335. }
  336. void TextBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  337. {
  338. if (_caretImage && (_state == ACTIVE || hasFocus()))
  339. {
  340. // Draw the cursor at its current location.
  341. const Rectangle& region = _caretImage->getRegion();
  342. if (!region.isEmpty())
  343. {
  344. GP_ASSERT(spriteBatch);
  345. const Theme::UVs uvs = _caretImage->getUVs();
  346. Vector4 color = _caretImage->getColor();
  347. color.w *= _opacity;
  348. spriteBatch->draw(_caretLocation.x - (region.width / 2.0f), _caretLocation.y, region.width, _fontSize, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  349. }
  350. }
  351. _dirty = false;
  352. }
  353. void TextBox::setCaretLocation(int x, int y)
  354. {
  355. // Get index into string and cursor location from the latest touch location.
  356. _prevCaretLocation.set(_caretLocation);
  357. _caretLocation.set(x + _absoluteBounds.x,
  358. y + _absoluteBounds.y);
  359. Font* font = getFont(_state);
  360. unsigned int fontSize = getFontSize(_state);
  361. Font::Justify textAlignment = getTextAlignment(_state);
  362. bool rightToLeft = getTextRightToLeft(_state);
  363. const std::string displayedText = getDisplayedText();
  364. int index = font->getIndexAtLocation(displayedText.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  365. textAlignment, true, rightToLeft);
  366. if (index == -1)
  367. {
  368. // Attempt to find the nearest valid caret location.
  369. Rectangle textBounds;
  370. font->measureText(displayedText.c_str(), _textBounds, fontSize, &textBounds, textAlignment, true, true);
  371. if (_caretLocation.x > textBounds.x + textBounds.width &&
  372. _caretLocation.y > textBounds.y + textBounds.height)
  373. {
  374. font->getLocationAtIndex(displayedText.c_str(), _textBounds, fontSize, &_caretLocation, (unsigned int)_text.length(),
  375. textAlignment, true, rightToLeft);
  376. return;
  377. }
  378. if (_caretLocation.x < textBounds.x)
  379. {
  380. _caretLocation.x = textBounds.x;
  381. }
  382. else if (_caretLocation.x > textBounds.x + textBounds.width)
  383. {
  384. _caretLocation.x = textBounds.x + textBounds.width;
  385. }
  386. if (_caretLocation.y < textBounds.y)
  387. {
  388. _caretLocation.y = textBounds.y;
  389. }
  390. else if (_caretLocation.y > textBounds.y + textBounds.height)
  391. {
  392. Font* font = getFont(_state);
  393. GP_ASSERT(font);
  394. unsigned int fontSize = getFontSize(_state);
  395. _caretLocation.y = textBounds.y + textBounds.height - fontSize;
  396. }
  397. index = font->getIndexAtLocation(displayedText.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  398. textAlignment, true, rightToLeft);
  399. if (index == -1)
  400. {
  401. // We failed to find a valid location; just put the caret back to where it was.
  402. _caretLocation.set(_prevCaretLocation);
  403. }
  404. }
  405. }
  406. const char* TextBox::getType() const
  407. {
  408. return "textBox";
  409. }
  410. void TextBox::setPasswordChar(char character)
  411. {
  412. _passwordChar = character;
  413. }
  414. char TextBox::getPasswordChar() const
  415. {
  416. return _passwordChar;
  417. }
  418. void TextBox::setInputMode(InputMode inputMode)
  419. {
  420. _inputMode = inputMode;
  421. }
  422. TextBox::InputMode TextBox::getInputMode() const
  423. {
  424. return _inputMode;
  425. }
  426. void TextBox::drawText(const Rectangle& clip)
  427. {
  428. if (_text.size() <= 0)
  429. return;
  430. // Draw the text.
  431. if (_font)
  432. {
  433. const std::string displayedText = getDisplayedText();
  434. _font->start();
  435. _font->drawText(displayedText.c_str(), _textBounds, _textColor, getFontSize(_state), getTextAlignment(_state), true, getTextRightToLeft(_state), &_viewportClipBounds);
  436. _font->finish();
  437. }
  438. }
  439. TextBox::InputMode TextBox::getInputMode(const char* inputMode)
  440. {
  441. if (!inputMode)
  442. {
  443. return TextBox::TEXT;
  444. }
  445. if (strcmp(inputMode, "TEXT") == 0)
  446. {
  447. return TextBox::TEXT;
  448. }
  449. else if (strcmp(inputMode, "PASSWORD") == 0)
  450. {
  451. return TextBox::PASSWORD;
  452. }
  453. else
  454. {
  455. GP_ERROR("Failed to get corresponding textbox inputmode for unsupported value '%s'.", inputMode);
  456. }
  457. // Default.
  458. return TextBox::TEXT;
  459. }
  460. std::string TextBox::getDisplayedText() const
  461. {
  462. std::string displayedText;
  463. switch (_inputMode) {
  464. case PASSWORD:
  465. displayedText.insert((size_t)0, _text.length(), _passwordChar);
  466. break;
  467. case TEXT:
  468. default:
  469. displayedText = _text;
  470. break;
  471. }
  472. return displayedText;
  473. }
  474. }