CComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #include "StdInc.h"
  2. #include "CComponent.h"
  3. #include "../gui/CGuiHandler.h"
  4. #include "../gui/CCursorHandler.h"
  5. #include "../CMessage.h"
  6. #include "../CGameInfo.h"
  7. #include "../widgets/Images.h"
  8. #include "../windows/CAdvmapInterface.h"
  9. #include "../../lib/CArtHandler.h"
  10. #include "../../lib/CTownHandler.h"
  11. #include "../../lib/CCreatureHandler.h"
  12. #include "../../lib/spells/CSpellHandler.h"
  13. #include "../../lib/CGeneralTextHandler.h"
  14. #include "../../lib/NetPacksBase.h"
  15. /*
  16. * CComponent.cpp, part of VCMI engine
  17. *
  18. * Authors: listed in file AUTHORS in main folder
  19. *
  20. * License: GNU General Public License v2.0 or later
  21. * Full text of license available in license.txt file, in main folder
  22. *
  23. */
  24. CComponent::CComponent(Etype Type, int Subtype, int Val, ESize imageSize):
  25. image(nullptr),
  26. perDay(false)
  27. {
  28. addUsedEvents(RCLICK);
  29. init(Type, Subtype, Val, imageSize);
  30. }
  31. CComponent::CComponent(const Component &c, ESize imageSize):
  32. image(nullptr),
  33. perDay(false)
  34. {
  35. addUsedEvents(RCLICK);
  36. if(c.id == Component::RESOURCE && c.when==-1)
  37. perDay = true;
  38. init((Etype)c.id,c.subtype,c.val, imageSize);
  39. }
  40. void CComponent::init(Etype Type, int Subtype, int Val, ESize imageSize)
  41. {
  42. OBJ_CONSTRUCTION_CAPTURING_ALL;
  43. compType = Type;
  44. subtype = Subtype;
  45. val = Val;
  46. size = imageSize;
  47. assert(compType < typeInvalid);
  48. assert(size < sizeInvalid);
  49. setSurface(getFileName()[size], 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 = graphics->fonts[font]->getLineHeight();
  60. auto label = new 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. }
  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->generaltexth->skillInfoTexts[subtype][val-1];
  130. case resource: return CGI->generaltexth->allTexts[242];
  131. case creature: return "";
  132. case artifact: return CGI->arth->artifacts[subtype]->Description();
  133. case experience: return CGI->generaltexth->allTexts[241];
  134. case spell: return CGI->spellh->objects[subtype]->getLevelInfo(val).description;
  135. case morale: return CGI->generaltexth->heroscrn[ 4 - (val>0) + (val<0)];
  136. case luck: return CGI->generaltexth->heroscrn[ 7 - (val>0) + (val<0)];
  137. case building: return CGI->townh->factions[subtype]->town->buildings[BuildingID(val)]->Description();
  138. case hero: return "";
  139. case flag: return "";
  140. }
  141. assert(0);
  142. return "";
  143. }
  144. std::string CComponent::getSubtitle()
  145. {
  146. if (!perDay)
  147. return getSubtitleInternal();
  148. std::string ret = CGI->generaltexth->allTexts[3];
  149. boost::replace_first(ret, "%d", getSubtitleInternal());
  150. return ret;
  151. }
  152. std::string CComponent::getSubtitleInternal()
  153. {
  154. //FIXME: some of these are horrible (e.g creature)
  155. switch(compType)
  156. {
  157. case primskill: return boost::str(boost::format("%+d %s") % val % (subtype < 4 ? CGI->generaltexth->primarySkillNames[subtype] : CGI->generaltexth->allTexts[387]));
  158. case secskill: return CGI->generaltexth->levels[val-1] + "\n" + CGI->generaltexth->skillName[subtype];
  159. case resource: return boost::lexical_cast<std::string>(val);
  160. case creature: return (val? boost::lexical_cast<std::string>(val) + " " : "") + CGI->creh->creatures[subtype]->*(val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  161. case artifact: return CGI->arth->artifacts[subtype]->Name();
  162. case experience:
  163. {
  164. if(subtype == 1) //+1 level - tree of knowledge
  165. {
  166. std::string level = CGI->generaltexth->allTexts[442];
  167. boost::replace_first(level, "1", boost::lexical_cast<std::string>(val));
  168. return level;
  169. }
  170. else
  171. {
  172. return boost::lexical_cast<std::string>(val); //amount of experience OR level required for seer hut;
  173. }
  174. }
  175. case spell: return CGI->spellh->objects[subtype]->name;
  176. case morale: return "";
  177. case luck: return "";
  178. case building:
  179. {
  180. auto building = CGI->townh->factions[subtype]->town->buildings[BuildingID(val)];
  181. if(!building)
  182. {
  183. logGlobal->errorStream() << boost::format("Town of faction %s has no building #%d")
  184. % CGI->townh->factions[subtype]->town->faction->name % val;
  185. return (boost::format("Missing building #%d") % val).str();
  186. }
  187. return building->Name();
  188. }
  189. case hero: return "";
  190. case flag: return CGI->generaltexth->capColors[subtype];
  191. }
  192. assert(0);
  193. return "";
  194. }
  195. void CComponent::setSurface(std::string defName, int imgPos)
  196. {
  197. OBJ_CONSTRUCTION_CAPTURING_ALL;
  198. vstd::clear_pointer(image);
  199. image = new CAnimImage(defName, imgPos);
  200. }
  201. void CComponent::clickRight(tribool down, bool previousState)
  202. {
  203. if(!getDescription().empty())
  204. adventureInt->handleRightClick(getDescription(), down);
  205. }
  206. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  207. {
  208. if (down)
  209. {
  210. if(onSelect)
  211. onSelect();
  212. }
  213. }
  214. void CSelectableComponent::init()
  215. {
  216. selected = false;
  217. }
  218. CSelectableComponent::CSelectableComponent(const Component &c, std::function<void()> OnSelect):
  219. CComponent(c),onSelect(OnSelect)
  220. {
  221. type |= REDRAW_PARENT;
  222. addUsedEvents(LCLICK | KEYBOARD);
  223. init();
  224. }
  225. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize, std::function<void()> OnSelect):
  226. CComponent(Type,Sub,Val, imageSize),onSelect(OnSelect)
  227. {
  228. type |= REDRAW_PARENT;
  229. addUsedEvents(LCLICK | KEYBOARD);
  230. init();
  231. }
  232. void CSelectableComponent::select(bool on)
  233. {
  234. if(on != selected)
  235. {
  236. selected = on;
  237. redraw();
  238. }
  239. }
  240. void CSelectableComponent::showAll(SDL_Surface * to)
  241. {
  242. CComponent::showAll(to);
  243. if(selected)
  244. {
  245. CSDL_Ext::drawBorder(to, Rect::around(image->pos), int3(239,215,123));
  246. }
  247. }
  248. void CComponentBox::selectionChanged(CSelectableComponent * newSelection)
  249. {
  250. if (newSelection == selected)
  251. return;
  252. if (selected)
  253. selected->select(false);
  254. selected = newSelection;
  255. if (onSelect)
  256. onSelect(selectedIndex());
  257. if (selected)
  258. selected->select(true);
  259. }
  260. int CComponentBox::selectedIndex()
  261. {
  262. if (selected)
  263. return std::find(components.begin(), components.end(), selected) - components.begin();
  264. return -1;
  265. }
  266. Point CComponentBox::getOrTextPos(CComponent *left, CComponent *right)
  267. {
  268. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  269. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  270. int fullDistance = getDistance(left, right) + leftSubtitle + rightSubtitle;
  271. return Point(fullDistance/2 - leftSubtitle, (left->image->pos.h + right->image->pos.h) / 4);
  272. }
  273. int CComponentBox::getDistance(CComponent *left, CComponent *right)
  274. {
  275. static const int betweenImagesMin = 20;
  276. static const int betweenSubtitlesMin = 10;
  277. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  278. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  279. int subtitlesOffset = leftSubtitle + rightSubtitle;
  280. return std::max(betweenSubtitlesMin, betweenImagesMin - subtitlesOffset);
  281. }
  282. void CComponentBox::placeComponents(bool selectable)
  283. {
  284. static const int betweenRows = 22;
  285. OBJ_CONSTRUCTION_CAPTURING_ALL;
  286. if (components.empty())
  287. return;
  288. //prepare components
  289. for(auto & comp : components)
  290. {
  291. addChild(comp);
  292. comp->moveTo(Point(pos.x, pos.y));
  293. }
  294. struct RowData
  295. {
  296. size_t comps;
  297. int width;
  298. int height;
  299. RowData (size_t Comps, int Width, int Height):
  300. comps(Comps), width (Width), height (Height){};
  301. };
  302. std::vector<RowData> rows;
  303. rows.push_back (RowData (0,0,0));
  304. //split components in rows
  305. CComponent * prevComp = nullptr;
  306. for(CComponent * comp : components)
  307. {
  308. //make sure that components are smaller than our width
  309. //assert(pos.w == 0 || pos.w < comp->pos.w);
  310. const int distance = prevComp ? getDistance(prevComp, comp) : 0;
  311. //start next row
  312. if ((pos.w != 0 && rows.back().width + comp->pos.w + distance > pos.w) // row is full
  313. || rows.back().comps >= 4) // no more than 4 comps per row
  314. {
  315. prevComp = nullptr;
  316. rows.push_back (RowData (0,0,0));
  317. }
  318. if (prevComp)
  319. rows.back().width += distance;
  320. rows.back().comps++;
  321. rows.back().width += comp->pos.w;
  322. vstd::amax(rows.back().height, comp->pos.h);
  323. prevComp = comp;
  324. }
  325. if (pos.w == 0)
  326. {
  327. for(auto & row : rows)
  328. vstd::amax(pos.w, row.width);
  329. }
  330. int height = (rows.size() - 1) * betweenRows;
  331. for(auto & row : rows)
  332. height += row.height;
  333. //assert(pos.h == 0 || pos.h < height);
  334. if (pos.h == 0)
  335. pos.h = height;
  336. auto iter = components.begin();
  337. int currentY = (pos.h - height) / 2;
  338. //move components to their positions
  339. for (auto & rows_row : rows)
  340. {
  341. // amount of free space we may add on each side of every component
  342. int freeSpace = (pos.w - rows_row.width) / (rows_row.comps * 2);
  343. prevComp = nullptr;
  344. int currentX = 0;
  345. for (size_t col = 0; col < rows_row.comps; col++)
  346. {
  347. currentX += freeSpace;
  348. if (prevComp)
  349. {
  350. if (selectable)
  351. {
  352. Point orPos = Point(currentX - freeSpace, currentY) + getOrTextPos(prevComp, *iter);
  353. new CLabel(orPos.x, orPos.y, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[4]);
  354. }
  355. currentX += getDistance(prevComp, *iter);
  356. }
  357. (*iter)->moveBy(Point(currentX, currentY));
  358. currentX += (*iter)->pos.w;
  359. currentX += freeSpace;
  360. prevComp = *(iter++);
  361. }
  362. currentY += rows_row.height + betweenRows;
  363. }
  364. }
  365. CComponentBox::CComponentBox(CComponent * _components, Rect position):
  366. components(1, _components),
  367. selected(nullptr)
  368. {
  369. type |= REDRAW_PARENT;
  370. pos = position + pos;
  371. placeComponents(false);
  372. }
  373. CComponentBox::CComponentBox(std::vector<CComponent *> _components, Rect position):
  374. components(_components),
  375. selected(nullptr)
  376. {
  377. type |= REDRAW_PARENT;
  378. pos = position + pos;
  379. placeComponents(false);
  380. }
  381. CComponentBox::CComponentBox(std::vector<CSelectableComponent *> _components, Rect position, std::function<void(int newID)> _onSelect):
  382. components(_components.begin(), _components.end()),
  383. selected(nullptr),
  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. }