TextControls.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. #include "StdInc.h"
  2. #include "TextControls.h"
  3. #include "Buttons.h"
  4. #include "Images.h"
  5. #include "../CMessage.h"
  6. #include "../gui/CGuiHandler.h"
  7. #include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff
  8. /*
  9. * TextControls.cpp, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. std::string CLabel::visibleText()
  18. {
  19. return text;
  20. }
  21. void CLabel::showAll(SDL_Surface * to)
  22. {
  23. CIntObject::showAll(to);
  24. if(!visibleText().empty())
  25. blitLine(to, pos, visibleText());
  26. }
  27. CLabel::CLabel(int x, int y, EFonts Font /*= FONT_SMALL*/, EAlignment Align, const SDL_Color &Color /*= Colors::WHITE*/, const std::string &Text /*= ""*/)
  28. :CTextContainer(Align, Font, Color), text(Text)
  29. {
  30. type |= REDRAW_PARENT;
  31. autoRedraw = true;
  32. pos.x += x;
  33. pos.y += y;
  34. pos.w = pos.h = 0;
  35. bg = nullptr;
  36. if (alignment == TOPLEFT) // causes issues for MIDDLE
  37. {
  38. pos.w = graphics->fonts[font]->getStringWidth(visibleText().c_str());
  39. pos.h = graphics->fonts[font]->getLineHeight();
  40. }
  41. }
  42. Point CLabel::getBorderSize()
  43. {
  44. return Point(0, 0);
  45. }
  46. std::string CLabel::getText()
  47. {
  48. return text;
  49. }
  50. void CLabel::setText(const std::string &Txt)
  51. {
  52. text = Txt;
  53. if(autoRedraw)
  54. {
  55. if(bg || !parent)
  56. redraw();
  57. else
  58. parent->redraw();
  59. }
  60. }
  61. CMultiLineLabel::CMultiLineLabel(Rect position, EFonts Font, EAlignment Align, const SDL_Color &Color, const std::string &Text):
  62. CLabel(position.x, position.y, Font, Align, Color, Text),
  63. visibleSize(0, 0, position.w, position.h)
  64. {
  65. pos.w = position.w;
  66. pos.h = position.h;
  67. splitText(Text);
  68. }
  69. void CMultiLineLabel::setVisibleSize(Rect visibleSize)
  70. {
  71. this->visibleSize = visibleSize;
  72. redraw();
  73. }
  74. void CMultiLineLabel::scrollTextBy(int distance)
  75. {
  76. scrollTextTo(visibleSize.y + distance);
  77. }
  78. void CMultiLineLabel::scrollTextTo(int distance)
  79. {
  80. Rect size = visibleSize;
  81. size.y = distance;
  82. setVisibleSize(size);
  83. }
  84. void CMultiLineLabel::setText(const std::string &Txt)
  85. {
  86. splitText(Txt);
  87. CLabel::setText(Txt);
  88. }
  89. void CTextContainer::blitLine(SDL_Surface *to, Rect destRect, std::string what)
  90. {
  91. const auto f = graphics->fonts[font];
  92. Point where = destRect.topLeft();
  93. // input is rect in which given text should be placed
  94. // calculate proper position for top-left corner of the text
  95. if (alignment == TOPLEFT)
  96. {
  97. where.x += getBorderSize().x;
  98. where.y += getBorderSize().y;
  99. }
  100. if (alignment == CENTER)
  101. {
  102. where.x += (int(destRect.w) - int(f->getStringWidth(what))) / 2;
  103. where.y += (int(destRect.h) - int(f->getLineHeight())) / 2;
  104. }
  105. if (alignment == BOTTOMRIGHT)
  106. {
  107. where.x += getBorderSize().x + destRect.w - f->getStringWidth(what);
  108. where.y += getBorderSize().y + destRect.h - f->getLineHeight();
  109. }
  110. size_t begin = 0;
  111. std::string delimeters = "{}";
  112. size_t currDelimeter = 0;
  113. do
  114. {
  115. size_t end = what.find_first_of(delimeters[currDelimeter % 2], begin);
  116. if (begin != end)
  117. {
  118. std::string toPrint = what.substr(begin, end - begin);
  119. if (currDelimeter % 2) // Enclosed in {} text - set to yellow
  120. f->renderTextLeft(to, toPrint, Colors::YELLOW, where);
  121. else // Non-enclosed text, use default color
  122. f->renderTextLeft(to, toPrint, color, where);
  123. begin = end;
  124. where.x += f->getStringWidth(toPrint);
  125. }
  126. currDelimeter++;
  127. }
  128. while (begin++ != std::string::npos);
  129. }
  130. CTextContainer::CTextContainer(EAlignment alignment, EFonts font, SDL_Color color):
  131. alignment(alignment),
  132. font(font),
  133. color(color)
  134. {}
  135. void CMultiLineLabel::showAll(SDL_Surface * to)
  136. {
  137. CIntObject::showAll(to);
  138. const auto f = graphics->fonts[font];
  139. // calculate which lines should be visible
  140. int totalLines = lines.size();
  141. int beginLine = visibleSize.y;
  142. int endLine = getTextLocation().h + visibleSize.y;
  143. if (beginLine < 0)
  144. beginLine = 0;
  145. else
  146. beginLine /= f->getLineHeight();
  147. if (endLine < 0)
  148. endLine = 0;
  149. else
  150. endLine /= f->getLineHeight();
  151. endLine++;
  152. // and where they should be displayed
  153. Point lineStart = getTextLocation().topLeft() - visibleSize + Point(0, beginLine * f->getLineHeight());
  154. Point lineSize = Point(getTextLocation().w, f->getLineHeight());
  155. CSDL_Ext::CClipRectGuard guard(to, getTextLocation()); // to properly trim text that is too big to fit
  156. for (int i = beginLine; i < std::min(totalLines, endLine); i++)
  157. {
  158. if (!lines[i].empty()) //non-empty line
  159. blitLine(to, Rect(lineStart, lineSize), lines[i]);
  160. lineStart.y += f->getLineHeight();
  161. }
  162. }
  163. void CMultiLineLabel::splitText(const std::string &Txt)
  164. {
  165. lines.clear();
  166. const auto f = graphics->fonts[font];
  167. int lineHeight = f->getLineHeight();
  168. lines = CMessage::breakText(Txt, pos.w, font);
  169. textSize.y = lineHeight * lines.size();
  170. textSize.x = 0;
  171. for(const std::string &line : lines)
  172. vstd::amax( textSize.x, f->getStringWidth(line.c_str()));
  173. redraw();
  174. }
  175. Rect CMultiLineLabel::getTextLocation()
  176. {
  177. // this method is needed for vertical alignment alignment of text
  178. // when height of available text is smaller than height of widget
  179. // in this case - we should add proper offset to display text at required position
  180. if (pos.h <= textSize.y)
  181. return pos;
  182. Point textSize(pos.w, graphics->fonts[font]->getLineHeight() * lines.size());
  183. Point textOffset(pos.w - textSize.x, pos.h - textSize.y);
  184. switch(alignment)
  185. {
  186. case TOPLEFT: return Rect(pos.topLeft(), textSize);
  187. case CENTER: return Rect(pos.topLeft() + textOffset / 2, textSize);
  188. case BOTTOMRIGHT: return Rect(pos.topLeft() + textOffset, textSize);
  189. }
  190. assert(0);
  191. return Rect();
  192. }
  193. CLabelGroup::CLabelGroup(EFonts Font, EAlignment Align, const SDL_Color &Color):
  194. font(Font), align(Align), color(Color)
  195. {}
  196. void CLabelGroup::add(int x, int y, const std::string &text)
  197. {
  198. OBJ_CONSTRUCTION_CAPTURING_ALL;
  199. new CLabel(x, y, font, align, color, text);
  200. }
  201. CTextBox::CTextBox(std::string Text, const Rect &rect, int SliderStyle, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= TOPLEFT*/, const SDL_Color &Color /*= Colors::WHITE*/):
  202. sliderStyle(SliderStyle),
  203. slider(nullptr)
  204. {
  205. OBJ_CONSTRUCTION_CAPTURING_ALL;
  206. label = new CMultiLineLabel(rect, Font, Align, Color);
  207. type |= REDRAW_PARENT;
  208. pos.x += rect.x;
  209. pos.y += rect.y;
  210. pos.h = rect.h;
  211. pos.w = rect.w;
  212. assert(pos.w >= 40); //we need some space
  213. setText(Text);
  214. }
  215. void CTextBox::sliderMoved(int to)
  216. {
  217. label->scrollTextTo(to);
  218. }
  219. void CTextBox::resize(Point newSize)
  220. {
  221. pos.w = newSize.x;
  222. pos.h = newSize.y;
  223. label->pos.w = pos.w;
  224. label->pos.h = pos.h;
  225. if (slider)
  226. vstd::clear_pointer(slider); // will be recreated if needed later
  227. setText(label->getText()); // force refresh
  228. }
  229. void CTextBox::setText(const std::string &text)
  230. {
  231. label->pos.w = pos.w; // reset to default before textSize.y check
  232. label->setText(text);
  233. if(label->textSize.y <= label->pos.h && slider)
  234. {
  235. // slider is no longer needed
  236. vstd::clear_pointer(slider);
  237. }
  238. else if(slider)
  239. {
  240. // decrease width again if slider still used
  241. label->pos.w = pos.w - 32;
  242. label->setText(text);
  243. slider->setAmount(label->textSize.y);
  244. }
  245. else if(label->textSize.y > label->pos.h)
  246. {
  247. // create slider and update widget
  248. label->pos.w = pos.w - 32;
  249. label->setText(text);
  250. OBJ_CONSTRUCTION_CAPTURING_ALL;
  251. slider = new CSlider(Point(pos.w - 32, 0), pos.h, std::bind(&CTextBox::sliderMoved, this, _1),
  252. label->pos.h, label->textSize.y, 0, false, CSlider::EStyle(sliderStyle));
  253. slider->setScrollStep(graphics->fonts[label->font]->getLineHeight());
  254. }
  255. }
  256. void CGStatusBar::setText(const std::string & Text)
  257. {
  258. if(!textLock)
  259. CLabel::setText(Text);
  260. }
  261. void CGStatusBar::clear()
  262. {
  263. setText("");
  264. }
  265. CGStatusBar::CGStatusBar(CPicture *BG, EFonts Font /*= FONT_SMALL*/, EAlignment Align /*= CENTER*/, const SDL_Color &Color /*= Colors::WHITE*/)
  266. : CLabel(BG->pos.x, BG->pos.y, Font, Align, Color, "")
  267. {
  268. init();
  269. bg = BG;
  270. addChild(bg);
  271. pos = bg->pos;
  272. getBorderSize();
  273. textLock = false;
  274. }
  275. CGStatusBar::CGStatusBar(int x, int y, std::string name/*="ADROLLVR.bmp"*/, int maxw/*=-1*/)
  276. : CLabel(x, y, FONT_SMALL, CENTER)
  277. {
  278. OBJ_CONSTRUCTION_CAPTURING_ALL;
  279. init();
  280. bg = new CPicture(name);
  281. pos = bg->pos;
  282. if((unsigned int)maxw < pos.w)
  283. {
  284. vstd::amin(pos.w, maxw);
  285. bg->srcRect = new Rect(0, 0, maxw, pos.h);
  286. }
  287. textLock = false;
  288. }
  289. CGStatusBar::~CGStatusBar()
  290. {
  291. GH.statusbar = oldStatusBar;
  292. }
  293. void CGStatusBar::show(SDL_Surface * to)
  294. {
  295. showAll(to);
  296. }
  297. void CGStatusBar::init()
  298. {
  299. oldStatusBar = GH.statusbar;
  300. GH.statusbar = this;
  301. }
  302. Point CGStatusBar::getBorderSize()
  303. {
  304. //Width of borders where text should not be printed
  305. static const Point borderSize(5,1);
  306. switch(alignment)
  307. {
  308. case TOPLEFT: return Point(borderSize.x, borderSize.y);
  309. case CENTER: return Point(pos.w/2, pos.h/2);
  310. case BOTTOMRIGHT: return Point(pos.w - borderSize.x, pos.h - borderSize.y);
  311. }
  312. assert(0);
  313. return Point();
  314. }
  315. void CGStatusBar::lock(bool shouldLock)
  316. {
  317. textLock = shouldLock;
  318. }
  319. CTextInput::CTextInput(const Rect &Pos, EFonts font, const CFunctionList<void(const std::string &)> &CB):
  320. CLabel(Pos.x, Pos.y, font, CENTER),
  321. cb(CB)
  322. {
  323. type |= REDRAW_PARENT;
  324. focus = false;
  325. pos.h = Pos.h;
  326. pos.w = Pos.w;
  327. captureAllKeys = true;
  328. bg = nullptr;
  329. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  330. giveFocus();
  331. }
  332. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  333. :cb(CB)
  334. {
  335. focus = false;
  336. pos += Pos;
  337. captureAllKeys = true;
  338. OBJ_CONSTRUCTION;
  339. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  340. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  341. giveFocus();
  342. }
  343. CTextInput::CTextInput(const Rect &Pos, SDL_Surface *srf)
  344. {
  345. focus = false;
  346. pos += Pos;
  347. captureAllKeys = true;
  348. OBJ_CONSTRUCTION;
  349. bg = new CPicture(Pos, 0, true);
  350. Rect hlp = Pos;
  351. if(srf)
  352. CSDL_Ext::blitSurface(srf, &hlp, *bg, nullptr);
  353. else
  354. SDL_FillRect(*bg, nullptr, 0);
  355. pos.w = bg->pos.w;
  356. pos.h = bg->pos.h;
  357. bg->pos = pos;
  358. addUsedEvents(LCLICK | KEYBOARD | TEXTINPUT);
  359. giveFocus();
  360. }
  361. void CTextInput::focusGot()
  362. {
  363. CSDL_Ext::startTextInput(&pos);
  364. }
  365. void CTextInput::focusLost()
  366. {
  367. CSDL_Ext::stopTextInput();
  368. }
  369. std::string CTextInput::visibleText()
  370. {
  371. return focus ? text + newText + "_" : text;
  372. }
  373. void CTextInput::clickLeft( tribool down, bool previousState )
  374. {
  375. if(down && !focus)
  376. giveFocus();
  377. }
  378. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  379. {
  380. if(!focus || key.state != SDL_PRESSED)
  381. return;
  382. if(key.keysym.sym == SDLK_TAB)
  383. {
  384. moveFocus();
  385. GH.breakEventHandling();
  386. return;
  387. }
  388. bool redrawNeeded = false;
  389. switch(key.keysym.sym)
  390. {
  391. case SDLK_DELETE: // have index > ' ' so it won't be filtered out by default section
  392. return;
  393. case SDLK_BACKSPACE:
  394. if(!newText.empty())
  395. {
  396. Unicode::trimRight(newText);
  397. redrawNeeded = true;
  398. }
  399. else if(!text.empty())
  400. {
  401. Unicode::trimRight(text);
  402. redrawNeeded = true;
  403. }
  404. break;
  405. default:
  406. break;
  407. }
  408. if (redrawNeeded)
  409. {
  410. redraw();
  411. cb(text);
  412. }
  413. }
  414. void CTextInput::setText( const std::string &nText, bool callCb )
  415. {
  416. CLabel::setText(nText);
  417. if(callCb)
  418. cb(text);
  419. }
  420. bool CTextInput::captureThisEvent(const SDL_KeyboardEvent & key)
  421. {
  422. if(key.keysym.sym == SDLK_RETURN || key.keysym.sym == SDLK_KP_ENTER || key.keysym.sym == SDLK_ESCAPE)
  423. return false;
  424. return true;
  425. }
  426. void CTextInput::textInputed(const SDL_TextInputEvent & event)
  427. {
  428. if(!focus)
  429. return;
  430. std::string oldText = text;
  431. text += event.text;
  432. filters(text,oldText);
  433. if (text != oldText)
  434. {
  435. redraw();
  436. cb(text);
  437. }
  438. newText = "";
  439. }
  440. void CTextInput::textEdited(const SDL_TextEditingEvent & event)
  441. {
  442. if(!focus)
  443. return;
  444. newText = event.text;
  445. redraw();
  446. cb(text+newText);
  447. }
  448. void CTextInput::filenameFilter(std::string & text, const std::string &)
  449. {
  450. static const std::string forbiddenChars = "<>:\"/\\|?*\r\n"; //if we are entering a filename, some special characters won't be allowed
  451. size_t pos;
  452. while ((pos = text.find_first_of(forbiddenChars)) != std::string::npos)
  453. text.erase(pos, 1);
  454. }
  455. void CTextInput::numberFilter(std::string & text, const std::string & oldText, int minValue, int maxValue)
  456. {
  457. assert(minValue < maxValue);
  458. if (text.empty())
  459. text = "0";
  460. size_t pos = 0;
  461. if (text[0] == '-') //allow '-' sign as first symbol only
  462. pos++;
  463. while (pos < text.size())
  464. {
  465. if (text[pos] < '0' || text[pos] > '9')
  466. {
  467. text = oldText;
  468. return; //new text is not number.
  469. }
  470. pos++;
  471. }
  472. try
  473. {
  474. int value = boost::lexical_cast<int>(text);
  475. if (value < minValue)
  476. text = boost::lexical_cast<std::string>(minValue);
  477. else if (value > maxValue)
  478. text = boost::lexical_cast<std::string>(maxValue);
  479. }
  480. catch(boost::bad_lexical_cast &)
  481. {
  482. //Should never happen. Unless I missed some cases
  483. logGlobal->warnStream() << "Warning: failed to convert "<< text << " to number!";
  484. text = oldText;
  485. }
  486. }
  487. CFocusable::CFocusable()
  488. {
  489. focusables.push_back(this);
  490. }
  491. CFocusable::~CFocusable()
  492. {
  493. if(inputWithFocus == this)
  494. {
  495. focusLost();
  496. inputWithFocus = nullptr;
  497. }
  498. focusables -= this;
  499. }
  500. void CFocusable::giveFocus()
  501. {
  502. if(inputWithFocus)
  503. {
  504. inputWithFocus->focus = false;
  505. inputWithFocus->focusLost();
  506. inputWithFocus->redraw();
  507. }
  508. focus = true;
  509. inputWithFocus = this;
  510. focusGot();
  511. redraw();
  512. }
  513. void CFocusable::moveFocus()
  514. {
  515. auto i = vstd::find(focusables, this),
  516. ourIt = i;
  517. for(i++; i != ourIt; i++)
  518. {
  519. if(i == focusables.end())
  520. i = focusables.begin();
  521. if((*i)->active)
  522. {
  523. (*i)->giveFocus();
  524. break;;
  525. }
  526. }
  527. }