MiscWidgets.cpp 23 KB

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