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. assert(TextOperations::isValidUnicodeString(Txt));
  70. text = Txt;
  71. trimText();
  72. if(autoRedraw)
  73. {
  74. if(background || !parent)
  75. redraw();
  76. else
  77. parent->redraw();
  78. }
  79. }
  80. void CLabel::setMaxWidth(int width)
  81. {
  82. maxWidth = width;
  83. }
  84. void CLabel::trimText()
  85. {
  86. if(maxWidth > 0)
  87. while ((int)graphics->fonts[font]->getStringWidth(visibleText().c_str()) > maxWidth)
  88. TextOperations::trimRightUnicode(text);
  89. }
  90. void CLabel::setColor(const ColorRGBA & Color)
  91. {
  92. color = Color;
  93. if(autoRedraw)
  94. {
  95. if(background || !parent)
  96. redraw();
  97. else
  98. parent->redraw();
  99. }
  100. }
  101. size_t CLabel::getWidth()
  102. {
  103. return graphics->fonts[font]->getStringWidth(visibleText());
  104. }
  105. CMultiLineLabel::CMultiLineLabel(Rect position, EFonts Font, ETextAlignment Align, const ColorRGBA & Color, const std::string & Text) :
  106. CLabel(position.x, position.y, Font, Align, Color, Text),
  107. visibleSize(0, 0, position.w, position.h)
  108. {
  109. pos.w = position.w;
  110. pos.h = position.h;
  111. splitText(Text, true);
  112. }
  113. void CMultiLineLabel::setVisibleSize(Rect visibleSize, bool redrawElement)
  114. {
  115. this->visibleSize = visibleSize;
  116. if(redrawElement)
  117. redraw();
  118. }
  119. void CMultiLineLabel::scrollTextBy(int distance)
  120. {
  121. scrollTextTo(visibleSize.y + distance);
  122. }
  123. void CMultiLineLabel::scrollTextTo(int distance, bool redrawAfterScroll)
  124. {
  125. Rect size = visibleSize;
  126. size.y = distance;
  127. setVisibleSize(size, redrawAfterScroll);
  128. }
  129. void CMultiLineLabel::setText(const std::string & Txt)
  130. {
  131. splitText(Txt, false); //setText used below can handle redraw
  132. CLabel::setText(Txt);
  133. }
  134. void CTextContainer::blitLine(Canvas & to, Rect destRect, std::string what)
  135. {
  136. const auto f = graphics->fonts[font];
  137. Point where = destRect.topLeft();
  138. const std::string delimeters = "{}";
  139. auto delimitersCount = std::count_if(what.cbegin(), what.cend(), [&delimeters](char c)
  140. {
  141. return delimeters.find(c) != std::string::npos;
  142. });
  143. //We should count delimiters length from string to correct centering later.
  144. delimitersCount *= f->getStringWidth(delimeters)/2;
  145. std::smatch match;
  146. std::regex expr("\\{(.*?)\\|");
  147. std::string::const_iterator searchStart( what.cbegin() );
  148. while(std::regex_search(searchStart, what.cend(), match, expr))
  149. {
  150. std::string colorText = match[1].str();
  151. if(auto c = Colors::parseColor(colorText))
  152. delimitersCount += f->getStringWidth(colorText + "|");
  153. searchStart = match.suffix().first;
  154. }
  155. // input is rect in which given text should be placed
  156. // calculate proper position for top-left corner of the text
  157. if(alignment == ETextAlignment::TOPLEFT || alignment == ETextAlignment::CENTERLEFT || alignment == ETextAlignment::BOTTOMLEFT)
  158. where.x += getBorderSize().x;
  159. if(alignment == ETextAlignment::CENTER || alignment == ETextAlignment::TOPCENTER || alignment == ETextAlignment::BOTTOMCENTER)
  160. where.x += (destRect.w - (static_cast<int>(f->getStringWidth(what)) - delimitersCount)) / 2;
  161. if(alignment == ETextAlignment::TOPRIGHT || alignment == ETextAlignment::BOTTOMRIGHT || alignment == ETextAlignment::CENTERRIGHT)
  162. where.x += getBorderSize().x + destRect.w - (static_cast<int>(f->getStringWidth(what)) - delimitersCount);
  163. if(alignment == ETextAlignment::TOPLEFT || alignment == ETextAlignment::TOPCENTER || alignment == ETextAlignment::TOPRIGHT)
  164. where.y += getBorderSize().y;
  165. if(alignment == ETextAlignment::CENTERLEFT || alignment == ETextAlignment::CENTER || alignment == ETextAlignment::CENTERRIGHT)
  166. where.y += (destRect.h - static_cast<int>(f->getLineHeight())) / 2;
  167. if(alignment == ETextAlignment::BOTTOMLEFT || alignment == ETextAlignment::BOTTOMCENTER || alignment == ETextAlignment::BOTTOMRIGHT)
  168. where.y += getBorderSize().y + destRect.h - static_cast<int>(f->getLineHeight());
  169. size_t begin = 0;
  170. size_t currDelimeter = 0;
  171. do
  172. {
  173. size_t end = what.find_first_of(delimeters[currDelimeter % 2], begin);
  174. if(begin != end)
  175. {
  176. std::string toPrint = what.substr(begin, end - begin);
  177. if(currDelimeter % 2) // Enclosed in {} text - set to yellow or defined color
  178. {
  179. std::smatch match;
  180. std::regex expr("^(.*?)\\|");
  181. if(std::regex_search(toPrint, match, expr))
  182. {
  183. std::string colorText = match[1].str();
  184. if(auto color = Colors::parseColor(colorText))
  185. {
  186. toPrint = toPrint.substr(colorText.length() + 1, toPrint.length() - colorText.length());
  187. to.drawText(where, font, *color, ETextAlignment::TOPLEFT, toPrint);
  188. }
  189. else
  190. to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
  191. }
  192. else
  193. to.drawText(where, font, Colors::YELLOW, ETextAlignment::TOPLEFT, toPrint);
  194. }
  195. else // Non-enclosed text, use default color
  196. to.drawText(where, font, color, ETextAlignment::TOPLEFT, toPrint);
  197. begin = end;
  198. where.x += (int)f->getStringWidth(toPrint);
  199. }
  200. currDelimeter++;
  201. } while(begin++ != std::string::npos);
  202. }
  203. CTextContainer::CTextContainer(ETextAlignment alignment, EFonts font, ColorRGBA color) :
  204. alignment(alignment),
  205. font(font),
  206. color(color)
  207. {
  208. }
  209. void CMultiLineLabel::showAll(Canvas & to)
  210. {
  211. CIntObject::showAll(to);
  212. const auto f = graphics->fonts[font];
  213. // calculate which lines should be visible
  214. int totalLines = static_cast<int>(lines.size());
  215. int beginLine = visibleSize.y;
  216. int endLine = getTextLocation().h + visibleSize.y;
  217. if(beginLine < 0)
  218. beginLine = 0;
  219. else
  220. beginLine /= (int)f->getLineHeight();
  221. if(endLine < 0)
  222. endLine = 0;
  223. else
  224. endLine /= (int)f->getLineHeight();
  225. endLine++;
  226. // and where they should be displayed
  227. Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * (int)f->getLineHeight());
  228. Point lineSize = Point(getTextLocation().w, (int)f->getLineHeight());
  229. CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), getTextLocation()); // to properly trim text that is too big to fit
  230. for(int i = beginLine; i < std::min(totalLines, endLine); i++)
  231. {
  232. if(!lines[i].empty()) //non-empty line
  233. blitLine(to, Rect(lineStart, lineSize), lines[i]);
  234. lineStart.y += (int)f->getLineHeight();
  235. }
  236. }
  237. void CMultiLineLabel::splitText(const std::string & Txt, bool redrawAfter)
  238. {
  239. lines.clear();
  240. const auto f = graphics->fonts[font];
  241. int lineHeight = static_cast<int>(f->getLineHeight());
  242. lines = CMessage::breakText(Txt, pos.w, font);
  243. textSize.y = lineHeight * (int)lines.size();
  244. textSize.x = 0;
  245. for(const std::string & line : lines)
  246. vstd::amax(textSize.x, f->getStringWidth(line.c_str()));
  247. if(redrawAfter)
  248. redraw();
  249. }
  250. Rect CMultiLineLabel::getTextLocation()
  251. {
  252. // this method is needed for vertical alignment alignment of text
  253. // when height of available text is smaller than height of widget
  254. // in this case - we should add proper offset to display text at required position
  255. if(pos.h <= textSize.y)
  256. return pos;
  257. Point textSize(pos.w, (int)graphics->fonts[font]->getLineHeight() * (int)lines.size());
  258. Point textOffset(pos.w - textSize.x, pos.h - textSize.y);
  259. switch(alignment)
  260. {
  261. case ETextAlignment::TOPLEFT: return Rect(pos.topLeft(), textSize);
  262. case ETextAlignment::TOPCENTER: return Rect(pos.topLeft(), textSize);
  263. case ETextAlignment::CENTER: return Rect(pos.topLeft() + textOffset / 2, textSize);
  264. case ETextAlignment::BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSize);
  265. }
  266. assert(0);
  267. return Rect();
  268. }
  269. CLabelGroup::CLabelGroup(EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  270. : font(Font), align(Align), color(Color)
  271. {
  272. defActions = 255 - DISPOSE;
  273. }
  274. void CLabelGroup::add(int x, int y, const std::string & text)
  275. {
  276. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  277. labels.push_back(std::make_shared<CLabel>(x, y, font, align, color, text));
  278. }
  279. size_t CLabelGroup::currentSize() const
  280. {
  281. return labels.size();
  282. }
  283. CTextBox::CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font, ETextAlignment Align, const ColorRGBA & Color) :
  284. sliderStyle(SliderStyle),
  285. slider(nullptr)
  286. {
  287. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  288. label = std::make_shared<CMultiLineLabel>(rect, Font, Align, Color);
  289. setRedrawParent(true);
  290. pos.x += rect.x;
  291. pos.y += rect.y;
  292. pos.h = rect.h;
  293. pos.w = rect.w;
  294. assert(pos.w >= 40); //we need some space
  295. setText(Text);
  296. }
  297. void CTextBox::sliderMoved(int to)
  298. {
  299. label->scrollTextTo(to);
  300. }
  301. void CTextBox::trimToFit()
  302. {
  303. if (slider)
  304. return;
  305. pos.w = label->textSize.x;
  306. pos.h = label->textSize.y;
  307. label->pos.w = label->textSize.x;
  308. label->pos.h = label->textSize.y;
  309. }
  310. void CTextBox::resize(Point newSize)
  311. {
  312. pos.w = newSize.x;
  313. pos.h = newSize.y;
  314. label->pos.w = pos.w;
  315. label->pos.h = pos.h;
  316. slider.reset();
  317. setText(label->getText()); // force refresh
  318. }
  319. void CTextBox::setText(const std::string & text)
  320. {
  321. label->pos.w = pos.w; // reset to default before textSize.y check
  322. label->setText(text);
  323. if(label->textSize.y <= label->pos.h && slider)
  324. {
  325. // slider is no longer needed
  326. slider.reset();
  327. }
  328. else if(slider)
  329. {
  330. // decrease width again if slider still used
  331. label->pos.w = pos.w - 16;
  332. assert(label->pos.w > 0);
  333. label->setText(text);
  334. slider->setAmount(label->textSize.y);
  335. }
  336. else if(label->textSize.y > label->pos.h)
  337. {
  338. // create slider and update widget
  339. label->pos.w = pos.w - 16;
  340. assert(label->pos.w > 0);
  341. label->setText(text);
  342. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  343. slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  344. label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
  345. slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight());
  346. slider->setPanningStep(1);
  347. slider->setScrollBounds(pos - slider->pos.topLeft());
  348. }
  349. }
  350. void CGStatusBar::setEnteringMode(bool on)
  351. {
  352. consoleText.clear();
  353. if (on)
  354. {
  355. //assert(enteringText == false);
  356. alignment = ETextAlignment::TOPLEFT;
  357. GH.startTextInput(pos);
  358. setText(consoleText);
  359. }
  360. else
  361. {
  362. //assert(enteringText == true);
  363. alignment = ETextAlignment::CENTER;
  364. GH.stopTextInput();
  365. setText(hoverText);
  366. }
  367. enteringText = on;
  368. }
  369. void CGStatusBar::setEnteredText(const std::string & text)
  370. {
  371. assert(enteringText == true);
  372. consoleText = text;
  373. setText(text);
  374. }
  375. void CGStatusBar::write(const std::string & Text)
  376. {
  377. hoverText = Text;
  378. if (enteringText == false)
  379. setText(hoverText);
  380. }
  381. void CGStatusBar::clearIfMatching(const std::string & Text)
  382. {
  383. if (hoverText == Text)
  384. clear();
  385. }
  386. void CGStatusBar::clear()
  387. {
  388. write({});
  389. }
  390. CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  391. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "", background_->pos.w)
  392. , enteringText(false)
  393. {
  394. addUsedEvents(LCLICK);
  395. background = background_;
  396. addChild(background.get());
  397. pos = background->pos;
  398. getBorderSize();
  399. autoRedraw = false;
  400. }
  401. CGStatusBar::CGStatusBar(int x, int y)
  402. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  403. , enteringText(false)
  404. {
  405. // without background
  406. addUsedEvents(LCLICK);
  407. }
  408. CGStatusBar::CGStatusBar(int x, int y, const ImagePath & name, int maxw)
  409. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  410. , enteringText(false)
  411. {
  412. addUsedEvents(LCLICK);
  413. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  414. auto backgroundImage = std::make_shared<CPicture>(name);
  415. background = backgroundImage;
  416. pos = background->pos;
  417. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  418. {
  419. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  420. vstd::amin(pos.w, maxw);
  421. backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
  422. }
  423. autoRedraw = false;
  424. }
  425. CGStatusBar::~CGStatusBar()
  426. {
  427. assert(GH.statusbar().get() != this);
  428. }
  429. void CGStatusBar::show(Canvas & to)
  430. {
  431. showAll(to);
  432. }
  433. void CGStatusBar::clickPressed(const Point & cursorPosition)
  434. {
  435. if(LOCPLINT && LOCPLINT->cingconsole->isActive())
  436. LOCPLINT->cingconsole->startEnteringText();
  437. }
  438. void CGStatusBar::activate()
  439. {
  440. GH.setStatusbar(shared_from_this());
  441. CIntObject::activate();
  442. }
  443. void CGStatusBar::deactivate()
  444. {
  445. assert(GH.statusbar().get() == this);
  446. GH.setStatusbar(nullptr);
  447. if (enteringText)
  448. LOCPLINT->cingconsole->endEnteringText(false);
  449. CIntObject::deactivate();
  450. }
  451. Point CGStatusBar::getBorderSize()
  452. {
  453. //Width of borders where text should not be printed
  454. static const Point borderSize(5, 1);
  455. switch(alignment)
  456. {
  457. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  458. case ETextAlignment::TOPCENTER: return Point(pos.w / 2, borderSize.y);
  459. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  460. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  461. }
  462. assert(0);
  463. return Point();
  464. }
  465. CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB, ETextAlignment alignment, bool giveFocusToInput)
  466. : CLabel(Pos.x, Pos.y, font, alignment),
  467. cb(CB)
  468. {
  469. setRedrawParent(true);
  470. pos.h = Pos.h;
  471. pos.w = Pos.w;
  472. maxWidth = Pos.w;
  473. background.reset();
  474. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  475. #if !defined(VCMI_MOBILE)
  476. if(giveFocusToInput)
  477. giveFocus();
  478. #endif
  479. }
  480. CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const ImagePath & bgName, const CFunctionList<void(const std::string &)> & CB)
  481. :cb(CB)
  482. {
  483. pos += Pos.topLeft();
  484. pos.h = Pos.h;
  485. pos.w = Pos.w;
  486. maxWidth = Pos.w;
  487. OBJ_CONSTRUCTION;
  488. background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
  489. addUsedEvents(LCLICK | SHOW_POPUP | KEYBOARD | TEXTINPUT);
  490. #if !defined(VCMI_MOBILE)
  491. giveFocus();
  492. #endif
  493. }
  494. CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
  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> CFocusable::usageIndex(0);
  509. void CFocusable::focusGot()
  510. {
  511. GH.startTextInput(pos);
  512. usageIndex++;
  513. }
  514. void CFocusable::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. {
  645. focus = false;
  646. focusables.push_back(this);
  647. }
  648. CFocusable::~CFocusable()
  649. {
  650. if(hasFocus())
  651. {
  652. inputWithFocus = nullptr;
  653. focusLost();
  654. }
  655. focusables -= this;
  656. }
  657. bool CFocusable::hasFocus() const
  658. {
  659. return inputWithFocus == this;
  660. }
  661. void CFocusable::giveFocus()
  662. {
  663. focus = true;
  664. focusGot();
  665. redraw();
  666. if(inputWithFocus)
  667. {
  668. inputWithFocus->focus = false;
  669. inputWithFocus->focusLost();
  670. inputWithFocus->redraw();
  671. }
  672. inputWithFocus = this;
  673. }
  674. void CFocusable::moveFocus()
  675. {
  676. auto i = vstd::find(focusables, this),
  677. ourIt = i;
  678. for(i++; i != ourIt; i++)
  679. {
  680. if(i == focusables.end())
  681. i = focusables.begin();
  682. if (*i == this)
  683. return;
  684. if((*i)->isActive())
  685. {
  686. (*i)->giveFocus();
  687. break;
  688. }
  689. }
  690. }
  691. void CFocusable::removeFocus()
  692. {
  693. if(this == inputWithFocus)
  694. {
  695. focus = false;
  696. focusLost();
  697. redraw();
  698. inputWithFocus = nullptr;
  699. }
  700. }