TextControls.cpp 14 KB

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