TextControls.cpp 19 KB

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