CComponent.cpp 19 KB

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