TextControls.cpp 18 KB

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