TextControls.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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 "Buttons.h"
  13. #include "Images.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../windows/CMessage.h"
  17. #include "../adventureMap/CInGameConsole.h"
  18. #include "../renderSDL/SDL_Extensions.h"
  19. #include "../../lib/CGeneralTextHandler.h"
  20. #ifdef VCMI_ANDROID
  21. #include "lib/CAndroidVMHelper.h"
  22. #endif
  23. std::list<CFocusable*> CFocusable::focusables;
  24. CFocusable * CFocusable::inputWithFocus;
  25. std::string CLabel::visibleText()
  26. {
  27. return text;
  28. }
  29. void CLabel::showAll(SDL_Surface * to)
  30. {
  31. CIntObject::showAll(to);
  32. if(!visibleText().empty())
  33. blitLine(to, pos, visibleText());
  34. }
  35. CLabel::CLabel(int x, int y, EFonts Font, ETextAlignment Align, const SDL_Color & Color, const std::string & Text)
  36. : CTextContainer(Align, Font, Color), text(Text)
  37. {
  38. type |= REDRAW_PARENT;
  39. autoRedraw = true;
  40. pos.x += x;
  41. pos.y += y;
  42. pos.w = pos.h = 0;
  43. if(alignment == ETextAlignment::TOPLEFT) // causes issues for MIDDLE
  44. {
  45. pos.w = (int)graphics->fonts[font]->getStringWidth(visibleText().c_str());
  46. pos.h = (int)graphics->fonts[font]->getLineHeight();
  47. }
  48. }
  49. Point CLabel::getBorderSize()
  50. {
  51. return Point(0, 0);
  52. }
  53. std::string CLabel::getText()
  54. {
  55. return text;
  56. }
  57. void CLabel::setAutoRedraw(bool value)
  58. {
  59. autoRedraw = value;
  60. }
  61. void CLabel::setText(const std::string & Txt)
  62. {
  63. text = Txt;
  64. if(autoRedraw)
  65. {
  66. if(background || !parent)
  67. redraw();
  68. else
  69. parent->redraw();
  70. }
  71. }
  72. void CLabel::setColor(const SDL_Color & Color)
  73. {
  74. color = Color;
  75. if(autoRedraw)
  76. {
  77. if(background || !parent)
  78. redraw();
  79. else
  80. parent->redraw();
  81. }
  82. }
  83. size_t CLabel::getWidth()
  84. {
  85. return graphics->fonts[font]->getStringWidth(visibleText());;
  86. }
  87. CMultiLineLabel::CMultiLineLabel(Rect position, EFonts Font, ETextAlignment Align, const SDL_Color & Color, const std::string & Text) :
  88. CLabel(position.x, position.y, Font, Align, Color, Text),
  89. visibleSize(0, 0, position.w, position.h)
  90. {
  91. pos.w = position.w;
  92. pos.h = position.h;
  93. splitText(Text, true);
  94. }
  95. void CMultiLineLabel::setVisibleSize(Rect visibleSize, bool redrawElement)
  96. {
  97. this->visibleSize = visibleSize;
  98. if(redrawElement)
  99. redraw();
  100. }
  101. void CMultiLineLabel::scrollTextBy(int distance)
  102. {
  103. scrollTextTo(visibleSize.y + distance);
  104. }
  105. void CMultiLineLabel::scrollTextTo(int distance, bool redrawAfterScroll)
  106. {
  107. Rect size = visibleSize;
  108. size.y = distance;
  109. setVisibleSize(size, redrawAfterScroll);
  110. }
  111. void CMultiLineLabel::setText(const std::string & Txt)
  112. {
  113. splitText(Txt, false); //setText used below can handle redraw
  114. CLabel::setText(Txt);
  115. }
  116. void CTextContainer::blitLine(SDL_Surface * to, Rect destRect, std::string what)
  117. {
  118. const auto f = graphics->fonts[font];
  119. Point where = destRect.topLeft();
  120. // input is rect in which given text should be placed
  121. // calculate proper position for top-left corner of the text
  122. if(alignment == ETextAlignment::TOPLEFT)
  123. {
  124. where.x += getBorderSize().x;
  125. where.y += getBorderSize().y;
  126. }
  127. if(alignment == ETextAlignment::CENTER)
  128. {
  129. where.x += (int(destRect.w) - int(f->getStringWidth(what))) / 2;
  130. where.y += (int(destRect.h) - int(f->getLineHeight())) / 2;
  131. }
  132. if(alignment == ETextAlignment::BOTTOMRIGHT)
  133. {
  134. where.x += getBorderSize().x + destRect.w - (int)f->getStringWidth(what);
  135. where.y += getBorderSize().y + destRect.h - (int)f->getLineHeight();
  136. }
  137. size_t begin = 0;
  138. std::string delimeters = "{}";
  139. size_t currDelimeter = 0;
  140. do
  141. {
  142. size_t end = what.find_first_of(delimeters[currDelimeter % 2], begin);
  143. if(begin != end)
  144. {
  145. std::string toPrint = what.substr(begin, end - begin);
  146. if(currDelimeter % 2) // Enclosed in {} text - set to yellow
  147. f->renderTextLeft(to, toPrint, Colors::YELLOW, where);
  148. else // Non-enclosed text, use default color
  149. f->renderTextLeft(to, toPrint, color, where);
  150. begin = end;
  151. where.x += (int)f->getStringWidth(toPrint);
  152. }
  153. currDelimeter++;
  154. } while(begin++ != std::string::npos);
  155. }
  156. CTextContainer::CTextContainer(ETextAlignment alignment, EFonts font, SDL_Color color) :
  157. alignment(alignment),
  158. font(font),
  159. color(color)
  160. {
  161. }
  162. void CMultiLineLabel::showAll(SDL_Surface * to)
  163. {
  164. CIntObject::showAll(to);
  165. const auto f = graphics->fonts[font];
  166. // calculate which lines should be visible
  167. int totalLines = static_cast<int>(lines.size());
  168. int beginLine = visibleSize.y;
  169. int endLine = getTextLocation().h + visibleSize.y;
  170. if(beginLine < 0)
  171. beginLine = 0;
  172. else
  173. beginLine /= (int)f->getLineHeight();
  174. if(endLine < 0)
  175. endLine = 0;
  176. else
  177. endLine /= (int)f->getLineHeight();
  178. endLine++;
  179. // and where they should be displayed
  180. Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * (int)f->getLineHeight());
  181. Point lineSize = Point(getTextLocation().w, (int)f->getLineHeight());
  182. CSDL_Ext::CClipRectGuard guard(to, getTextLocation()); // to properly trim text that is too big to fit
  183. for(int i = beginLine; i < std::min(totalLines, endLine); i++)
  184. {
  185. if(!lines[i].empty()) //non-empty line
  186. blitLine(to, Rect(lineStart, lineSize), lines[i]);
  187. lineStart.y += (int)f->getLineHeight();
  188. }
  189. }
  190. void CMultiLineLabel::splitText(const std::string & Txt, bool redrawAfter)
  191. {
  192. lines.clear();
  193. const auto f = graphics->fonts[font];
  194. int lineHeight = static_cast<int>(f->getLineHeight());
  195. lines = CMessage::breakText(Txt, pos.w, font);
  196. textSize.y = lineHeight * (int)lines.size();
  197. textSize.x = 0;
  198. for(const std::string & line : lines)
  199. vstd::amax(textSize.x, f->getStringWidth(line.c_str()));
  200. if(redrawAfter)
  201. redraw();
  202. }
  203. Rect CMultiLineLabel::getTextLocation()
  204. {
  205. // this method is needed for vertical alignment alignment of text
  206. // when height of available text is smaller than height of widget
  207. // in this case - we should add proper offset to display text at required position
  208. if(pos.h <= textSize.y)
  209. return pos;
  210. Point textSize(pos.w, (int)graphics->fonts[font]->getLineHeight() * (int)lines.size());
  211. Point textOffset(pos.w - textSize.x, pos.h - textSize.y);
  212. switch(alignment)
  213. {
  214. case ETextAlignment::TOPLEFT: return Rect(pos.topLeft(), textSize);
  215. case ETextAlignment::CENTER: return Rect(pos.topLeft() + textOffset / 2, textSize);
  216. case ETextAlignment::BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSize);
  217. }
  218. assert(0);
  219. return Rect();
  220. }
  221. CLabelGroup::CLabelGroup(EFonts Font, ETextAlignment Align, const SDL_Color & Color)
  222. : font(Font), align(Align), color(Color)
  223. {
  224. defActions = 255 - DISPOSE;
  225. }
  226. void CLabelGroup::add(int x, int y, const std::string & text)
  227. {
  228. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  229. labels.push_back(std::make_shared<CLabel>(x, y, font, align, color, text));
  230. }
  231. size_t CLabelGroup::currentSize() const
  232. {
  233. return labels.size();
  234. }
  235. CTextBox::CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font, ETextAlignment Align, const SDL_Color & Color) :
  236. sliderStyle(SliderStyle),
  237. slider(nullptr)
  238. {
  239. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  240. label = std::make_shared<CMultiLineLabel>(rect, Font, Align, Color);
  241. type |= REDRAW_PARENT;
  242. pos.x += rect.x;
  243. pos.y += rect.y;
  244. pos.h = rect.h;
  245. pos.w = rect.w;
  246. assert(pos.w >= 40); //we need some space
  247. setText(Text);
  248. }
  249. void CTextBox::sliderMoved(int to)
  250. {
  251. label->scrollTextTo(to);
  252. }
  253. void CTextBox::resize(Point newSize)
  254. {
  255. pos.w = newSize.x;
  256. pos.h = newSize.y;
  257. label->pos.w = pos.w;
  258. label->pos.h = pos.h;
  259. slider.reset();
  260. setText(label->getText()); // force refresh
  261. }
  262. void CTextBox::setText(const std::string & text)
  263. {
  264. label->pos.w = pos.w; // reset to default before textSize.y check
  265. label->setText(text);
  266. if(label->textSize.y <= label->pos.h && slider)
  267. {
  268. // slider is no longer needed
  269. slider.reset();
  270. }
  271. else if(slider)
  272. {
  273. // decrease width again if slider still used
  274. label->pos.w = pos.w - 32;
  275. label->setText(text);
  276. slider->setAmount(label->textSize.y);
  277. }
  278. else if(label->textSize.y > label->pos.h)
  279. {
  280. // create slider and update widget
  281. label->pos.w = pos.w - 32;
  282. label->setText(text);
  283. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  284. slider = std::make_shared<CSlider>(Point(pos.w - 32, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  285. label->pos.h, label->textSize.y, 0, false, CSlider::EStyle(sliderStyle));
  286. slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight());
  287. }
  288. }
  289. void CGStatusBar::setEnteringMode(bool on)
  290. {
  291. consoleText.clear();
  292. if (on)
  293. {
  294. assert(enteringText == false);
  295. alignment = ETextAlignment::TOPLEFT;
  296. GH.startTextInput(pos);
  297. setText(consoleText);
  298. }
  299. else
  300. {
  301. assert(enteringText == true);
  302. alignment = ETextAlignment::CENTER;
  303. GH.stopTextInput();
  304. setText(hoverText);
  305. }
  306. enteringText = on;
  307. }
  308. void CGStatusBar::setEnteredText(const std::string & text)
  309. {
  310. assert(enteringText == true);
  311. consoleText = text;
  312. setText(text);
  313. }
  314. void CGStatusBar::write(const std::string & Text)
  315. {
  316. hoverText = Text;
  317. if (enteringText == false)
  318. setText(hoverText);
  319. }
  320. void CGStatusBar::clearIfMatching(const std::string & Text)
  321. {
  322. if (hoverText == Text)
  323. clear();
  324. }
  325. void CGStatusBar::clear()
  326. {
  327. write({});
  328. }
  329. CGStatusBar::CGStatusBar(std::shared_ptr<CPicture> background_, EFonts Font, ETextAlignment Align, const SDL_Color & Color)
  330. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "")
  331. , enteringText(false)
  332. {
  333. addUsedEvents(LCLICK);
  334. background = background_;
  335. addChild(background.get());
  336. pos = background->pos;
  337. getBorderSize();
  338. autoRedraw = false;
  339. }
  340. CGStatusBar::CGStatusBar(int x, int y, std::string name, int maxw)
  341. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  342. , enteringText(false)
  343. {
  344. addUsedEvents(LCLICK);
  345. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  346. background = std::make_shared<CPicture>(name);
  347. pos = background->pos;
  348. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  349. {
  350. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  351. vstd::amin(pos.w, maxw);
  352. background->srcRect = Rect(0, 0, maxw, pos.h);
  353. }
  354. autoRedraw = false;
  355. }
  356. void CGStatusBar::show(SDL_Surface * to)
  357. {
  358. showAll(to);
  359. }
  360. void CGStatusBar::init()
  361. {
  362. GH.statusbar = shared_from_this();
  363. }
  364. void CGStatusBar::clickLeft(tribool down, bool previousState)
  365. {
  366. if(!down)
  367. {
  368. if(LOCPLINT && LOCPLINT->cingconsole->active)
  369. LOCPLINT->cingconsole->startEnteringText();
  370. }
  371. }
  372. void CGStatusBar::deactivate()
  373. {
  374. if (enteringText)
  375. LOCPLINT->cingconsole->endEnteringText(false);
  376. CIntObject::deactivate();
  377. }
  378. Point CGStatusBar::getBorderSize()
  379. {
  380. //Width of borders where text should not be printed
  381. static const Point borderSize(5, 1);
  382. switch(alignment)
  383. {
  384. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  385. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  386. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  387. }
  388. assert(0);
  389. return Point();
  390. }
  391. CTextInput::CTextInput(const Rect & Pos, EFonts font, const CFunctionList<void(const std::string &)> & CB)
  392. : CLabel(Pos.x, Pos.y, font, ETextAlignment::CENTER),
  393. cb(CB),
  394. CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  395. {
  396. type |= REDRAW_PARENT;
  397. pos.h = Pos.h;
  398. pos.w = Pos.w;
  399. captureAllKeys = true;
  400. background.reset();
  401. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  402. #if !defined(VCMI_MOBILE)
  403. giveFocus();
  404. #endif
  405. }
  406. CTextInput::CTextInput(const Rect & Pos, const Point & bgOffset, const std::string & bgName, const CFunctionList<void(const std::string &)> & CB)
  407. :cb(CB), CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  408. {
  409. pos += Pos.topLeft();
  410. pos.h = Pos.h;
  411. pos.w = Pos.w;
  412. captureAllKeys = true;
  413. OBJ_CONSTRUCTION;
  414. background = std::make_shared<CPicture>(bgName, bgOffset.x, bgOffset.y);
  415. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  416. #if !defined(VCMI_MOBILE)
  417. giveFocus();
  418. #endif
  419. }
  420. CTextInput::CTextInput(const Rect & Pos, std::shared_ptr<IImage> srf)
  421. :CFocusable(std::make_shared<CKeyboardFocusListener>(this))
  422. {
  423. pos += Pos.topLeft();
  424. captureAllKeys = true;
  425. OBJ_CONSTRUCTION;
  426. background = std::make_shared<CPicture>(srf, Pos);
  427. pos.w = background->pos.w;
  428. pos.h = background->pos.h;
  429. background->pos = pos;
  430. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  431. #if !defined(VCMI_MOBILE)
  432. giveFocus();
  433. #endif
  434. }
  435. std::atomic<int> CKeyboardFocusListener::usageIndex(0);
  436. CKeyboardFocusListener::CKeyboardFocusListener(CTextInput * textInput)
  437. :textInput(textInput)
  438. {
  439. }
  440. void CKeyboardFocusListener::focusGot()
  441. {
  442. GH.startTextInput(textInput->pos);
  443. usageIndex++;
  444. }
  445. void CKeyboardFocusListener::focusLost()
  446. {
  447. if(0 == --usageIndex)
  448. {
  449. GH.stopTextInput();
  450. }
  451. }
  452. std::string CTextInput::visibleText()
  453. {
  454. return focus ? text + newText + "_" : text;
  455. }
  456. void CTextInput::clickLeft(tribool down, bool previousState)
  457. {
  458. if(down && !focus)
  459. giveFocus();
  460. }
  461. void CTextInput::keyPressed(const SDL_Keycode & key)
  462. {
  463. if(!focus)
  464. return;
  465. if(key == SDLK_TAB)
  466. {
  467. moveFocus();
  468. GH.breakEventHandling();
  469. return;
  470. }
  471. bool redrawNeeded = false;
  472. switch(key)
  473. {
  474. case SDLK_DELETE: // have index > ' ' so it won't be filtered out by default section
  475. return;
  476. case SDLK_BACKSPACE:
  477. if(!newText.empty())
  478. {
  479. Unicode::trimRight(newText);
  480. redrawNeeded = true;
  481. }
  482. else if(!text.empty())
  483. {
  484. Unicode::trimRight(text);
  485. redrawNeeded = true;
  486. }
  487. break;
  488. default:
  489. break;
  490. }
  491. if(redrawNeeded)
  492. {
  493. redraw();
  494. cb(text);
  495. }
  496. }
  497. void CTextInput::setText(const std::string & nText)
  498. {
  499. setText(nText, false);
  500. }
  501. void CTextInput::setText(const std::string & nText, bool callCb)
  502. {
  503. CLabel::setText(nText);
  504. if(callCb)
  505. cb(text);
  506. }
  507. bool CTextInput::captureThisKey(const SDL_Keycode & key)
  508. {
  509. if(key == SDLK_RETURN || key == SDLK_KP_ENTER || key == SDLK_ESCAPE)
  510. return false;
  511. return true;
  512. }
  513. void CTextInput::textInputed(const std::string & enteredText)
  514. {
  515. if(!focus)
  516. return;
  517. std::string oldText = text;
  518. text += enteredText;
  519. filters(text, oldText);
  520. if(text != oldText)
  521. {
  522. redraw();
  523. cb(text);
  524. }
  525. newText.clear();
  526. }
  527. void CTextInput::textEdited(const std::string & enteredText)
  528. {
  529. if(!focus)
  530. return;
  531. newText = enteredText;
  532. redraw();
  533. cb(text + newText);
  534. }
  535. void CTextInput::filenameFilter(std::string & text, const std::string &)
  536. {
  537. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  538. size_t pos;
  539. while((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  540. text.erase(pos, 1);
  541. }
  542. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  543. {
  544. assert(minValue < maxValue);
  545. if(text.empty())
  546. text = "0";
  547. size_t pos = 0;
  548. if(text[0] == '-') //allow '-' sign as first symbol only
  549. pos++;
  550. while(pos < text.size())
  551. {
  552. if(text[pos] < '0' || text[pos] > '9')
  553. {
  554. text = oldText;
  555. return; //new text is not number.
  556. }
  557. pos++;
  558. }
  559. try
  560. {
  561. int value = boost::lexical_cast<int>(text);
  562. if(value < minValue)
  563. text = boost::lexical_cast<std::string>(minValue);
  564. else if(value > maxValue)
  565. text = boost::lexical_cast<std::string>(maxValue);
  566. }
  567. catch(boost::bad_lexical_cast &)
  568. {
  569. //Should never happen. Unless I missed some cases
  570. logGlobal->warn("Warning: failed to convert %s to number!", text);
  571. text = oldText;
  572. }
  573. }
  574. CFocusable::CFocusable()
  575. :CFocusable(std::make_shared<IFocusListener>())
  576. {
  577. }
  578. CFocusable::CFocusable(std::shared_ptr<IFocusListener> focusListener)
  579. : focusListener(focusListener)
  580. {
  581. focus = false;
  582. focusables.push_back(this);
  583. }
  584. CFocusable::~CFocusable()
  585. {
  586. if(hasFocus())
  587. {
  588. inputWithFocus = nullptr;
  589. focusListener->focusLost();
  590. }
  591. focusables -= this;
  592. }
  593. bool CFocusable::hasFocus() const
  594. {
  595. return inputWithFocus == this;
  596. }
  597. void CFocusable::giveFocus()
  598. {
  599. focus = true;
  600. focusListener->focusGot();
  601. redraw();
  602. if(inputWithFocus)
  603. {
  604. inputWithFocus->focus = false;
  605. inputWithFocus->focusListener->focusLost();
  606. inputWithFocus->redraw();
  607. }
  608. inputWithFocus = this;
  609. }
  610. void CFocusable::moveFocus()
  611. {
  612. auto i = vstd::find(focusables, this),
  613. ourIt = i;
  614. for(i++; i != ourIt; i++)
  615. {
  616. if(i == focusables.end())
  617. i = focusables.begin();
  618. if((*i)->active)
  619. {
  620. (*i)->giveFocus();
  621. break;
  622. }
  623. }
  624. }