TextControls.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. * TextControls.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 "TextControls.h"
  12. #include "Slider.h"
  13. #include "Images.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/Shortcut.h"
  17. #include "../windows/CMessage.h"
  18. #include "../windows/InfoWindows.h"
  19. #include "../adventureMap/CInGameConsole.h"
  20. #include "../renderSDL/SDL_Extensions.h"
  21. #include "../render/Canvas.h"
  22. #include "../render/Graphics.h"
  23. #include "../render/IFont.h"
  24. #include "../../lib/TextOperations.h"
  25. #ifdef VCMI_ANDROID
  26. #include "lib/CAndroidVMHelper.h"
  27. #endif
  28. std::list<CFocusable*> CFocusable::focusables;
  29. CFocusable * CFocusable::inputWithFocus;
  30. std::string CLabel::visibleText()
  31. {
  32. return text;
  33. }
  34. void CLabel::showAll(Canvas & to)
  35. {
  36. CIntObject::showAll(to);
  37. if(!visibleText().empty())
  38. blitLine(to, pos, visibleText());
  39. }
  40. CLabel::CLabel(int x, int y, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text)
  41. : CTextContainer(Align, Font, Color), text(Text)
  42. {
  43. setRedrawParent(true);
  44. autoRedraw = true;
  45. pos.x += x;
  46. pos.y += y;
  47. pos.w = pos.h = 0;
  48. if(alignment == ETextAlignment::TOPLEFT) // causes issues for MIDDLE
  49. {
  50. pos.w = (int)graphics->fonts[font]->getStringWidth(visibleText().c_str());
  51. pos.h = (int)graphics->fonts[font]->getLineHeight();
  52. }
  53. }
  54. Point CLabel::getBorderSize()
  55. {
  56. return Point(0, 0);
  57. }
  58. std::string CLabel::getText()
  59. {
  60. return text;
  61. }
  62. void CLabel::setAutoRedraw(bool value)
  63. {
  64. autoRedraw = value;
  65. }
  66. void CLabel::setText(const std::string & Txt)
  67. {
  68. text = Txt;
  69. if(autoRedraw)
  70. {
  71. if(background || !parent)
  72. redraw();
  73. else
  74. parent->redraw();
  75. }
  76. }
  77. void CLabel::setColor(const ColorRGBA & Color)
  78. {
  79. color = Color;
  80. if(autoRedraw)
  81. {
  82. if(background || !parent)
  83. redraw();
  84. else
  85. parent->redraw();
  86. }
  87. }
  88. size_t CLabel::getWidth()
  89. {
  90. return graphics->fonts[font]->getStringWidth(visibleText());
  91. }
  92. CMultiLineLabel::CMultiLineLabel(Rect position, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text) :
  93. CLabel(position.x, position.y, Font, Align, Color, Text),
  94. visibleSize(0, 0, position.w, position.h)
  95. {
  96. pos.w = position.w;
  97. pos.h = position.h;
  98. splitText(Text, true);
  99. }
  100. void CMultiLineLabel::setVisibleSize(Rect visibleSize, bool redrawElement)
  101. {
  102. this->visibleSize = visibleSize;
  103. if(redrawElement)
  104. redraw();
  105. }
  106. void CMultiLineLabel::scrollTextBy(int distance)
  107. {
  108. scrollTextTo(visibleSize.y + distance);
  109. }
  110. void CMultiLineLabel::scrollTextTo(int distance, bool redrawAfterScroll)
  111. {
  112. Rect size = visibleSize;
  113. size.y = distance;
  114. setVisibleSize(size, redrawAfterScroll);
  115. }
  116. void CMultiLineLabel::setText(const std::string & Txt)
  117. {
  118. splitText(Txt, false); //setText used below can handle redraw
  119. CLabel::setText(Txt);
  120. }
  121. void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what)
  122. {
  123. const auto f = graphics->fonts[font];
  124. Point where = destRect.topLeft();
  125. const std::string delimeters = "{}";
  126. auto delimitersCount = std::count_if(what.cbegin(), what.cend(), [&delimeters](char c)
  127. {
  128. return delimeters.find(c) != std::string::npos;
  129. });
  130. //We should count delimiters length from string to correct centering later.
  131. delimitersCount *= f->getStringWidth(delimeters)/2;
  132. std::smatch match;
  133. std::regex expr("\\{(.*?)\\|");
  134. std::string::const_iterator searchStart( what.cbegin() );
  135. while(std::regex_search(searchStart, what.cend(), match, expr))
  136. {
  137. std::string colorText = match[1].str();
  138. if(auto c = Colors::parseColor(colorText))
  139. delimitersCount += f->getStringWidth(colorText + "|");
  140. searchStart = match.suffix().first;
  141. }
  142. // input is rect in which given text should be placed
  143. // calculate proper position for top-left corner of the text
  144. if(alignment == ETextAlignment::TOPLEFT)
  145. {
  146. where.x += getBorderSize().x;
  147. where.y += getBorderSize().y;
  148. }
  149. if(alignment == ETextAlignment::TOPCENTER)
  150. {
  151. where.x += (int(destRect.w) - int(f->getStringWidth(what) - delimitersCount)) / 2;
  152. where.y += getBorderSize().y;
  153. }
  154. if(alignment == ETextAlignment::CENTER)
  155. {
  156. where.x += (int(destRect.w) - int(f->getStringWidth(what) - delimitersCount)) / 2;
  157. where.y += (int(destRect.h) - int(f->getLineHeight())) / 2;
  158. }
  159. if(alignment == ETextAlignment::BOTTOMRIGHT)
  160. {
  161. where.x += getBorderSize().x + destRect.w - ((int)f->getStringWidth(what) - delimitersCount);
  162. where.y += getBorderSize().y + destRect.h - (int)f->getLineHeight();
  163. }
  164. size_t begin = 0;
  165. size_t currDelimeter = 0;
  166. do
  167. {
  168. size_t end = what.find_first_of(delimeters[currDelimeter % 2], begin);
  169. if(begin != end)
  170. {
  171. std::string toPrint = what.substr(begin, end - begin);
  172. if(currDelimeter % 2) // Enclosed in {} text - set to yellow or defined color
  173. {
  174. std::smatch match;
  175. std::regex expr("^(.*?)\\|");
  176. if(std::regex_search(toPrint, match, expr))
  177. {
  178. std::string colorText = match[1].str();
  179. if(auto color = Colors::parseColor(colorText))
  180. {
  181. toPrint = toPrint.substr(colorText.length() + 1, toPrint.length() - colorText.length());
  182. to.drawText(where, font, *color, ETextAlignment::TOPLEFT, toPrint);
  183. }
  184. else
  185. to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
  186. }
  187. else
  188. to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
  189. }
  190. else // Non-enclosed text, use default color
  191. to.drawText(where, font, color, ETextAlignment::TOPLEFT, toPrint);
  192. begin = end;
  193. where.x += (int)f->getStringWidth(toPrint);
  194. }
  195. currDelimeter++;
  196. } while(begin++ != std::string::npos);
  197. }
  198. CTextContainer::CTextContainer(ETextAlignment alignment, EFonts font, ColorRGBA color) :
  199. alignment(alignment),
  200. font(font),
  201. color(color)
  202. {
  203. }
  204. void CMultiLineLabel::showAll(Canvas & to)
  205. {
  206. CIntObject::showAll(to);
  207. const auto f = graphics->fonts[font];
  208. // calculate which lines should be visible
  209. int totalLines = static_cast<int>(lines.size());
  210. int beginLine = visibleSize.y;
  211. int endLine = getTextLocation().h + visibleSize.y;
  212. if(beginLine < 0)
  213. beginLine = 0;
  214. else
  215. beginLine /= (int)f->getLineHeight();
  216. if(endLine < 0)
  217. endLine = 0;
  218. else
  219. endLine /= (int)f->getLineHeight();
  220. endLine++;
  221. // and where they should be displayed
  222. Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * (int)f->getLineHeight());
  223. Point lineSize = Point(getTextLocation().w, (int)f->getLineHeight());
  224. CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), getTextLocation()); // to properly trim text that is too big to fit
  225. for(int i = beginLine; i < std::min(totalLines, endLine); i++)
  226. {
  227. if(!lines[i].empty()) //non-empty line
  228. blitLine(to, Rect(lineStart, lineSize), lines[i]);
  229. lineStart.y += (int)f->getLineHeight();
  230. }
  231. }
  232. void CMultiLineLabel::splitText(const std::string & Txt, bool redrawAfter)
  233. {
  234. lines.clear();
  235. const auto f = graphics->fonts[font];
  236. int lineHeight = static_cast<int>(f->getLineHeight());
  237. lines = CMessage::breakText(Txt, pos.w, font);
  238. textSize.y = lineHeight * (int)lines.size();
  239. textSize.x = 0;
  240. for(const std::string & line : lines)
  241. vstd::amax(textSize.x, f->getStringWidth(line.c_str()));
  242. if(redrawAfter)
  243. redraw();
  244. }
  245. Rect CMultiLineLabel::getTextLocation()
  246. {
  247. // this method is needed for vertical alignment alignment of text
  248. // when height of available text is smaller than height of widget
  249. // in this case - we should add proper offset to display text at required position
  250. if(pos.h <= textSize.y)
  251. return pos;
  252. Point textSize(pos.w, (int)graphics->fonts[font]->getLineHeight() * (int)lines.size());
  253. Point textOffset(pos.w - textSize.x, pos.h - textSize.y);
  254. switch(alignment)
  255. {
  256. case ETextAlignment::TOPLEFT: return Rect(pos.topLeft(), textSize);
  257. case ETextAlignment::TOPCENTER: return Rect(pos.topLeft(), textSize);
  258. case ETextAlignment::CENTER: return Rect(pos.topLeft() + textOffset / 2, textSize);
  259. case ETextAlignment::BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSize);
  260. }
  261. assert(0);
  262. return Rect();
  263. }
  264. CLabelGroup::CLabelGroup(EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  265. : font(Font), align(Align), color(Color)
  266. {
  267. defActions = 255 - DISPOSE;
  268. }
  269. void CLabelGroup::add(int x, int y, const std::string & text)
  270. {
  271. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  272. labels.push_back(std::make_shared<CLabel>(x, y, font, align, color, text));
  273. }
  274. size_t CLabelGroup::currentSize() const
  275. {
  276. return labels.size();
  277. }
  278. CTextBox::CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font, ETextAlignment Align, const ColorRGBA & Color) :
  279. sliderStyle(SliderStyle),
  280. slider(nullptr)
  281. {
  282. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  283. label = std::make_shared<CMultiLineLabel>(rect, Font, Align, Color);
  284. setRedrawParent(true);
  285. pos.x += rect.x;
  286. pos.y += rect.y;
  287. pos.h = rect.h;
  288. pos.w = rect.w;
  289. assert(pos.w >= 40); //we need some space
  290. setText(Text);
  291. }
  292. void CTextBox::sliderMoved(int to)
  293. {
  294. label->scrollTextTo(to);
  295. }
  296. void CTextBox::resize(Point newSize)
  297. {
  298. pos.w = newSize.x;
  299. pos.h = newSize.y;
  300. label->pos.w = pos.w;
  301. label->pos.h = pos.h;
  302. slider.reset();
  303. setText(label->getText()); // force refresh
  304. }
  305. void CTextBox::setText(const std::string & text)
  306. {
  307. label->pos.w = pos.w; // reset to default before textSize.y check
  308. label->setText(text);
  309. if(label->textSize.y <= label->pos.h && slider)
  310. {
  311. // slider is no longer needed
  312. slider.reset();
  313. }
  314. else if(slider)
  315. {
  316. // decrease width again if slider still used
  317. label->pos.w = pos.w - 16;
  318. assert(label->pos.w > 0);
  319. label->setText(text);
  320. slider->setAmount(label->textSize.y);
  321. }
  322. else if(label->textSize.y > label->pos.h)
  323. {
  324. // create slider and update widget
  325. label->pos.w = pos.w - 16;
  326. assert(label->pos.w > 0);
  327. label->setText(text);
  328. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  329. slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  330. label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
  331. slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight());
  332. slider->setPanningStep(1);
  333. slider->setScrollBounds(pos - slider->pos.topLeft());
  334. }
  335. }
  336. void CGStatusBar::setEnteringMode(bool on)
  337. {
  338. consoleText.clear();
  339. if (on)
  340. {
  341. //assert(enteringText == false);
  342. alignment = ETextAlignment::TOPLEFT;
  343. GH.startTextInput(pos);
  344. setText(consoleText);
  345. }
  346. else
  347. {
  348. //assert(enteringText == true);
  349. alignment = ETextAlignment::CENTER;
  350. GH.stopTextInput();
  351. setText(hoverText);
  352. }
  353. enteringText = on;
  354. }
  355. void CGStatusBar::setEnteredText(const std::string & text)
  356. {
  357. assert(enteringText == true);
  358. consoleText = text;
  359. setText(text);
  360. }
  361. void CGStatusBar::write(const std::string & Text)
  362. {
  363. hoverText = Text;
  364. if (enteringText == false)
  365. setText(hoverText);
  366. }
  367. void CGStatusBar::clearIfMatching(const std::string & Text)
  368. {
  369. if (hoverText == Text)
  370. clear();
  371. }
  372. void CGStatusBar::clear()
  373. {
  374. write({});
  375. }
  376. CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  377. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "")
  378. , enteringText(false)
  379. {
  380. addUsedEvents(LCLICK);
  381. background = background_;
  382. addChild(background.get());
  383. pos = background->pos;
  384. getBorderSize();
  385. autoRedraw = false;
  386. }
  387. CGStatusBar::CGStatusBar(int x, int y)
  388. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  389. , enteringText(false)
  390. {
  391. // without background
  392. addUsedEvents(LCLICK);
  393. }
  394. CGStatusBar::CGStatusBar(int x, int y, const ImagePath & name, int maxw)
  395. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  396. , enteringText(false)
  397. {
  398. addUsedEvents(LCLICK);
  399. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  400. auto backgroundImage = std::make_shared<CPicture>(name);
  401. background = backgroundImage;
  402. pos = background->pos;
  403. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  404. {
  405. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  406. vstd::amin(pos.w, maxw);
  407. backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
  408. }
  409. autoRedraw = false;
  410. }
  411. CGStatusBar::~CGStatusBar()
  412. {
  413. assert(GH.statusbar().get() != this);
  414. }
  415. void CGStatusBar::show(Canvas & to)
  416. {
  417. showAll(to);
  418. }
  419. void CGStatusBar::clickPressed(const Point & cursorPosition)
  420. {
  421. if(LOCPLINT && LOCPLINT->cingconsole->isActive())
  422. LOCPLINT->cingconsole->startEnteringText();
  423. }
  424. void CGStatusBar::activate()
  425. {
  426. GH.setStatusbar(shared_from_this());
  427. CIntObject::activate();
  428. }
  429. void CGStatusBar::deactivate()
  430. {
  431. assert(GH.statusbar().get() == this);
  432. GH.setStatusbar(nullptr);
  433. if (enteringText)
  434. LOCPLINT->cingconsole->endEnteringText(false);
  435. CIntObject::deactivate();
  436. }
  437. Point CGStatusBar::getBorderSize()
  438. {
  439. //Width of borders where text should not be printed
  440. static const Point borderSize(5, 1);
  441. switch(alignment)
  442. {
  443. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  444. case ETextAlignment::TOPCENTER: return Point(pos.w / 2, borderSize.y);
  445. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  446. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  447. }
  448. assert(0);
  449. return Point();
  450. }
  451. CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, bool giveFocusToInput)
  452. : CLabel(Pos.x, Pos.y, font, ETextAlignment::CENTER),
  453. cb(CB),
  454. CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  455. {
  456. setRedrawParent(true);
  457. pos.h = Pos.h;
  458. pos.w = Pos.w;
  459. background.reset();
  460. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  461. #if !defined(VCMI_MOBILE)
  462. if(giveFocusToInput)
  463. giveFocus();
  464. #endif
  465. }
  466. CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB)
  467. :cb(CB), CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  468. {
  469. pos += Pos.topLeft();
  470. pos.h = Pos.h;
  471. pos.w = Pos.w;
  472. OBJ_CONSTRUCTION;
  473. background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
  474. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  475. #if !defined(VCMI_MOBILE)
  476. giveFocus();
  477. #endif
  478. }
  479. CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
  480. :CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  481. {
  482. pos += Pos.topLeft();
  483. OBJ_CONSTRUCTION;
  484. background = std::make_shared<CPicture>(srf, Pos);
  485. pos.w = background->pos.w;
  486. pos.h = background->pos.h;
  487. background->pos = pos;
  488. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  489. #if !defined(VCMI_MOBILE)
  490. giveFocus();
  491. #endif
  492. }
  493. std::atomic<int> CKeyboardFocusListener::usageIndex(0);
  494. CKeyboardFocusListener::CKeyboardFocusListener(CTextInput * textInput)
  495. :textInput(textInput)
  496. {
  497. }
  498. void CKeyboardFocusListener::focusGot()
  499. {
  500. GH.startTextInput(textInput->pos);
  501. usageIndex++;
  502. }
  503. void CKeyboardFocusListener::focusLost()
  504. {
  505. if(0 == --usageIndex)
  506. {
  507. GH.stopTextInput();
  508. }
  509. }
  510. std::string CTextInput::visibleText()
  511. {
  512. return focus ? text + newText + "_" : text;
  513. }
  514. void CTextInput::clickPressed(const Point & cursorPosition)
  515. {
  516. if(!focus)
  517. giveFocus();
  518. }
  519. void CTextInput::keyPressed(EShortcut key)
  520. {
  521. if(!focus)
  522. return;
  523. if(key == EShortcut::GLOBAL_MOVE_FOCUS)
  524. {
  525. moveFocus();
  526. return;
  527. }
  528. bool redrawNeeded = false;
  529. switch(key)
  530. {
  531. case EShortcut::GLOBAL_BACKSPACE:
  532. if(!newText.empty())
  533. {
  534. TextOperations::trimRightUnicode(newText);
  535. redrawNeeded = true;
  536. }
  537. else if(!text.empty())
  538. {
  539. TextOperations::trimRightUnicode(text);
  540. redrawNeeded = true;
  541. }
  542. break;
  543. default:
  544. break;
  545. }
  546. if(redrawNeeded)
  547. {
  548. redraw();
  549. cb(text);
  550. }
  551. }
  552. void CTextInput::showPopupWindow(const Point & cursorPosition)
  553. {
  554. if(!helpBox.empty()) //there is no point to show window with nothing inside...
  555. CRClickPopup::createAndPush(helpBox);
  556. }
  557. void CTextInput::setText(const std::string & nText)
  558. {
  559. setText(nText, false);
  560. }
  561. void CTextInput::setText(const std::string & nText, bool callCb)
  562. {
  563. CLabel::setText(nText);
  564. if(callCb)
  565. cb(text);
  566. }
  567. void CTextInput::setHelpText(const std::string & text)
  568. {
  569. helpBox = text;
  570. }
  571. void CTextInput::textInputed(const std::string & enteredText)
  572. {
  573. if(!focus)
  574. return;
  575. std::string oldText = text;
  576. text += enteredText;
  577. filters(text, oldText);
  578. if(text != oldText)
  579. {
  580. redraw();
  581. cb(text);
  582. }
  583. newText.clear();
  584. }
  585. void CTextInput::textEdited(const std::string & enteredText)
  586. {
  587. if(!focus)
  588. return;
  589. newText = enteredText;
  590. redraw();
  591. cb(text + newText);
  592. }
  593. void CTextInput::filenameFilter(std::string & text, const std::string &)
  594. {
  595. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  596. size_t pos;
  597. while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  598. text.erase(pos, 1);
  599. }
  600. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  601. {
  602. assert(minValue < maxValue);
  603. if(text.empty())
  604. text = "0";
  605. size_t pos = 0;
  606. if(text[0] == '-') //allow '-' sign as first symbol only
  607. pos++;
  608. while(pos < text.size())
  609. {
  610. if(text[pos] < '0' || text[pos] > '9')
  611. {
  612. text = oldText;
  613. return; //new text is not number.
  614. }
  615. pos++;
  616. }
  617. try
  618. {
  619. int value = boost::lexical_cast<int>(text);
  620. if(value < minValue)
  621. text = std::to_string(minValue);
  622. else if(value > maxValue)
  623. text = std::to_string(maxValue);
  624. }
  625. catch(boost::bad_lexical_cast &)
  626. {
  627. //Should never happen. Unless I missed some cases
  628. logGlobal->warn("Warning: failed to convert %s to number!", text);
  629. text = oldText;
  630. }
  631. }
  632. CFocusable::CFocusable()
  633. :CFocusable(std::make_shared<IFocusListener>())
  634. {
  635. }
  636. CFocusable::CFocusable(std::shared_ptr<IFocusListener> focusListener)
  637. : focusListener(focusListener)
  638. {
  639. focus = false;
  640. focusables.push_back(this);
  641. }
  642. CFocusable::~CFocusable()
  643. {
  644. if(hasFocus())
  645. {
  646. inputWithFocus = nullptr;
  647. focusListener->focusLost();
  648. }
  649. focusables -= this;
  650. }
  651. bool CFocusable::hasFocus() const
  652. {
  653. return inputWithFocus == this;
  654. }
  655. void CFocusable::giveFocus()
  656. {
  657. focus = true;
  658. focusListener->focusGot();
  659. redraw();
  660. if(inputWithFocus)
  661. {
  662. inputWithFocus->focus = false;
  663. inputWithFocus->focusListener->focusLost();
  664. inputWithFocus->redraw();
  665. }
  666. inputWithFocus = this;
  667. }
  668. void CFocusable::moveFocus()
  669. {
  670. auto i = vstd::find(focusables, this),
  671. ourIt = i;
  672. for(i++; i != ourIt; i++)
  673. {
  674. if(i == focusables.end())
  675. i = focusables.begin();
  676. if (*i == this)
  677. return;
  678. if((*i)->isActive())
  679. {
  680. (*i)->giveFocus();
  681. break;
  682. }
  683. }
  684. }
  685. void CFocusable::removeFocus()
  686. {
  687. if(this == inputWithFocus)
  688. {
  689. focus = false;
  690. focusListener->focusLost();
  691. redraw();
  692. inputWithFocus = nullptr;
  693. }
  694. }