CTextInput.cpp 7.1 KB

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