CTextInput.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * CTextInput.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CTextInput.h"
  12. #include "Images.h"
  13. #include "TextControls.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../gui/Shortcut.h"
  16. #include "../render/Graphics.h"
  17. #include "../render/IFont.h"
  18. #include "../../lib/TextOperations.h"
  19. std::list<CFocusable *> CFocusable::focusables;
  20. CFocusable * CFocusable::inputWithFocus;
  21. CTextInput::CTextInput(const Rect & Pos)
  22. :originalAlignment(ETextAlignment::CENTERLEFT)
  23. {
  24. pos += Pos.topLeft();
  25. pos.h = Pos.h;
  26. pos.w = Pos.w;
  27. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  28. }
  29. void CTextInput::createLabel(bool giveFocusToInput)
  30. {
  31. OBJ_CONSTRUCTION;
  32. label = std::make_shared<CLabel>();
  33. label->pos = pos;
  34. label->alignment = originalAlignment;
  35. #if !defined(VCMI_MOBILE)
  36. if(giveFocusToInput)
  37. giveFocus();
  38. #endif
  39. }
  40. CTextInput::CTextInput(const Rect & Pos, EFonts font, ETextAlignment alignment, bool giveFocusToInput)
  41. : CTextInput(Pos)
  42. {
  43. originalAlignment = alignment;
  44. setRedrawParent(true);
  45. createLabel(giveFocusToInput);
  46. setFont(font);
  47. setAlignment(alignment);
  48. }
  49. CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName)
  50. : CTextInput(Pos)
  51. {
  52. OBJ_CONSTRUCTION;
  53. if (!bgName.empty())
  54. background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
  55. else
  56. setRedrawParent(true);
  57. createLabel(true);
  58. }
  59. CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
  60. : CTextInput(Pos)
  61. {
  62. OBJ_CONSTRUCTION;
  63. background = std::make_shared<CPicture>(srf, Pos);
  64. pos.w = background->pos.w;
  65. pos.h = background->pos.h;
  66. background->pos = pos;
  67. createLabel(true);
  68. }
  69. void CTextInput::setFont(EFonts font)
  70. {
  71. label->font = font;
  72. }
  73. void CTextInput::setColor(const ColorRGBA & color)
  74. {
  75. label->color = color;
  76. }
  77. void CTextInput::setAlignment(ETextAlignment alignment)
  78. {
  79. originalAlignment = alignment;
  80. label->alignment = alignment;
  81. }
  82. const std::string & CTextInput::getText() const
  83. {
  84. return currentText;
  85. }
  86. void CTextInput::setCallback(const TextEditedCallback & cb)
  87. {
  88. assert(!onTextEdited);
  89. onTextEdited = cb;
  90. }
  91. void CTextInput::setFilterFilename()
  92. {
  93. assert(!onTextFiltering);
  94. onTextFiltering = std::bind(&CTextInput::filenameFilter, _1, _2);
  95. }
  96. void CTextInput::setFilterNumber(int minValue, int maxValue)
  97. {
  98. onTextFiltering = std::bind(&CTextInput::numberFilter, _1, _2, minValue, maxValue);
  99. }
  100. std::string CTextInput::getVisibleText()
  101. {
  102. return hasFocus() ? currentText + composedText + "_" : currentText;
  103. }
  104. void CTextInput::clickPressed(const Point & cursorPosition)
  105. {
  106. // attempt to give focus unconditionally, even if we already have it
  107. // this forces on-screen keyboard to show up again, even if player have closed it before
  108. giveFocus();
  109. }
  110. void CTextInput::keyPressed(EShortcut key)
  111. {
  112. if(!hasFocus())
  113. return;
  114. if(key == EShortcut::GLOBAL_MOVE_FOCUS)
  115. {
  116. moveFocus();
  117. return;
  118. }
  119. bool redrawNeeded = false;
  120. switch(key)
  121. {
  122. case EShortcut::GLOBAL_BACKSPACE:
  123. if(!composedText.empty())
  124. {
  125. TextOperations::trimRightUnicode(composedText);
  126. redrawNeeded = true;
  127. }
  128. else if(!currentText.empty())
  129. {
  130. TextOperations::trimRightUnicode(currentText);
  131. redrawNeeded = true;
  132. }
  133. break;
  134. default:
  135. break;
  136. }
  137. if(redrawNeeded)
  138. {
  139. updateLabel();
  140. if(onTextEdited)
  141. onTextEdited(currentText);
  142. }
  143. }
  144. void CTextInput::setText(const std::string & nText)
  145. {
  146. currentText = nText;
  147. updateLabel();
  148. }
  149. void CTextInput::updateLabel()
  150. {
  151. std::string visibleText = getVisibleText();
  152. label->alignment = originalAlignment;
  153. while (graphics->fonts[label->font]->getStringWidth(visibleText) > pos.w)
  154. {
  155. label->alignment = ETextAlignment::CENTERRIGHT;
  156. visibleText = visibleText.substr(TextOperations::getUnicodeCharacterSize(visibleText[0]));
  157. }
  158. label->setText(visibleText);
  159. }
  160. void CTextInput::textInputed(const std::string & enteredText)
  161. {
  162. if(!hasFocus())
  163. return;
  164. std::string oldText = currentText;
  165. setText(getText() + enteredText);
  166. if(onTextFiltering)
  167. onTextFiltering(currentText, oldText);
  168. if(currentText != oldText)
  169. {
  170. updateLabel();
  171. if(onTextEdited)
  172. onTextEdited(currentText);
  173. }
  174. composedText.clear();
  175. }
  176. void CTextInput::textEdited(const std::string & enteredText)
  177. {
  178. if(!hasFocus())
  179. return;
  180. composedText = enteredText;
  181. updateLabel();
  182. //onTextEdited(currentText + composedText);
  183. }
  184. void CTextInput::filenameFilter(std::string & text, const std::string &oldText)
  185. {
  186. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  187. size_t pos;
  188. while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  189. text.erase(pos, 1);
  190. }
  191. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  192. {
  193. assert(minValue < maxValue);
  194. if(text.empty())
  195. text = "0";
  196. size_t pos = 0;
  197. if(text[0] == '-') //allow '-' sign as first symbol only
  198. pos++;
  199. while(pos < text.size())
  200. {
  201. if(text[pos] < '0' || text[pos] > '9')
  202. {
  203. text = oldText;
  204. return; //new text is not number.
  205. }
  206. pos++;
  207. }
  208. try
  209. {
  210. int value = boost::lexical_cast<int>(text);
  211. if(value < minValue)
  212. text = std::to_string(minValue);
  213. else if(value > maxValue)
  214. text = std::to_string(maxValue);
  215. }
  216. catch(boost::bad_lexical_cast &)
  217. {
  218. //Should never happen. Unless I missed some cases
  219. logGlobal->warn("Warning: failed to convert %s to number!", text);
  220. text = oldText;
  221. }
  222. }
  223. void CTextInput::activate()
  224. {
  225. CFocusable::activate();
  226. if (hasFocus())
  227. {
  228. #if defined(VCMI_MOBILE)
  229. //giveFocus();
  230. #else
  231. GH.startTextInput(pos);
  232. #endif
  233. }
  234. }
  235. void CTextInput::deactivate()
  236. {
  237. CFocusable::deactivate();
  238. if (hasFocus())
  239. {
  240. #if defined(VCMI_MOBILE)
  241. removeFocus();
  242. #else
  243. GH.stopTextInput();
  244. #endif
  245. }
  246. }
  247. void CTextInput::onFocusGot()
  248. {
  249. updateLabel();
  250. }
  251. void CTextInput::onFocusLost()
  252. {
  253. updateLabel();
  254. }
  255. void CFocusable::focusGot()
  256. {
  257. if (isActive())
  258. GH.startTextInput(pos);
  259. onFocusGot();
  260. }
  261. void CFocusable::focusLost()
  262. {
  263. if (isActive())
  264. GH.stopTextInput();
  265. onFocusLost();
  266. }
  267. CFocusable::CFocusable()
  268. {
  269. focusables.push_back(this);
  270. }
  271. CFocusable::~CFocusable()
  272. {
  273. if(hasFocus())
  274. inputWithFocus = nullptr;
  275. focusables -= this;
  276. }
  277. bool CFocusable::hasFocus() const
  278. {
  279. return inputWithFocus == this;
  280. }
  281. void CFocusable::giveFocus()
  282. {
  283. auto previousInput = inputWithFocus;
  284. inputWithFocus = this;
  285. if(previousInput)
  286. previousInput->focusLost();
  287. focusGot();
  288. }
  289. void CFocusable::moveFocus()
  290. {
  291. auto i = vstd::find(focusables, this);
  292. auto ourIt = i;
  293. for(i++; i != ourIt; i++)
  294. {
  295. if(i == focusables.end())
  296. i = focusables.begin();
  297. if(*i == this)
  298. return;
  299. if((*i)->isActive())
  300. {
  301. (*i)->giveFocus();
  302. break;
  303. }
  304. }
  305. }
  306. void CFocusable::removeFocus()
  307. {
  308. if(this == inputWithFocus)
  309. {
  310. inputWithFocus = nullptr;
  311. focusLost();
  312. }
  313. }