MiscWidgets.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 "../widgets/TextControls.h"
  18. #include "../windows/CCastleInterface.h"
  19. #include "../windows/InfoWindows.h"
  20. #include "../renderSDL/SDL_Extensions.h"
  21. #include "../../CCallback.h"
  22. #include "../../lib/CConfigHandler.h"
  23. #include "../../lib/CGameState.h"
  24. #include "../../lib/CGeneralTextHandler.h"
  25. #include "../../lib/CModHandler.h"
  26. #include "../../lib/GameSettings.h"
  27. #include "../../lib/TextOperations.h"
  28. #include "../../lib/mapObjects/CGHeroInstance.h"
  29. #include "../../lib/mapObjects/CGTownInstance.h"
  30. void CHoverableArea::hover (bool on)
  31. {
  32. if (on)
  33. GH.statusbar->write(hoverText);
  34. else
  35. GH.statusbar->clearIfMatching(hoverText);
  36. }
  37. CHoverableArea::CHoverableArea()
  38. {
  39. addUsedEvents(HOVER);
  40. }
  41. CHoverableArea::~CHoverableArea()
  42. {
  43. }
  44. void LRClickableAreaWText::clickLeft(tribool down, bool previousState)
  45. {
  46. if(!down && previousState && !text.empty())
  47. {
  48. LOCPLINT->showInfoDialog(text);
  49. }
  50. }
  51. void LRClickableAreaWText::clickRight(tribool down, bool previousState)
  52. {
  53. if (down && !text.empty())
  54. CRClickPopup::createAndPush(text);
  55. }
  56. LRClickableAreaWText::LRClickableAreaWText()
  57. {
  58. init();
  59. }
  60. LRClickableAreaWText::LRClickableAreaWText(const Rect &Pos, const std::string &HoverText, const std::string &ClickText)
  61. {
  62. init();
  63. pos = Pos + pos.topLeft();
  64. hoverText = HoverText;
  65. text = ClickText;
  66. }
  67. LRClickableAreaWText::~LRClickableAreaWText()
  68. {
  69. }
  70. void LRClickableAreaWText::init()
  71. {
  72. addUsedEvents(LCLICK | RCLICK | HOVER);
  73. }
  74. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  75. {
  76. if((!down) && previousState)
  77. {
  78. std::vector<std::shared_ptr<CComponent>> comp(1, createComponent());
  79. LOCPLINT->showInfoDialog(text, comp);
  80. }
  81. }
  82. LRClickableAreaWTextComp::LRClickableAreaWTextComp(const Rect &Pos, int BaseType)
  83. : LRClickableAreaWText(Pos), baseType(BaseType), bonusValue(-1)
  84. {
  85. type = -1;
  86. }
  87. std::shared_ptr<CComponent> LRClickableAreaWTextComp::createComponent() const
  88. {
  89. if(baseType >= 0)
  90. return std::make_shared<CComponent>(CComponent::Etype(baseType), type, bonusValue);
  91. else
  92. return std::shared_ptr<CComponent>();
  93. }
  94. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  95. {
  96. if(down)
  97. {
  98. if(auto comp = createComponent())
  99. {
  100. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  101. return;
  102. }
  103. }
  104. LRClickableAreaWText::clickRight(down, previousState); //only if with-component variant not occurred
  105. }
  106. CHeroArea::CHeroArea(int x, int y, const CGHeroInstance * _hero)
  107. : CIntObject(LCLICK | RCLICK | HOVER),
  108. hero(_hero)
  109. {
  110. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  111. pos.x += x;
  112. pos.w = 58;
  113. pos.y += y;
  114. pos.h = 64;
  115. if(hero)
  116. portrait = std::make_shared<CAnimImage>("PortraitsLarge", hero->portrait);
  117. }
  118. void CHeroArea::clickLeft(tribool down, bool previousState)
  119. {
  120. if(hero && (!down) && previousState)
  121. LOCPLINT->openHeroWindow(hero);
  122. }
  123. void CHeroArea::clickRight(tribool down, bool previousState)
  124. {
  125. if(hero && (!down) && previousState)
  126. LOCPLINT->openHeroWindow(hero);
  127. }
  128. void CHeroArea::hover(bool on)
  129. {
  130. if (on && hero)
  131. GH.statusbar->write(hero->getObjectName());
  132. else
  133. GH.statusbar->clear();
  134. }
  135. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  136. {
  137. if(town && (!down) && previousState)
  138. {
  139. LOCPLINT->openTownWindow(town);
  140. if ( type == 2 )
  141. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::VILLAGE_HALL);
  142. else if ( type == 3 && town->fortLevel() )
  143. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::FORT);
  144. }
  145. }
  146. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  147. {
  148. if(town && (!down) && previousState)
  149. LOCPLINT->openTownWindow(town);//TODO: popup?
  150. }
  151. LRClickableAreaOpenTown::LRClickableAreaOpenTown(const Rect & Pos, const CGTownInstance * Town)
  152. : LRClickableAreaWTextComp(Pos, -1), town(Town)
  153. {
  154. }
  155. void CMinorResDataBar::show(SDL_Surface * to)
  156. {
  157. }
  158. std::string CMinorResDataBar::buildDateString()
  159. {
  160. std::string pattern = "%s: %d, %s: %d, %s: %d";
  161. auto formatted = boost::format(pattern)
  162. % CGI->generaltexth->translate("core.genrltxt.62") % LOCPLINT->cb->getDate(Date::MONTH)
  163. % CGI->generaltexth->translate("core.genrltxt.63") % LOCPLINT->cb->getDate(Date::WEEK)
  164. % CGI->generaltexth->translate("core.genrltxt.64") % LOCPLINT->cb->getDate(Date::DAY_OF_WEEK);
  165. return boost::str(formatted);
  166. }
  167. void CMinorResDataBar::showAll(SDL_Surface * to)
  168. {
  169. CIntObject::showAll(to);
  170. for (EGameResID i=EGameResID::WOOD; i<=EGameResID::GOLD; vstd::advance(i, 1))
  171. {
  172. std::string text = std::to_string(LOCPLINT->cb->getResourceAmount(i));
  173. graphics->fonts[FONT_SMALL]->renderTextCenter(to, text, Colors::WHITE, Point(pos.x + 50 + 76 * GameResID(i), pos.y + pos.h/2));
  174. }
  175. graphics->fonts[FONT_SMALL]->renderTextCenter(to, buildDateString(), Colors::WHITE, Point(pos.x+545+(pos.w-545)/2,pos.y+pos.h/2));
  176. }
  177. CMinorResDataBar::CMinorResDataBar()
  178. {
  179. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  180. pos.x = 7;
  181. pos.y = 575;
  182. background = std::make_shared<CPicture>("KRESBAR.bmp");
  183. background->colorize(LOCPLINT->playerID);
  184. pos.w = background->pos.w;
  185. pos.h = background->pos.h;
  186. }
  187. CMinorResDataBar::~CMinorResDataBar() = default;
  188. void CArmyTooltip::init(const InfoAboutArmy &army)
  189. {
  190. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  191. title = std::make_shared<CLabel>(66, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, army.name);
  192. std::vector<Point> slotsPos;
  193. slotsPos.push_back(Point(36, 73));
  194. slotsPos.push_back(Point(72, 73));
  195. slotsPos.push_back(Point(108, 73));
  196. slotsPos.push_back(Point(18, 122));
  197. slotsPos.push_back(Point(54, 122));
  198. slotsPos.push_back(Point(90, 122));
  199. slotsPos.push_back(Point(126, 122));
  200. for(auto & slot : army.army)
  201. {
  202. if(slot.first.getNum() >= GameConstants::ARMY_SIZE)
  203. {
  204. logGlobal->warn("%s has stack in slot %d", army.name, slot.first.getNum());
  205. continue;
  206. }
  207. icons.push_back(std::make_shared<CAnimImage>("CPRSMALL", slot.second.type->getIconIndex(), 0, slotsPos[slot.first.getNum()].x, slotsPos[slot.first.getNum()].y));
  208. std::string subtitle;
  209. if(army.army.isDetailed)
  210. {
  211. subtitle = TextOperations::formatMetric(slot.second.count, 4);
  212. }
  213. else
  214. {
  215. //if =0 - we have no information about stack size at all
  216. if(slot.second.count)
  217. {
  218. if(settings["gameTweaks"]["numericCreaturesQuantities"].Bool())
  219. {
  220. subtitle = CCreature::getQuantityRangeStringForId((CCreature::CreatureQuantityId)slot.second.count);
  221. }
  222. else
  223. {
  224. subtitle = CGI->generaltexth->arraytxt[171 + 3*(slot.second.count)];
  225. }
  226. }
  227. }
  228. 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));
  229. }
  230. }
  231. CArmyTooltip::CArmyTooltip(Point pos, const InfoAboutArmy & army):
  232. CIntObject(0, pos)
  233. {
  234. init(army);
  235. }
  236. CArmyTooltip::CArmyTooltip(Point pos, const CArmedInstance * army):
  237. CIntObject(0, pos)
  238. {
  239. init(InfoAboutArmy(army, true));
  240. }
  241. void CHeroTooltip::init(const InfoAboutHero & hero)
  242. {
  243. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  244. portrait = std::make_shared<CAnimImage>("PortraitsLarge", hero.portrait, 0, 3, 2);
  245. if(hero.details)
  246. {
  247. for(size_t i = 0; i < hero.details->primskills.size(); i++)
  248. labels.push_back(std::make_shared<CLabel>(75 + 28 * (int)i, 58, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE,
  249. std::to_string(hero.details->primskills[i])));
  250. labels.push_back(std::make_shared<CLabel>(158, 98, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, std::to_string(hero.details->mana)));
  251. morale = std::make_shared<CAnimImage>("IMRL22", hero.details->morale + 3, 0, 5, 74);
  252. luck = std::make_shared<CAnimImage>("ILCK22", hero.details->luck + 3, 0, 5, 91);
  253. }
  254. }
  255. CHeroTooltip::CHeroTooltip(Point pos, const InfoAboutHero &hero):
  256. CArmyTooltip(pos, hero)
  257. {
  258. init(hero);
  259. }
  260. CHeroTooltip::CHeroTooltip(Point pos, const CGHeroInstance * hero):
  261. CArmyTooltip(pos, InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED))
  262. {
  263. init(InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED));
  264. }
  265. void CTownTooltip::init(const InfoAboutTown & town)
  266. {
  267. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  268. //order of icons in def: fort, citadel, castle, no fort
  269. size_t fortIndex = town.fortLevel ? town.fortLevel - 1 : 3;
  270. fort = std::make_shared<CAnimImage>("ITMCLS", fortIndex, 0, 105, 31);
  271. assert(town.tType);
  272. size_t iconIndex = town.tType->clientInfo.icons[town.fortLevel > 0][town.built >= CGI->settings()->getInteger(EGameSettings::TOWNS_BUILDINGS_PER_TURN_CAP)];
  273. build = std::make_shared<CAnimImage>("itpt", iconIndex, 0, 3, 2);
  274. if(town.details)
  275. {
  276. fort = std::make_shared<CAnimImage>("ITMTLS", town.details->hallLevel, 0, 67, 31);
  277. if(town.details->goldIncome)
  278. {
  279. income = std::make_shared<CLabel>(157, 58, FONT_TINY, ETextAlignment::CENTER, Colors::WHITE,
  280. std::to_string(town.details->goldIncome));
  281. }
  282. if(town.details->garrisonedHero) //garrisoned hero icon
  283. garrisonedHero = std::make_shared<CPicture>("TOWNQKGH", 149, 76);
  284. if(town.details->customRes)//silo is built
  285. {
  286. if(town.tType->primaryRes == EGameResID::WOOD_AND_ORE )// wood & ore
  287. {
  288. res1 = std::make_shared<CAnimImage>("SMALRES", GameResID(EGameResID::WOOD), 0, 7, 75);
  289. res2 = std::make_shared<CAnimImage>("SMALRES", GameResID(EGameResID::ORE), 0, 7, 88);
  290. }
  291. else
  292. {
  293. res1 = std::make_shared<CAnimImage>("SMALRES", town.tType->primaryRes, 0, 7, 81);
  294. }
  295. }
  296. }
  297. }
  298. CTownTooltip::CTownTooltip(Point pos, const InfoAboutTown & town)
  299. : CArmyTooltip(pos, town)
  300. {
  301. init(town);
  302. }
  303. CTownTooltip::CTownTooltip(Point pos, const CGTownInstance * town)
  304. : CArmyTooltip(pos, InfoAboutTown(town, true))
  305. {
  306. init(InfoAboutTown(town, true));
  307. }
  308. void MoraleLuckBox::set(const IBonusBearer * node)
  309. {
  310. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  311. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  312. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  313. const int neutralDescr[] = {60, 86}; //eg {Neutral Morale} \n\n Neutral morale means your armies will neither be blessed with extra attacks or freeze in combat.
  314. const int componentType[] = {CComponent::luck, CComponent::morale};
  315. const int hoverTextBase[] = {7, 4};
  316. TConstBonusListPtr modifierList = std::make_shared<const BonusList>();
  317. bonusValue = 0;
  318. if(node)
  319. bonusValue = morale ? node->MoraleValAndBonusList(modifierList) : node->LuckValAndBonusList(modifierList);
  320. int mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  321. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  322. baseType = componentType[morale];
  323. text = CGI->generaltexth->arraytxt[textId[morale]];
  324. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  325. if (morale && node && (node->hasBonusOfType(Bonus::UNDEAD)
  326. || node->hasBonusOfType(Bonus::NON_LIVING)))
  327. {
  328. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  329. bonusValue = 0;
  330. }
  331. else if(morale && node && node->hasBonusOfType(Bonus::NO_MORALE))
  332. {
  333. auto noMorale = node->getBonus(Selector::type()(Bonus::NO_MORALE));
  334. text += "\n" + noMorale->Description();
  335. bonusValue = 0;
  336. }
  337. else if (!morale && node && node->hasBonusOfType(Bonus::NO_LUCK))
  338. {
  339. auto noLuck = node->getBonus(Selector::type()(Bonus::NO_LUCK));
  340. text += "\n" + noLuck->Description();
  341. bonusValue = 0;
  342. }
  343. else
  344. {
  345. std::string addInfo = "";
  346. for(auto & bonus : * modifierList)
  347. {
  348. if(bonus->val)
  349. addInfo += "\n" + bonus->Description();
  350. }
  351. text = addInfo.empty()
  352. ? text + CGI->generaltexth->arraytxt[noneTxtId]
  353. : text + addInfo;
  354. }
  355. std::string imageName;
  356. if (small)
  357. imageName = morale ? "IMRL30": "ILCK30";
  358. else
  359. imageName = morale ? "IMRL42" : "ILCK42";
  360. image = std::make_shared<CAnimImage>(imageName, bonusValue + 3);
  361. image->moveBy(Point(pos.w/2 - image->pos.w/2, pos.h/2 - image->pos.h/2));//center icon
  362. }
  363. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r, bool Small)
  364. : morale(Morale),
  365. small(Small)
  366. {
  367. bonusValue = 0;
  368. pos = r + pos.topLeft();
  369. defActions = 255-DISPOSE;
  370. }
  371. CCreaturePic::CCreaturePic(int x, int y, const CCreature * cre, bool Big, bool Animated)
  372. {
  373. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  374. pos.x+=x;
  375. pos.y+=y;
  376. TFaction faction = cre->getFactionIndex();
  377. assert(CGI->townh->size() > faction);
  378. if(Big)
  379. bg = std::make_shared<CPicture>((*CGI->townh)[faction]->creatureBg130);
  380. else
  381. bg = std::make_shared<CPicture>((*CGI->townh)[faction]->creatureBg120);
  382. anim = std::make_shared<CCreatureAnim>(0, 0, cre->animDefName);
  383. anim->clipRect(cre->isDoubleWide()?170:150, 155, bg->pos.w, bg->pos.h);
  384. anim->startPreview(cre->hasBonusOfType(Bonus::SIEGE_WEAPON));
  385. amount = std::make_shared<CLabel>(bg->pos.w, bg->pos.h, FONT_MEDIUM, ETextAlignment::BOTTOMRIGHT, Colors::WHITE);
  386. pos.w = bg->pos.w;
  387. pos.h = bg->pos.h;
  388. }
  389. void CCreaturePic::show(SDL_Surface * to)
  390. {
  391. // redraw everything in a proper order
  392. bg->showAll(to);
  393. anim->show(to);
  394. amount->showAll(to);
  395. }
  396. void CCreaturePic::setAmount(int newAmount)
  397. {
  398. if(newAmount != 0)
  399. amount->setText(std::to_string(newAmount));
  400. else
  401. amount->setText("");
  402. }