TextControls.cpp 18 KB

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