MiscWidgets.cpp 13 KB

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