TextControls.cpp 19 KB

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