2
0

TextBox.cpp 15 KB

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