CTextInput.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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/texts/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 | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  28. }
  29. void CTextInput::createLabel(bool giveFocusToInput)
  30. {
  31. OBJECT_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. OBJECT_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. OBJECT_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::setPopupCallback(const std::function<void()> & cb)
  92. {
  93. callbackPopup = cb;
  94. }
  95. void CTextInput::setFilterFilename()
  96. {
  97. assert(!onTextFiltering);
  98. onTextFiltering = std::bind(&CTextInput::filenameFilter, _1, _2);
  99. }
  100. void CTextInput::setFilterNumber(int minValue, int maxValue)
  101. {
  102. onTextFiltering = std::bind(&CTextInput::numberFilter, _1, _2, minValue, maxValue);
  103. }
  104. std::string CTextInput::getVisibleText() const
  105. {
  106. return hasFocus() ? currentText + composedText + "_" : currentText;
  107. }
  108. void CTextInput::showPopupWindow(const Point & cursorPosition)
  109. {
  110. if(callbackPopup)
  111. callbackPopup();
  112. }
  113. void CTextInput::clickPressed(const Point & cursorPosition)
  114. {
  115. // attempt to give focus unconditionally, even if we already have it
  116. // this forces on-screen keyboard to show up again, even if player have closed it before
  117. giveFocus();
  118. }
  119. void CTextInput::keyPressed(EShortcut key)
  120. {
  121. if(!hasFocus())
  122. return;
  123. if(key == EShortcut::GLOBAL_MOVE_FOCUS)
  124. {
  125. moveFocus();
  126. return;
  127. }
  128. bool redrawNeeded = false;
  129. switch(key)
  130. {
  131. case EShortcut::GLOBAL_BACKSPACE:
  132. if(!composedText.empty())
  133. {
  134. TextOperations::trimRightUnicode(composedText);
  135. redrawNeeded = true;
  136. }
  137. else if(!currentText.empty())
  138. {
  139. TextOperations::trimRightUnicode(currentText);
  140. redrawNeeded = true;
  141. }
  142. break;
  143. default:
  144. break;
  145. }
  146. if(redrawNeeded)
  147. {
  148. updateLabel();
  149. if(onTextEdited)
  150. onTextEdited(currentText);
  151. }
  152. }
  153. void CTextInput::setText(const std::string & nText)
  154. {
  155. currentText = nText;
  156. updateLabel();
  157. }
  158. void CTextInput::updateLabel()
  159. {
  160. std::string visibleText = getVisibleText();
  161. label->alignment = originalAlignment;
  162. while (graphics->fonts[label->font]->getStringWidth(visibleText) > pos.w)
  163. {
  164. label->alignment = ETextAlignment::CENTERRIGHT;
  165. visibleText = visibleText.substr(TextOperations::getUnicodeCharacterSize(visibleText[0]));
  166. }
  167. label->setText(visibleText);
  168. }
  169. void CTextInput::textInputted(const std::string & enteredText)
  170. {
  171. if(!hasFocus())
  172. return;
  173. std::string oldText = currentText;
  174. setText(getText() + enteredText);
  175. if(onTextFiltering)
  176. onTextFiltering(currentText, oldText);
  177. if(currentText != oldText)
  178. {
  179. updateLabel();
  180. if(onTextEdited)
  181. onTextEdited(currentText);
  182. }
  183. composedText.clear();
  184. }
  185. void CTextInput::textEdited(const std::string & enteredText)
  186. {
  187. if(!hasFocus())
  188. return;
  189. composedText = enteredText;
  190. updateLabel();
  191. }
  192. void CTextInput::filenameFilter(std::string & text, const std::string &oldText)
  193. {
  194. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  195. size_t pos;
  196. while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  197. text.erase(pos, 1);
  198. }
  199. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  200. {
  201. assert(minValue < maxValue);
  202. if(text.empty())
  203. text = "0";
  204. size_t pos = 0;
  205. if(text[0] == '-') //allow '-' sign as first symbol only
  206. pos++;
  207. while(pos < text.size())
  208. {
  209. if(text[pos] < '0' || text[pos] > '9')
  210. {
  211. text = oldText;
  212. return; //new text is not number.
  213. }
  214. pos++;
  215. }
  216. try
  217. {
  218. int value = boost::lexical_cast<int>(text);
  219. if(value < minValue)
  220. text = std::to_string(minValue);
  221. else if(value > maxValue)
  222. text = std::to_string(maxValue);
  223. }
  224. catch(boost::bad_lexical_cast &)
  225. {
  226. //Should never happen. Unless I missed some cases
  227. logGlobal->warn("Warning: failed to convert %s to number!", text);
  228. text = oldText;
  229. }
  230. }
  231. void CTextInput::activate()
  232. {
  233. CFocusable::activate();
  234. if (hasFocus())
  235. {
  236. #if defined(VCMI_MOBILE)
  237. //giveFocus();
  238. #else
  239. GH.startTextInput(pos);
  240. #endif
  241. }
  242. }
  243. void CTextInput::deactivate()
  244. {
  245. CFocusable::deactivate();
  246. if (hasFocus())
  247. {
  248. #if defined(VCMI_MOBILE)
  249. removeFocus();
  250. #else
  251. GH.stopTextInput();
  252. #endif
  253. }
  254. }
  255. void CTextInput::onFocusGot()
  256. {
  257. updateLabel();
  258. }
  259. void CTextInput::onFocusLost()
  260. {
  261. updateLabel();
  262. }
  263. void CFocusable::focusGot()
  264. {
  265. if (isActive())
  266. GH.startTextInput(pos);
  267. onFocusGot();
  268. }
  269. void CFocusable::focusLost()
  270. {
  271. if (isActive())
  272. GH.stopTextInput();
  273. onFocusLost();
  274. }
  275. CFocusable::CFocusable()
  276. {
  277. focusables.push_back(this);
  278. }
  279. CFocusable::~CFocusable()
  280. {
  281. if(hasFocus())
  282. inputWithFocus = nullptr;
  283. focusables -= this;
  284. }
  285. bool CFocusable::hasFocus() const
  286. {
  287. return inputWithFocus == this;
  288. }
  289. void CFocusable::giveFocus()
  290. {
  291. auto previousInput = inputWithFocus;
  292. inputWithFocus = this;
  293. if(previousInput)
  294. previousInput->focusLost();
  295. focusGot();
  296. }
  297. void CFocusable::moveFocus()
  298. {
  299. auto i = vstd::find(focusables, this);
  300. auto ourIt = i;
  301. for(i++; i != ourIt; i++)
  302. {
  303. if(i == focusables.end())
  304. i = focusables.begin();
  305. if(*i == this)
  306. return;
  307. if((*i)->isActive())
  308. {
  309. (*i)->giveFocus();
  310. break;
  311. }
  312. }
  313. }
  314. void CFocusable::removeFocus()
  315. {
  316. if(this == inputWithFocus)
  317. {
  318. inputWithFocus = nullptr;
  319. focusLost();
  320. }
  321. }