TextBox.cpp 14 KB

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