CComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 "../gui/CGuiHandler.h"
  13. #include "../gui/CCursorHandler.h"
  14. #include "../CMessage.h"
  15. #include "../CGameInfo.h"
  16. #include "../widgets/Images.h"
  17. #include "../widgets/CArtifactHolder.h"
  18. #include "../windows/CAdvmapInterface.h"
  19. #include "../../lib/CArtHandler.h"
  20. #include "../../lib/CTownHandler.h"
  21. #include "../../lib/CCreatureHandler.h"
  22. #include "../../lib/CSkillHandler.h"
  23. #include "../../lib/spells/CSpellHandler.h"
  24. #include "../../lib/CGeneralTextHandler.h"
  25. #include "../../lib/NetPacksBase.h"
  26. CComponent::CComponent(Etype Type, int Subtype, int Val, ESize imageSize)
  27. : perDay(false)
  28. {
  29. init(Type, Subtype, Val, imageSize);
  30. }
  31. CComponent::CComponent(const Component & c, ESize imageSize)
  32. : perDay(false)
  33. {
  34. if(c.id == Component::RESOURCE && c.when==-1)
  35. perDay = true;
  36. init((Etype)c.id, c.subtype, c.val, imageSize);
  37. }
  38. void CComponent::init(Etype Type, int Subtype, int Val, ESize imageSize)
  39. {
  40. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  41. addUsedEvents(RCLICK);
  42. compType = Type;
  43. subtype = Subtype;
  44. val = Val;
  45. size = imageSize;
  46. assert(compType < typeInvalid);
  47. assert(size < sizeInvalid);
  48. setSurface(getFileName()[size], getIndex());
  49. pos.w = image->pos.w;
  50. pos.h = image->pos.h;
  51. EFonts font = FONT_SMALL;
  52. if (imageSize < small)
  53. font = FONT_TINY; //other sizes?
  54. pos.h += 4; //distance between text and image
  55. std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(80, pos.w), font);
  56. for(auto & line : textLines)
  57. {
  58. int height = graphics->fonts[font]->getLineHeight();
  59. auto label = std::make_shared<CLabel>(pos.w/2, pos.h + height/2, font, CENTER, Colors::WHITE, line);
  60. pos.h += height;
  61. if(label->pos.w > pos.w)
  62. {
  63. pos.x -= (label->pos.w - pos.w)/2;
  64. pos.w = label->pos.w;
  65. }
  66. lines.push_back(label);
  67. }
  68. }
  69. const std::vector<std::string> CComponent::getFileName()
  70. {
  71. static const std::string primSkillsArr [] = {"PSKIL32", "PSKIL32", "PSKIL42", "PSKILL"};
  72. static const std::string secSkillsArr [] = {"SECSK32", "SECSK32", "SECSKILL", "SECSK82"};
  73. static const std::string resourceArr [] = {"SMALRES", "RESOURCE", "RESOUR82", "RESOUR82"};
  74. static const std::string creatureArr [] = {"CPRSMALL", "CPRSMALL", "TWCRPORT", "TWCRPORT"};
  75. static const std::string artifactArr[] = {"Artifact", "Artifact", "Artifact", "Artifact"};
  76. static const std::string spellsArr [] = {"SpellInt", "SpellInt", "SPELLSCR", "SPELLSCR"};
  77. static const std::string moraleArr [] = {"IMRL22", "IMRL30", "IMRL42", "imrl82"};
  78. static const std::string luckArr [] = {"ILCK22", "ILCK30", "ILCK42", "ilck82"};
  79. static const std::string heroArr [] = {"PortraitsSmall", "PortraitsSmall", "PortraitsLarge", "PortraitsLarge"};
  80. static const std::string flagArr [] = {"CREST58", "CREST58", "CREST58", "CREST58"};
  81. auto gen = [](const std::string * arr)
  82. {
  83. return std::vector<std::string>(arr, arr + 4);
  84. };
  85. switch(compType)
  86. {
  87. case primskill: return gen(primSkillsArr);
  88. case secskill: return gen(secSkillsArr);
  89. case resource: return gen(resourceArr);
  90. case creature: return gen(creatureArr);
  91. case artifact: return gen(artifactArr);
  92. case experience: return gen(primSkillsArr);
  93. case spell: return gen(spellsArr);
  94. case morale: return gen(moraleArr);
  95. case luck: return gen(luckArr);
  96. case building: return std::vector<std::string>(4, CGI->townh->factions[subtype]->town->clientInfo.buildingsIcons);
  97. case hero: return gen(heroArr);
  98. case flag: return gen(flagArr);
  99. }
  100. assert(0);
  101. return std::vector<std::string>();
  102. }
  103. size_t CComponent::getIndex()
  104. {
  105. switch(compType)
  106. {
  107. case primskill: return subtype;
  108. case secskill: return subtype*3 + 3 + val - 1;
  109. case resource: return subtype;
  110. case creature: return CGI->creh->creatures[subtype]->iconIndex;
  111. case artifact: return CGI->arth->artifacts[subtype]->iconIndex;
  112. case experience: return 4;
  113. case spell: return subtype;
  114. case morale: return val+3;
  115. case luck: return val+3;
  116. case building: return val;
  117. case hero: return subtype;
  118. case flag: return subtype;
  119. }
  120. assert(0);
  121. return 0;
  122. }
  123. std::string CComponent::getDescription()
  124. {
  125. switch(compType)
  126. {
  127. case primskill: return (subtype < 4)? CGI->generaltexth->arraytxt[2+subtype] //Primary skill
  128. : CGI->generaltexth->allTexts[149]; //mana
  129. case secskill: return CGI->skillh->skillInfo(subtype, val);
  130. case resource: return CGI->generaltexth->allTexts[242];
  131. case creature: return "";
  132. case artifact:
  133. {
  134. std::unique_ptr<CArtifactInstance> art;
  135. if (subtype != ArtifactID::SPELL_SCROLL)
  136. {
  137. art.reset(CArtifactInstance::createNewArtifactInstance(subtype));
  138. }
  139. else
  140. {
  141. art.reset(CArtifactInstance::createScroll(static_cast<SpellID>(val)));
  142. }
  143. return art->getEffectiveDescription();
  144. }
  145. case experience: return CGI->generaltexth->allTexts[241];
  146. case spell: return CGI->spellh->objects[subtype]->getLevelInfo(val).description;
  147. case morale: return CGI->generaltexth->heroscrn[ 4 - (val>0) + (val<0)];
  148. case luck: return CGI->generaltexth->heroscrn[ 7 - (val>0) + (val<0)];
  149. case building: return CGI->townh->factions[subtype]->town->buildings[BuildingID(val)]->Description();
  150. case hero: return "";
  151. case flag: return "";
  152. }
  153. assert(0);
  154. return "";
  155. }
  156. std::string CComponent::getSubtitle()
  157. {
  158. if(!perDay)
  159. return getSubtitleInternal();
  160. std::string ret = CGI->generaltexth->allTexts[3];
  161. boost::replace_first(ret, "%d", getSubtitleInternal());
  162. return ret;
  163. }
  164. std::string CComponent::getSubtitleInternal()
  165. {
  166. //FIXME: some of these are horrible (e.g creature)
  167. switch(compType)
  168. {
  169. case primskill: return boost::str(boost::format("%+d %s") % val % (subtype < 4 ? CGI->generaltexth->primarySkillNames[subtype] : CGI->generaltexth->allTexts[387]));
  170. case secskill: return CGI->generaltexth->levels[val-1] + "\n" + CGI->skillh->skillName(subtype);
  171. case resource: return boost::lexical_cast<std::string>(val);
  172. case creature: return (val? boost::lexical_cast<std::string>(val) + " " : "") + CGI->creh->creatures[subtype]->*(val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  173. case artifact: return CGI->arth->artifacts[subtype]->Name();
  174. case experience:
  175. {
  176. if(subtype == 1) //+1 level - tree of knowledge
  177. {
  178. std::string level = CGI->generaltexth->allTexts[442];
  179. boost::replace_first(level, "1", boost::lexical_cast<std::string>(val));
  180. return level;
  181. }
  182. else
  183. {
  184. return boost::lexical_cast<std::string>(val); //amount of experience OR level required for seer hut;
  185. }
  186. }
  187. case spell: return CGI->spellh->objects[subtype]->name;
  188. case morale: return "";
  189. case luck: return "";
  190. case building:
  191. {
  192. auto building = CGI->townh->factions[subtype]->town->buildings[BuildingID(val)];
  193. if(!building)
  194. {
  195. logGlobal->error("Town of faction %s has no building #%d", CGI->townh->factions[subtype]->town->faction->name, val);
  196. return (boost::format("Missing building #%d") % val).str();
  197. }
  198. return building->Name();
  199. }
  200. case hero: return "";
  201. case flag: return CGI->generaltexth->capColors[subtype];
  202. }
  203. assert(0);
  204. return "";
  205. }
  206. void CComponent::setSurface(std::string defName, int imgPos)
  207. {
  208. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  209. image = std::make_shared<CAnimImage>(defName, imgPos);
  210. }
  211. void CComponent::clickRight(tribool down, bool previousState)
  212. {
  213. if(!getDescription().empty())
  214. adventureInt->handleRightClick(getDescription(), down);
  215. }
  216. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  217. {
  218. if (down)
  219. {
  220. if(onSelect)
  221. onSelect();
  222. }
  223. }
  224. void CSelectableComponent::init()
  225. {
  226. selected = false;
  227. }
  228. CSelectableComponent::CSelectableComponent(const Component &c, std::function<void()> OnSelect):
  229. CComponent(c),onSelect(OnSelect)
  230. {
  231. type |= REDRAW_PARENT;
  232. addUsedEvents(LCLICK | KEYBOARD);
  233. init();
  234. }
  235. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize, std::function<void()> OnSelect):
  236. CComponent(Type,Sub,Val, imageSize),onSelect(OnSelect)
  237. {
  238. type |= REDRAW_PARENT;
  239. addUsedEvents(LCLICK | KEYBOARD);
  240. init();
  241. }
  242. void CSelectableComponent::select(bool on)
  243. {
  244. if(on != selected)
  245. {
  246. selected = on;
  247. redraw();
  248. }
  249. }
  250. void CSelectableComponent::showAll(SDL_Surface * to)
  251. {
  252. CComponent::showAll(to);
  253. if(selected)
  254. {
  255. CSDL_Ext::drawBorder(to, Rect::around(image->pos), int3(239,215,123));
  256. }
  257. }
  258. void CComponentBox::selectionChanged(std::shared_ptr<CSelectableComponent> newSelection)
  259. {
  260. if (newSelection == selected)
  261. return;
  262. if (selected)
  263. selected->select(false);
  264. selected = newSelection;
  265. if (onSelect)
  266. onSelect(selectedIndex());
  267. if (selected)
  268. selected->select(true);
  269. }
  270. int CComponentBox::selectedIndex()
  271. {
  272. if (selected)
  273. return std::find(components.begin(), components.end(), selected) - components.begin();
  274. return -1;
  275. }
  276. Point CComponentBox::getOrTextPos(CComponent *left, CComponent *right)
  277. {
  278. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  279. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  280. int fullDistance = getDistance(left, right) + leftSubtitle + rightSubtitle;
  281. return Point(fullDistance/2 - leftSubtitle, (left->image->pos.h + right->image->pos.h) / 4);
  282. }
  283. int CComponentBox::getDistance(CComponent *left, CComponent *right)
  284. {
  285. static const int betweenImagesMin = 20;
  286. static const int betweenSubtitlesMin = 10;
  287. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  288. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  289. int subtitlesOffset = leftSubtitle + rightSubtitle;
  290. return std::max(betweenSubtitlesMin, betweenImagesMin - subtitlesOffset);
  291. }
  292. void CComponentBox::placeComponents(bool selectable)
  293. {
  294. static const int betweenRows = 22;
  295. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  296. if (components.empty())
  297. return;
  298. //prepare components
  299. for(auto & comp : components)
  300. {
  301. addChild(comp.get());
  302. comp->moveTo(Point(pos.x, pos.y));
  303. }
  304. struct RowData
  305. {
  306. size_t comps;
  307. int width;
  308. int height;
  309. RowData (size_t Comps, int Width, int Height):
  310. comps(Comps), width (Width), height (Height){};
  311. };
  312. std::vector<RowData> rows;
  313. rows.push_back (RowData (0,0,0));
  314. //split components in rows
  315. std::shared_ptr<CComponent> prevComp;
  316. for(std::shared_ptr<CComponent> comp : components)
  317. {
  318. //make sure that components are smaller than our width
  319. //assert(pos.w == 0 || pos.w < comp->pos.w);
  320. const int distance = prevComp ? getDistance(prevComp.get(), comp.get()) : 0;
  321. //start next row
  322. if ((pos.w != 0 && rows.back().width + comp->pos.w + distance > pos.w) // row is full
  323. || rows.back().comps >= 4) // no more than 4 comps per row
  324. {
  325. prevComp = nullptr;
  326. rows.push_back (RowData (0,0,0));
  327. }
  328. if (prevComp)
  329. rows.back().width += distance;
  330. rows.back().comps++;
  331. rows.back().width += comp->pos.w;
  332. vstd::amax(rows.back().height, comp->pos.h);
  333. prevComp = comp;
  334. }
  335. if (pos.w == 0)
  336. {
  337. for(auto & row : rows)
  338. vstd::amax(pos.w, row.width);
  339. }
  340. int height = (rows.size() - 1) * betweenRows;
  341. for(auto & row : rows)
  342. height += row.height;
  343. //assert(pos.h == 0 || pos.h < height);
  344. if (pos.h == 0)
  345. pos.h = height;
  346. auto iter = components.begin();
  347. int currentY = (pos.h - height) / 2;
  348. //move components to their positions
  349. for (auto & rows_row : rows)
  350. {
  351. // amount of free space we may add on each side of every component
  352. int freeSpace = (pos.w - rows_row.width) / (rows_row.comps * 2);
  353. prevComp = nullptr;
  354. int currentX = 0;
  355. for (size_t col = 0; col < rows_row.comps; col++)
  356. {
  357. currentX += freeSpace;
  358. if (prevComp)
  359. {
  360. if (selectable)
  361. {
  362. Point orPos = Point(currentX - freeSpace, currentY) + getOrTextPos(prevComp.get(), iter->get());
  363. orLabels.push_back(std::make_shared<CLabel>(orPos.x, orPos.y, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[4]));
  364. }
  365. currentX += getDistance(prevComp.get(), iter->get());
  366. }
  367. (*iter)->moveBy(Point(currentX, currentY));
  368. currentX += (*iter)->pos.w;
  369. currentX += freeSpace;
  370. prevComp = *(iter++);
  371. }
  372. currentY += rows_row.height + betweenRows;
  373. }
  374. }
  375. CComponentBox::CComponentBox(std::vector<std::shared_ptr<CComponent>> _components, Rect position):
  376. components(_components)
  377. {
  378. type |= REDRAW_PARENT;
  379. pos = position + pos;
  380. placeComponents(false);
  381. }
  382. CComponentBox::CComponentBox(std::vector<std::shared_ptr<CSelectableComponent>> _components, Rect position, std::function<void(int newID)> _onSelect):
  383. components(_components.begin(), _components.end()),
  384. onSelect(_onSelect)
  385. {
  386. type |= REDRAW_PARENT;
  387. pos = position + pos;
  388. placeComponents(true);
  389. assert(!components.empty());
  390. int key = SDLK_1;
  391. for(auto & comp : _components)
  392. {
  393. comp->onSelect = std::bind(&CComponentBox::selectionChanged, this, comp);
  394. comp->assignedKeys.insert(key++);
  395. }
  396. selectionChanged(_components.front());
  397. }