TextControls.cpp 18 KB

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