TextControls.cpp 18 KB

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