CComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 "../widgets/CArtifactHolder.h"
  9. #include "../windows/CAdvmapInterface.h"
  10. #include "../../lib/CArtHandler.h"
  11. #include "../../lib/CTownHandler.h"
  12. #include "../../lib/CCreatureHandler.h"
  13. #include "../../lib/spells/CSpellHandler.h"
  14. #include "../../lib/CGeneralTextHandler.h"
  15. #include "../../lib/NetPacksBase.h"
  16. /*
  17. * CComponent.cpp, part of VCMI engine
  18. *
  19. * Authors: listed in file AUTHORS in main folder
  20. *
  21. * License: GNU General Public License v2.0 or later
  22. * Full text of license available in license.txt file, in main folder
  23. *
  24. */
  25. CComponent::CComponent(Etype Type, int Subtype, int Val, ESize imageSize):
  26. image(nullptr),
  27. perDay(false)
  28. {
  29. addUsedEvents(RCLICK);
  30. init(Type, Subtype, Val, imageSize);
  31. }
  32. CComponent::CComponent(const Component &c, ESize imageSize):
  33. image(nullptr),
  34. perDay(false)
  35. {
  36. addUsedEvents(RCLICK);
  37. if(c.id == Component::RESOURCE && c.when==-1)
  38. perDay = true;
  39. init((Etype)c.id,c.subtype,c.val, imageSize);
  40. }
  41. void CComponent::init(Etype Type, int Subtype, int Val, ESize imageSize)
  42. {
  43. OBJ_CONSTRUCTION_CAPTURING_ALL;
  44. compType = Type;
  45. subtype = Subtype;
  46. val = Val;
  47. size = imageSize;
  48. assert(compType < typeInvalid);
  49. assert(size < sizeInvalid);
  50. setSurface(getFileName()[size], getIndex());
  51. pos.w = image->pos.w;
  52. pos.h = image->pos.h;
  53. EFonts font = FONT_SMALL;
  54. if (imageSize < small)
  55. font = FONT_TINY; //other sizes?
  56. pos.h += 4; //distance between text and image
  57. std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(80, pos.w), font);
  58. for(auto & line : textLines)
  59. {
  60. int height = graphics->fonts[font]->getLineHeight();
  61. auto label = new CLabel(pos.w/2, pos.h + height/2, font, CENTER, Colors::WHITE, line);
  62. pos.h += height;
  63. if (label->pos.w > pos.w)
  64. {
  65. pos.x -= (label->pos.w - pos.w)/2;
  66. pos.w = label->pos.w;
  67. }
  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->factions[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->creh->creatures[subtype]->iconIndex;
  112. case artifact: return CGI->arth->artifacts[subtype]->iconIndex;
  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->generaltexth->skillInfoTexts[subtype][val-1];
  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(static_cast<SpellID>(val)));
  143. }
  144. return art->getEffectiveDescription();
  145. }
  146. case experience: return CGI->generaltexth->allTexts[241];
  147. case spell: return CGI->spellh->objects[subtype]->getLevelInfo(val).description;
  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->factions[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->generaltexth->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->creatures[subtype]->*(val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  174. case artifact: return CGI->arth->artifacts[subtype]->Name();
  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->spellh->objects[subtype]->name;
  189. case morale: return "";
  190. case luck: return "";
  191. case building:
  192. {
  193. auto building = CGI->townh->factions[subtype]->town->buildings[BuildingID(val)];
  194. if(!building)
  195. {
  196. logGlobal->errorStream() << boost::format("Town of faction %s has no building #%d")
  197. % CGI->townh->factions[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. assert(0);
  206. return "";
  207. }
  208. void CComponent::setSurface(std::string defName, int imgPos)
  209. {
  210. OBJ_CONSTRUCTION_CAPTURING_ALL;
  211. vstd::clear_pointer(image);
  212. image = new CAnimImage(defName, imgPos);
  213. }
  214. void CComponent::clickRight(tribool down, bool previousState)
  215. {
  216. if(!getDescription().empty())
  217. adventureInt->handleRightClick(getDescription(), down);
  218. }
  219. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  220. {
  221. if (down)
  222. {
  223. if(onSelect)
  224. onSelect();
  225. }
  226. }
  227. void CSelectableComponent::init()
  228. {
  229. selected = false;
  230. }
  231. CSelectableComponent::CSelectableComponent(const Component &c, std::function<void()> OnSelect):
  232. CComponent(c),onSelect(OnSelect)
  233. {
  234. type |= REDRAW_PARENT;
  235. addUsedEvents(LCLICK | KEYBOARD);
  236. init();
  237. }
  238. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, ESize imageSize, std::function<void()> OnSelect):
  239. CComponent(Type,Sub,Val, imageSize),onSelect(OnSelect)
  240. {
  241. type |= REDRAW_PARENT;
  242. addUsedEvents(LCLICK | KEYBOARD);
  243. init();
  244. }
  245. void CSelectableComponent::select(bool on)
  246. {
  247. if(on != selected)
  248. {
  249. selected = on;
  250. redraw();
  251. }
  252. }
  253. void CSelectableComponent::showAll(SDL_Surface * to)
  254. {
  255. CComponent::showAll(to);
  256. if(selected)
  257. {
  258. CSDL_Ext::drawBorder(to, Rect::around(image->pos), int3(239,215,123));
  259. }
  260. }
  261. void CComponentBox::selectionChanged(CSelectableComponent * newSelection)
  262. {
  263. if (newSelection == selected)
  264. return;
  265. if (selected)
  266. selected->select(false);
  267. selected = newSelection;
  268. if (onSelect)
  269. onSelect(selectedIndex());
  270. if (selected)
  271. selected->select(true);
  272. }
  273. int CComponentBox::selectedIndex()
  274. {
  275. if (selected)
  276. return std::find(components.begin(), components.end(), selected) - components.begin();
  277. return -1;
  278. }
  279. Point CComponentBox::getOrTextPos(CComponent *left, CComponent *right)
  280. {
  281. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  282. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  283. int fullDistance = getDistance(left, right) + leftSubtitle + rightSubtitle;
  284. return Point(fullDistance/2 - leftSubtitle, (left->image->pos.h + right->image->pos.h) / 4);
  285. }
  286. int CComponentBox::getDistance(CComponent *left, CComponent *right)
  287. {
  288. static const int betweenImagesMin = 20;
  289. static const int betweenSubtitlesMin = 10;
  290. int leftSubtitle = ( left->pos.w - left->image->pos.w) / 2;
  291. int rightSubtitle = (right->pos.w - right->image->pos.w) / 2;
  292. int subtitlesOffset = leftSubtitle + rightSubtitle;
  293. return std::max(betweenSubtitlesMin, betweenImagesMin - subtitlesOffset);
  294. }
  295. void CComponentBox::placeComponents(bool selectable)
  296. {
  297. static const int betweenRows = 22;
  298. OBJ_CONSTRUCTION_CAPTURING_ALL;
  299. if (components.empty())
  300. return;
  301. //prepare components
  302. for(auto & comp : components)
  303. {
  304. addChild(comp);
  305. comp->moveTo(Point(pos.x, pos.y));
  306. }
  307. struct RowData
  308. {
  309. size_t comps;
  310. int width;
  311. int height;
  312. RowData (size_t Comps, int Width, int Height):
  313. comps(Comps), width (Width), height (Height){};
  314. };
  315. std::vector<RowData> rows;
  316. rows.push_back (RowData (0,0,0));
  317. //split components in rows
  318. CComponent * prevComp = nullptr;
  319. for(CComponent * comp : components)
  320. {
  321. //make sure that components are smaller than our width
  322. //assert(pos.w == 0 || pos.w < comp->pos.w);
  323. const int distance = prevComp ? getDistance(prevComp, comp) : 0;
  324. //start next row
  325. if ((pos.w != 0 && rows.back().width + comp->pos.w + distance > pos.w) // row is full
  326. || rows.back().comps >= 4) // no more than 4 comps per row
  327. {
  328. prevComp = nullptr;
  329. rows.push_back (RowData (0,0,0));
  330. }
  331. if (prevComp)
  332. rows.back().width += distance;
  333. rows.back().comps++;
  334. rows.back().width += comp->pos.w;
  335. vstd::amax(rows.back().height, comp->pos.h);
  336. prevComp = comp;
  337. }
  338. if (pos.w == 0)
  339. {
  340. for(auto & row : rows)
  341. vstd::amax(pos.w, row.width);
  342. }
  343. int height = (rows.size() - 1) * betweenRows;
  344. for(auto & row : rows)
  345. height += row.height;
  346. //assert(pos.h == 0 || pos.h < height);
  347. if (pos.h == 0)
  348. pos.h = height;
  349. auto iter = components.begin();
  350. int currentY = (pos.h - height) / 2;
  351. //move components to their positions
  352. for (auto & rows_row : rows)
  353. {
  354. // amount of free space we may add on each side of every component
  355. int freeSpace = (pos.w - rows_row.width) / (rows_row.comps * 2);
  356. prevComp = nullptr;
  357. int currentX = 0;
  358. for (size_t col = 0; col < rows_row.comps; col++)
  359. {
  360. currentX += freeSpace;
  361. if (prevComp)
  362. {
  363. if (selectable)
  364. {
  365. Point orPos = Point(currentX - freeSpace, currentY) + getOrTextPos(prevComp, *iter);
  366. new CLabel(orPos.x, orPos.y, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[4]);
  367. }
  368. currentX += getDistance(prevComp, *iter);
  369. }
  370. (*iter)->moveBy(Point(currentX, currentY));
  371. currentX += (*iter)->pos.w;
  372. currentX += freeSpace;
  373. prevComp = *(iter++);
  374. }
  375. currentY += rows_row.height + betweenRows;
  376. }
  377. }
  378. CComponentBox::CComponentBox(CComponent * _components, Rect position):
  379. components(1, _components),
  380. selected(nullptr)
  381. {
  382. type |= REDRAW_PARENT;
  383. pos = position + pos;
  384. placeComponents(false);
  385. }
  386. CComponentBox::CComponentBox(std::vector<CComponent *> _components, Rect position):
  387. components(_components),
  388. selected(nullptr)
  389. {
  390. type |= REDRAW_PARENT;
  391. pos = position + pos;
  392. placeComponents(false);
  393. }
  394. CComponentBox::CComponentBox(std::vector<CSelectableComponent *> _components, Rect position, std::function<void(int newID)> _onSelect):
  395. components(_components.begin(), _components.end()),
  396. selected(nullptr),
  397. onSelect(_onSelect)
  398. {
  399. type |= REDRAW_PARENT;
  400. pos = position + pos;
  401. placeComponents(true);
  402. assert(!components.empty());
  403. int key = SDLK_1;
  404. for(auto & comp : _components)
  405. {
  406. comp->onSelect = std::bind(&CComponentBox::selectionChanged, this, comp);
  407. comp->assignedKeys.insert(key++);
  408. }
  409. selectionChanged(_components.front());
  410. }