TextBox.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #include "TextBox.h"
  2. #include "Game.h"
  3. namespace gameplay
  4. {
  5. TextBox::TextBox() : _lastKeypress(0), _fontSize(0), _caretImage(NULL)
  6. {
  7. }
  8. TextBox::~TextBox()
  9. {
  10. }
  11. TextBox* TextBox::create(const char* id, Theme::Style* style)
  12. {
  13. GP_ASSERT(style);
  14. TextBox* textBox = new TextBox();
  15. if (id)
  16. textBox->_id = id;
  17. textBox->setStyle(style);
  18. return textBox;
  19. }
  20. TextBox* TextBox::create(Theme::Style* style, Properties* properties)
  21. {
  22. TextBox* textBox = new TextBox();
  23. textBox->initialize(style, properties);
  24. return textBox;
  25. }
  26. int TextBox::getLastKeypress()
  27. {
  28. return _lastKeypress;
  29. }
  30. void TextBox::addListener(Control::Listener* listener, int eventFlags)
  31. {
  32. if ((eventFlags & Control::Listener::VALUE_CHANGED) == Control::Listener::VALUE_CHANGED)
  33. {
  34. GP_ERROR("VALUE_CHANGED event is not applicable to TextBox.");
  35. }
  36. Control::addListener(listener, eventFlags);
  37. }
  38. bool TextBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  39. {
  40. switch (evt)
  41. {
  42. case Touch::TOUCH_PRESS:
  43. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  44. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  45. {
  46. _contactIndex = (int) contactIndex;
  47. if (_state == NORMAL)
  48. Game::getInstance()->displayKeyboard(true);
  49. setCaretLocation(x, y);
  50. _state = ACTIVE;
  51. _dirty = true;
  52. }
  53. else
  54. {
  55. _contactIndex = INVALID_CONTACT_INDEX;
  56. _state = NORMAL;
  57. Game::getInstance()->displayKeyboard(false);
  58. _dirty = true;
  59. return false;
  60. }
  61. break;
  62. case Touch::TOUCH_MOVE:
  63. if (_state == ACTIVE &&
  64. x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  65. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  66. {
  67. setCaretLocation(x, y);
  68. _dirty = true;
  69. }
  70. break;
  71. case Touch::TOUCH_RELEASE:
  72. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  73. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  74. {
  75. setCaretLocation(x, y);
  76. _state = FOCUS;
  77. }
  78. else
  79. {
  80. _state = NORMAL;
  81. Game::getInstance()->displayKeyboard(false);
  82. }
  83. _contactIndex = INVALID_CONTACT_INDEX;
  84. _dirty = true;
  85. break;
  86. }
  87. return _consumeInputEvents;
  88. }
  89. bool TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
  90. {
  91. if (_state == FOCUS)
  92. {
  93. switch (evt)
  94. {
  95. case Keyboard::KEY_PRESS:
  96. {
  97. switch (key)
  98. {
  99. case Keyboard::KEY_HOME:
  100. {
  101. // TODO: Move cursor to beginning of line.
  102. // This only works for left alignment...
  103. //_caretLocation.x = _viewportClipBounds.x;
  104. //_dirty = true;
  105. break;
  106. }
  107. case Keyboard::KEY_END:
  108. {
  109. // TODO: Move cursor to end of line.
  110. break;
  111. }
  112. case Keyboard::KEY_DELETE:
  113. {
  114. Font* font = getFont(_state);
  115. GP_ASSERT(font);
  116. unsigned int fontSize = getFontSize(_state);
  117. Font::Justify textAlignment = getTextAlignment(_state);
  118. bool rightToLeft = getTextRightToLeft(_state);
  119. int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  120. textAlignment, true, rightToLeft);
  121. _text.erase(textIndex, 1);
  122. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  123. textAlignment, true, rightToLeft);
  124. _dirty = true;
  125. notifyListeners(Control::Listener::TEXT_CHANGED);
  126. break;
  127. }
  128. case Keyboard::KEY_LEFT_ARROW:
  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(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  136. textAlignment, true, rightToLeft);
  137. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex - 1,
  138. textAlignment, true, rightToLeft);
  139. _dirty = true;
  140. break;
  141. }
  142. case Keyboard::KEY_RIGHT_ARROW:
  143. {
  144. Font* font = getFont(_state);
  145. GP_ASSERT(font);
  146. unsigned int fontSize = getFontSize(_state);
  147. Font::Justify textAlignment = getTextAlignment(_state);
  148. bool rightToLeft = getTextRightToLeft(_state);
  149. int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  150. textAlignment, true, rightToLeft);
  151. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
  152. textAlignment, true, rightToLeft);
  153. _dirty = true;
  154. break;
  155. }
  156. case Keyboard::KEY_UP_ARROW:
  157. {
  158. Font* font = getFont(_state);
  159. GP_ASSERT(font);
  160. unsigned int fontSize = getFontSize(_state);
  161. Font::Justify textAlignment = getTextAlignment(_state);
  162. bool rightToLeft = getTextRightToLeft(_state);
  163. _prevCaretLocation.set(_caretLocation);
  164. _caretLocation.y -= fontSize;
  165. int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  166. textAlignment, true, rightToLeft);
  167. if (textIndex == -1)
  168. {
  169. _caretLocation.set(_prevCaretLocation);
  170. }
  171. _dirty = true;
  172. break;
  173. }
  174. case Keyboard::KEY_DOWN_ARROW:
  175. {
  176. Font* font = getFont(_state);
  177. GP_ASSERT(font);
  178. unsigned int fontSize = getFontSize(_state);
  179. Font::Justify textAlignment = getTextAlignment(_state);
  180. bool rightToLeft = getTextRightToLeft(_state);
  181. _prevCaretLocation.set(_caretLocation);
  182. _caretLocation.y += fontSize;
  183. int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  184. textAlignment, true, rightToLeft);
  185. if (textIndex == -1)
  186. {
  187. _caretLocation.set(_prevCaretLocation);
  188. }
  189. _dirty = true;
  190. break;
  191. }
  192. }
  193. break;
  194. }
  195. case Keyboard::KEY_CHAR:
  196. {
  197. Font* font = getFont(_state);
  198. GP_ASSERT(font);
  199. unsigned int fontSize = getFontSize(_state);
  200. Font::Justify textAlignment = getTextAlignment(_state);
  201. bool rightToLeft = getTextRightToLeft(_state);
  202. int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  203. textAlignment, true, rightToLeft);
  204. if (textIndex == -1)
  205. {
  206. textIndex = 0;
  207. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, 0,
  208. textAlignment, true, rightToLeft);
  209. }
  210. switch (key)
  211. {
  212. case Keyboard::KEY_BACKSPACE:
  213. {
  214. if (textIndex > 0)
  215. {
  216. --textIndex;
  217. _text.erase(textIndex, 1);
  218. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  219. textAlignment, true, rightToLeft);
  220. _dirty = true;
  221. }
  222. break;
  223. }
  224. case Keyboard::KEY_RETURN:
  225. // TODO: Handle line-break insertion correctly.
  226. break;
  227. case Keyboard::KEY_ESCAPE:
  228. break;
  229. case Keyboard::KEY_TAB:
  230. // Allow tab to move the focus forward.
  231. return false;
  232. break;
  233. default:
  234. {
  235. // Insert character into string.
  236. _text.insert(textIndex, 1, (char)key);
  237. // Get new location of caret.
  238. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
  239. textAlignment, true, rightToLeft);
  240. if (key == ' ')
  241. {
  242. // If a space was entered, check that caret is still within bounds.
  243. if (_caretLocation.x >= _textBounds.x + _textBounds.width ||
  244. _caretLocation.y >= _textBounds.y + _textBounds.height)
  245. {
  246. // If not, undo the character insertion.
  247. _text.erase(textIndex, 1);
  248. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  249. textAlignment, true, rightToLeft);
  250. // No need to check again.
  251. break;
  252. }
  253. }
  254. // Always check that the text still fits within the clip region.
  255. Rectangle textBounds;
  256. font->measureText(_text.c_str(), _textBounds, fontSize, &textBounds, textAlignment, true, true);
  257. if (textBounds.x < _textBounds.x || textBounds.y < _textBounds.y ||
  258. textBounds.width >= _textBounds.width || textBounds.height >= _textBounds.height)
  259. {
  260. // If not, undo the character insertion.
  261. _text.erase(textIndex, 1);
  262. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  263. textAlignment, true, rightToLeft);
  264. // TextBox is not dirty.
  265. break;
  266. }
  267. _dirty = true;
  268. break;
  269. }
  270. break;
  271. }
  272. notifyListeners(Control::Listener::TEXT_CHANGED);
  273. break;
  274. }
  275. }
  276. }
  277. _lastKeypress = key;
  278. return _consumeInputEvents;
  279. }
  280. void TextBox::update(const Control* container, const Vector2& offset)
  281. {
  282. Label::update(container, offset);
  283. _fontSize = getFontSize(_state);
  284. _caretImage = getImage("textCaret", _state);
  285. }
  286. void TextBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  287. {
  288. if (_caretImage && (_state == ACTIVE || _state == FOCUS))
  289. {
  290. // Draw the cursor at its current location.
  291. const Rectangle& region = _caretImage->getRegion();
  292. if (!region.isEmpty())
  293. {
  294. GP_ASSERT(spriteBatch);
  295. const Theme::UVs uvs = _caretImage->getUVs();
  296. Vector4 color = _caretImage->getColor();
  297. color.w *= _opacity;
  298. spriteBatch->draw(_caretLocation.x - (region.width / 2.0f), _caretLocation.y, region.width, _fontSize, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _viewportClipBounds);
  299. }
  300. }
  301. _dirty = false;
  302. }
  303. void TextBox::setCaretLocation(int x, int y)
  304. {
  305. // Get index into string and cursor location from the latest touch location.
  306. _prevCaretLocation.set(_caretLocation);
  307. _caretLocation.set(x + _absoluteBounds.x,
  308. y + _absoluteBounds.y);
  309. Font* font = getFont(_state);
  310. unsigned int fontSize = getFontSize(_state);
  311. Font::Justify textAlignment = getTextAlignment(_state);
  312. bool rightToLeft = getTextRightToLeft(_state);
  313. int index = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  314. textAlignment, true, rightToLeft);
  315. if (index == -1)
  316. {
  317. // Attempt to find the nearest valid caret location.
  318. Rectangle textBounds;
  319. font->measureText(_text.c_str(), _textBounds, fontSize, &textBounds, textAlignment, true, true);
  320. if (_caretLocation.x > textBounds.x + textBounds.width &&
  321. _caretLocation.y > textBounds.y + textBounds.height)
  322. {
  323. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, (unsigned int)_text.length(),
  324. textAlignment, true, rightToLeft);
  325. return;
  326. }
  327. if (_caretLocation.x < textBounds.x)
  328. {
  329. _caretLocation.x = textBounds.x;
  330. }
  331. else if (_caretLocation.x > textBounds.x + textBounds.width)
  332. {
  333. _caretLocation.x = textBounds.x + textBounds.width;
  334. }
  335. if (_caretLocation.y < textBounds.y)
  336. {
  337. _caretLocation.y = textBounds.y;
  338. }
  339. else if (_caretLocation.y > textBounds.y + textBounds.height)
  340. {
  341. Font* font = getFont(_state);
  342. GP_ASSERT(font);
  343. unsigned int fontSize = getFontSize(_state);
  344. _caretLocation.y = textBounds.y + textBounds.height - fontSize;
  345. }
  346. index = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  347. textAlignment, true, rightToLeft);
  348. if (index == -1)
  349. {
  350. // We failed to find a valid location; just put the caret back to where it was.
  351. _caretLocation.set(_prevCaretLocation);
  352. }
  353. }
  354. }
  355. const char* TextBox::getType() const
  356. {
  357. return "textBox";
  358. }
  359. }