CComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 "../gui/CGuiHandler.h"
  13. #include "../gui/CCursorHandler.h"
  14. #include "../CMessage.h"
  15. #include "../CGameInfo.h"
  16. #include "../widgets/Images.h"
  17. #include "../widgets/CArtifactHolder.h"
  18. #include "../windows/CAdvmapInterface.h"
  19. #include "../../lib/CArtHandler.h"
  20. #include "../../lib/CTownHandler.h"
  21. #include "../../lib/CCreatureHandler.h"
  22. #include "../../lib/CSkillHandler.h"
  23. #include "../../lib/spells/CSpellHandler.h"
  24. #include "../../lib/CGeneralTextHandler.h"
  25. #include "../../lib/NetPacksBase.h"
  26. CComponent::CComponent(Etype Type, int Subtype, int Val, ESize imageSize):
  27. image(nullptr),
  28. perDay(false)
  29. {
  30. addUsedEvents(RCLICK);
  31. init(Type, Subtype, Val, imageSize);
  32. }
  33. CComponent::CComponent(const Component &c, ESize imageSize):
  34. image(nullptr),
  35. perDay(false)
  36. {
  37. addUsedEvents(RCLICK);
  38. if(c.id == Component::RESOURCE && c.when==-1)
  39. perDay = true;
  40. init((Etype)c.id,c.subtype,c.val, imageSize);
  41. }
  42. void CComponent::init(Etype Type, int Subtype, int Val, ESize imageSize)
  43. {
  44. OBJ_CONSTRUCTION_CAPTURING_ALL;
  45. compType = Type;
  46. subtype = Subtype;
  47. val = Val;
  48. size = imageSize;
  49. assert(compType < typeInvalid);
  50. assert(size < sizeInvalid);
  51. setSurface(getFileName()[size], getIndex());
  52. pos.w = image->pos.w;
  53. pos.h = image->pos.h;
  54. EFonts font = FONT_SMALL;
  55. if (imageSize < small)
  56. font = FONT_TINY; //other sizes?
  57. pos.h += 4; //distance between text and image
  58. std::vector<std::string> textLines = CMessage::breakText(getSubtitle(), std::max<int>(80, pos.w), font);
  59. for(auto & line : textLines)
  60. {
  61. int height = graphics->fonts[font]->getLineHeight();
  62. auto label = new CLabel(pos.w/2, pos.h + height/2, font, CENTER, Colors::WHITE, line);
  63. pos.h += height;
  64. if (label->pos.w > pos.w)
  65. {
  66. pos.x -= (label->pos.w - pos.w)/2;
  67. pos.w = label->pos.w;
  68. }
  69. }
  70. }
  71. const std::vector<std::string> CComponent::getFileName()
  72. {
  73. static const std::string primSkillsArr [] = {"PSKIL32", "PSKIL32", "PSKIL42", "PSKILL"};
  74. static const std::string secSkillsArr [] = {"SECSK32", "SECSK32", "SECSKILL", "SECSK82"};
  75. static const std::string resourceArr [] = {"SMALRES", "RESOURCE", "RESOUR82", "RESOUR82"};
  76. static const std::string creatureArr [] = {"CPRSMALL", "CPRSMALL", "TWCRPORT", "TWCRPORT"};
  77. static const std::string artifactArr[] = {"Artifact", "Artifact", "Artifact", "Artifact"};
  78. static const std::string spellsArr [] = {"SpellInt", "SpellInt", "SPELLSCR", "SPELLSCR"};
  79. static const std::string moraleArr [] = {"IMRL22", "IMRL30", "IMRL42", "imrl82"};
  80. static const std::string luckArr [] = {"ILCK22", "ILCK30", "ILCK42", "ilck82"};
  81. static const std::string heroArr [] = {"PortraitsSmall", "PortraitsSmall", "PortraitsLarge", "PortraitsLarge"};
  82. static const std::string flagArr [] = {"CREST58", "CREST58", "CREST58", "CREST58"};
  83. auto gen = [](const std::string * arr)
  84. {
  85. return std::vector<std::string>(arr, arr + 4);
  86. };
  87. switch(compType)
  88. {
  89. case primskill: return gen(primSkillsArr);
  90. case secskill: return gen(secSkillsArr);
  91. case resource: return gen(resourceArr);
  92. case creature: return gen(creatureArr);
  93. case artifact: return gen(artifactArr);
  94. case experience: return gen(primSkillsArr);
  95. case spell: return gen(spellsArr);
  96. case morale: return gen(moraleArr);
  97. case luck: return gen(luckArr);
  98. case building: return std::vector<std::string>(4, CGI->townh->factions[subtype]->town->clientInfo.buildingsIcons);
  99. case hero: return gen(heroArr);
  100. case flag: return gen(flagArr);
  101. }
  102. assert(0);
  103. return std::vector<std::string>();
  104. }
  105. size_t CComponent::getIndex()
  106. {
  107. switch(compType)
  108. {
  109. case primskill: return subtype;
  110. case secskill: return subtype*3 + 3 + val - 1;
  111. case resource: return subtype;
  112. case creature: return CGI->creh->creatures[subtype]->iconIndex;
  113. case artifact: return CGI->arth->artifacts[subtype]->iconIndex;
  114. case experience: return 4;
  115. case spell: return subtype;
  116. case morale: return val+3;
  117. case luck: return val+3;
  118. case building: return val;
  119. case hero: return subtype;
  120. case flag: return subtype;
  121. }
  122. assert(0);
  123. return 0;
  124. }
  125. std::string CComponent::getDescription()
  126. {
  127. switch(compType)
  128. {
  129. case primskill: return (subtype < 4)? CGI->generaltexth->arraytxt[2+subtype] //Primary skill
  130. : CGI->generaltexth->allTexts[149]; //mana
  131. case secskill: return CGI->skillh->skillInfo(subtype, val);
  132. case resource: return CGI->generaltexth->allTexts[242];
  133. case creature: return "";
  134. case artifact:
  135. {
  136. std::unique_ptr<CArtifactInstance> art;
  137. if (subtype != ArtifactID::SPELL_SCROLL)
  138. {
  139. art.reset(CArtifactInstance::createNewArtifactInstance(subtype));
  140. }
  141. else
  142. {
  143. art.reset(CArtifactInstance::createScroll(static_cast<SpellID>(val)));
  144. }
  145. return art->getEffectiveDescription();
  146. }
  147. case experience: return CGI->generaltexth->allTexts[241];
  148. case spell: return CGI->spellh->objects[subtype]->getLevelInfo(val).description;
  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->factions[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->creatures[subtype]->*(val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  175. case artifact: return CGI->arth->artifacts[subtype]->Name();
  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->spellh->objects[subtype]->name;
  190. case morale: return "";
  191. case luck: return "";
  192. case building:
  193. {
  194. auto building = CGI->townh->factions[subtype]->town->buildings[BuildingID(val)];
  195. if(!building)
  196. {
  197. logGlobal->error("Town of faction %s has no building #%d", 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. }