TextControls.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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(CMessage::parseColor(colorText).a != Colors::TRANSPARENCY.ALPHA_TRANSPARENT)
  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. ColorRGBA color = CMessage::parseColor(colorText);
  180. if(color.a != Colors::TRANSPARENCY.ALPHA_TRANSPARENT)
  181. {
  182. toPrint = toPrint.substr(colorText.length() + 1, toPrint.length() - colorText.length());
  183. to.drawText(where, font, color, ETextAlignment::TOPLEFT, toPrint);
  184. }
  185. else
  186. to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
  187. }
  188. else
  189. to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
  190. }
  191. else // Non-enclosed text, use default color
  192. to.drawText(where, font, color, ETextAlignment::TOPLEFT, toPrint);
  193. begin = end;
  194. where.x += (int)f->getStringWidth(toPrint);
  195. }
  196. currDelimeter++;
  197. } while(begin++ != std::string::npos);
  198. }
  199. CTextContainer::CTextContainer(ETextAlignment alignment, EFonts font, ColorRGBA color) :
  200. alignment(alignment),
  201. font(font),
  202. color(color)
  203. {
  204. }
  205. void CMultiLineLabel::showAll(Canvas & to)
  206. {
  207. CIntObject::showAll(to);
  208. const auto f = graphics->fonts[font];
  209. // calculate which lines should be visible
  210. int totalLines = static_cast<int>(lines.size());
  211. int beginLine = visibleSize.y;
  212. int endLine = getTextLocation().h + visibleSize.y;
  213. if(beginLine < 0)
  214. beginLine = 0;
  215. else
  216. beginLine /= (int)f->getLineHeight();
  217. if(endLine < 0)
  218. endLine = 0;
  219. else
  220. endLine /= (int)f->getLineHeight();
  221. endLine++;
  222. // and where they should be displayed
  223. Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * (int)f->getLineHeight());
  224. Point lineSize = Point(getTextLocation().w, (int)f->getLineHeight());
  225. CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), getTextLocation()); // to properly trim text that is too big to fit
  226. for(int i = beginLine; i < std::min(totalLines, endLine); i++)
  227. {
  228. if(!lines[i].empty()) //non-empty line
  229. blitLine(to, Rect(lineStart, lineSize), lines[i]);
  230. lineStart.y += (int)f->getLineHeight();
  231. }
  232. }
  233. void CMultiLineLabel::splitText(const std::string & Txt, bool redrawAfter)
  234. {
  235. lines.clear();
  236. const auto f = graphics->fonts[font];
  237. int lineHeight = static_cast<int>(f->getLineHeight());
  238. lines = CMessage::breakText(Txt, pos.w, font);
  239. textSize.y = lineHeight * (int)lines.size();
  240. textSize.x = 0;
  241. for(const std::string & line : lines)
  242. vstd::amax(textSize.x, f->getStringWidth(line.c_str()));
  243. if(redrawAfter)
  244. redraw();
  245. }
  246. Rect CMultiLineLabel::getTextLocation()
  247. {
  248. // this method is needed for vertical alignment alignment of text
  249. // when height of available text is smaller than height of widget
  250. // in this case - we should add proper offset to display text at required position
  251. if(pos.h <= textSize.y)
  252. return pos;
  253. Point textSize(pos.w, (int)graphics->fonts[font]->getLineHeight() * (int)lines.size());
  254. Point textOffset(pos.w - textSize.x, pos.h - textSize.y);
  255. switch(alignment)
  256. {
  257. case ETextAlignment::TOPLEFT: return Rect(pos.topLeft(), textSize);
  258. case ETextAlignment::TOPCENTER: return Rect(pos.topLeft(), textSize);
  259. case ETextAlignment::CENTER: return Rect(pos.topLeft() + textOffset / 2, textSize);
  260. case ETextAlignment::BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSize);
  261. }
  262. assert(0);
  263. return Rect();
  264. }
  265. CLabelGroup::CLabelGroup(EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  266. : font(Font), align(Align), color(Color)
  267. {
  268. defActions = 255 - DISPOSE;
  269. }
  270. void CLabelGroup::add(int x, int y, const std::string & text)
  271. {
  272. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  273. labels.push_back(std::make_shared<CLabel>(x, y, font, align, color, text));
  274. }
  275. size_t CLabelGroup::currentSize() const
  276. {
  277. return labels.size();
  278. }
  279. CTextBox::CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font, ETextAlignment Align, const ColorRGBA & Color) :
  280. sliderStyle(SliderStyle),
  281. slider(nullptr)
  282. {
  283. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  284. label = std::make_shared<CMultiLineLabel>(rect, Font, Align, Color);
  285. setRedrawParent(true);
  286. pos.x += rect.x;
  287. pos.y += rect.y;
  288. pos.h = rect.h;
  289. pos.w = rect.w;
  290. assert(pos.w >= 40); //we need some space
  291. setText(Text);
  292. }
  293. void CTextBox::sliderMoved(int to)
  294. {
  295. label->scrollTextTo(to);
  296. }
  297. void CTextBox::resize(Point newSize)
  298. {
  299. pos.w = newSize.x;
  300. pos.h = newSize.y;
  301. label->pos.w = pos.w;
  302. label->pos.h = pos.h;
  303. slider.reset();
  304. setText(label->getText()); // force refresh
  305. }
  306. void CTextBox::setText(const std::string & text)
  307. {
  308. label->pos.w = pos.w; // reset to default before textSize.y check
  309. label->setText(text);
  310. if(label->textSize.y <= label->pos.h && slider)
  311. {
  312. // slider is no longer needed
  313. slider.reset();
  314. }
  315. else if(slider)
  316. {
  317. // decrease width again if slider still used
  318. label->pos.w = pos.w - 32;
  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 - 32;
  326. label->setText(text);
  327. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  328. slider = std::make_shared<CSlider>(Point(pos.w - 32, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  329. label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
  330. slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight());
  331. slider->setPanningStep(1);
  332. slider->setScrollBounds(pos - slider->pos.topLeft());
  333. }
  334. }
  335. void CGStatusBar::setEnteringMode(bool on)
  336. {
  337. consoleText.clear();
  338. if (on)
  339. {
  340. //assert(enteringText == false);
  341. alignment = ETextAlignment::TOPLEFT;
  342. GH.startTextInput(pos);
  343. setText(consoleText);
  344. }
  345. else
  346. {
  347. //assert(enteringText == true);
  348. alignment = ETextAlignment::CENTER;
  349. GH.stopTextInput();
  350. setText(hoverText);
  351. }
  352. enteringText = on;
  353. }
  354. void CGStatusBar::setEnteredText(const std::string & text)
  355. {
  356. assert(enteringText == true);
  357. consoleText = text;
  358. setText(text);
  359. }
  360. void CGStatusBar::write(const std::string & Text)
  361. {
  362. hoverText = Text;
  363. if (enteringText == false)
  364. setText(hoverText);
  365. }
  366. void CGStatusBar::clearIfMatching(const std::string & Text)
  367. {
  368. if (hoverText == Text)
  369. clear();
  370. }
  371. void CGStatusBar::clear()
  372. {
  373. write({});
  374. }
  375. CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  376. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "")
  377. , enteringText(false)
  378. {
  379. addUsedEvents(LCLICK);
  380. background = background_;
  381. addChild(background.get());
  382. pos = background->pos;
  383. getBorderSize();
  384. autoRedraw = false;
  385. }
  386. CGStatusBar::CGStatusBar(int x, int y, const ImagePath & name, int maxw)
  387. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  388. , enteringText(false)
  389. {
  390. addUsedEvents(LCLICK);
  391. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  392. auto backgroundImage = std::make_shared<CPicture>(name);
  393. background = backgroundImage;
  394. pos = background->pos;
  395. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  396. {
  397. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  398. vstd::amin(pos.w, maxw);
  399. backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
  400. }
  401. autoRedraw = false;
  402. }
  403. CGStatusBar::~CGStatusBar()
  404. {
  405. assert(GH.statusbar().get() != this);
  406. }
  407. void CGStatusBar::show(Canvas & to)
  408. {
  409. showAll(to);
  410. }
  411. void CGStatusBar::clickPressed(const Point & cursorPosition)
  412. {
  413. if(LOCPLINT && LOCPLINT->cingconsole->isActive())
  414. LOCPLINT->cingconsole->startEnteringText();
  415. }
  416. void CGStatusBar::activate()
  417. {
  418. GH.setStatusbar(shared_from_this());
  419. CIntObject::activate();
  420. }
  421. void CGStatusBar::deactivate()
  422. {
  423. assert(GH.statusbar().get() == this);
  424. GH.setStatusbar(nullptr);
  425. if (enteringText)
  426. LOCPLINT->cingconsole->endEnteringText(false);
  427. CIntObject::deactivate();
  428. }
  429. Point CGStatusBar::getBorderSize()
  430. {
  431. //Width of borders where text should not be printed
  432. static const Point borderSize(5, 1);
  433. switch(alignment)
  434. {
  435. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  436. case ETextAlignment::TOPCENTER: return Point(pos.w / 2, borderSize.y);
  437. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  438. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  439. }
  440. assert(0);
  441. return Point();
  442. }
  443. CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB)
  444. : CLabel(Pos.x, Pos.y, font, ETextAlignment::CENTER),
  445. cb(CB),
  446. CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  447. {
  448. setRedrawParent(true);
  449. pos.h = Pos.h;
  450. pos.w = Pos.w;
  451. background.reset();
  452. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  453. #if !defined(VCMI_MOBILE)
  454. giveFocus();
  455. #endif
  456. }
  457. CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB)
  458. :cb(CB), CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  459. {
  460. pos += Pos.topLeft();
  461. pos.h = Pos.h;
  462. pos.w = Pos.w;
  463. OBJ_CONSTRUCTION;
  464. background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
  465. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  466. #if !defined(VCMI_MOBILE)
  467. giveFocus();
  468. #endif
  469. }
  470. CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
  471. :CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  472. {
  473. pos += Pos.topLeft();
  474. OBJ_CONSTRUCTION;
  475. background = std::make_shared<CPicture>(srf, Pos);
  476. pos.w = background->pos.w;
  477. pos.h = background->pos.h;
  478. background->pos = pos;
  479. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  480. #if !defined(VCMI_MOBILE)
  481. giveFocus();
  482. #endif
  483. }
  484. std::atomic<int> CKeyboardFocusListener::usageIndex(0);
  485. CKeyboardFocusListener::CKeyboardFocusListener(CTextInput * textInput)
  486. :textInput(textInput)
  487. {
  488. }
  489. void CKeyboardFocusListener::focusGot()
  490. {
  491. GH.startTextInput(textInput->pos);
  492. usageIndex++;
  493. }
  494. void CKeyboardFocusListener::focusLost()
  495. {
  496. if(0 == --usageIndex)
  497. {
  498. GH.stopTextInput();
  499. }
  500. }
  501. std::string CTextInput::visibleText()
  502. {
  503. return focus ? text + newText + "_" : text;
  504. }
  505. void CTextInput::clickPressed(const Point & cursorPosition)
  506. {
  507. if(!focus)
  508. giveFocus();
  509. }
  510. void CTextInput::keyPressed(EShortcut key)
  511. {
  512. if(!focus)
  513. return;
  514. if(key == EShortcut::GLOBAL_MOVE_FOCUS)
  515. {
  516. moveFocus();
  517. return;
  518. }
  519. bool redrawNeeded = false;
  520. switch(key)
  521. {
  522. case EShortcut::GLOBAL_BACKSPACE:
  523. if(!newText.empty())
  524. {
  525. TextOperations::trimRightUnicode(newText);
  526. redrawNeeded = true;
  527. }
  528. else if(!text.empty())
  529. {
  530. TextOperations::trimRightUnicode(text);
  531. redrawNeeded = true;
  532. }
  533. break;
  534. default:
  535. break;
  536. }
  537. if(redrawNeeded)
  538. {
  539. redraw();
  540. cb(text);
  541. }
  542. }
  543. void CTextInput::showPopupWindow(const Point & cursorPosition)
  544. {
  545. if(!helpBox.empty()) //there is no point to show window with nothing inside...
  546. CRClickPopup::createAndPush(helpBox);
  547. }
  548. void CTextInput::setText(const std::string & nText)
  549. {
  550. setText(nText, false);
  551. }
  552. void CTextInput::setText(const std::string & nText, bool callCb)
  553. {
  554. CLabel::setText(nText);
  555. if(callCb)
  556. cb(text);
  557. }
  558. void CTextInput::setHelpText(const std::string & text)
  559. {
  560. helpBox = text;
  561. }
  562. void CTextInput::textInputed(const std::string & enteredText)
  563. {
  564. if(!focus)
  565. return;
  566. std::string oldText = text;
  567. text += enteredText;
  568. filters(text, oldText);
  569. if(text != oldText)
  570. {
  571. redraw();
  572. cb(text);
  573. }
  574. newText.clear();
  575. }
  576. void CTextInput::textEdited(const std::string & enteredText)
  577. {
  578. if(!focus)
  579. return;
  580. newText = enteredText;
  581. redraw();
  582. cb(text + newText);
  583. }
  584. void CTextInput::filenameFilter(std::string & text, const std::string &)
  585. {
  586. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  587. size_t pos;
  588. while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  589. text.erase(pos, 1);
  590. }
  591. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  592. {
  593. assert(minValue < maxValue);
  594. if(text.empty())
  595. text = "0";
  596. size_t pos = 0;
  597. if(text[0] == '-') //allow '-' sign as first symbol only
  598. pos++;
  599. while(pos < text.size())
  600. {
  601. if(text[pos] < '0' || text[pos] > '9')
  602. {
  603. text = oldText;
  604. return; //new text is not number.
  605. }
  606. pos++;
  607. }
  608. try
  609. {
  610. int value = boost::lexical_cast<int>(text);
  611. if(value < minValue)
  612. text = std::to_string(minValue);
  613. else if(value > maxValue)
  614. text = std::to_string(maxValue);
  615. }
  616. catch(boost::bad_lexical_cast &)
  617. {
  618. //Should never happen. Unless I missed some cases
  619. logGlobal->warn("Warning: failed to convert %s to number!", text);
  620. text = oldText;
  621. }
  622. }
  623. CFocusable::CFocusable()
  624. :CFocusable(std::make_shared<IFocusListener>())
  625. {
  626. }
  627. CFocusable::CFocusable(std::shared_ptr<IFocusListener> focusListener)
  628. : focusListener(focusListener)
  629. {
  630. focus = false;
  631. focusables.push_back(this);
  632. }
  633. CFocusable::~CFocusable()
  634. {
  635. if(hasFocus())
  636. {
  637. inputWithFocus = nullptr;
  638. focusListener->focusLost();
  639. }
  640. focusables -= this;
  641. }
  642. bool CFocusable::hasFocus() const
  643. {
  644. return inputWithFocus == this;
  645. }
  646. void CFocusable::giveFocus()
  647. {
  648. focus = true;
  649. focusListener->focusGot();
  650. redraw();
  651. if(inputWithFocus)
  652. {
  653. inputWithFocus->focus = false;
  654. inputWithFocus->focusListener->focusLost();
  655. inputWithFocus->redraw();
  656. }
  657. inputWithFocus = this;
  658. }
  659. void CFocusable::moveFocus()
  660. {
  661. auto i = vstd::find(focusables, this),
  662. ourIt = i;
  663. for(i++; i != ourIt; i++)
  664. {
  665. if(i == focusables.end())
  666. i = focusables.begin();
  667. if((*i)->isActive())
  668. {
  669. (*i)->giveFocus();
  670. break;
  671. }
  672. }
  673. }