MiscWidgets.cpp 21 KB

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