MiscWidgets.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #include "StdInc.h"
  2. #include "MiscWidgets.h"
  3. #include "CComponent.h"
  4. #include "../gui/CGuiHandler.h"
  5. #include "../gui/CCursorHandler.h"
  6. #include "../CBitmapHandler.h"
  7. #include "../CPlayerInterface.h"
  8. #include "../CMessage.h"
  9. #include "../CGameInfo.h"
  10. #include "../windows/CAdvmapInterface.h"
  11. #include "../windows/CCastleInterface.h"
  12. #include "../windows/InfoWindows.h"
  13. #include "../../CCallback.h"
  14. #include "../../lib/mapObjects/CGHeroInstance.h"
  15. #include "../../lib/mapObjects/CGTownInstance.h"
  16. #include "../../lib/CGeneralTextHandler.h"
  17. #include "../../lib/CModHandler.h"
  18. #include "../../lib/CGameState.h"
  19. /*
  20. * MiscWidgets.cpp, part of VCMI engine
  21. *
  22. * Authors: listed in file AUTHORS in main folder
  23. *
  24. * License: GNU General Public License v2.0 or later
  25. * Full text of license available in license.txt file, in main folder
  26. *
  27. */
  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. }
  84. CComponent * LRClickableAreaWTextComp::createComponent() const
  85. {
  86. if(baseType >= 0)
  87. return new CComponent(CComponent::Etype(baseType), type, bonusValue);
  88. else
  89. return nullptr;
  90. }
  91. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  92. {
  93. if(down)
  94. {
  95. if(CComponent *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):hero(_hero)
  104. {
  105. OBJ_CONSTRUCTION_CAPTURING_ALL;
  106. addUsedEvents(LCLICK | RCLICK | HOVER);
  107. pos.x += x; pos.w = 58;
  108. pos.y += y; pos.h = 64;
  109. if (hero)
  110. new CAnimImage("PortraitsLarge", hero->portrait);
  111. }
  112. void CHeroArea::clickLeft(tribool down, bool previousState)
  113. {
  114. if((!down) && previousState && hero)
  115. LOCPLINT->openHeroWindow(hero);
  116. }
  117. void CHeroArea::clickRight(tribool down, bool previousState)
  118. {
  119. if((!down) && previousState && hero)
  120. LOCPLINT->openHeroWindow(hero);
  121. }
  122. void CHeroArea::hover(bool on)
  123. {
  124. if (on && hero)
  125. GH.statusbar->setText(hero->getObjectName());
  126. else
  127. GH.statusbar->clear();
  128. }
  129. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  130. {
  131. if((!down) && previousState && town)
  132. {
  133. LOCPLINT->openTownWindow(town);
  134. if ( type == 2 )
  135. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::VILLAGE_HALL);
  136. else if ( type == 3 && town->fortLevel() )
  137. LOCPLINT->castleInt->builds->buildingClicked(BuildingID::FORT);
  138. }
  139. }
  140. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  141. {
  142. if((!down) && previousState && town)
  143. LOCPLINT->openTownWindow(town);//TODO: popup?
  144. }
  145. LRClickableAreaOpenTown::LRClickableAreaOpenTown()
  146. : LRClickableAreaWTextComp(Rect(0,0,0,0), -1)
  147. {
  148. }
  149. void CMinorResDataBar::show(SDL_Surface * to)
  150. {
  151. }
  152. void CMinorResDataBar::showAll(SDL_Surface * to)
  153. {
  154. blitAt(bg,pos.x,pos.y,to);
  155. for (Res::ERes i=Res::WOOD; i<=Res::GOLD; vstd::advance(i, 1))
  156. {
  157. std::string text = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(i));
  158. graphics->fonts[FONT_SMALL]->renderTextCenter(to, text, Colors::WHITE, Point(pos.x + 50 + 76 * i, pos.y + pos.h/2));
  159. }
  160. std::vector<std::string> temp;
  161. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::MONTH)));
  162. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::WEEK)));
  163. temp.push_back(boost::lexical_cast<std::string>(LOCPLINT->cb->getDate(Date::DAY_OF_WEEK)));
  164. std::string datetext = CGI->generaltexth->allTexts[62]+": %s, " + CGI->generaltexth->allTexts[63]
  165. + ": %s, " + CGI->generaltexth->allTexts[64] + ": %s";
  166. 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));
  167. }
  168. CMinorResDataBar::CMinorResDataBar()
  169. {
  170. bg = BitmapHandler::loadBitmap("KRESBAR.bmp");
  171. CSDL_Ext::setDefaultColorKey(bg);
  172. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  173. pos.x = 7;
  174. pos.y = 575;
  175. pos.w = bg->w;
  176. pos.h = bg->h;
  177. }
  178. CMinorResDataBar::~CMinorResDataBar()
  179. {
  180. SDL_FreeSurface(bg);
  181. }
  182. void CArmyTooltip::init(const InfoAboutArmy &army)
  183. {
  184. OBJ_CONSTRUCTION_CAPTURING_ALL;
  185. new CLabel(66, 2, FONT_SMALL, 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->warnStream() << "Warning: " << army.name << " has stack in slot " << slot.first;
  199. continue;
  200. }
  201. new CAnimImage("CPRSMALL", slot.second.type->iconIndex, 0, slotsPos[slot.first.getNum()].x, slotsPos[slot.first.getNum()].y);
  202. std::string subtitle;
  203. if(army.army.isDetailed)
  204. subtitle = boost::lexical_cast<std::string>(slot.second.count);
  205. else
  206. {
  207. //if =0 - we have no information about stack size at all
  208. if (slot.second.count)
  209. subtitle = CGI->generaltexth->arraytxt[171 + 3*(slot.second.count)];
  210. }
  211. new CLabel(slotsPos[slot.first.getNum()].x + 17, slotsPos[slot.first.getNum()].y + 41, FONT_TINY, CENTER, Colors::WHITE, subtitle);
  212. }
  213. }
  214. CArmyTooltip::CArmyTooltip(Point pos, const InfoAboutArmy &army):
  215. CIntObject(0, pos)
  216. {
  217. init(army);
  218. }
  219. CArmyTooltip::CArmyTooltip(Point pos, const CArmedInstance * army):
  220. CIntObject(0, pos)
  221. {
  222. init(InfoAboutArmy(army, true));
  223. }
  224. void CHeroTooltip::init(const InfoAboutHero &hero)
  225. {
  226. OBJ_CONSTRUCTION_CAPTURING_ALL;
  227. new CAnimImage("PortraitsLarge", hero.portrait, 0, 3, 2);
  228. if(hero.details)
  229. {
  230. for (size_t i = 0; i < hero.details->primskills.size(); i++)
  231. new CLabel(75 + 28 * i, 58, FONT_SMALL, CENTER, Colors::WHITE,
  232. boost::lexical_cast<std::string>(hero.details->primskills[i]));
  233. new CLabel(158, 98, FONT_TINY, CENTER, Colors::WHITE,
  234. boost::lexical_cast<std::string>(hero.details->mana));
  235. new CAnimImage("IMRL22", hero.details->morale + 3, 0, 5, 74);
  236. new CAnimImage("ILCK22", hero.details->luck + 3, 0, 5, 91);
  237. }
  238. }
  239. CHeroTooltip::CHeroTooltip(Point pos, const InfoAboutHero &hero):
  240. CArmyTooltip(pos, hero)
  241. {
  242. init(hero);
  243. }
  244. CHeroTooltip::CHeroTooltip(Point pos, const CGHeroInstance * hero):
  245. CArmyTooltip(pos, InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED))
  246. {
  247. init(InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED));
  248. }
  249. void CTownTooltip::init(const InfoAboutTown &town)
  250. {
  251. OBJ_CONSTRUCTION_CAPTURING_ALL;
  252. //order of icons in def: fort, citadel, castle, no fort
  253. size_t fortIndex = town.fortLevel ? town.fortLevel - 1 : 3;
  254. new CAnimImage("ITMCLS", fortIndex, 0, 105, 31);
  255. assert(town.tType);
  256. size_t iconIndex = town.tType->clientInfo.icons[town.fortLevel > 0][town.built >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  257. new CAnimImage("itpt", iconIndex, 0, 3, 2);
  258. if(town.details)
  259. {
  260. new CAnimImage("ITMTLS", town.details->hallLevel, 0, 67, 31);
  261. if (town.details->goldIncome)
  262. new CLabel(157, 58, FONT_TINY, CENTER, Colors::WHITE,
  263. boost::lexical_cast<std::string>(town.details->goldIncome));
  264. if(town.details->garrisonedHero) //garrisoned hero icon
  265. new CPicture("TOWNQKGH", 149, 76);
  266. if(town.details->customRes)//silo is built
  267. {
  268. if (town.tType->primaryRes == Res::WOOD_AND_ORE )// wood & ore
  269. {
  270. new CAnimImage("SMALRES", Res::WOOD, 0, 7, 75);
  271. new CAnimImage("SMALRES", Res::ORE , 0, 7, 88);
  272. }
  273. else
  274. new CAnimImage("SMALRES", town.tType->primaryRes, 0, 7, 81);
  275. }
  276. }
  277. }
  278. CTownTooltip::CTownTooltip(Point pos, const InfoAboutTown &town):
  279. CArmyTooltip(pos, town)
  280. {
  281. init(town);
  282. }
  283. CTownTooltip::CTownTooltip(Point pos, const CGTownInstance * town):
  284. CArmyTooltip(pos, InfoAboutTown(town, true))
  285. {
  286. init(InfoAboutTown(town, true));
  287. }
  288. void MoraleLuckBox::set(const IBonusBearer *node)
  289. {
  290. OBJ_CONSTRUCTION_CAPTURING_ALL;
  291. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  292. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  293. 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.
  294. const int componentType[] = {CComponent::luck, CComponent::morale};
  295. const int hoverTextBase[] = {7, 4};
  296. const Bonus::BonusType bonusType[] = {Bonus::LUCK, Bonus::MORALE};
  297. int (IBonusBearer::*getValue[])() const = {&IBonusBearer::LuckVal, &IBonusBearer::MoraleVal};
  298. TBonusListPtr modifierList(new BonusList());
  299. if (node)
  300. {
  301. modifierList = node->getBonuses(Selector::type(bonusType[morale]));
  302. bonusValue = (node->*getValue[morale])();
  303. }
  304. else
  305. bonusValue = 0;
  306. int mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  307. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  308. baseType = componentType[morale];
  309. text = CGI->generaltexth->arraytxt[textId[morale]];
  310. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  311. if (morale && node && (node->hasBonusOfType(Bonus::UNDEAD)
  312. || node->hasBonusOfType(Bonus::BLOCK_MORALE)
  313. || node->hasBonusOfType(Bonus::NON_LIVING)))
  314. {
  315. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  316. bonusValue = 0;
  317. }
  318. else if(!morale && node && node->hasBonusOfType(Bonus::BLOCK_LUCK))
  319. {
  320. // TODO: there is no text like "Unaffected by luck" so probably we need own text
  321. text += CGI->generaltexth->arraytxt[noneTxtId];
  322. bonusValue = 0;
  323. }
  324. else if(modifierList->empty())
  325. text += CGI->generaltexth->arraytxt[noneTxtId];//no modifiers
  326. else
  327. {
  328. for(const Bonus * elem : *modifierList)
  329. {
  330. if(elem->val != 0)
  331. //no bonuses with value 0
  332. text += "\n" + elem->Description();
  333. }
  334. }
  335. std::string imageName;
  336. if (small)
  337. imageName = morale ? "IMRL30": "ILCK30";
  338. else
  339. imageName = morale ? "IMRL42" : "ILCK42";
  340. delete image;
  341. image = new CAnimImage(imageName, bonusValue + 3);
  342. image->moveBy(Point(pos.w/2 - image->pos.w/2, pos.h/2 - image->pos.h/2));//center icon
  343. }
  344. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r, bool Small):
  345. image(nullptr),
  346. morale(Morale),
  347. small(Small)
  348. {
  349. bonusValue = 0;
  350. pos = r + pos;
  351. }
  352. CCreaturePic::CCreaturePic(int x, int y, const CCreature *cre, bool Big, bool Animated)
  353. {
  354. OBJ_CONSTRUCTION_CAPTURING_ALL;
  355. pos.x+=x;
  356. pos.y+=y;
  357. TFaction faction = cre->faction;
  358. assert(CGI->townh->factions.size() > faction);
  359. if(Big)
  360. bg = new CPicture(CGI->townh->factions[faction]->creatureBg130);
  361. else
  362. bg = new CPicture(CGI->townh->factions[faction]->creatureBg120);
  363. anim = new CCreatureAnim(0, 0, cre->animDefName, Rect());
  364. anim->clipRect(cre->isDoubleWide()?170:150, 155, bg->pos.w, bg->pos.h);
  365. anim->startPreview(cre->hasBonusOfType(Bonus::SIEGE_WEAPON));
  366. amount = new CLabel(bg->pos.w, bg->pos.h, FONT_MEDIUM, BOTTOMRIGHT, Colors::WHITE);
  367. pos.w = bg->pos.w;
  368. pos.h = bg->pos.h;
  369. }
  370. void CCreaturePic::show(SDL_Surface *to)
  371. {
  372. // redraw everything in a proper order
  373. bg->showAll(to);
  374. anim->show(to);
  375. amount->showAll(to);
  376. }
  377. void CCreaturePic::setAmount(int newAmount)
  378. {
  379. if (newAmount != 0)
  380. amount->setText(boost::lexical_cast<std::string>(newAmount));
  381. else
  382. amount->setText("");
  383. }