CComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 "CArtifactHolder.h"
  13. #include "Images.h"
  14. #include <vcmi/spells/Service.h>
  15. #include <vcmi/spells/Spell.h>
  16. #include "../gui/CGuiHandler.h"
  17. #include "../gui/CursorHandler.h"
  18. #include "../CMessage.h"
  19. #include "../CGameInfo.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, ETextAlignment::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. auto artID = ArtifactID(subtype);
  136. std::unique_ptr<CArtifactInstance> art;
  137. if (artID != ArtifactID::SPELL_SCROLL)
  138. {
  139. art.reset(CArtifactInstance::createNewArtifactInstance(artID));
  140. }
  141. else
  142. {
  143. art.reset(CArtifactInstance::createScroll(SpellID(val)));
  144. }
  145. return art->getEffectiveDescription();
  146. }
  147. case experience: return CGI->generaltexth->allTexts[241];
  148. case spell: return SpellID(subtype).toSpell(CGI->spells())->getLevelDescription(val);
  149. case morale: return CGI->generaltexth->heroscrn[ 4 - (val>0) + (val<0)];
  150. case luck: return CGI->generaltexth->heroscrn[ 7 - (val>0) + (val<0)];
  151. case building: return (*CGI->townh)[subtype]->town->buildings[BuildingID(val)]->Description();
  152. case hero: return "";
  153. case flag: return "";
  154. }
  155. assert(0);
  156. return "";
  157. }
  158. std::string CComponent::getSubtitle()
  159. {
  160. if(!perDay)
  161. return getSubtitleInternal();
  162. std::string ret = CGI->generaltexth->allTexts[3];
  163. boost::replace_first(ret, "%d", getSubtitleInternal());
  164. return ret;
  165. }
  166. std::string CComponent::getSubtitleInternal()
  167. {
  168. //FIXME: some of these are horrible (e.g creature)
  169. switch(compType)
  170. {
  171. case primskill: return boost::str(boost::format("%+d %s") % val % (subtype < 4 ? CGI->generaltexth->primarySkillNames[subtype] : CGI->generaltexth->allTexts[387]));
  172. case secskill: return CGI->generaltexth->levels[val-1] + "\n" + CGI->skillh->skillName(subtype);
  173. case resource: return boost::lexical_cast<std::string>(val);
  174. case creature: return (val? boost::lexical_cast<std::string>(val) + " " : "") + CGI->creh->objects[subtype]->*(val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  175. case artifact: return CGI->artifacts()->getByIndex(subtype)->getName();
  176. case experience:
  177. {
  178. if(subtype == 1) //+1 level - tree of knowledge
  179. {
  180. std::string level = CGI->generaltexth->allTexts[442];
  181. boost::replace_first(level, "1", boost::lexical_cast<std::string>(val));
  182. return level;
  183. }
  184. else
  185. {
  186. return boost::lexical_cast<std::string>(val); //amount of experience OR level required for seer hut;
  187. }
  188. }
  189. case spell: return CGI->spells()->getByIndex(subtype)->getName();
  190. case morale: return "";
  191. case luck: return "";
  192. case building:
  193. {
  194. auto building = (*CGI->townh)[subtype]->town->buildings[BuildingID(val)];
  195. if(!building)
  196. {
  197. logGlobal->error("Town of faction %s has no building #%d", (*CGI->townh)[subtype]->town->faction->name, val);
  198. return (boost::format("Missing building #%d") % val).str();
  199. }
  200. return building->Name();
  201. }
  202. case hero: return "";
  203. case flag: return CGI->generaltexth->capColors[subtype];
  204. }
  205. logGlobal->error("Invalid CComponent type: %d", (int)compType);
  206. return "";
  207. }
  208. void CComponent::setSurface(std::string defName, int imgPos)
  209. {
  210. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  211. image = std::make_shared<CAnimImage>(defName, imgPos);
  212. }
  213. void CComponent::clickRight(tribool down, bool previousState)
  214. {
  215. if(!getDescription().empty())
  216. adventureInt->handleRightClick(getDescription(), down);
  217. }
  218. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  219. {
  220. if (down)
  221. {
  222. if(onSelect)
  223. onSelect();
  224. }
  225. }
  226. void CSelectableComponent::init()
  227. {
  228. selected = false;
  229. }
  230. CSelectableComponent::CSelectableComponent(const Component &c, std::function<void()> OnSelect):
  231. CComponent(c),onSelect(OnSelect)
  232. {
  233. type |= REDRAW_PARENT;
  234. addUsedEvents(LCLICK | KEYBOARD);
  235. init();
  236. }
  237. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize, std::function<void()> OnSelect):
  238. CComponent(Type,Sub,Val, imageSize),onSelect(OnSelect)
  239. {
  240. type |= REDRAW_PARENT;
  241. addUsedEvents(LCLICK | KEYBOARD);
  242. init();
  243. }
  244. void CSelectableComponent::select(bool on)
  245. {
  246. if(on != selected)
  247. {
  248. selected = on;
  249. redraw();
  250. }
  251. }
  252. void CSelectableComponent::showAll(SDL_Surface * to)
  253. {
  254. CComponent::showAll(to);
  255. if(selected)
  256. {
  257. CSDL_Ext::drawBorder(to, Rect::around(image->pos), int3(239,215,123));
  258. }
  259. }
  260. void CComponentBox::selectionChanged(std::shared_ptr<CSelectableComponent> newSelection)
  261. {
  262. if (newSelection == selected)
  263. return;
  264. if (selected)
  265. selected->select(false);
  266. selected = newSelection;
  267. if (onSelect)
  268. onSelect(selectedIndex());
  269. if (selected)
  270. selected->select(true);
  271. }
  272. int CComponentBox::selectedIndex()
  273. {
  274. if (selected)
  275. return static_cast<int>(std::find(components.begin(), components.end(), selected) - components.begin());
  276. return -1;
  277. }
  278. Point CComponentBox::getOrTextPos(CComponent *left, CComponent *right)
  279. {
  280. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  281. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  282. int fullDistance = getDistance(left, right) + leftSubtitle + rightSubtitle;
  283. return Point(fullDistance/2 - leftSubtitle, (left->image->pos.h + right->image->pos.h) / 4);
  284. }
  285. int CComponentBox::getDistance(CComponent *left, CComponent *right)
  286. {
  287. static const int betweenImagesMin = 20;
  288. static const int betweenSubtitlesMin = 10;
  289. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  290. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  291. int subtitlesOffset = leftSubtitle + rightSubtitle;
  292. return std::max(betweenSubtitlesMin, betweenImagesMin - subtitlesOffset);
  293. }
  294. void CComponentBox::placeComponents(bool selectable)
  295. {
  296. static const int betweenRows = 22;
  297. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  298. if (components.empty())
  299. return;
  300. //prepare components
  301. for(auto & comp : components)
  302. {
  303. addChild(comp.get());
  304. comp->moveTo(Point(pos.x, pos.y));
  305. }
  306. struct RowData
  307. {
  308. size_t comps;
  309. int width;
  310. int height;
  311. RowData (size_t Comps, int Width, int Height):
  312. comps(Comps), width (Width), height (Height){};
  313. };
  314. std::vector<RowData> rows;
  315. rows.push_back (RowData (0,0,0));
  316. //split components in rows
  317. std::shared_ptr<CComponent> prevComp;
  318. for(std::shared_ptr<CComponent> comp : components)
  319. {
  320. //make sure that components are smaller than our width
  321. //assert(pos.w == 0 || pos.w < comp->pos.w);
  322. const int distance = prevComp ? getDistance(prevComp.get(), comp.get()) : 0;
  323. //start next row
  324. if ((pos.w != 0 && rows.back().width + comp->pos.w + distance > pos.w) // row is full
  325. || rows.back().comps >= 4) // no more than 4 comps per row
  326. {
  327. prevComp = nullptr;
  328. rows.push_back (RowData (0,0,0));
  329. }
  330. if (prevComp)
  331. rows.back().width += distance;
  332. rows.back().comps++;
  333. rows.back().width += comp->pos.w;
  334. vstd::amax(rows.back().height, comp->pos.h);
  335. prevComp = comp;
  336. }
  337. if (pos.w == 0)
  338. {
  339. for(auto & row : rows)
  340. vstd::amax(pos.w, row.width);
  341. }
  342. int height = ((int)rows.size() - 1) * betweenRows;
  343. for(auto & row : rows)
  344. height += row.height;
  345. //assert(pos.h == 0 || pos.h < height);
  346. if (pos.h == 0)
  347. pos.h = height;
  348. auto iter = components.begin();
  349. int currentY = (pos.h - height) / 2;
  350. //move components to their positions
  351. for (auto & rows_row : rows)
  352. {
  353. // amount of free space we may add on each side of every component
  354. int freeSpace = (pos.w - rows_row.width) / ((int)rows_row.comps * 2);
  355. prevComp = nullptr;
  356. int currentX = 0;
  357. for (size_t col = 0; col < rows_row.comps; col++)
  358. {
  359. currentX += freeSpace;
  360. if (prevComp)
  361. {
  362. if (selectable)
  363. {
  364. Point orPos = Point(currentX - freeSpace, currentY) + getOrTextPos(prevComp.get(), iter->get());
  365. orLabels.push_back(std::make_shared<CLabel>(orPos.x, orPos.y, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[4]));
  366. }
  367. currentX += getDistance(prevComp.get(), iter->get());
  368. }
  369. (*iter)->moveBy(Point(currentX, currentY));
  370. currentX += (*iter)->pos.w;
  371. currentX += freeSpace;
  372. prevComp = *(iter++);
  373. }
  374. currentY += rows_row.height + betweenRows;
  375. }
  376. }
  377. CComponentBox::CComponentBox(std::vector<std::shared_ptr<CComponent>> _components, Rect position):
  378. components(_components)
  379. {
  380. type |= REDRAW_PARENT;
  381. pos = position + pos.topLeft();
  382. placeComponents(false);
  383. }
  384. CComponentBox::CComponentBox(std::vector<std::shared_ptr<CSelectableComponent>> _components, Rect position, std::function<void(int newID)> _onSelect):
  385. components(_components.begin(), _components.end()),
  386. onSelect(_onSelect)
  387. {
  388. type |= REDRAW_PARENT;
  389. pos = position + pos.topLeft();
  390. placeComponents(true);
  391. assert(!components.empty());
  392. int key = SDLK_1;
  393. for(auto & comp : _components)
  394. {
  395. comp->onSelect = std::bind(&CComponentBox::selectionChanged, this, comp);
  396. comp->assignedKeys.insert(key++);
  397. }
  398. selectionChanged(_components.front());
  399. }