MiscWidgets.cpp 22 KB

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