2
0

TextControls.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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 - 32;
  318. label->setText(text);
  319. slider->setAmount(label->textSize.y);
  320. }
  321. else if(label->textSize.y > label->pos.h)
  322. {
  323. // create slider and update widget
  324. label->pos.w = pos.w - 32;
  325. label->setText(text);
  326. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  327. slider = std::make_shared<CSlider>(Point(pos.w - 32, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  328. label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
  329. slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight());
  330. slider->setPanningStep(1);
  331. slider->setScrollBounds(pos - slider->pos.topLeft());
  332. }
  333. }
  334. void CGStatusBar::setEnteringMode(bool on)
  335. {
  336. consoleText.clear();
  337. if (on)
  338. {
  339. //assert(enteringText == false);
  340. alignment = ETextAlignment::TOPLEFT;
  341. GH.startTextInput(pos);
  342. setText(consoleText);
  343. }
  344. else
  345. {
  346. //assert(enteringText == true);
  347. alignment = ETextAlignment::CENTER;
  348. GH.stopTextInput();
  349. setText(hoverText);
  350. }
  351. enteringText = on;
  352. }
  353. void CGStatusBar::setEnteredText(const std::string & text)
  354. {
  355. assert(enteringText == true);
  356. consoleText = text;
  357. setText(text);
  358. }
  359. void CGStatusBar::write(const std::string & Text)
  360. {
  361. hoverText = Text;
  362. if (enteringText == false)
  363. setText(hoverText);
  364. }
  365. void CGStatusBar::clearIfMatching(const std::string & Text)
  366. {
  367. if (hoverText == Text)
  368. clear();
  369. }
  370. void CGStatusBar::clear()
  371. {
  372. write({});
  373. }
  374. CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  375. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "")
  376. , enteringText(false)
  377. {
  378. addUsedEvents(LCLICK);
  379. background = background_;
  380. addChild(background.get());
  381. pos = background->pos;
  382. getBorderSize();
  383. autoRedraw = false;
  384. }
  385. CGStatusBar::CGStatusBar(int x, int y)
  386. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  387. , enteringText(false)
  388. {
  389. // without background
  390. addUsedEvents(LCLICK);
  391. }
  392. CGStatusBar::CGStatusBar(int x, int y, const ImagePath & name, int maxw)
  393. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  394. , enteringText(false)
  395. {
  396. addUsedEvents(LCLICK);
  397. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  398. auto backgroundImage = std::make_shared<CPicture>(name);
  399. background = backgroundImage;
  400. pos = background->pos;
  401. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  402. {
  403. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  404. vstd::amin(pos.w, maxw);
  405. backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
  406. }
  407. autoRedraw = false;
  408. }
  409. CGStatusBar::~CGStatusBar()
  410. {
  411. assert(GH.statusbar().get() != this);
  412. }
  413. void CGStatusBar::show(Canvas & to)
  414. {
  415. showAll(to);
  416. }
  417. void CGStatusBar::clickPressed(const Point & cursorPosition)
  418. {
  419. if(LOCPLINT && LOCPLINT->cingconsole->isActive())
  420. LOCPLINT->cingconsole->startEnteringText();
  421. }
  422. void CGStatusBar::activate()
  423. {
  424. GH.setStatusbar(shared_from_this());
  425. CIntObject::activate();
  426. }
  427. void CGStatusBar::deactivate()
  428. {
  429. assert(GH.statusbar().get() == this);
  430. GH.setStatusbar(nullptr);
  431. if (enteringText)
  432. LOCPLINT->cingconsole->endEnteringText(false);
  433. CIntObject::deactivate();
  434. }
  435. Point CGStatusBar::getBorderSize()
  436. {
  437. //Width of borders where text should not be printed
  438. static const Point borderSize(5, 1);
  439. switch(alignment)
  440. {
  441. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  442. case ETextAlignment::TOPCENTER: return Point(pos.w / 2, borderSize.y);
  443. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  444. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  445. }
  446. assert(0);
  447. return Point();
  448. }
  449. CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, bool giveFocusToInput)
  450. : CLabel(Pos.x, Pos.y, font, ETextAlignment::CENTER),
  451. cb(CB),
  452. CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  453. {
  454. setRedrawParent(true);
  455. pos.h = Pos.h;
  456. pos.w = Pos.w;
  457. background.reset();
  458. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  459. #if !defined(VCMI_MOBILE)
  460. if(giveFocusToInput)
  461. giveFocus();
  462. #endif
  463. }
  464. CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB)
  465. :cb(CB), CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  466. {
  467. pos += Pos.topLeft();
  468. pos.h = Pos.h;
  469. pos.w = Pos.w;
  470. OBJ_CONSTRUCTION;
  471. background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
  472. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  473. #if !defined(VCMI_MOBILE)
  474. giveFocus();
  475. #endif
  476. }
  477. CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
  478. :CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  479. {
  480. pos += Pos.topLeft();
  481. OBJ_CONSTRUCTION;
  482. background = std::make_shared<CPicture>(srf, Pos);
  483. pos.w = background->pos.w;
  484. pos.h = background->pos.h;
  485. background->pos = pos;
  486. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  487. #if !defined(VCMI_MOBILE)
  488. giveFocus();
  489. #endif
  490. }
  491. std::atomic<int> CKeyboardFocusListener::usageIndex(0);
  492. CKeyboardFocusListener::CKeyboardFocusListener(CTextInput * textInput)
  493. :textInput(textInput)
  494. {
  495. }
  496. void CKeyboardFocusListener::focusGot()
  497. {
  498. GH.startTextInput(textInput->pos);
  499. usageIndex++;
  500. }
  501. void CKeyboardFocusListener::focusLost()
  502. {
  503. if(0 == --usageIndex)
  504. {
  505. GH.stopTextInput();
  506. }
  507. }
  508. std::string CTextInput::visibleText()
  509. {
  510. return focus ? text + newText + "_" : text;
  511. }
  512. void CTextInput::clickPressed(const Point & cursorPosition)
  513. {
  514. if(!focus)
  515. giveFocus();
  516. }
  517. void CTextInput::keyPressed(EShortcut key)
  518. {
  519. if(!focus)
  520. return;
  521. if(key == EShortcut::GLOBAL_MOVE_FOCUS)
  522. {
  523. moveFocus();
  524. return;
  525. }
  526. bool redrawNeeded = false;
  527. switch(key)
  528. {
  529. case EShortcut::GLOBAL_BACKSPACE:
  530. if(!newText.empty())
  531. {
  532. TextOperations::trimRightUnicode(newText);
  533. redrawNeeded = true;
  534. }
  535. else if(!text.empty())
  536. {
  537. TextOperations::trimRightUnicode(text);
  538. redrawNeeded = true;
  539. }
  540. break;
  541. default:
  542. break;
  543. }
  544. if(redrawNeeded)
  545. {
  546. redraw();
  547. cb(text);
  548. }
  549. }
  550. void CTextInput::showPopupWindow(const Point & cursorPosition)
  551. {
  552. if(!helpBox.empty()) //there is no point to show window with nothing inside...
  553. CRClickPopup::createAndPush(helpBox);
  554. }
  555. void CTextInput::setText(const std::string & nText)
  556. {
  557. setText(nText, false);
  558. }
  559. void CTextInput::setText(const std::string & nText, bool callCb)
  560. {
  561. CLabel::setText(nText);
  562. if(callCb)
  563. cb(text);
  564. }
  565. void CTextInput::setHelpText(const std::string & text)
  566. {
  567. helpBox = text;
  568. }
  569. void CTextInput::textInputed(const std::string & enteredText)
  570. {
  571. if(!focus)
  572. return;
  573. std::string oldText = text;
  574. text += enteredText;
  575. filters(text, oldText);
  576. if(text != oldText)
  577. {
  578. redraw();
  579. cb(text);
  580. }
  581. newText.clear();
  582. }
  583. void CTextInput::textEdited(const std::string & enteredText)
  584. {
  585. if(!focus)
  586. return;
  587. newText = enteredText;
  588. redraw();
  589. cb(text + newText);
  590. }
  591. void CTextInput::filenameFilter(std::string & text, const std::string &)
  592. {
  593. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  594. size_t pos;
  595. while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  596. text.erase(pos, 1);
  597. }
  598. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  599. {
  600. assert(minValue < maxValue);
  601. if(text.empty())
  602. text = "0";
  603. size_t pos = 0;
  604. if(text[0] == '-') //allow '-' sign as first symbol only
  605. pos++;
  606. while(pos < text.size())
  607. {
  608. if(text[pos] < '0' || text[pos] > '9')
  609. {
  610. text = oldText;
  611. return; //new text is not number.
  612. }
  613. pos++;
  614. }
  615. try
  616. {
  617. int value = boost::lexical_cast<int>(text);
  618. if(value < minValue)
  619. text = std::to_string(minValue);
  620. else if(value > maxValue)
  621. text = std::to_string(maxValue);
  622. }
  623. catch(boost::bad_lexical_cast &)
  624. {
  625. //Should never happen. Unless I missed some cases
  626. logGlobal->warn("Warning: failed to convert %s to number!", text);
  627. text = oldText;
  628. }
  629. }
  630. CFocusable::CFocusable()
  631. :CFocusable(std::make_shared<IFocusListener>())
  632. {
  633. }
  634. CFocusable::CFocusable(std::shared_ptr<IFocusListener> focusListener)
  635. : focusListener(focusListener)
  636. {
  637. focus = false;
  638. focusables.push_back(this);
  639. }
  640. CFocusable::~CFocusable()
  641. {
  642. if(hasFocus())
  643. {
  644. inputWithFocus = nullptr;
  645. focusListener->focusLost();
  646. }
  647. focusables -= this;
  648. }
  649. bool CFocusable::hasFocus() const
  650. {
  651. return inputWithFocus == this;
  652. }
  653. void CFocusable::giveFocus()
  654. {
  655. focus = true;
  656. focusListener->focusGot();
  657. redraw();
  658. if(inputWithFocus)
  659. {
  660. inputWithFocus->focus = false;
  661. inputWithFocus->focusListener->focusLost();
  662. inputWithFocus->redraw();
  663. }
  664. inputWithFocus = this;
  665. }
  666. void CFocusable::moveFocus()
  667. {
  668. auto i = vstd::find(focusables, this),
  669. ourIt = i;
  670. for(i++; i != ourIt; i++)
  671. {
  672. if(i == focusables.end())
  673. i = focusables.begin();
  674. if((*i)->isActive())
  675. {
  676. (*i)->giveFocus();
  677. break;
  678. }
  679. }
  680. }
  681. void CFocusable::removeFocus()
  682. {
  683. if(this == inputWithFocus)
  684. {
  685. focus = false;
  686. focusListener->focusLost();
  687. redraw();
  688. inputWithFocus = nullptr;
  689. }
  690. }