CComponent.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * CComponent.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 "CComponent.h"
  12. #include "Images.h"
  13. #include <vcmi/spells/Service.h>
  14. #include <vcmi/spells/Spell.h>
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/CursorHandler.h"
  17. #include "../gui/TextAlignment.h"
  18. #include "../gui/Shortcut.h"
  19. #include "../render/Canvas.h"
  20. #include "../render/IFont.h"
  21. #include "../render/IRenderHandler.h"
  22. #include "../render/Graphics.h"
  23. #include "../windows/CMessage.h"
  24. #include "../windows/InfoWindows.h"
  25. #include "../widgets/TextControls.h"
  26. #include "../CGameInfo.h"
  27. #include "../../lib/ArtifactUtils.h"
  28. #include "../../lib/CHeroHandler.h"
  29. #include "../../lib/entities/building/CBuilding.h"
  30. #include "../../lib/entities/faction/CFaction.h"
  31. #include "../../lib/entities/faction/CTown.h"
  32. #include "../../lib/entities/faction/CTownHandler.h"
  33. #include "../../lib/networkPacks/Component.h"
  34. #include "../../lib/spells/CSpellHandler.h"
  35. #include "../../lib/CCreatureHandler.h"
  36. #include "../../lib/CSkillHandler.h"
  37. #include "../../lib/texts/CGeneralTextHandler.h"
  38. #include "../../lib/CArtHandler.h"
  39. #include "../../lib/CArtifactInstance.h"
  40. CComponent::CComponent(ComponentType Type, ComponentSubType Subtype, std::optional<int32_t> Val, ESize imageSize, EFonts font)
  41. {
  42. init(Type, Subtype, Val, imageSize, font, "");
  43. }
  44. CComponent::CComponent(ComponentType Type, ComponentSubType Subtype, const std::string & Val, ESize imageSize, EFonts font)
  45. {
  46. init(Type, Subtype, std::nullopt, imageSize, font, Val);
  47. }
  48. CComponent::CComponent(const Component & c, ESize imageSize, EFonts font)
  49. {
  50. init(c.type, c.subType, c.value, imageSize, font, "");
  51. }
  52. void CComponent::init(ComponentType Type, ComponentSubType Subtype, std::optional<int32_t> Val, ESize imageSize, EFonts fnt, const std::string & ValText)
  53. {
  54. OBJECT_CONSTRUCTION;
  55. addUsedEvents(SHOW_POPUP);
  56. data.type = Type;
  57. data.subType = Subtype;
  58. data.value = Val;
  59. customSubtitle = ValText;
  60. size = imageSize;
  61. font = fnt;
  62. assert(size < sizeInvalid);
  63. setSurface(getFileName()[size], (int)getIndex());
  64. pos.w = image->pos.w;
  65. pos.h = image->pos.h;
  66. if (imageSize < small)
  67. font = FONT_TINY; //for tiny images - tiny font
  68. pos.h += 4; //distance between text and image
  69. // WARNING: too low values will lead to bad line-breaks in CPlayerOptionTooltipBox - check right-click on starting town in pregame
  70. int max = 80;
  71. if (size < large)
  72. max = 72;
  73. if (size < medium)
  74. max = 60;
  75. if (size < small)
  76. max = 55;
  77. if(Type == ComponentType::RESOURCE && !ValText.empty())
  78. max = 80;
  79. std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(max, pos.w), font);
  80. const auto & fontPtr = GH.renderHandler().loadFont(font);
  81. const int height = static_cast<int>(fontPtr->getLineHeight());
  82. for(auto & line : textLines)
  83. {
  84. auto label = std::make_shared<CLabel>(pos.w/2, pos.h + height/2, font, ETextAlignment::CENTER, Colors::WHITE, line);
  85. pos.h += height;
  86. if(label->pos.w > pos.w)
  87. {
  88. pos.x -= (label->pos.w - pos.w)/2;
  89. pos.w = label->pos.w;
  90. }
  91. lines.push_back(label);
  92. }
  93. }
  94. std::vector<AnimationPath> CComponent::getFileName() const
  95. {
  96. static const std::array<std::string, 4> primSkillsArr = {"PSKIL32", "PSKIL32", "PSKIL42", "PSKILL"};
  97. static const std::array<std::string, 4> secSkillsArr = {"SECSK32", "SECSK32", "SECSKILL", "SECSK82"};
  98. static const std::array<std::string, 4> resourceArr = {"SMALRES", "RESOURCE", "RESOURCE", "RESOUR82"};
  99. static const std::array<std::string, 4> creatureArr = {"CPRSMALL", "CPRSMALL", "CPRSMALL", "TWCRPORT"};
  100. static const std::array<std::string, 4> artifactArr = {"Artifact", "Artifact", "Artifact", "Artifact"};
  101. static const std::array<std::string, 4> spellsArr = {"SpellInt", "SpellInt", "SpellInt", "SPELLSCR"};
  102. static const std::array<std::string, 4> moraleArr = {"IMRL22", "IMRL30", "IMRL42", "imrl82"};
  103. static const std::array<std::string, 4> luckArr = {"ILCK22", "ILCK30", "ILCK42", "ilck82"};
  104. static const std::array<std::string, 4> heroArr = {"PortraitsSmall", "PortraitsSmall", "PortraitsSmall", "PortraitsLarge"};
  105. static const std::array<std::string, 4> flagArr = {"CREST58", "CREST58", "CREST58", "CREST58"};
  106. auto gen = [](const std::array<std::string, 4> & arr) -> std::vector<AnimationPath>
  107. {
  108. return { AnimationPath::builtin(arr[0]), AnimationPath::builtin(arr[1]), AnimationPath::builtin(arr[2]), AnimationPath::builtin(arr[3]) };
  109. };
  110. switch(data.type)
  111. {
  112. case ComponentType::PRIM_SKILL:
  113. case ComponentType::EXPERIENCE:
  114. case ComponentType::MANA:
  115. case ComponentType::LEVEL:
  116. return gen(primSkillsArr);
  117. case ComponentType::NONE:
  118. case ComponentType::SEC_SKILL:
  119. return gen(secSkillsArr);
  120. case ComponentType::RESOURCE:
  121. case ComponentType::RESOURCE_PER_DAY:
  122. return gen(resourceArr);
  123. case ComponentType::CREATURE:
  124. return gen(creatureArr);
  125. case ComponentType::ARTIFACT:
  126. return gen(artifactArr);
  127. case ComponentType::SPELL_SCROLL:
  128. case ComponentType::SPELL:
  129. return gen(spellsArr);
  130. case ComponentType::MORALE:
  131. return gen(moraleArr);
  132. case ComponentType::LUCK:
  133. return gen(luckArr);
  134. case ComponentType::BUILDING:
  135. return std::vector<AnimationPath>(4, (*CGI->townh)[data.subType.as<BuildingTypeUniqueID>().getFaction()]->town->clientInfo.buildingsIcons);
  136. case ComponentType::HERO_PORTRAIT:
  137. return gen(heroArr);
  138. case ComponentType::FLAG:
  139. return gen(flagArr);
  140. default:
  141. assert(0);
  142. return {};
  143. }
  144. }
  145. size_t CComponent::getIndex() const
  146. {
  147. switch(data.type)
  148. {
  149. case ComponentType::NONE:
  150. return 0;
  151. case ComponentType::PRIM_SKILL:
  152. return data.subType.getNum();
  153. case ComponentType::EXPERIENCE:
  154. case ComponentType::LEVEL:
  155. return 4; // for whatever reason, in H3 experience icon is located in primary skills icons
  156. case ComponentType::MANA:
  157. return 5; // for whatever reason, in H3 mana points icon is located in primary skills icons
  158. case ComponentType::SEC_SKILL:
  159. return data.subType.getNum() * 3 + 3 + data.value.value_or(0) - 1;
  160. case ComponentType::RESOURCE:
  161. case ComponentType::RESOURCE_PER_DAY:
  162. return data.subType.getNum();
  163. case ComponentType::CREATURE:
  164. return CGI->creatures()->getById(data.subType.as<CreatureID>())->getIconIndex();
  165. case ComponentType::ARTIFACT:
  166. return CGI->artifacts()->getById(data.subType.as<ArtifactID>())->getIconIndex();
  167. case ComponentType::SPELL_SCROLL:
  168. case ComponentType::SPELL:
  169. return (size < large) ? data.subType.getNum() + 1 : data.subType.getNum();
  170. case ComponentType::MORALE:
  171. return data.value.value_or(0) + 3;
  172. case ComponentType::LUCK:
  173. return data.value.value_or(0) + 3;
  174. case ComponentType::BUILDING:
  175. return data.subType.as<BuildingTypeUniqueID>().getBuilding();
  176. case ComponentType::HERO_PORTRAIT:
  177. return CGI->heroTypes()->getById(data.subType.as<HeroTypeID>())->getIconIndex();
  178. case ComponentType::FLAG:
  179. return data.subType.getNum();
  180. default:
  181. assert(0);
  182. return 0;
  183. }
  184. }
  185. std::string CComponent::getDescription() const
  186. {
  187. switch(data.type)
  188. {
  189. case ComponentType::PRIM_SKILL:
  190. return CGI->generaltexth->arraytxt[2+data.subType.getNum()];
  191. case ComponentType::EXPERIENCE:
  192. case ComponentType::LEVEL:
  193. return CGI->generaltexth->allTexts[241];
  194. case ComponentType::MANA:
  195. return CGI->generaltexth->allTexts[149];
  196. case ComponentType::SEC_SKILL:
  197. return CGI->skillh->getByIndex(data.subType.getNum())->getDescriptionTranslated(data.value.value_or(0));
  198. case ComponentType::RESOURCE:
  199. case ComponentType::RESOURCE_PER_DAY:
  200. return CGI->generaltexth->allTexts[242];
  201. case ComponentType::NONE:
  202. case ComponentType::CREATURE:
  203. return "";
  204. case ComponentType::ARTIFACT:
  205. return CGI->artifacts()->getById(data.subType.as<ArtifactID>())->getDescriptionTranslated();
  206. case ComponentType::SPELL_SCROLL:
  207. {
  208. auto description = ArtifactID(ArtifactID::SPELL_SCROLL).toEntity(CGI)->getDescriptionTranslated();
  209. ArtifactUtils::insertScrrollSpellName(description, data.subType.as<SpellID>());
  210. return description;
  211. }
  212. case ComponentType::SPELL:
  213. return CGI->spells()->getById(data.subType.as<SpellID>())->getDescriptionTranslated(std::max(0, data.value.value_or(0)));
  214. case ComponentType::MORALE:
  215. return CGI->generaltexth->heroscrn[ 4 - (data.value.value_or(0)>0) + (data.value.value_or(0)<0)];
  216. case ComponentType::LUCK:
  217. return CGI->generaltexth->heroscrn[ 7 - (data.value.value_or(0)>0) + (data.value.value_or(0)<0)];
  218. case ComponentType::BUILDING:
  219. {
  220. auto index = data.subType.as<BuildingTypeUniqueID>();
  221. return (*CGI->townh)[index.getFaction()]->town->buildings[index.getBuilding()]->getDescriptionTranslated();
  222. }
  223. case ComponentType::HERO_PORTRAIT:
  224. return "";
  225. case ComponentType::FLAG:
  226. return "";
  227. default:
  228. assert(0);
  229. return "";
  230. }
  231. }
  232. std::string CComponent::getSubtitle() const
  233. {
  234. if (!customSubtitle.empty())
  235. return customSubtitle;
  236. switch(data.type)
  237. {
  238. case ComponentType::PRIM_SKILL:
  239. if (data.value)
  240. return boost::str(boost::format("%+d %s") % data.value.value_or(0) % CGI->generaltexth->primarySkillNames[data.subType.getNum()]);
  241. else
  242. return CGI->generaltexth->primarySkillNames[data.subType.getNum()];
  243. case ComponentType::EXPERIENCE:
  244. return std::to_string(data.value.value_or(0));
  245. case ComponentType::LEVEL:
  246. {
  247. std::string level = CGI->generaltexth->allTexts[442];
  248. boost::replace_first(level, "1", std::to_string(data.value.value_or(0)));
  249. return level;
  250. }
  251. case ComponentType::MANA:
  252. return boost::str(boost::format("%+d %s") % data.value.value_or(0) % CGI->generaltexth->allTexts[387]);
  253. case ComponentType::SEC_SKILL:
  254. return CGI->generaltexth->levels[data.value.value_or(0)-1] + "\n" + CGI->skillh->getById(data.subType.as<SecondarySkill>())->getNameTranslated();
  255. case ComponentType::RESOURCE:
  256. return std::to_string(data.value.value_or(0));
  257. case ComponentType::RESOURCE_PER_DAY:
  258. return boost::str(boost::format(CGI->generaltexth->allTexts[3]) % data.value.value_or(0));
  259. case ComponentType::CREATURE:
  260. {
  261. auto creature = CGI->creh->getById(data.subType.as<CreatureID>());
  262. if(data.value)
  263. return std::to_string(*data.value) + " " + (*data.value > 1 ? creature->getNamePluralTranslated() : creature->getNameSingularTranslated());
  264. else
  265. return creature->getNamePluralTranslated();
  266. }
  267. case ComponentType::ARTIFACT:
  268. return CGI->artifacts()->getById(data.subType.as<ArtifactID>())->getNameTranslated();
  269. case ComponentType::SPELL_SCROLL:
  270. case ComponentType::SPELL:
  271. if (data.value.value_or(0) < 0)
  272. return "{#A9A9A9|" + CGI->spells()->getById(data.subType.as<SpellID>())->getNameTranslated() + "}";
  273. else
  274. return CGI->spells()->getById(data.subType.as<SpellID>())->getNameTranslated();
  275. case ComponentType::NONE:
  276. case ComponentType::MORALE:
  277. case ComponentType::LUCK:
  278. case ComponentType::HERO_PORTRAIT:
  279. return "";
  280. case ComponentType::BUILDING:
  281. {
  282. auto index = data.subType.as<BuildingTypeUniqueID>();
  283. auto building = (*CGI->townh)[index.getFaction()]->town->buildings[index.getBuilding()];
  284. if(!building)
  285. {
  286. logGlobal->error("Town of faction %s has no building #%d", (*CGI->townh)[index.getFaction()]->town->faction->getNameTranslated(), index.getBuilding().getNum());
  287. return (boost::format("Missing building #%d") % index.getBuilding().getNum()).str();
  288. }
  289. return building->getNameTranslated();
  290. }
  291. case ComponentType::FLAG:
  292. return CGI->generaltexth->capColors[data.subType.as<PlayerColor>().getNum()];
  293. default:
  294. assert(0);
  295. return "";
  296. }
  297. }
  298. void CComponent::setSurface(const AnimationPath & defName, int imgPos)
  299. {
  300. OBJECT_CONSTRUCTION;
  301. image = std::make_shared<CAnimImage>(defName, imgPos);
  302. }
  303. void CComponent::showPopupWindow(const Point & cursorPosition)
  304. {
  305. if(!getDescription().empty())
  306. CRClickPopup::createAndPush(getDescription());
  307. }
  308. void CSelectableComponent::clickPressed(const Point & cursorPosition)
  309. {
  310. if(onSelect)
  311. onSelect();
  312. }
  313. void CSelectableComponent::clickDouble(const Point & cursorPosition)
  314. {
  315. if (!selected)
  316. {
  317. clickPressed(cursorPosition);
  318. }
  319. else
  320. {
  321. if (onChoose)
  322. onChoose();
  323. }
  324. }
  325. void CSelectableComponent::init()
  326. {
  327. selected = false;
  328. }
  329. CSelectableComponent::CSelectableComponent(const Component &c, std::function<void()> OnSelect):
  330. CComponent(c),onSelect(OnSelect)
  331. {
  332. setRedrawParent(true);
  333. addUsedEvents(LCLICK | DOUBLECLICK | KEYBOARD);
  334. init();
  335. }
  336. CSelectableComponent::CSelectableComponent(ComponentType Type, ComponentSubType Sub, int Val, ESize imageSize, std::function<void()> OnSelect):
  337. CComponent(Type,Sub,Val, imageSize),onSelect(OnSelect)
  338. {
  339. setRedrawParent(true);
  340. addUsedEvents(LCLICK | DOUBLECLICK | KEYBOARD);
  341. init();
  342. }
  343. void CSelectableComponent::select(bool on)
  344. {
  345. if(on != selected)
  346. {
  347. selected = on;
  348. redraw();
  349. }
  350. }
  351. void CSelectableComponent::showAll(Canvas & to)
  352. {
  353. CComponent::showAll(to);
  354. if(selected)
  355. {
  356. to.drawBorder(Rect::createAround(image->pos, 1), Colors::BRIGHT_YELLOW);
  357. }
  358. }
  359. void CComponentBox::selectionChanged(std::shared_ptr<CSelectableComponent> newSelection)
  360. {
  361. if (newSelection == selected)
  362. return;
  363. if (selected)
  364. selected->select(false);
  365. selected = newSelection;
  366. if (onSelect)
  367. onSelect(selectedIndex());
  368. if (selected)
  369. selected->select(true);
  370. }
  371. int CComponentBox::selectedIndex()
  372. {
  373. if (selected)
  374. return static_cast<int>(std::find(components.begin(), components.end(), selected) - components.begin());
  375. return -1;
  376. }
  377. Point CComponentBox::getOrTextPos(CComponent *left, CComponent *right)
  378. {
  379. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  380. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  381. int fullDistance = getDistance(left, right) + leftSubtitle + rightSubtitle;
  382. return Point(fullDistance/2 - leftSubtitle, (left->image->pos.h + right->image->pos.h) / 4);
  383. }
  384. int CComponentBox::getDistance(CComponent *left, CComponent *right)
  385. {
  386. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  387. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  388. int subtitlesOffset = leftSubtitle + rightSubtitle;
  389. return std::max(betweenSubtitlesMin, betweenImagesMin - subtitlesOffset);
  390. }
  391. void CComponentBox::placeComponents(bool selectable)
  392. {
  393. OBJECT_CONSTRUCTION;
  394. if (components.empty())
  395. return;
  396. //prepare components
  397. for(auto & comp : components)
  398. {
  399. addChild(comp.get());
  400. comp->moveTo(Point(pos.x, pos.y));
  401. }
  402. struct RowData
  403. {
  404. size_t comps;
  405. int width;
  406. int height;
  407. RowData (size_t Comps, int Width, int Height):
  408. comps(Comps), width (Width), height (Height){};
  409. };
  410. std::vector<RowData> rows;
  411. rows.push_back (RowData (0,0,0));
  412. //split components in rows
  413. std::shared_ptr<CComponent> prevComp;
  414. for(std::shared_ptr<CComponent> comp : components)
  415. {
  416. //make sure that components are smaller than our width
  417. //assert(pos.w == 0 || pos.w < comp->pos.w);
  418. const int distance = prevComp ? getDistance(prevComp.get(), comp.get()) : 0;
  419. //start next row
  420. if ((pos.w != 0 && rows.back().width + comp->pos.w + distance > pos.w) // row is full
  421. || rows.back().comps >= componentsInRow)
  422. {
  423. prevComp = nullptr;
  424. rows.push_back (RowData (0,0,0));
  425. }
  426. if (prevComp)
  427. rows.back().width += distance;
  428. rows.back().comps++;
  429. rows.back().width += comp->pos.w;
  430. vstd::amax(rows.back().height, comp->pos.h);
  431. prevComp = comp;
  432. }
  433. if (pos.w == 0)
  434. {
  435. for(auto & row : rows)
  436. vstd::amax(pos.w, row.width);
  437. }
  438. int height = ((int)rows.size() - 1) * betweenRows;
  439. for(auto & row : rows)
  440. height += row.height;
  441. //assert(pos.h == 0 || pos.h < height);
  442. if (pos.h == 0)
  443. pos.h = height;
  444. auto iter = components.begin();
  445. int currentY = (pos.h - height) / 2;
  446. //move components to their positions
  447. for (auto & rows_row : rows)
  448. {
  449. // amount of free space we may add on each side of every component
  450. int freeSpace = (pos.w - rows_row.width) / ((int)rows_row.comps * 2);
  451. prevComp = nullptr;
  452. int currentX = 0;
  453. for (size_t col = 0; col < rows_row.comps; col++)
  454. {
  455. currentX += freeSpace;
  456. if (prevComp)
  457. {
  458. if (selectable)
  459. {
  460. Point orPos = Point(currentX - freeSpace, currentY) + getOrTextPos(prevComp.get(), iter->get());
  461. orLabels.push_back(std::make_shared<CLabel>(orPos.x, orPos.y, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[4]));
  462. }
  463. currentX += getDistance(prevComp.get(), iter->get());
  464. }
  465. (*iter)->moveBy(Point(currentX, currentY));
  466. currentX += (*iter)->pos.w;
  467. currentX += freeSpace;
  468. prevComp = *(iter++);
  469. }
  470. currentY += rows_row.height + betweenRows;
  471. }
  472. }
  473. CComponentBox::CComponentBox(std::vector<std::shared_ptr<CComponent>> _components, Rect position):
  474. CComponentBox(_components, position, defaultBetweenImagesMin, defaultBetweenSubtitlesMin, defaultBetweenRows, defaultComponentsInRow)
  475. {
  476. }
  477. CComponentBox::CComponentBox(std::vector<std::shared_ptr<CComponent>> _components, Rect position, int betweenImagesMin, int betweenSubtitlesMin, int betweenRows, int componentsInRow):
  478. components(_components),
  479. betweenImagesMin(betweenImagesMin),
  480. betweenSubtitlesMin(betweenSubtitlesMin),
  481. betweenRows(betweenRows),
  482. componentsInRow(componentsInRow)
  483. {
  484. setRedrawParent(true);
  485. pos = position + pos.topLeft();
  486. placeComponents(false);
  487. }
  488. CComponentBox::CComponentBox(std::vector<std::shared_ptr<CSelectableComponent>> _components, Rect position, std::function<void(int newID)> _onSelect):
  489. CComponentBox(_components, position, _onSelect, defaultBetweenImagesMin, defaultBetweenSubtitlesMin, defaultBetweenRows, defaultComponentsInRow)
  490. {
  491. }
  492. CComponentBox::CComponentBox(std::vector<std::shared_ptr<CSelectableComponent>> _components, Rect position, std::function<void(int newID)> _onSelect, int betweenImagesMin, int betweenSubtitlesMin, int betweenRows, int componentsInRow):
  493. components(_components.begin(), _components.end()),
  494. onSelect(_onSelect),
  495. betweenImagesMin(betweenImagesMin),
  496. betweenSubtitlesMin(betweenSubtitlesMin),
  497. betweenRows(betweenRows),
  498. componentsInRow(componentsInRow)
  499. {
  500. setRedrawParent(true);
  501. pos = position + pos.topLeft();
  502. placeComponents(true);
  503. assert(!components.empty());
  504. auto key = EShortcut::SELECT_INDEX_1;
  505. for(auto & comp : _components)
  506. {
  507. comp->onSelect = std::bind(&CComponentBox::selectionChanged, this, comp);
  508. comp->assignedKey = key;
  509. key = vstd::next(key, 1);
  510. }
  511. selectionChanged(_components.front());
  512. }