TextControls.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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/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::BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSize);
  273. }
  274. assert(0);
  275. return Rect();
  276. }
  277. CLabelGroup::CLabelGroup(EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  278. : font(Font), align(Align), color(Color)
  279. {
  280. defActions = 255 - DISPOSE;
  281. }
  282. void CLabelGroup::add(int x, int y, const std::string & text)
  283. {
  284. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  285. labels.push_back(std::make_shared<CLabel>(x, y, font, align, color, text));
  286. }
  287. size_t CLabelGroup::currentSize() const
  288. {
  289. return labels.size();
  290. }
  291. CTextBox::CTextBox(std::string Text, const Rect & rect, int SliderStyle, EFonts Font, ETextAlignment Align, const ColorRGBA & Color) :
  292. sliderStyle(SliderStyle),
  293. slider(nullptr)
  294. {
  295. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  296. label = std::make_shared<CMultiLineLabel>(rect, Font, Align, Color);
  297. setRedrawParent(true);
  298. pos.x += rect.x;
  299. pos.y += rect.y;
  300. pos.h = rect.h;
  301. pos.w = rect.w;
  302. assert(pos.w >= 40); //we need some space
  303. setText(Text);
  304. }
  305. void CTextBox::sliderMoved(int to)
  306. {
  307. label->scrollTextTo(to);
  308. }
  309. void CTextBox::trimToFit()
  310. {
  311. if (slider)
  312. return;
  313. pos.w = label->textSize.x;
  314. pos.h = label->textSize.y;
  315. label->pos.w = label->textSize.x;
  316. label->pos.h = label->textSize.y;
  317. }
  318. void CTextBox::resize(Point newSize)
  319. {
  320. pos.w = newSize.x;
  321. pos.h = newSize.y;
  322. label->pos.w = pos.w;
  323. label->pos.h = pos.h;
  324. slider.reset();
  325. setText(label->getText()); // force refresh
  326. }
  327. void CTextBox::setText(const std::string & text)
  328. {
  329. label->pos.w = pos.w; // reset to default before textSize.y check
  330. label->setText(text);
  331. if(label->textSize.y <= label->pos.h && slider)
  332. {
  333. // slider is no longer needed
  334. slider.reset();
  335. label->scrollTextTo(0);
  336. }
  337. else if(slider)
  338. {
  339. // decrease width again if slider still used
  340. label->pos.w = pos.w - 16;
  341. assert(label->pos.w > 0);
  342. label->setText(text);
  343. slider->setAmount(label->textSize.y);
  344. }
  345. else if(label->textSize.y > label->pos.h)
  346. {
  347. // create slider and update widget
  348. label->pos.w = pos.w - 16;
  349. assert(label->pos.w > 0);
  350. label->setText(text);
  351. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255 - DISPOSE);
  352. slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  353. label->pos.h, label->textSize.y, 0, Orientation::VERTICAL, CSlider::EStyle(sliderStyle));
  354. slider->setScrollStep((int)graphics->fonts[label->font]->getLineHeight());
  355. slider->setPanningStep(1);
  356. slider->setScrollBounds(pos - slider->pos.topLeft());
  357. }
  358. }
  359. void CGStatusBar::setEnteringMode(bool on)
  360. {
  361. consoleText.clear();
  362. if (on)
  363. {
  364. //assert(enteringText == false);
  365. alignment = ETextAlignment::TOPLEFT;
  366. GH.startTextInput(pos);
  367. setText(consoleText);
  368. }
  369. else
  370. {
  371. //assert(enteringText == true);
  372. alignment = ETextAlignment::CENTER;
  373. GH.stopTextInput();
  374. setText(hoverText);
  375. }
  376. enteringText = on;
  377. }
  378. void CGStatusBar::setEnteredText(const std::string & text)
  379. {
  380. assert(enteringText == true);
  381. consoleText = text;
  382. setText(text);
  383. }
  384. void CGStatusBar::write(const std::string & Text)
  385. {
  386. hoverText = Text;
  387. if (enteringText == false)
  388. setText(hoverText);
  389. }
  390. void CGStatusBar::clearIfMatching(const std::string & Text)
  391. {
  392. if (hoverText == Text)
  393. clear();
  394. }
  395. void CGStatusBar::clear()
  396. {
  397. write({});
  398. }
  399. CGStatusBar::CGStatusBar(std::shared_ptr<CIntObject> background_, EFonts Font, ETextAlignment Align, const ColorRGBA & Color)
  400. : CLabel(background_->pos.x, background_->pos.y, Font, Align, Color, "", background_->pos.w)
  401. , enteringText(false)
  402. {
  403. addUsedEvents(LCLICK);
  404. background = background_;
  405. addChild(background.get());
  406. pos = background->pos;
  407. getBorderSize();
  408. autoRedraw = false;
  409. }
  410. CGStatusBar::CGStatusBar(int x, int y)
  411. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  412. , enteringText(false)
  413. {
  414. // without background
  415. addUsedEvents(LCLICK);
  416. }
  417. CGStatusBar::CGStatusBar(int x, int y, const ImagePath & name, int maxw)
  418. : CLabel(x, y, FONT_SMALL, ETextAlignment::CENTER)
  419. , enteringText(false)
  420. {
  421. addUsedEvents(LCLICK);
  422. OBJECT_CONSTRUCTION_CAPTURING(255 - DISPOSE);
  423. auto backgroundImage = std::make_shared<CPicture>(name);
  424. background = backgroundImage;
  425. pos = background->pos;
  426. if((unsigned)maxw < (unsigned)pos.w) //(insigned)-1 > than any correct value of pos.w
  427. {
  428. //execution of this block when maxw is incorrect breaks text centralization (issue #3151)
  429. vstd::amin(pos.w, maxw);
  430. backgroundImage->srcRect = Rect(0, 0, maxw, pos.h);
  431. }
  432. autoRedraw = false;
  433. }
  434. CGStatusBar::~CGStatusBar()
  435. {
  436. assert(GH.statusbar().get() != this);
  437. }
  438. void CGStatusBar::show(Canvas & to)
  439. {
  440. showAll(to);
  441. }
  442. void CGStatusBar::clickPressed(const Point & cursorPosition)
  443. {
  444. if(LOCPLINT && LOCPLINT->cingconsole->isActive())
  445. LOCPLINT->cingconsole->startEnteringText();
  446. }
  447. void CGStatusBar::activate()
  448. {
  449. GH.setStatusbar(shared_from_this());
  450. CIntObject::activate();
  451. }
  452. void CGStatusBar::deactivate()
  453. {
  454. assert(GH.statusbar().get() == this);
  455. GH.setStatusbar(nullptr);
  456. if (enteringText)
  457. LOCPLINT->cingconsole->endEnteringText(false);
  458. CIntObject::deactivate();
  459. }
  460. Point CGStatusBar::getBorderSize()
  461. {
  462. //Width of borders where text should not be printed
  463. static const Point borderSize(5, 1);
  464. switch(alignment)
  465. {
  466. case ETextAlignment::TOPLEFT: return Point(borderSize.x, borderSize.y);
  467. case ETextAlignment::TOPCENTER: return Point(pos.w / 2, borderSize.y);
  468. case ETextAlignment::CENTER: return Point(pos.w / 2, pos.h / 2);
  469. case ETextAlignment::BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  470. }
  471. assert(0);
  472. return Point();
  473. }