MiscWidgets.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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 "../PlayerLocalState.h"
  18. #include "../gui/WindowHandler.h"
  19. #include "../windows/CTradeWindow.h"
  20. #include "../widgets/TextControls.h"
  21. #include "../widgets/CGarrisonInt.h"
  22. #include "../windows/CCastleInterface.h"
  23. #include "../windows/InfoWindows.h"
  24. #include "../render/Canvas.h"
  25. #include "../render/Graphics.h"
  26. #include "../../CCallback.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/gameState/InfoAboutArmy.h"
  29. #include "../../lib/CGeneralTextHandler.h"
  30. #include "../../lib/GameSettings.h"
  31. #include "../../lib/TextOperations.h"
  32. #include "../../lib/mapObjects/CGCreature.h"
  33. #include "../../lib/mapObjects/CGHeroInstance.h"
  34. #include "../../lib/mapObjects/CGTownInstance.h"
  35. void CHoverableArea::hover (bool on)
  36. {
  37. if (on)
  38. GH.statusbar()->write(hoverText);
  39. else
  40. GH.statusbar()->clearIfMatching(hoverText);
  41. }
  42. CHoverableArea::CHoverableArea()
  43. {
  44. addUsedEvents(HOVER);
  45. }
  46. CHoverableArea::~CHoverableArea()
  47. {
  48. }
  49. void LRClickableAreaWText::clickPressed(const Point & cursorPosition)
  50. {
  51. if(!text.empty())
  52. LOCPLINT->showInfoDialog(text);
  53. }
  54. void LRClickableAreaWText::showPopupWindow(const Point & cursorPosition)
  55. {
  56. if (!text.empty())
  57. CRClickPopup::createAndPush(text);
  58. }
  59. LRClickableAreaWText::LRClickableAreaWText()
  60. {
  61. init();
  62. }
  63. LRClickableAreaWText::LRClickableAreaWText(const Rect &Pos, const std::string &HoverText, const std::string &ClickText)
  64. {
  65. init();
  66. pos = Pos + pos.topLeft();
  67. hoverText = HoverText;
  68. text = ClickText;
  69. }
  70. LRClickableAreaWText::~LRClickableAreaWText()
  71. {
  72. }
  73. void LRClickableAreaWText::init()
  74. {
  75. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  76. }
  77. void LRClickableAreaWTextComp::clickPressed(const Point & cursorPosition)
  78. {
  79. std::vector<std::shared_ptr<CComponent>> comp(1, createComponent());
  80. LOCPLINT->showInfoDialog(text, comp);
  81. }
  82. LRClickableAreaWTextComp::LRClickableAreaWTextComp(const Rect &Pos, int BaseType)
  83. : LRClickableAreaWText(Pos), baseType(BaseType), bonusValue(-1)
  84. {
  85. type = -1;
  86. }
  87. std::shared_ptr<CComponent> LRClickableAreaWTextComp::createComponent() const
  88. {
  89. if(baseType >= 0)
  90. return std::make_shared<CComponent>(CComponent::Etype(baseType), type, bonusValue);
  91. else
  92. return std::shared_ptr<CComponent>();
  93. }
  94. void LRClickableAreaWTextComp::showPopupWindow(const Point & cursorPosition)
  95. {
  96. if(auto comp = createComponent())
  97. {
  98. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  99. return;
  100. }
  101. LRClickableAreaWText::showPopupWindow(cursorPosition); //only if with-component variant not occurred
  102. }
  103. CHeroArea::CHeroArea(int x, int y, const CGHeroInstance * hero)
  104. : CIntObject(LCLICK | HOVER),
  105. hero(hero),
  106. clickFunctor(nullptr)
  107. {
  108. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  109. pos.x += x;
  110. pos.w = 58;
  111. pos.y += y;
  112. pos.h = 64;
  113. if(hero)
  114. {
  115. portrait = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsLarge"), hero->getIconIndex());
  116. clickFunctor = [hero]() -> void
  117. {
  118. LOCPLINT->openHeroWindow(hero);
  119. };
  120. }
  121. }
  122. void CHeroArea::addClickCallback(ClickFunctor callback)
  123. {
  124. clickFunctor = callback;
  125. }
  126. void CHeroArea::clickPressed(const Point & cursorPosition)
  127. {
  128. if(clickFunctor)
  129. clickFunctor();
  130. }
  131. void CHeroArea::hover(bool on)
  132. {
  133. if (on && hero)
  134. GH.statusbar()->write(hero->getObjectName());
  135. else
  136. GH.statusbar()->clear();
  137. }
  138. void LRClickableAreaOpenTown::clickPressed(const Point & cursorPosition)
  139. {
  140. if(town)
  141. {
  142. LOCPLINT->openTownWindow(town);
  143. if ( type == 2 )
  144. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::VILLAGE_HALL);
  145. else if ( type == 3 && town->fortLevel() )
  146. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::FORT);
  147. }
  148. }
  149. LRClickableAreaOpenTown::LRClickableAreaOpenTown(const Rect & Pos, const CGTownInstance * Town)
  150. : LRClickableAreaWTextComp(Pos, -1), town(Town)
  151. {
  152. }
  153. void LRClickableArea::clickPressed(const Point & cursorPosition)
  154. {
  155. if(onClick)
  156. onClick();
  157. }
  158. void LRClickableArea::showPopupWindow(const Point & cursorPosition)
  159. {
  160. if(onPopup)
  161. onPopup();
  162. }
  163. LRClickableArea::LRClickableArea(const Rect & Pos, std::function<void()> onClick, std::function<void()> onPopup)
  164. : CIntObject(LCLICK | SHOW_POPUP), onClick(onClick), onPopup(onPopup)
  165. {
  166. pos = Pos + pos.topLeft();
  167. }
  168. void CMinorResDataBar::show(Canvas & to)
  169. {
  170. }
  171. std::string CMinorResDataBar::buildDateString()
  172. {
  173. std::string pattern = "%s: %d, %s: %d, %s: %d";
  174. auto formatted = boost::format(pattern)
  175. % CGI->generaltexth->translate("core.genrltxt.62") % LOCPLINT->cb->getDate(Date::MONTH)
  176. % CGI->generaltexth->translate("core.genrltxt.63") % LOCPLINT->cb->getDate(Date::WEEK)
  177. % CGI->generaltexth->translate("core.genrltxt.64") % LOCPLINT->cb->getDate(Date::DAY_OF_WEEK);
  178. return boost::str(formatted);
  179. }
  180. void CMinorResDataBar::showAll(Canvas & to)
  181. {
  182. CIntObject::showAll(to);
  183. for (GameResID i=EGameResID::WOOD; i<=EGameResID::GOLD; ++i)
  184. {
  185. std::string text = std::to_string(LOCPLINT->cb->getResourceAmount(i));
  186. Point target(pos.x + 50 + 76 * GameResID(i), pos.y + pos.h/2);
  187. to.drawText(target, FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, text);
  188. }
  189. Point target(pos.x+545+(pos.w-545)/2,pos.y+pos.h/2);
  190. to.drawText(target, FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, buildDateString());
  191. }
  192. CMinorResDataBar::CMinorResDataBar()
  193. {
  194. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  195. pos.x = 7;
  196. pos.y = 575;
  197. background = std::make_shared<CPicture>(ImagePath::builtin("KRESBAR.bmp"));
  198. background->colorize(LOCPLINT->playerID);
  199. pos.w = background->pos.w;
  200. pos.h = background->pos.h;
  201. }
  202. CMinorResDataBar::~CMinorResDataBar() = default;
  203. void CArmyTooltip::init(const InfoAboutArmy &army)
  204. {
  205. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  206. title = std::make_shared<CLabel>(66, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, army.name);
  207. std::vector<Point> slotsPos;
  208. slotsPos.push_back(Point(36, 73));
  209. slotsPos.push_back(Point(72, 73));
  210. slotsPos.push_back(Point(108, 73));
  211. slotsPos.push_back(Point(18, 122));
  212. slotsPos.push_back(Point(54, 122));
  213. slotsPos.push_back(Point(90, 122));
  214. slotsPos.push_back(Point(126, 122));
  215. for(auto & slot : army.army)
  216. {
  217. if(slot.first.getNum() >= GameConstants::ARMY_SIZE)
  218. {
  219. logGlobal->warn("%s has stack in slot %d", army.name, slot.first.getNum());
  220. continue;
  221. }
  222. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), slot.second.type->getIconIndex(), 0, slotsPos[slot.first.getNum()].x, slotsPos[slot.first.getNum()].y));
  223. std::string subtitle;
  224. if(army.army.isDetailed)
  225. {
  226. subtitle = TextOperations::formatMetric(slot.second.count, 4);
  227. }
  228. else
  229. {
  230. //if =0 - we have no information about stack size at all
  231. if(slot.second.count)
  232. {
  233. if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool())
  234. {
  235. subtitle = CCreature::getQuantityRangeStringForId((CCreature::CreatureQuantityId)slot.second.count);
  236. }
  237. else
  238. {
  239. subtitle = CGI->generaltexth->arraytxt[171 + 3*(slot.second.count)];
  240. }
  241. }
  242. }
  243. 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));
  244. }
  245. }
  246. CArmyTooltip::CArmyTooltip(Point pos, const InfoAboutArmy & army):
  247. CIntObject(0, pos)
  248. {
  249. init(army);
  250. }
  251. CArmyTooltip::CArmyTooltip(Point pos, const CArmedInstance * army):
  252. CIntObject(0, pos)
  253. {
  254. init(InfoAboutArmy(army, true));
  255. }
  256. void CHeroTooltip::init(const InfoAboutHero & hero)
  257. {
  258. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  259. portrait = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsLarge"), hero.getIconIndex(), 0, 3, 2);
  260. if(hero.details)
  261. {
  262. for(size_t i = 0; i < hero.details->primskills.size(); i++)
  263. labels.push_back(std::make_shared<CLabel>(75 + 28 * (int)i, 58, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
  264. std::to_string(hero.details->primskills[i])));
  265. labels.push_back(std::make_shared<CLabel>(158, 98, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, std::to_string(hero.details->mana)));
  266. morale = std::make_shared<CAnimImage>(AnimationPath::builtin("IMRL22"), hero.details->morale + 3, 0, 5, 74);
  267. luck = std::make_shared<CAnimImage>(AnimationPath::builtin("ILCK22"), hero.details->luck + 3, 0, 5, 91);
  268. }
  269. }
  270. CHeroTooltip::CHeroTooltip(Point pos, const InfoAboutHero &hero):
  271. CArmyTooltip(pos, hero)
  272. {
  273. init(hero);
  274. }
  275. CHeroTooltip::CHeroTooltip(Point pos, const CGHeroInstance * hero):
  276. CArmyTooltip(pos, InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED))
  277. {
  278. init(InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED));
  279. }
  280. CInteractableHeroTooltip::CInteractableHeroTooltip(Point pos, const CGHeroInstance * hero)
  281. {
  282. init(InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED));
  283. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  284. garrison = std::make_shared<CGarrisonInt>(pos + Point(0, 73), 4, Point(0, 0), hero, nullptr, true, true, CGarrisonInt::ESlotsLayout::REVERSED_TWO_ROWS);
  285. }
  286. void CInteractableHeroTooltip::init(const InfoAboutHero & hero)
  287. {
  288. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  289. portrait = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsLarge"), hero.getIconIndex(), 0, 3, 2);
  290. title = std::make_shared<CLabel>(66, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, hero.name);
  291. if(hero.details)
  292. {
  293. for(size_t i = 0; i < hero.details->primskills.size(); i++)
  294. labels.push_back(std::make_shared<CLabel>(75 + 28 * (int)i, 58, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
  295. std::to_string(hero.details->primskills[i])));
  296. labels.push_back(std::make_shared<CLabel>(158, 98, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, std::to_string(hero.details->mana)));
  297. morale = std::make_shared<CAnimImage>(AnimationPath::builtin("IMRL22"), hero.details->morale + 3, 0, 5, 74);
  298. luck = std::make_shared<CAnimImage>(AnimationPath::builtin("ILCK22"), hero.details->luck + 3, 0, 5, 91);
  299. }
  300. }
  301. void CTownTooltip::init(const InfoAboutTown & town)
  302. {
  303. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  304. //order of icons in def: fort, citadel, castle, no fort
  305. size_t fortIndex = town.fortLevel ? town.fortLevel - 1 : 3;
  306. fort = std::make_shared<CAnimImage>(AnimationPath::builtin("ITMCLS"), fortIndex, 0, 105, 31);
  307. assert(town.tType);
  308. size_t iconIndex = town.tType->clientInfo.icons[town.fortLevel > 0][town.built >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  309. build = std::make_shared<CAnimImage>(AnimationPath::builtin("itpt"), iconIndex, 0, 3, 2);
  310. if(town.details)
  311. {
  312. hall = std::make_shared<CAnimImage>(AnimationPath::builtin("ITMTLS"), town.details->hallLevel, 0, 67, 31);
  313. if(town.details->goldIncome)
  314. {
  315. income = std::make_shared<CLabel>(157, 58, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE,
  316. std::to_string(town.details->goldIncome));
  317. }
  318. if(town.details->garrisonedHero) //garrisoned hero icon
  319. garrisonedHero = std::make_shared<CPicture>(ImagePath::builtin("TOWNQKGH"), 149, 76);
  320. if(town.details->customRes)//silo is built
  321. {
  322. if(town.tType->primaryRes == EGameResID::WOOD_AND_ORE )// wood & ore
  323. {
  324. res1 = std::make_shared<CAnimImage>(AnimationPath::builtin("SMALRES"), GameResID(EGameResID::WOOD), 0, 7, 75);
  325. res2 = std::make_shared<CAnimImage>(AnimationPath::builtin("SMALRES"), GameResID(EGameResID::ORE), 0, 7, 88);
  326. }
  327. else
  328. {
  329. res1 = std::make_shared<CAnimImage>(AnimationPath::builtin("SMALRES"), town.tType->primaryRes, 0, 7, 81);
  330. }
  331. }
  332. }
  333. }
  334. CTownTooltip::CTownTooltip(Point pos, const InfoAboutTown & town)
  335. : CArmyTooltip(pos, town)
  336. {
  337. init(town);
  338. }
  339. CTownTooltip::CTownTooltip(Point pos, const CGTownInstance * town)
  340. : CArmyTooltip(pos, InfoAboutTown(town, true))
  341. {
  342. init(InfoAboutTown(town, true));
  343. }
  344. CInteractableTownTooltip::CInteractableTownTooltip(Point pos, const CGTownInstance * town)
  345. {
  346. init(town);
  347. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  348. garrison = std::make_shared<CGarrisonInt>(pos + Point(0, 73), 4, Point(0, 0), town->getUpperArmy(), nullptr, true, true, CGarrisonInt::ESlotsLayout::REVERSED_TWO_ROWS);
  349. }
  350. void CInteractableTownTooltip::init(const CGTownInstance * town)
  351. {
  352. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  353. const InfoAboutTown townInfo = InfoAboutTown(town, true);
  354. int townId = town->id;
  355. //order of icons in def: fort, citadel, castle, no fort
  356. size_t fortIndex = townInfo.fortLevel ? townInfo.fortLevel - 1 : 3;
  357. fort = std::make_shared<CAnimImage>(AnimationPath::builtin("ITMCLS"), fortIndex, 0, 105, 31);
  358. fastArmyPurchase = std::make_shared<LRClickableArea>(Rect(105, 31, 34, 34), [townId]()
  359. {
  360. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  361. for(auto & town : towns)
  362. {
  363. if(town->id == townId)
  364. std::make_shared<CCastleBuildings>(town)->enterToTheQuickRecruitmentWindow();
  365. }
  366. });
  367. fastMarket = std::make_shared<LRClickableArea>(Rect(143, 31, 30, 34), []()
  368. {
  369. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  370. for(auto & town : towns)
  371. {
  372. if(town->builtBuildings.count(BuildingID::MARKETPLACE))
  373. {
  374. GH.windows().createAndPushWindow<CMarketplaceWindow>(town, nullptr, nullptr, EMarketMode::RESOURCE_RESOURCE);
  375. return;
  376. }
  377. }
  378. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.noTownWithMarket"));
  379. });
  380. assert(townInfo.tType);
  381. size_t iconIndex = townInfo.tType->clientInfo.icons[townInfo.fortLevel > 0][townInfo.built >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  382. build = std::make_shared<CAnimImage>(AnimationPath::builtin("itpt"), iconIndex, 0, 3, 2);
  383. title = std::make_shared<CLabel>(66, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, townInfo.name);
  384. if(townInfo.details)
  385. {
  386. hall = std::make_shared<CAnimImage>(AnimationPath::builtin("ITMTLS"), townInfo.details->hallLevel, 0, 67, 31);
  387. fastTownHall = std::make_shared<LRClickableArea>(Rect(67, 31, 34, 34), [townId]()
  388. {
  389. std::vector<const CGTownInstance*> towns = LOCPLINT->cb->getTownsInfo(true);
  390. for(auto & town : towns)
  391. {
  392. if(town->id == townId)
  393. std::make_shared<CCastleBuildings>(town)->enterTownHall();
  394. }
  395. });
  396. if(townInfo.details->goldIncome)
  397. {
  398. income = std::make_shared<CLabel>(157, 58, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE,
  399. std::to_string(townInfo.details->goldIncome));
  400. }
  401. if(townInfo.details->garrisonedHero) //garrisoned hero icon
  402. garrisonedHero = std::make_shared<CPicture>(ImagePath::builtin("TOWNQKGH"), 149, 76);
  403. if(townInfo.details->customRes)//silo is built
  404. {
  405. if(townInfo.tType->primaryRes == EGameResID::WOOD_AND_ORE )// wood & ore
  406. {
  407. res1 = std::make_shared<CAnimImage>(AnimationPath::builtin("SMALRES"), GameResID(EGameResID::WOOD), 0, 7, 75);
  408. res2 = std::make_shared<CAnimImage>(AnimationPath::builtin("SMALRES"), GameResID(EGameResID::ORE), 0, 7, 88);
  409. }
  410. else
  411. {
  412. res1 = std::make_shared<CAnimImage>(AnimationPath::builtin("SMALRES"), townInfo.tType->primaryRes, 0, 7, 81);
  413. }
  414. }
  415. }
  416. }
  417. CreatureTooltip::CreatureTooltip(Point pos, const CGCreature * creature)
  418. {
  419. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  420. auto creatureData = (*CGI->creh)[creature->stacks.begin()->second->getCreatureID()].get();
  421. creatureImage = std::make_shared<CAnimImage>(graphics->getAnimation(AnimationPath::builtin("TWCRPORT")), creatureData->getIconIndex());
  422. creatureImage->center(Point(parent->pos.x + parent->pos.w / 2, parent->pos.y + creatureImage->pos.h / 2 + 11));
  423. bool isHeroSelected = LOCPLINT->localState->getCurrentHero() != nullptr;
  424. std::string textContent = isHeroSelected
  425. ? creature->getHoverText(LOCPLINT->localState->getCurrentHero())
  426. : creature->getHoverText(LOCPLINT->playerID);
  427. //TODO: window is bigger than OH3
  428. //TODO: vertical alignment does not match H3. Commented below example that matches H3 for creatures count but supports only 1 line:
  429. /*std::shared_ptr<CLabel> = std::make_shared<CLabel>(parent->pos.w / 2, 103,
  430. FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, creature->getHoverText(LOCPLINT->playerID));*/
  431. tooltipTextbox = std::make_shared<CTextBox>(textContent, Rect(15, 95, 230, 150), 0, FONT_SMALL, ETextAlignment::TOPCENTER, Colors::WHITE);
  432. }
  433. void MoraleLuckBox::set(const AFactionMember * node)
  434. {
  435. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  436. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  437. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  438. 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.
  439. const int componentType[] = {CComponent::luck, CComponent::morale};
  440. const int hoverTextBase[] = {7, 4};
  441. TConstBonusListPtr modifierList = std::make_shared<const BonusList>();
  442. bonusValue = 0;
  443. if(node)
  444. bonusValue = morale ? node->moraleValAndBonusList(modifierList) : node->luckValAndBonusList(modifierList);
  445. int mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  446. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  447. baseType = componentType[morale];
  448. text = CGI->generaltexth->arraytxt[textId[morale]];
  449. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  450. if (morale && node && (node->getBonusBearer()->hasBonusOfType(BonusType::UNDEAD)
  451. || node->getBonusBearer()->hasBonusOfType(BonusType::NON_LIVING)))
  452. {
  453. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  454. bonusValue = 0;
  455. }
  456. else if(morale && node && node->getBonusBearer()->hasBonusOfType(BonusType::NO_MORALE))
  457. {
  458. auto noMorale = node->getBonusBearer()->getBonus(Selector::type()(BonusType::NO_MORALE));
  459. text += "\n" + noMorale->Description();
  460. bonusValue = 0;
  461. }
  462. else if (!morale && node && node->getBonusBearer()->hasBonusOfType(BonusType::NO_LUCK))
  463. {
  464. auto noLuck = node->getBonusBearer()->getBonus(Selector::type()(BonusType::NO_LUCK));
  465. text += "\n" + noLuck->Description();
  466. bonusValue = 0;
  467. }
  468. else
  469. {
  470. std::string addInfo = "";
  471. for(auto & bonus : * modifierList)
  472. {
  473. if(bonus->val)
  474. addInfo += "\n" + bonus->Description();
  475. }
  476. text = addInfo.empty()
  477. ? text + CGI->generaltexth->arraytxt[noneTxtId]
  478. : text + addInfo;
  479. }
  480. std::string imageName;
  481. if (small)
  482. imageName = morale ? "IMRL30": "ILCK30";
  483. else
  484. imageName = morale ? "IMRL42" : "ILCK42";
  485. image = std::make_shared<CAnimImage>(AnimationPath::builtin(imageName), bonusValue + 3);
  486. image->moveBy(Point(pos.w/2 - image->pos.w/2, pos.h/2 - image->pos.h/2));//center icon
  487. }
  488. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r, bool Small)
  489. : morale(Morale),
  490. small(Small)
  491. {
  492. bonusValue = 0;
  493. pos = r + pos.topLeft();
  494. defActions = 255-DISPOSE;
  495. }
  496. CCreaturePic::CCreaturePic(int x, int y, const CCreature * cre, bool Big, bool Animated)
  497. {
  498. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  499. pos.x+=x;
  500. pos.y+=y;
  501. auto faction = cre->getFaction();
  502. assert(CGI->townh->size() > faction);
  503. if(Big)
  504. bg = std::make_shared<CPicture>((*CGI->townh)[faction]->creatureBg130);
  505. else
  506. bg = std::make_shared<CPicture>((*CGI->townh)[faction]->creatureBg120);
  507. anim = std::make_shared<CCreatureAnim>(0, 0, cre->animDefName);
  508. anim->clipRect(cre->isDoubleWide()?170:150, 155, bg->pos.w, bg->pos.h);
  509. anim->startPreview(cre->hasBonusOfType(BonusType::SIEGE_WEAPON));
  510. amount = std::make_shared<CLabel>(bg->pos.w, bg->pos.h, FONT_MEDIUM, ETextAlignment::BOTTOMRIGHT, Colors::WHITE);
  511. pos.w = bg->pos.w;
  512. pos.h = bg->pos.h;
  513. }
  514. void CCreaturePic::show(Canvas & to)
  515. {
  516. // redraw everything in a proper order
  517. bg->showAll(to);
  518. anim->show(to);
  519. amount->showAll(to);
  520. }
  521. void CCreaturePic::setAmount(int newAmount)
  522. {
  523. if(newAmount != 0)
  524. amount->setText(std::to_string(newAmount));
  525. else
  526. amount->setText("");
  527. }
  528. TransparentFilledRectangle::TransparentFilledRectangle(Rect position, ColorRGBA color) :
  529. color(color), colorLine(ColorRGBA()), drawLine(false)
  530. {
  531. pos = position + pos.topLeft();
  532. }
  533. TransparentFilledRectangle::TransparentFilledRectangle(Rect position, ColorRGBA color, ColorRGBA colorLine) :
  534. color(color), colorLine(colorLine), drawLine(true)
  535. {
  536. pos = position + pos.topLeft();
  537. }
  538. void TransparentFilledRectangle::showAll(Canvas & to)
  539. {
  540. to.drawColorBlended(pos, color);
  541. if(drawLine)
  542. to.drawBorder(pos, colorLine);
  543. }
  544. SimpleLine::SimpleLine(Point pos1, Point pos2, ColorRGBA color) :
  545. pos1(pos1), pos2(pos2), color(color)
  546. {}
  547. void SimpleLine::showAll(Canvas & to)
  548. {
  549. to.drawLine(pos1 + pos.topLeft(), pos2 + pos.topLeft(), color, color);
  550. }