TextBox.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 > 0 && x <= _clipBounds.width &&
  50. y > 0 && y <= _clipBounds.height))
  51. {
  52. _state = NORMAL;
  53. Game::getInstance()->displayKeyboard(false);
  54. _dirty = true;
  55. return _consumeTouchEvents;
  56. }
  57. break;
  58. case Touch::TOUCH_MOVE:
  59. if (_state == FOCUS &&
  60. x > 0 && x <= _clipBounds.width &&
  61. y > 0 && y <= _clipBounds.height)
  62. {
  63. setCaretLocation(x, y);
  64. _dirty = true;
  65. return _consumeTouchEvents;
  66. }
  67. break;
  68. case Touch::TOUCH_RELEASE:
  69. if (x > 0 && x <= _clipBounds.width &&
  70. y > 0 && y <= _clipBounds.height)
  71. {
  72. setCaretLocation(x, y);
  73. _state = FOCUS;
  74. _dirty = true;
  75. return _consumeTouchEvents;
  76. }
  77. break;
  78. }
  79. return _consumeTouchEvents;
  80. }
  81. void TextBox::keyEvent(Keyboard::KeyEvent evt, int key)
  82. {
  83. if (_state == FOCUS)
  84. {
  85. switch (evt)
  86. {
  87. case Keyboard::KEY_PRESS:
  88. {
  89. switch (key)
  90. {
  91. case Keyboard::KEY_HOME:
  92. {
  93. // TODO: Move cursor to beginning of line.
  94. // This only works for left alignment...
  95. //_caretLocation.x = _clip.x;
  96. //_dirty = true;
  97. break;
  98. }
  99. case Keyboard::KEY_END:
  100. {
  101. // TODO: Move cursor to end of line.
  102. break;
  103. }
  104. case Keyboard::KEY_DELETE:
  105. {
  106. Font* font = getFont(_state);
  107. unsigned int fontSize = getFontSize(_state);
  108. Font::Justify textAlignment = getTextAlignment(_state);
  109. bool rightToLeft = getTextRightToLeft(_state);
  110. unsigned int textIndex = font->getIndexAtLocation(_text.c_str(), _clip, fontSize, _caretLocation, &_caretLocation,
  111. textAlignment, true, rightToLeft);
  112. _text.erase(textIndex, 1);
  113. font->getLocationAtIndex(_text.c_str(), _clip, fontSize, &_caretLocation, textIndex,
  114. textAlignment, true, rightToLeft);
  115. _dirty = true;
  116. notifyListeners(Listener::TEXT_CHANGED);
  117. break;
  118. }
  119. case Keyboard::KEY_LEFT_ARROW:
  120. {
  121. Font* font = getFont(_state);
  122. unsigned int fontSize = getFontSize(_state);
  123. Font::Justify textAlignment = getTextAlignment(_state);
  124. bool rightToLeft = getTextRightToLeft(_state);
  125. unsigned int textIndex = font->getIndexAtLocation(_text.c_str(), _clip, fontSize, _caretLocation, &_caretLocation,
  126. textAlignment, true, rightToLeft);
  127. font->getLocationAtIndex(_text.c_str(), _clip, fontSize, &_caretLocation, textIndex - 1,
  128. textAlignment, true, rightToLeft);
  129. _dirty = true;
  130. break;
  131. }
  132. case Keyboard::KEY_RIGHT_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(), _clip, fontSize, _caretLocation, &_caretLocation,
  139. textAlignment, true, rightToLeft);
  140. font->getLocationAtIndex(_text.c_str(), _clip, fontSize, &_caretLocation, textIndex + 1,
  141. textAlignment, true, rightToLeft);
  142. _dirty = true;
  143. break;
  144. }
  145. case Keyboard::KEY_UP_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. _caretLocation.y -= fontSize;
  152. font->getIndexAtLocation(_text.c_str(), _clip, fontSize, _caretLocation, &_caretLocation,
  153. textAlignment, true, rightToLeft);
  154. _dirty = true;
  155. break;
  156. }
  157. case Keyboard::KEY_DOWN_ARROW:
  158. {
  159. Font* font = getFont(_state);
  160. unsigned int fontSize = getFontSize(_state);
  161. Font::Justify textAlignment = getTextAlignment(_state);
  162. bool rightToLeft = getTextRightToLeft(_state);
  163. _caretLocation.y += fontSize;
  164. font->getIndexAtLocation(_text.c_str(), _clip, fontSize, _caretLocation, &_caretLocation,
  165. textAlignment, true, rightToLeft);
  166. _dirty = true;
  167. break;
  168. }
  169. }
  170. break;
  171. }
  172. case Keyboard::KEY_CHAR:
  173. {
  174. Font* font = getFont(_state);
  175. unsigned int fontSize = getFontSize(_state);
  176. Font::Justify textAlignment = getTextAlignment(_state);
  177. bool rightToLeft = getTextRightToLeft(_state);
  178. unsigned int textIndex = font->getIndexAtLocation(_text.c_str(), _clip, fontSize, _caretLocation, &_caretLocation,
  179. textAlignment, true, rightToLeft);
  180. switch (key)
  181. {
  182. case Keyboard::KEY_BACKSPACE:
  183. {
  184. if (textIndex > 0)
  185. {
  186. --textIndex;
  187. _text.erase(textIndex, 1);
  188. font->getLocationAtIndex(_text.c_str(), _clip, fontSize, &_caretLocation, textIndex,
  189. textAlignment, true, rightToLeft);
  190. _dirty = true;
  191. }
  192. break;
  193. }
  194. case Keyboard::KEY_RETURN:
  195. // TODO: Handle line-break insertion correctly.
  196. break;
  197. default:
  198. {
  199. // Insert character into string.
  200. _text.insert(textIndex, 1, (char)key);
  201. // Get new location of cursor.
  202. font->getLocationAtIndex(_text.c_str(), _clip, fontSize, &_caretLocation, textIndex + 1,
  203. textAlignment, true, rightToLeft);
  204. _dirty = true;
  205. break;
  206. }
  207. break;
  208. }
  209. notifyListeners(Listener::TEXT_CHANGED);
  210. }
  211. }
  212. }
  213. _lastKeypress = key;
  214. }
  215. void TextBox::update(const Rectangle& clip)
  216. {
  217. Control::update(clip);
  218. // Get index into string and cursor location from the last recorded touch location.
  219. if (_state == FOCUS)
  220. {
  221. Font* font = getFont(_state);
  222. unsigned int fontSize = getFontSize(_state);
  223. Font::Justify textAlignment = getTextAlignment(_state);
  224. bool rightToLeft = getTextRightToLeft(_state);
  225. font->getIndexAtLocation(_text.c_str(), _clip, fontSize, _caretLocation, &_caretLocation,
  226. textAlignment, true, rightToLeft);
  227. }
  228. }
  229. void TextBox::drawImages(SpriteBatch* spriteBatch, const Rectangle& clip)
  230. {
  231. if (_state == FOCUS)
  232. {
  233. // Draw the cursor at its current location.
  234. const Rectangle& region = getImageRegion("textCaret", _state);
  235. if (!region.isEmpty())
  236. {
  237. const Vector4& color = getImageColor("textCaret", _state);
  238. const Theme::UVs uvs = getImageUVs("textCaret", _state);
  239. unsigned int fontSize = getFontSize(_state);
  240. spriteBatch->draw(_caretLocation.x - (region.width / 2.0f), _caretLocation.y, region.width, fontSize, uvs.u1, uvs.v1, uvs.u2, uvs.v2, color);
  241. }
  242. }
  243. _dirty = false;
  244. }
  245. void TextBox::setCaretLocation(int x, int y)
  246. {
  247. Theme::Border border = getBorder(_state);
  248. Theme::Padding padding = getPadding();
  249. _caretLocation.set(x - border.left - padding.left + _clip.x,
  250. y - border.top - padding.top + _clip.y);
  251. }
  252. }