CComponent.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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/spells/CSpellHandler.h"
  23. #include "../../lib/CGeneralTextHandler.h"
  24. #include "../../lib/NetPacksBase.h"
  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->error("Town of faction %s has no building #%d", CGI->townh->factions[subtype]->town->faction->name, val);
  197. return (boost::format("Missing building #%d") % val).str();
  198. }
  199. return building->Name();
  200. }
  201. case hero: return "";
  202. case flag: return CGI->generaltexth->capColors[subtype];
  203. }
  204. assert(0);
  205. return "";
  206. }
  207. void CComponent::setSurface(std::string defName, int imgPos)
  208. {
  209. OBJ_CONSTRUCTION_CAPTURING_ALL;
  210. vstd::clear_pointer(image);
  211. image = new 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(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 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. OBJ_CONSTRUCTION_CAPTURING_ALL;
  298. if (components.empty())
  299. return;
  300. //prepare components
  301. for(auto & comp : components)
  302. {
  303. addChild(comp);
  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. CComponent * prevComp = nullptr;
  318. for(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, comp) : 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 = (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) / (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, *iter);
  365. new CLabel(orPos.x, orPos.y, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[4]);
  366. }
  367. currentX += getDistance(prevComp, *iter);
  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(CComponent * _components, Rect position):
  378. components(1, _components),
  379. selected(nullptr)
  380. {
  381. type |= REDRAW_PARENT;
  382. pos = position + pos;
  383. placeComponents(false);
  384. }
  385. CComponentBox::CComponentBox(std::vector<CComponent *> _components, Rect position):
  386. components(_components),
  387. selected(nullptr)
  388. {
  389. type |= REDRAW_PARENT;
  390. pos = position + pos;
  391. placeComponents(false);
  392. }
  393. CComponentBox::CComponentBox(std::vector<CSelectableComponent *> _components, Rect position, std::function<void(int newID)> _onSelect):
  394. components(_components.begin(), _components.end()),
  395. selected(nullptr),
  396. onSelect(_onSelect)
  397. {
  398. type |= REDRAW_PARENT;
  399. pos = position + pos;
  400. placeComponents(true);
  401. assert(!components.empty());
  402. int key = SDLK_1;
  403. for(auto & comp : _components)
  404. {
  405. comp->onSelect = std::bind(&CComponentBox::selectionChanged, this, comp);
  406. comp->assignedKeys.insert(key++);
  407. }
  408. selectionChanged(_components.front());
  409. }