CComponent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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/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. return boost::lexical_cast<std::string>(val); //amount of experience OR level required for seer hut;
  172. }
  173. case spell: return CGI->spellh->objects[subtype]->name;
  174. case morale: return "";
  175. case luck: return "";
  176. case building: return CGI->townh->factions[subtype]->town->buildings[BuildingID(val)]->Name();
  177. case hero: return "";
  178. case flag: return CGI->generaltexth->capColors[subtype];
  179. }
  180. assert(0);
  181. return "";
  182. }
  183. void CComponent::setSurface(std::string defName, int imgPos)
  184. {
  185. OBJ_CONSTRUCTION_CAPTURING_ALL;
  186. vstd::clear_pointer(image);
  187. image = new CAnimImage(defName, imgPos);
  188. }
  189. void CComponent::clickRight(tribool down, bool previousState)
  190. {
  191. if(!getDescription().empty())
  192. adventureInt->handleRightClick(getDescription(), down);
  193. }
  194. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  195. {
  196. if (down)
  197. {
  198. if(onSelect)
  199. onSelect();
  200. }
  201. }
  202. void CSelectableComponent::init()
  203. {
  204. selected = false;
  205. }
  206. CSelectableComponent::CSelectableComponent(const Component &c, std::function<void()> OnSelect):
  207. CComponent(c),onSelect(OnSelect)
  208. {
  209. type |= REDRAW_PARENT;
  210. addUsedEvents(LCLICK | KEYBOARD);
  211. init();
  212. }
  213. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize, std::function<void()> OnSelect):
  214. CComponent(Type,Sub,Val, imageSize),onSelect(OnSelect)
  215. {
  216. type |= REDRAW_PARENT;
  217. addUsedEvents(LCLICK | KEYBOARD);
  218. init();
  219. }
  220. void CSelectableComponent::select(bool on)
  221. {
  222. if(on != selected)
  223. {
  224. selected = on;
  225. redraw();
  226. }
  227. }
  228. void CSelectableComponent::showAll(SDL_Surface * to)
  229. {
  230. CComponent::showAll(to);
  231. if(selected)
  232. {
  233. CSDL_Ext::drawBorder(to, Rect::around(image->pos), int3(239,215,123));
  234. }
  235. }
  236. void CComponentBox::selectionChanged(CSelectableComponent * newSelection)
  237. {
  238. if (newSelection == selected)
  239. return;
  240. if (selected)
  241. selected->select(false);
  242. selected = newSelection;
  243. if (onSelect)
  244. onSelect(selectedIndex());
  245. if (selected)
  246. selected->select(true);
  247. }
  248. int CComponentBox::selectedIndex()
  249. {
  250. if (selected)
  251. return std::find(components.begin(), components.end(), selected) - components.begin();
  252. return -1;
  253. }
  254. Point CComponentBox::getOrTextPos(CComponent *left, CComponent *right)
  255. {
  256. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  257. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  258. int fullDistance = getDistance(left, right) + leftSubtitle + rightSubtitle;
  259. return Point(fullDistance/2 - leftSubtitle, (left->image->pos.h + right->image->pos.h) / 4);
  260. }
  261. int CComponentBox::getDistance(CComponent *left, CComponent *right)
  262. {
  263. static const int betweenImagesMin = 20;
  264. static const int betweenSubtitlesMin = 10;
  265. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  266. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  267. int subtitlesOffset = leftSubtitle + rightSubtitle;
  268. return std::max(betweenSubtitlesMin, betweenImagesMin - subtitlesOffset);
  269. }
  270. void CComponentBox::placeComponents(bool selectable)
  271. {
  272. static const int betweenRows = 22;
  273. OBJ_CONSTRUCTION_CAPTURING_ALL;
  274. if (components.empty())
  275. return;
  276. //prepare components
  277. for(auto & comp : components)
  278. {
  279. addChild(comp);
  280. comp->moveTo(Point(pos.x, pos.y));
  281. }
  282. struct RowData
  283. {
  284. size_t comps;
  285. int width;
  286. int height;
  287. RowData (size_t Comps, int Width, int Height):
  288. comps(Comps), width (Width), height (Height){};
  289. };
  290. std::vector<RowData> rows;
  291. rows.push_back (RowData (0,0,0));
  292. //split components in rows
  293. CComponent * prevComp = nullptr;
  294. for(CComponent * comp : components)
  295. {
  296. //make sure that components are smaller than our width
  297. //assert(pos.w == 0 || pos.w < comp->pos.w);
  298. const int distance = prevComp ? getDistance(prevComp, comp) : 0;
  299. //start next row
  300. if ((pos.w != 0 && rows.back().width + comp->pos.w + distance > pos.w) // row is full
  301. || rows.back().comps >= 4) // no more than 4 comps per row
  302. {
  303. prevComp = nullptr;
  304. rows.push_back (RowData (0,0,0));
  305. }
  306. if (prevComp)
  307. rows.back().width += distance;
  308. rows.back().comps++;
  309. rows.back().width += comp->pos.w;
  310. vstd::amax(rows.back().height, comp->pos.h);
  311. prevComp = comp;
  312. }
  313. if (pos.w == 0)
  314. {
  315. for(auto & row : rows)
  316. vstd::amax(pos.w, row.width);
  317. }
  318. int height = (rows.size() - 1) * betweenRows;
  319. for(auto & row : rows)
  320. height += row.height;
  321. //assert(pos.h == 0 || pos.h < height);
  322. if (pos.h == 0)
  323. pos.h = height;
  324. auto iter = components.begin();
  325. int currentY = (pos.h - height) / 2;
  326. //move components to their positions
  327. for (auto & rows_row : rows)
  328. {
  329. // amount of free space we may add on each side of every component
  330. int freeSpace = (pos.w - rows_row.width) / (rows_row.comps * 2);
  331. prevComp = nullptr;
  332. int currentX = 0;
  333. for (size_t col = 0; col < rows_row.comps; col++)
  334. {
  335. currentX += freeSpace;
  336. if (prevComp)
  337. {
  338. if (selectable)
  339. {
  340. Point orPos = Point(currentX - freeSpace, currentY) + getOrTextPos(prevComp, *iter);
  341. new CLabel(orPos.x, orPos.y, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[4]);
  342. }
  343. currentX += getDistance(prevComp, *iter);
  344. }
  345. (*iter)->moveBy(Point(currentX, currentY));
  346. currentX += (*iter)->pos.w;
  347. currentX += freeSpace;
  348. prevComp = *(iter++);
  349. }
  350. currentY += rows_row.height + betweenRows;
  351. }
  352. }
  353. CComponentBox::CComponentBox(CComponent * _components, Rect position):
  354. components(1, _components),
  355. selected(nullptr)
  356. {
  357. type |= REDRAW_PARENT;
  358. pos = position + pos;
  359. placeComponents(false);
  360. }
  361. CComponentBox::CComponentBox(std::vector<CComponent *> _components, Rect position):
  362. components(_components),
  363. selected(nullptr)
  364. {
  365. type |= REDRAW_PARENT;
  366. pos = position + pos;
  367. placeComponents(false);
  368. }
  369. CComponentBox::CComponentBox(std::vector<CSelectableComponent *> _components, Rect position, std::function<void(int newID)> _onSelect):
  370. components(_components.begin(), _components.end()),
  371. selected(nullptr),
  372. onSelect(_onSelect)
  373. {
  374. type |= REDRAW_PARENT;
  375. pos = position + pos;
  376. placeComponents(true);
  377. assert(!components.empty());
  378. int key = SDLK_1;
  379. for(auto & comp : _components)
  380. {
  381. comp->onSelect = std::bind(&CComponentBox::selectionChanged, this, comp);
  382. comp->assignedKeys.insert(key++);
  383. }
  384. selectionChanged(_components.front());
  385. }