TextBox.cpp 15 KB

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