CTextInput.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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() const
  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. }
  183. void CTextInput::filenameFilter(std::string & text, const std::string &oldText)
  184. {
  185. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  186. size_t pos;
  187. while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  188. text.erase(pos, 1);
  189. }
  190. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  191. {
  192. assert(minValue < maxValue);
  193. if(text.empty())
  194. text = "0";
  195. size_t pos = 0;
  196. if(text[0] == '-') //allow '-' sign as first symbol only
  197. pos++;
  198. while(pos < text.size())
  199. {
  200. if(text[pos] < '0' || text[pos] > '9')
  201. {
  202. text = oldText;
  203. return; //new text is not number.
  204. }
  205. pos++;
  206. }
  207. try
  208. {
  209. int value = boost::lexical_cast<int>(text);
  210. if(value < minValue)
  211. text = std::to_string(minValue);
  212. else if(value > maxValue)
  213. text = std::to_string(maxValue);
  214. }
  215. catch(boost::bad_lexical_cast &)
  216. {
  217. //Should never happen. Unless I missed some cases
  218. logGlobal->warn("Warning: failed to convert %s to number!", text);
  219. text = oldText;
  220. }
  221. }
  222. void CTextInput::activate()
  223. {
  224. CFocusable::activate();
  225. if (hasFocus())
  226. {
  227. #if defined(VCMI_MOBILE)
  228. //giveFocus();
  229. #else
  230. GH.startTextInput(pos);
  231. #endif
  232. }
  233. }
  234. void CTextInput::deactivate()
  235. {
  236. CFocusable::deactivate();
  237. if (hasFocus())
  238. {
  239. #if defined(VCMI_MOBILE)
  240. removeFocus();
  241. #else
  242. GH.stopTextInput();
  243. #endif
  244. }
  245. }
  246. void CTextInput::onFocusGot()
  247. {
  248. updateLabel();
  249. }
  250. void CTextInput::onFocusLost()
  251. {
  252. updateLabel();
  253. }
  254. void CFocusable::focusGot()
  255. {
  256. if (isActive())
  257. GH.startTextInput(pos);
  258. onFocusGot();
  259. }
  260. void CFocusable::focusLost()
  261. {
  262. if (isActive())
  263. GH.stopTextInput();
  264. onFocusLost();
  265. }
  266. CFocusable::CFocusable()
  267. {
  268. focusables.push_back(this);
  269. }
  270. CFocusable::~CFocusable()
  271. {
  272. if(hasFocus())
  273. inputWithFocus = nullptr;
  274. focusables -= this;
  275. }
  276. bool CFocusable::hasFocus() const
  277. {
  278. return inputWithFocus == this;
  279. }
  280. void CFocusable::giveFocus()
  281. {
  282. auto previousInput = inputWithFocus;
  283. inputWithFocus = this;
  284. if(previousInput)
  285. previousInput->focusLost();
  286. focusGot();
  287. }
  288. void CFocusable::moveFocus()
  289. {
  290. auto i = vstd::find(focusables, this);
  291. auto ourIt = i;
  292. for(i++; i != ourIt; i++)
  293. {
  294. if(i == focusables.end())
  295. i = focusables.begin();
  296. if(*i == this)
  297. return;
  298. if((*i)->isActive())
  299. {
  300. (*i)->giveFocus();
  301. break;
  302. }
  303. }
  304. }
  305. void CFocusable::removeFocus()
  306. {
  307. if(this == inputWithFocus)
  308. {
  309. inputWithFocus = nullptr;
  310. focusLost();
  311. }
  312. }