TextBox.cpp 13 KB

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