2
0

MiscWidgets.cpp 24 KB

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