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