TextControls.cpp 17 KB

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