TextControls.cpp 19 KB

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