CTextInput.cpp 7.2 KB

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