TextControls.cpp 17 KB

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