TextBox.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. assert("VALUE_CHANGED event is not applicable to TextBox.");
  29. eventFlags &= ~Listener::VALUE_CHANGED;
  30. }
  31. Control::addListener(listener, eventFlags);
  32. }
  33. bool TextBox::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  34. {
  35. if (!isEnabled())
  36. {
  37. return false;
  38. }
  39. switch (evt)
  40. {
  41. case Touch::TOUCH_PRESS:
  42. if (_state == NORMAL)
  43. {
  44. _state = ACTIVE;
  45. Game::getInstance()->displayKeyboard(true);
  46. _dirty = true;
  47. return _consumeTouchEvents;
  48. }
  49. else if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  50. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  51. {
  52. setCaretLocation(x, y);
  53. _dirty = true;
  54. return _consumeTouchEvents;
  55. }
  56. else
  57. {
  58. _state = NORMAL;
  59. Game::getInstance()->displayKeyboard(false);
  60. _dirty = true;
  61. return _consumeTouchEvents;
  62. }
  63. break;
  64. case Touch::TOUCH_MOVE:
  65. if (_state == FOCUS &&
  66. x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  67. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  68. {
  69. setCaretLocation(x, y);
  70. _dirty = true;
  71. return _consumeTouchEvents;
  72. }
  73. break;
  74. case Touch::TOUCH_RELEASE:
  75. if (x > _clipBounds.x && x <= _clipBounds.x + _clipBounds.width &&
  76. y > _clipBounds.y && y <= _clipBounds.y + _clipBounds.height)
  77. {
  78. setCaretLocation(x, y);
  79. _state = FOCUS;
  80. _dirty = true;
  81. return _consumeTouchEvents;
  82. }
  83. else
  84. {
  85. _state = NORMAL;
  86. Game::getInstance()->displayKeyboard(false);
  87. _dirty = true;
  88. return _consumeTouchEvents;
  89. }
  90. break;
  91. }
  92. return _consumeTouchEvents;
  93. }
  94. void TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
  95. {
  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 = _clip.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. 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. unsigned int fontSize = getFontSize(_state);
  136. Font::Justify textAlignment = getTextAlignment(_state);
  137. bool rightToLeft = getTextRightToLeft(_state);
  138. unsigned int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  139. textAlignment, true, rightToLeft);
  140. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex - 1,
  141. textAlignment, true, rightToLeft);
  142. _dirty = true;
  143. break;
  144. }
  145. case Keyboard::KEY_RIGHT_ARROW:
  146. {
  147. Font* font = getFont(_state);
  148. unsigned int fontSize = getFontSize(_state);
  149. Font::Justify textAlignment = getTextAlignment(_state);
  150. bool rightToLeft = getTextRightToLeft(_state);
  151. int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  152. textAlignment, true, rightToLeft);
  153. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
  154. textAlignment, true, rightToLeft);
  155. _dirty = true;
  156. break;
  157. }
  158. case Keyboard::KEY_UP_ARROW:
  159. {
  160. Font* font = getFont(_state);
  161. unsigned int fontSize = getFontSize(_state);
  162. Font::Justify textAlignment = getTextAlignment(_state);
  163. bool rightToLeft = getTextRightToLeft(_state);
  164. _caretLocation.y -= fontSize;
  165. font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  166. textAlignment, true, rightToLeft);
  167. _dirty = true;
  168. break;
  169. }
  170. case Keyboard::KEY_DOWN_ARROW:
  171. {
  172. Font* font = getFont(_state);
  173. unsigned int fontSize = getFontSize(_state);
  174. Font::Justify textAlignment = getTextAlignment(_state);
  175. bool rightToLeft = getTextRightToLeft(_state);
  176. _caretLocation.y += fontSize;
  177. font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  178. textAlignment, true, rightToLeft);
  179. _dirty = true;
  180. break;
  181. }
  182. }
  183. break;
  184. }
  185. case Keyboard::KEY_CHAR:
  186. {
  187. Font* font = getFont(_state);
  188. unsigned int fontSize = getFontSize(_state);
  189. Font::Justify textAlignment = getTextAlignment(_state);
  190. bool rightToLeft = getTextRightToLeft(_state);
  191. int textIndex = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  192. textAlignment, true, rightToLeft);
  193. switch (key)
  194. {
  195. case Keyboard::KEY_BACKSPACE:
  196. {
  197. if (textIndex > 0)
  198. {
  199. --textIndex;
  200. _text.erase(textIndex, 1);
  201. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  202. textAlignment, true, rightToLeft);
  203. _dirty = true;
  204. }
  205. break;
  206. }
  207. case Keyboard::KEY_RETURN:
  208. // TODO: Handle line-break insertion correctly.
  209. break;
  210. default:
  211. {
  212. // Insert character into string.
  213. _text.insert(textIndex, 1, (char)key);
  214. // Get new location of caret.
  215. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex + 1,
  216. textAlignment, true, rightToLeft);
  217. if (key == ' ')
  218. {
  219. // If a space was entered, check that caret is still within bounds.
  220. if (_caretLocation.x >= _textBounds.x + _textBounds.width ||
  221. _caretLocation.y >= _textBounds.y + _textBounds.height)
  222. {
  223. // If not, undo the character insertion.
  224. _text.erase(textIndex, 1);
  225. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  226. textAlignment, true, rightToLeft);
  227. // No need to check again.
  228. break;
  229. }
  230. }
  231. // Always check that the text still fits within the clip region.
  232. Rectangle textBounds;
  233. font->measureText(_text.c_str(), _textBounds, fontSize, &textBounds, textAlignment, true, true);
  234. if (textBounds.x <= _textBounds.x || textBounds.y <= _textBounds.y ||
  235. textBounds.width >= _textBounds.width || textBounds.height >= _textBounds.height)
  236. {
  237. // If not, undo the character insertion.
  238. _text.erase(textIndex, 1);
  239. font->getLocationAtIndex(_text.c_str(), _textBounds, fontSize, &_caretLocation, textIndex,
  240. textAlignment, true, rightToLeft);
  241. // TextBox is not dirty.
  242. break;
  243. }
  244. _dirty = true;
  245. break;
  246. }
  247. break;
  248. }
  249. notifyListeners(Listener::TEXT_CHANGED);
  250. }
  251. }
  252. }
  253. _lastKeypress = key;
  254. }
  255. void TextBox::update(const Rectangle& clip, const Vector2& offset)
  256. {
  257. Label::update(clip, offset);
  258. _fontSize = getFontSize(_state);
  259. _caretImage = getImage("textCaret", _state);
  260. }
  261. void TextBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  262. {
  263. if (_state == FOCUS)
  264. {
  265. // Draw the cursor at its current location.
  266. const Rectangle& region = _caretImage->getRegion();
  267. if (!region.isEmpty())
  268. {
  269. const Theme::UVs uvs = _caretImage->getUVs();
  270. Vector4 color = _caretImage->getColor();
  271. color.w *= _opacity;
  272. spriteBatch->draw(_caretLocation.x - (region.width / 2.0f), _caretLocation.y, region.width, _fontSize, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color, _clip);
  273. }
  274. }
  275. _dirty = false;
  276. }
  277. void TextBox::setCaretLocation(int x, int y)
  278. {
  279. // Get index into string and cursor location from the latest touch location.
  280. _prevCaretLocation.set(_caretLocation);
  281. _caretLocation.set(x + _absoluteBounds.x,
  282. y + _absoluteBounds.y);
  283. Font* font = getFont(_state);
  284. unsigned int fontSize = getFontSize(_state);
  285. Font::Justify textAlignment = getTextAlignment(_state);
  286. bool rightToLeft = getTextRightToLeft(_state);
  287. int index = font->getIndexAtLocation(_text.c_str(), _textBounds, fontSize, _caretLocation, &_caretLocation,
  288. textAlignment, true, rightToLeft);
  289. if (index == -1)
  290. {
  291. _caretLocation.set(_prevCaretLocation);
  292. }
  293. }
  294. }