MiscWidgets.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * MiscWidgets.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 "MiscWidgets.h"
  12. #include "CComponent.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../gui/CursorHandler.h"
  15. #include "../CPlayerInterface.h"
  16. #include "../CGameInfo.h"
  17. #include "../widgets/TextControls.h"
  18. #include "../windows/CCastleInterface.h"
  19. #include "../windows/InfoWindows.h"
  20. #include "../render/Canvas.h"
  21. #include "../../CCallback.h"
  22. #include "../../lib/CConfigHandler.h"
  23. #include "../../lib/gameState/InfoAboutArmy.h"
  24. #include "../../lib/CGeneralTextHandler.h"
  25. #include "../../lib/GameSettings.h"
  26. #include "../../lib/TextOperations.h"
  27. #include "../../lib/mapObjects/CGHeroInstance.h"
  28. #include "../../lib/mapObjects/CGTownInstance.h"
  29. void CHoverableArea::hover (bool on)
  30. {
  31. if (on)
  32. GH.statusbar()->write(hoverText);
  33. else
  34. GH.statusbar()->clearIfMatching(hoverText);
  35. }
  36. CHoverableArea::CHoverableArea()
  37. {
  38. addUsedEvents(HOVER);
  39. }
  40. CHoverableArea::~CHoverableArea()
  41. {
  42. }
  43. void LRClickableAreaWText::clickPressed(const Point & cursorPosition)
  44. {
  45. if(!text.empty())
  46. LOCPLINT->showInfoDialog(text);
  47. }
  48. void LRClickableAreaWText::showPopupWindow(const Point & cursorPosition)
  49. {
  50. if (!text.empty())
  51. CRClickPopup::createAndPush(text);
  52. }
  53. LRClickableAreaWText::LRClickableAreaWText()
  54. {
  55. init();
  56. }
  57. LRClickableAreaWText::LRClickableAreaWText(const Rect &Pos, const std::string &HoverText, const std::string &ClickText)
  58. {
  59. init();
  60. pos = Pos + pos.topLeft();
  61. hoverText = HoverText;
  62. text = ClickText;
  63. }
  64. LRClickableAreaWText::~LRClickableAreaWText()
  65. {
  66. }
  67. void LRClickableAreaWText::init()
  68. {
  69. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  70. }
  71. void LRClickableAreaWTextComp::clickPressed(const Point & cursorPosition)
  72. {
  73. std::vector<std::shared_ptr<CComponent>> comp(1, createComponent());
  74. LOCPLINT->showInfoDialog(text, comp);
  75. }
  76. LRClickableAreaWTextComp::LRClickableAreaWTextComp(const Rect &Pos, int BaseType)
  77. : LRClickableAreaWText(Pos), baseType(BaseType), bonusValue(-1)
  78. {
  79. type = -1;
  80. }
  81. std::shared_ptr<CComponent> LRClickableAreaWTextComp::createComponent() const
  82. {
  83. if(baseType >= 0)
  84. return std::make_shared<CComponent>(CComponent::Etype(baseType), type, bonusValue);
  85. else
  86. return std::shared_ptr<CComponent>();
  87. }
  88. void LRClickableAreaWTextComp::showPopupWindow(const Point & cursorPosition)
  89. {
  90. if(auto comp = createComponent())
  91. {
  92. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  93. return;
  94. }
  95. LRClickableAreaWText::showPopupWindow(cursorPosition); //only if with-component variant not occurred
  96. }
  97. CHeroArea::CHeroArea(int x, int y, const CGHeroInstance * _hero)
  98. : CIntObject(LCLICK | HOVER),
  99. hero(_hero)
  100. {
  101. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  102. pos.x += x;
  103. pos.w = 58;
  104. pos.y += y;
  105. pos.h = 64;
  106. if(hero)
  107. portrait = std::make_shared<CAnimImage>("PortraitsLarge", hero->portrait);
  108. }
  109. void CHeroArea::clickPressed(const Point & cursorPosition)
  110. {
  111. if(hero)
  112. LOCPLINT->openHeroWindow(hero);
  113. }
  114. void CHeroArea::hover(bool on)
  115. {
  116. if (on && hero)
  117. GH.statusbar()->write(hero->getObjectName());
  118. else
  119. GH.statusbar()->clear();
  120. }
  121. void LRClickableAreaOpenTown::clickPressed(const Point & cursorPosition)
  122. {
  123. if(town)
  124. {
  125. LOCPLINT->openTownWindow(town);
  126. if ( type == 2 )
  127. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::VILLAGE_HALL);
  128. else if ( type == 3 && town->fortLevel() )
  129. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::FORT);
  130. }
  131. }
  132. LRClickableAreaOpenTown::LRClickableAreaOpenTown(const Rect & Pos, const CGTownInstance * Town)
  133. : LRClickableAreaWTextComp(Pos, -1), town(Town)
  134. {
  135. }
  136. void CMinorResDataBar::show(Canvas & to)
  137. {
  138. }
  139. std::string CMinorResDataBar::buildDateString()
  140. {
  141. std::string pattern = "%s: %d, %s: %d, %s: %d";
  142. auto formatted = boost::format(pattern)
  143. % CGI->generaltexth->translate("core.genrltxt.62") % LOCPLINT->cb->getDate(Date::MONTH)
  144. % CGI->generaltexth->translate("core.genrltxt.63") % LOCPLINT->cb->getDate(Date::WEEK)
  145. % CGI->generaltexth->translate("core.genrltxt.64") % LOCPLINT->cb->getDate(Date::DAY_OF_WEEK);
  146. return boost::str(formatted);
  147. }
  148. void CMinorResDataBar::showAll(Canvas & to)
  149. {
  150. CIntObject::showAll(to);
  151. for (GameResID i=EGameResID::WOOD; i<=EGameResID::GOLD; ++i)
  152. {
  153. std::string text = std::to_string(LOCPLINT->cb->getResourceAmount(i));
  154. Point target(pos.x + 50 + 76 * GameResID(i), pos.y + pos.h/2);
  155. to.drawText(target, FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, text);
  156. }
  157. Point target(pos.x+545+(pos.w-545)/2,pos.y+pos.h/2);
  158. to.drawText(target, FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, buildDateString());
  159. }
  160. CMinorResDataBar::CMinorResDataBar()
  161. {
  162. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  163. pos.x = 7;
  164. pos.y = 575;
  165. background = std::make_shared<CPicture>("KRESBAR.bmp");
  166. background->colorize(LOCPLINT->playerID);
  167. pos.w = background->pos.w;
  168. pos.h = background->pos.h;
  169. }
  170. CMinorResDataBar::~CMinorResDataBar() = default;
  171. void CArmyTooltip::init(const InfoAboutArmy &army)
  172. {
  173. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  174. title = std::make_shared<CLabel>(66, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, army.name);
  175. std::vector<Point> slotsPos;
  176. slotsPos.push_back(Point(36, 73));
  177. slotsPos.push_back(Point(72, 73));
  178. slotsPos.push_back(Point(108, 73));
  179. slotsPos.push_back(Point(18, 122));
  180. slotsPos.push_back(Point(54, 122));
  181. slotsPos.push_back(Point(90, 122));
  182. slotsPos.push_back(Point(126, 122));
  183. for(auto & slot : army.army)
  184. {
  185. if(slot.first.getNum() >= GameConstants::ARMY_SIZE)
  186. {
  187. logGlobal->warn("%s has stack in slot %d", army.name, slot.first.getNum());
  188. continue;
  189. }
  190. icons.push_back(std::make_shared<CAnimImage>("CPRSMALL", slot.second.type->getIconIndex(), 0, slotsPos[slot.first.getNum()].x, slotsPos[slot.first.getNum()].y));
  191. std::string subtitle;
  192. if(army.army.isDetailed)
  193. {
  194. subtitle = TextOperations::formatMetric(slot.second.count, 4);
  195. }
  196. else
  197. {
  198. //if =0 - we have no information about stack size at all
  199. if(slot.second.count)
  200. {
  201. if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool())
  202. {
  203. subtitle = CCreature::getQuantityRangeStringForId((CCreature::CreatureQuantityId)slot.second.count);
  204. }
  205. else
  206. {
  207. subtitle = CGI->generaltexth->arraytxt[171 + 3*(slot.second.count)];
  208. }
  209. }
  210. }
  211. subtitles.push_back(std::make_shared<CLabel>(slotsPos[slot.first.getNum()].x + 17, slotsPos[slot.first.getNum()].y + 39, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, subtitle));
  212. }
  213. }
  214. CArmyTooltip::CArmyTooltip(Point pos, const InfoAboutArmy & army):
  215. CIntObject(0, pos)
  216. {
  217. init(army);
  218. }
  219. CArmyTooltip::CArmyTooltip(Point pos, const CArmedInstance * army):
  220. CIntObject(0, pos)
  221. {
  222. init(InfoAboutArmy(army, true));
  223. }
  224. void CHeroTooltip::init(const InfoAboutHero & hero)
  225. {
  226. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  227. portrait = std::make_shared<CAnimImage>("PortraitsLarge", hero.portrait, 0, 3, 2);
  228. if(hero.details)
  229. {
  230. for(size_t i = 0; i < hero.details->primskills.size(); i++)
  231. labels.push_back(std::make_shared<CLabel>(75 + 28 * (int)i, 58, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
  232. std::to_string(hero.details->primskills[i])));
  233. labels.push_back(std::make_shared<CLabel>(158, 98, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, std::to_string(hero.details->mana)));
  234. morale = std::make_shared<CAnimImage>("IMRL22", hero.details->morale + 3, 0, 5, 74);
  235. luck = std::make_shared<CAnimImage>("ILCK22", hero.details->luck + 3, 0, 5, 91);
  236. }
  237. }
  238. CHeroTooltip::CHeroTooltip(Point pos, const InfoAboutHero &hero):
  239. CArmyTooltip(pos, hero)
  240. {
  241. init(hero);
  242. }
  243. CHeroTooltip::CHeroTooltip(Point pos, const CGHeroInstance * hero):
  244. CArmyTooltip(pos, InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED))
  245. {
  246. init(InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED));
  247. }
  248. CInteractableHeroTooltip::CInteractableHeroTooltip(Point pos, const CGHeroInstance * hero):
  249. CGarrisonInt(pos + Point(0, 73), 4, Point(0, 0), hero, nullptr, true, true, CGarrisonInt::ESlotsLayout::REVERSED_TWO_ROWS)
  250. {
  251. init(InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED));
  252. }
  253. void CInteractableHeroTooltip::init(const InfoAboutHero & hero)
  254. {
  255. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  256. portrait = std::make_shared<CAnimImage>("PortraitsLarge", hero.portrait, 0, 3, 2-73);
  257. title = std::make_shared<CLabel>(66, 2-73, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, hero.name);
  258. if(hero.details)
  259. {
  260. for(size_t i = 0; i < hero.details->primskills.size(); i++)
  261. labels.push_back(std::make_shared<CLabel>(75 + 28 * (int)i, 58-73, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
  262. std::to_string(hero.details->primskills[i])));
  263. labels.push_back(std::make_shared<CLabel>(158, 98-73, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, std::to_string(hero.details->mana)));
  264. morale = std::make_shared<CAnimImage>("IMRL22", hero.details->morale + 3, 0, 5, 74-73);
  265. luck = std::make_shared<CAnimImage>("ILCK22", hero.details->luck + 3, 0, 5, 91-73);
  266. }
  267. }
  268. void CTownTooltip::init(const InfoAboutTown & town)
  269. {
  270. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  271. //order of icons in def: fort, citadel, castle, no fort
  272. size_t fortIndex = town.fortLevel ? town.fortLevel - 1 : 3;
  273. fort = std::make_shared<CAnimImage>("ITMCLS", fortIndex, 0, 105, 31);
  274. assert(town.tType);
  275. size_t iconIndex = town.tType->clientInfo.icons[town.fortLevel > 0][town.built >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  276. build = std::make_shared<CAnimImage>("itpt", iconIndex, 0, 3, 2);
  277. if(town.details)
  278. {
  279. hall = std::make_shared<CAnimImage>("ITMTLS", town.details->hallLevel, 0, 67, 31);
  280. if(town.details->goldIncome)
  281. {
  282. income = std::make_shared<CLabel>(157, 58, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE,
  283. std::to_string(town.details->goldIncome));
  284. }
  285. if(town.details->garrisonedHero) //garrisoned hero icon
  286. garrisonedHero = std::make_shared<CPicture>("TOWNQKGH", 149, 76);
  287. if(town.details->customRes)//silo is built
  288. {
  289. if(town.tType->primaryRes == EGameResID::WOOD_AND_ORE )// wood & ore
  290. {
  291. res1 = std::make_shared<CAnimImage>("SMALRES", GameResID(EGameResID::WOOD), 0, 7, 75);
  292. res2 = std::make_shared<CAnimImage>("SMALRES", GameResID(EGameResID::ORE), 0, 7, 88);
  293. }
  294. else
  295. {
  296. res1 = std::make_shared<CAnimImage>("SMALRES", town.tType->primaryRes, 0, 7, 81);
  297. }
  298. }
  299. }
  300. }
  301. CTownTooltip::CTownTooltip(Point pos, const InfoAboutTown & town)
  302. : CArmyTooltip(pos, town)
  303. {
  304. init(town);
  305. }
  306. CTownTooltip::CTownTooltip(Point pos, const CGTownInstance * town)
  307. : CArmyTooltip(pos, InfoAboutTown(town, true))
  308. {
  309. init(InfoAboutTown(town, true));
  310. }
  311. CInteractableTownTooltip::CInteractableTownTooltip(Point pos, const CGTownInstance * town)
  312. : CGarrisonInt(pos + Point(0, 73), 4, Point(0, 0), town->getUpperArmy(), nullptr, true, true, CGarrisonInt::ESlotsLayout::REVERSED_TWO_ROWS)
  313. {
  314. init(InfoAboutTown(town, true));
  315. }
  316. void CInteractableTownTooltip::init(const InfoAboutTown & town)
  317. {
  318. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  319. //order of icons in def: fort, citadel, castle, no fort
  320. size_t fortIndex = town.fortLevel ? town.fortLevel - 1 : 3;
  321. fort = std::make_shared<CAnimImage>("ITMCLS", fortIndex, 0, 105, 31-73);
  322. assert(town.tType);
  323. size_t iconIndex = town.tType->clientInfo.icons[town.fortLevel > 0][town.built >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  324. build = std::make_shared<CAnimImage>("itpt", iconIndex, 0, 3, 2-73);
  325. title = std::make_shared<CLabel>(66, 2-73, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, town.name);
  326. if(town.details)
  327. {
  328. hall = std::make_shared<CAnimImage>("ITMTLS", town.details->hallLevel, 0, 67, 31-73);
  329. if(town.details->goldIncome)
  330. {
  331. income = std::make_shared<CLabel>(157, 58-73, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE,
  332. std::to_string(town.details->goldIncome));
  333. }
  334. if(town.details->garrisonedHero) //garrisoned hero icon
  335. garrisonedHero = std::make_shared<CPicture>("TOWNQKGH", 149, 76-73);
  336. if(town.details->customRes)//silo is built
  337. {
  338. if(town.tType->primaryRes == EGameResID::WOOD_AND_ORE )// wood & ore
  339. {
  340. res1 = std::make_shared<CAnimImage>("SMALRES", GameResID(EGameResID::WOOD), 0, 7, 75-73);
  341. res2 = std::make_shared<CAnimImage>("SMALRES", GameResID(EGameResID::ORE), 0, 7, 88-73);
  342. }
  343. else
  344. {
  345. res1 = std::make_shared<CAnimImage>("SMALRES", town.tType->primaryRes, 0, 7, 81-73);
  346. }
  347. }
  348. }
  349. }
  350. void MoraleLuckBox::set(const AFactionMember * node)
  351. {
  352. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  353. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  354. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  355. const int neutralDescr[] = {60, 86}; //eg {Neutral Morale} \n\n Neutral morale means your armies will neither be blessed with extra attacks or freeze in combat.
  356. const int componentType[] = {CComponent::luck, CComponent::morale};
  357. const int hoverTextBase[] = {7, 4};
  358. TConstBonusListPtr modifierList = std::make_shared<const BonusList>();
  359. bonusValue = 0;
  360. if(node)
  361. bonusValue = morale ? node->moraleValAndBonusList(modifierList) : node->luckValAndBonusList(modifierList);
  362. int mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  363. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  364. baseType = componentType[morale];
  365. text = CGI->generaltexth->arraytxt[textId[morale]];
  366. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  367. if (morale && node && (node->getBonusBearer()->hasBonusOfType(BonusType::UNDEAD)
  368. || node->getBonusBearer()->hasBonusOfType(BonusType::NON_LIVING)))
  369. {
  370. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  371. bonusValue = 0;
  372. }
  373. else if(morale && node && node->getBonusBearer()->hasBonusOfType(BonusType::NO_MORALE))
  374. {
  375. auto noMorale = node->getBonusBearer()->getBonus(Selector::type()(BonusType::NO_MORALE));
  376. text += "\n" + noMorale->Description();
  377. bonusValue = 0;
  378. }
  379. else if (!morale && node && node->getBonusBearer()->hasBonusOfType(BonusType::NO_LUCK))
  380. {
  381. auto noLuck = node->getBonusBearer()->getBonus(Selector::type()(BonusType::NO_LUCK));
  382. text += "\n" + noLuck->Description();
  383. bonusValue = 0;
  384. }
  385. else
  386. {
  387. std::string addInfo = "";
  388. for(auto & bonus : * modifierList)
  389. {
  390. if(bonus->val)
  391. addInfo += "\n" + bonus->Description();
  392. }
  393. text = addInfo.empty()
  394. ? text + CGI->generaltexth->arraytxt[noneTxtId]
  395. : text + addInfo;
  396. }
  397. std::string imageName;
  398. if (small)
  399. imageName = morale ? "IMRL30": "ILCK30";
  400. else
  401. imageName = morale ? "IMRL42" : "ILCK42";
  402. image = std::make_shared<CAnimImage>(imageName, bonusValue + 3);
  403. image->moveBy(Point(pos.w/2 - image->pos.w/2, pos.h/2 - image->pos.h/2));//center icon
  404. }
  405. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r, bool Small)
  406. : morale(Morale),
  407. small(Small)
  408. {
  409. bonusValue = 0;
  410. pos = r + pos.topLeft();
  411. defActions = 255-DISPOSE;
  412. }
  413. CCreaturePic::CCreaturePic(int x, int y, const CCreature * cre, bool Big, bool Animated)
  414. {
  415. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  416. pos.x+=x;
  417. pos.y+=y;
  418. auto faction = cre->getFaction();
  419. assert(CGI->townh->size() > faction);
  420. if(Big)
  421. bg = std::make_shared<CPicture>((*CGI->townh)[faction]->creatureBg130);
  422. else
  423. bg = std::make_shared<CPicture>((*CGI->townh)[faction]->creatureBg120);
  424. anim = std::make_shared<CCreatureAnim>(0, 0, cre->animDefName);
  425. anim->clipRect(cre->isDoubleWide()?170:150, 155, bg->pos.w, bg->pos.h);
  426. anim->startPreview(cre->hasBonusOfType(BonusType::SIEGE_WEAPON));
  427. amount = std::make_shared<CLabel>(bg->pos.w, bg->pos.h, FONT_MEDIUM, ETextAlignment::BOTTOMRIGHT, Colors::WHITE);
  428. pos.w = bg->pos.w;
  429. pos.h = bg->pos.h;
  430. }
  431. void CCreaturePic::show(Canvas & to)
  432. {
  433. // redraw everything in a proper order
  434. bg->showAll(to);
  435. anim->show(to);
  436. amount->showAll(to);
  437. }
  438. void CCreaturePic::setAmount(int newAmount)
  439. {
  440. if(newAmount != 0)
  441. amount->setText(std::to_string(newAmount));
  442. else
  443. amount->setText("");
  444. }