CComponent.cpp 13 KB

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