CTextInput.cpp 7.2 KB

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