TextControls.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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::trimToFit()
  309. {
  310. if (slider)
  311. return;
  312. pos.w = label->textSize.x;
  313. pos.h = label->textSize.y;
  314. label->pos.w = label->textSize.x;
  315. label->pos.h = label->textSize.y;
  316. }
  317. void CTextBox::resize(Point newSize)
  318. {
  319. pos.w = newSize.x;
  320. pos.h = newSize.y;
  321. label->pos.w = pos.w;
  322. label->pos.h = pos.h;
  323. slider.reset();
  324. setText(label->getText()); // force refresh
  325. }
  326. void CTextBox::setText(const std::string & text)
  327. {
  328. label->pos.w = pos.w; // reset to default before textSize.y check
  329. label->setText(text);
  330. if(label->textSize.y <= label->pos.h && slider)
  331. {
  332. // slider is no longer needed
  333. slider.reset();
  334. }
  335. else if(slider)
  336. {
  337. // decrease width again if slider still used
  338. label->pos.w = pos.w - 16;
  339. assert(label->pos.w > 0);
  340. label->setText(text);
  341. slider->setAmount(label->textSize.y);
  342. }
  343. else if(label->textSize.y > label->pos.h)
  344. {
  345. // create slider and update widget
  346. label->pos.w = pos.w - 16;
  347. assert(label->pos.w > 0);
  348. label->setText(text);
  349. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  350. slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  351. label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
  352. slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight());
  353. slider->setPanningStep(1);
  354. slider->setScrollBounds(pos - slider->pos.topLeft());
  355. }
  356. }
  357. void CGStatusBar::setEnteringMode(bool on)
  358. {
  359. consoleText.clear();
  360. if (on)
  361. {
  362. //assert(enteringText == false);
  363. alignment = ETextAlignment::TOPLEFT;
  364. GH.startTextInput(pos);
  365. setText(consoleText);
  366. }
  367. else
  368. {
  369. //assert(enteringText == true);
  370. alignment = ETextAlignment::CENTER;
  371. GH.stopTextInput();
  372. setText(hoverText);
  373. }
  374. enteringText = on;
  375. }
  376. void CGStatusBar::setEnteredText(const std::string & text)
  377. {
  378. assert(enteringText == true);
  379. consoleText = text;
  380. setText(text);
  381. }
  382. void CGStatusBar::write(const std::string & Text)
  383. {
  384. hoverText = Text;
  385. if (enteringText == false)
  386. setText(hoverText);
  387. }
  388. void CGStatusBar::clearIfMatching(const std::string & Text)
  389. {
  390. if (hoverText == Text)
  391. clear();
  392. }
  393. void CGStatusBar::clear()
  394. {
  395. write({});
  396. }
  397. CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  398. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "", background_->pos.w)
  399. , enteringText(false)
  400. {
  401. addUsedEvents(LCLICK);
  402. background = background_;
  403. addChild(background.get());
  404. pos = background->pos;
  405. getBorderSize();
  406. autoRedraw = false;
  407. }
  408. CGStatusBar::CGStatusBar(int x, int y)
  409. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  410. , enteringText(false)
  411. {
  412. // without background
  413. addUsedEvents(LCLICK);
  414. }
  415. CGStatusBar::CGStatusBar(int x, int y, const ImagePath & name, int maxw)
  416. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  417. , enteringText(false)
  418. {
  419. addUsedEvents(LCLICK);
  420. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  421. auto backgroundImage = std::make_shared<CPicture>(name);
  422. background = backgroundImage;
  423. pos = background->pos;
  424. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  425. {
  426. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  427. vstd::amin(pos.w, maxw);
  428. backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
  429. }
  430. autoRedraw = false;
  431. }
  432. CGStatusBar::~CGStatusBar()
  433. {
  434. assert(GH.statusbar().get() != this);
  435. }
  436. void CGStatusBar::show(Canvas & to)
  437. {
  438. showAll(to);
  439. }
  440. void CGStatusBar::clickPressed(const Point & cursorPosition)
  441. {
  442. if(LOCPLINT && LOCPLINT->cingconsole->isActive())
  443. LOCPLINT->cingconsole->startEnteringText();
  444. }
  445. void CGStatusBar::activate()
  446. {
  447. GH.setStatusbar(shared_from_this());
  448. CIntObject::activate();
  449. }
  450. void CGStatusBar::deactivate()
  451. {
  452. assert(GH.statusbar().get() == this);
  453. GH.setStatusbar(nullptr);
  454. if (enteringText)
  455. LOCPLINT->cingconsole->endEnteringText(false);
  456. CIntObject::deactivate();
  457. }
  458. Point CGStatusBar::getBorderSize()
  459. {
  460. //Width of borders where text should not be printed
  461. static const Point borderSize(5, 1);
  462. switch(alignment)
  463. {
  464. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  465. case ETextAlignment::TOPCENTER: return Point(pos.w / 2, borderSize.y);
  466. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  467. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  468. }
  469. assert(0);
  470. return Point();
  471. }
  472. CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, ETextAlignment alignment, bool giveFocusToInput)
  473. : CLabel(Pos.x, Pos.y, font, alignment),
  474. cb(CB)
  475. {
  476. setRedrawParent(true);
  477. pos.h = Pos.h;
  478. pos.w = Pos.w;
  479. maxWidth = Pos.w;
  480. background.reset();
  481. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  482. #if !defined(VCMI_MOBILE)
  483. if(giveFocusToInput)
  484. giveFocus();
  485. #endif
  486. }
  487. CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB)
  488. :cb(CB)
  489. {
  490. pos += Pos.topLeft();
  491. pos.h = Pos.h;
  492. pos.w = Pos.w;
  493. maxWidth = Pos.w;
  494. OBJ_CONSTRUCTION;
  495. background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
  496. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  497. #if !defined(VCMI_MOBILE)
  498. giveFocus();
  499. #endif
  500. }
  501. CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
  502. {
  503. pos += Pos.topLeft();
  504. OBJ_CONSTRUCTION;
  505. background = std::make_shared<CPicture>(srf, Pos);
  506. pos.w = background->pos.w;
  507. pos.h = background->pos.h;
  508. maxWidth = Pos.w;
  509. background->pos = pos;
  510. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  511. #if !defined(VCMI_MOBILE)
  512. giveFocus();
  513. #endif
  514. }
  515. std::atomic<int> CFocusable::usageIndex(0);
  516. void CFocusable::focusGot()
  517. {
  518. GH.startTextInput(pos);
  519. usageIndex++;
  520. }
  521. void CFocusable::focusLost()
  522. {
  523. if(0 == --usageIndex)
  524. {
  525. GH.stopTextInput();
  526. }
  527. }
  528. std::string CTextInput::visibleText()
  529. {
  530. return focus ? text + newText + "_" : text;
  531. }
  532. void CTextInput::clickPressed(const Point & cursorPosition)
  533. {
  534. if(!focus)
  535. giveFocus();
  536. }
  537. void CTextInput::keyPressed(EShortcut key)
  538. {
  539. if(!focus)
  540. return;
  541. if(key == EShortcut::GLOBAL_MOVE_FOCUS)
  542. {
  543. moveFocus();
  544. return;
  545. }
  546. bool redrawNeeded = false;
  547. switch(key)
  548. {
  549. case EShortcut::GLOBAL_BACKSPACE:
  550. if(!newText.empty())
  551. {
  552. TextOperations::trimRightUnicode(newText);
  553. redrawNeeded = true;
  554. }
  555. else if(!text.empty())
  556. {
  557. TextOperations::trimRightUnicode(text);
  558. redrawNeeded = true;
  559. }
  560. break;
  561. default:
  562. break;
  563. }
  564. if(redrawNeeded)
  565. {
  566. redraw();
  567. cb(text);
  568. }
  569. }
  570. void CTextInput::showPopupWindow(const Point & cursorPosition)
  571. {
  572. if(!helpBox.empty()) //there is no point to show window with nothing inside...
  573. CRClickPopup::createAndPush(helpBox);
  574. }
  575. void CTextInput::setText(const std::string & nText)
  576. {
  577. setText(nText, false);
  578. }
  579. void CTextInput::setText(const std::string & nText, bool callCb)
  580. {
  581. CLabel::setText(nText);
  582. if(callCb)
  583. cb(text);
  584. }
  585. void CTextInput::setHelpText(const std::string & text)
  586. {
  587. helpBox = text;
  588. }
  589. void CTextInput::textInputed(const std::string & enteredText)
  590. {
  591. if(!focus)
  592. return;
  593. std::string oldText = text;
  594. setText(getText() + enteredText);
  595. filters(text, oldText);
  596. if(text != oldText)
  597. {
  598. redraw();
  599. cb(text);
  600. }
  601. newText.clear();
  602. }
  603. void CTextInput::textEdited(const std::string & enteredText)
  604. {
  605. if(!focus)
  606. return;
  607. newText = enteredText;
  608. redraw();
  609. cb(text + newText);
  610. }
  611. void CTextInput::filenameFilter(std::string & text, const std::string &)
  612. {
  613. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  614. size_t pos;
  615. while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  616. text.erase(pos, 1);
  617. }
  618. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  619. {
  620. assert(minValue < maxValue);
  621. if(text.empty())
  622. text = "0";
  623. size_t pos = 0;
  624. if(text[0] == '-') //allow '-' sign as first symbol only
  625. pos++;
  626. while(pos < text.size())
  627. {
  628. if(text[pos] < '0' || text[pos] > '9')
  629. {
  630. text = oldText;
  631. return; //new text is not number.
  632. }
  633. pos++;
  634. }
  635. try
  636. {
  637. int value = boost::lexical_cast<int>(text);
  638. if(value < minValue)
  639. text = std::to_string(minValue);
  640. else if(value > maxValue)
  641. text = std::to_string(maxValue);
  642. }
  643. catch(boost::bad_lexical_cast &)
  644. {
  645. //Should never happen. Unless I missed some cases
  646. logGlobal->warn("Warning: failed to convert %s to number!", text);
  647. text = oldText;
  648. }
  649. }
  650. CFocusable::CFocusable()
  651. {
  652. focus = false;
  653. focusables.push_back(this);
  654. }
  655. CFocusable::~CFocusable()
  656. {
  657. if(hasFocus())
  658. {
  659. inputWithFocus = nullptr;
  660. focusLost();
  661. }
  662. focusables -= this;
  663. }
  664. bool CFocusable::hasFocus() const
  665. {
  666. return inputWithFocus == this;
  667. }
  668. void CFocusable::giveFocus()
  669. {
  670. focus = true;
  671. focusGot();
  672. redraw();
  673. if(inputWithFocus)
  674. {
  675. inputWithFocus->focus = false;
  676. inputWithFocus->focusLost();
  677. inputWithFocus->redraw();
  678. }
  679. inputWithFocus = this;
  680. }
  681. void CFocusable::moveFocus()
  682. {
  683. auto i = vstd::find(focusables, this),
  684. ourIt = i;
  685. for(i++; i != ourIt; i++)
  686. {
  687. if(i == focusables.end())
  688. i = focusables.begin();
  689. if (*i == this)
  690. return;
  691. if((*i)->isActive())
  692. {
  693. (*i)->giveFocus();
  694. break;
  695. }
  696. }
  697. }
  698. void CFocusable::removeFocus()
  699. {
  700. if(this == inputWithFocus)
  701. {
  702. focus = false;
  703. focusLost();
  704. redraw();
  705. inputWithFocus = nullptr;
  706. }
  707. }