MiscWidgets.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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), type(-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)
  104. : hero(_hero)
  105. {
  106. OBJ_CONSTRUCTION_CAPTURING_ALL;
  107. addUsedEvents(LCLICK | RCLICK | HOVER);
  108. pos.x += x;
  109. pos.w = 58;
  110. pos.y += y;
  111. pos.h = 64;
  112. if(hero)
  113. new 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->setText(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. blitAt(bg, pos.x, pos.y, 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. bg = BitmapHandler::loadBitmap("KRESBAR.bmp");
  174. CSDL_Ext::setDefaultColorKey(bg);
  175. graphics->blueToPlayersAdv(bg, LOCPLINT->playerID);
  176. pos.x = 7;
  177. pos.y = 575;
  178. pos.w = bg->w;
  179. pos.h = bg->h;
  180. }
  181. CMinorResDataBar::~CMinorResDataBar()
  182. {
  183. SDL_FreeSurface(bg);
  184. }
  185. void CArmyTooltip::init(const InfoAboutArmy & army)
  186. {
  187. OBJ_CONSTRUCTION_CAPTURING_ALL;
  188. new CLabel(66, 2, FONT_SMALL, TOPLEFT, Colors::WHITE, army.name);
  189. std::vector<Point> slotsPos;
  190. slotsPos.push_back(Point(36, 73));
  191. slotsPos.push_back(Point(72, 73));
  192. slotsPos.push_back(Point(108, 73));
  193. slotsPos.push_back(Point(18, 122));
  194. slotsPos.push_back(Point(54, 122));
  195. slotsPos.push_back(Point(90, 122));
  196. slotsPos.push_back(Point(126, 122));
  197. for(auto & slot : army.army)
  198. {
  199. if(slot.first.getNum() >= GameConstants::ARMY_SIZE)
  200. {
  201. logGlobal->warnStream() << "Warning: " << army.name << " has stack in slot " << slot.first;
  202. continue;
  203. }
  204. new CAnimImage("CPRSMALL", slot.second.type->iconIndex, 0, slotsPos[slot.first.getNum()].x, slotsPos[slot.first.getNum()].y);
  205. std::string subtitle;
  206. if(army.army.isDetailed)
  207. subtitle = boost::lexical_cast<std::string>(slot.second.count);
  208. else
  209. {
  210. //if =0 - we have no information about stack size at all
  211. if(slot.second.count)
  212. subtitle = CGI->generaltexth->arraytxt[171 + 3 * (slot.second.count)];
  213. }
  214. new CLabel(slotsPos[slot.first.getNum()].x + 17, slotsPos[slot.first.getNum()].y + 41, FONT_TINY, CENTER, Colors::WHITE, subtitle);
  215. }
  216. }
  217. CArmyTooltip::CArmyTooltip(Point pos, const InfoAboutArmy & army)
  218. : CIntObject(0, pos)
  219. {
  220. init(army);
  221. }
  222. CArmyTooltip::CArmyTooltip(Point pos, const CArmedInstance * army)
  223. : CIntObject(0, pos)
  224. {
  225. init(InfoAboutArmy(army, true));
  226. }
  227. void CHeroTooltip::init(const InfoAboutHero & hero)
  228. {
  229. OBJ_CONSTRUCTION_CAPTURING_ALL;
  230. new CAnimImage("PortraitsLarge", hero.portrait, 0, 3, 2);
  231. if(hero.details)
  232. {
  233. for(size_t i = 0; i < hero.details->primskills.size(); i++)
  234. new CLabel(75 + 28 * i, 58, FONT_SMALL, CENTER, Colors::WHITE,
  235. boost::lexical_cast<std::string>(hero.details->primskills[i]));
  236. new CLabel(158, 98, FONT_TINY, CENTER, Colors::WHITE,
  237. boost::lexical_cast<std::string>(hero.details->mana));
  238. new CAnimImage("IMRL22", hero.details->morale + 3, 0, 5, 74);
  239. new CAnimImage("ILCK22", hero.details->luck + 3, 0, 5, 91);
  240. }
  241. }
  242. CHeroTooltip::CHeroTooltip(Point pos, const InfoAboutHero & hero)
  243. : CArmyTooltip(pos, hero)
  244. {
  245. init(hero);
  246. }
  247. CHeroTooltip::CHeroTooltip(Point pos, const CGHeroInstance * hero)
  248. : CArmyTooltip(pos, InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED))
  249. {
  250. init(InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED));
  251. }
  252. void CTownTooltip::init(const InfoAboutTown & town)
  253. {
  254. OBJ_CONSTRUCTION_CAPTURING_ALL;
  255. //order of icons in def: fort, citadel, castle, no fort
  256. size_t fortIndex = town.fortLevel ? town.fortLevel - 1 : 3;
  257. new CAnimImage("ITMCLS", fortIndex, 0, 105, 31);
  258. assert(town.tType);
  259. size_t iconIndex = town.tType->clientInfo.icons[town.fortLevel > 0][town.built >= CGI->modh->settings.MAX_BUILDING_PER_TURN];
  260. new CAnimImage("itpt", iconIndex, 0, 3, 2);
  261. if(town.details)
  262. {
  263. new CAnimImage("ITMTLS", town.details->hallLevel, 0, 67, 31);
  264. if(town.details->goldIncome)
  265. new CLabel(157, 58, FONT_TINY, CENTER, Colors::WHITE,
  266. boost::lexical_cast<std::string>(town.details->goldIncome));
  267. if(town.details->garrisonedHero) //garrisoned hero icon
  268. new 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. new CAnimImage("SMALRES", Res::WOOD, 0, 7, 75);
  274. new CAnimImage("SMALRES", Res::ORE, 0, 7, 88);
  275. }
  276. else
  277. new CAnimImage("SMALRES", town.tType->primaryRes, 0, 7, 81);
  278. }
  279. }
  280. }
  281. CTownTooltip::CTownTooltip(Point pos, const InfoAboutTown & town)
  282. : CArmyTooltip(pos, town)
  283. {
  284. init(town);
  285. }
  286. CTownTooltip::CTownTooltip(Point pos, const CGTownInstance * town)
  287. : CArmyTooltip(pos, InfoAboutTown(town, true))
  288. {
  289. init(InfoAboutTown(town, true));
  290. }
  291. void MoraleLuckBox::set(const IBonusBearer * node)
  292. {
  293. OBJ_CONSTRUCTION_CAPTURING_ALL;
  294. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  295. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  296. 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.
  297. const int componentType[] = {CComponent::luck, CComponent::morale};
  298. const int hoverTextBase[] = {7, 4};
  299. const Bonus::BonusType bonusType[] = {Bonus::LUCK, Bonus::MORALE};
  300. int (IBonusBearer::* getValue[])() const = {&IBonusBearer::LuckVal, &IBonusBearer::MoraleVal};
  301. TBonusListPtr modifierList(new BonusList());
  302. if(node)
  303. {
  304. modifierList = node->getBonuses(Selector::type(bonusType[morale]));
  305. bonusValue = (node->*getValue[morale])();
  306. }
  307. else
  308. bonusValue = 0;
  309. int mrlt = (bonusValue > 0) - (bonusValue < 0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  310. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  311. baseType = componentType[morale];
  312. text = CGI->generaltexth->arraytxt[textId[morale]];
  313. boost::algorithm::replace_first(text, "%s", CGI->generaltexth->arraytxt[neutralDescr[morale] - mrlt]);
  314. if(morale && node && (node->hasBonusOfType(Bonus::UNDEAD)
  315. || node->hasBonusOfType(Bonus::BLOCK_MORALE)
  316. || node->hasBonusOfType(Bonus::NON_LIVING)))
  317. {
  318. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  319. bonusValue = 0;
  320. }
  321. else if(!morale && node && node->hasBonusOfType(Bonus::BLOCK_LUCK))
  322. {
  323. // TODO: there is no text like "Unaffected by luck" so probably we need own text
  324. text += CGI->generaltexth->arraytxt[noneTxtId];
  325. bonusValue = 0;
  326. }
  327. else if(modifierList->empty())
  328. text += CGI->generaltexth->arraytxt[noneTxtId]; //no modifiers
  329. else
  330. {
  331. for(auto & elem : *modifierList)
  332. {
  333. if(elem->val != 0)
  334. //no bonuses with value 0
  335. text += "\n" + elem->Description();
  336. }
  337. }
  338. std::string imageName;
  339. if(small)
  340. imageName = morale ? "IMRL30" : "ILCK30";
  341. else
  342. imageName = morale ? "IMRL42" : "ILCK42";
  343. delete image;
  344. image = new CAnimImage(imageName, bonusValue + 3);
  345. image->moveBy(Point(pos.w / 2 - image->pos.w / 2, pos.h / 2 - image->pos.h / 2)); //center icon
  346. }
  347. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect & r, bool Small)
  348. : image(nullptr), morale(Morale), small(Small)
  349. {
  350. bonusValue = 0;
  351. pos = r + pos;
  352. }
  353. CCreaturePic::CCreaturePic(int x, int y, const CCreature * cre, bool Big, bool Animated)
  354. {
  355. OBJ_CONSTRUCTION_CAPTURING_ALL;
  356. pos.x += x;
  357. pos.y += y;
  358. TFaction faction = cre->faction;
  359. assert(CGI->townh->factions.size() > faction);
  360. if(Big)
  361. bg = new CPicture(CGI->townh->factions[faction]->creatureBg130);
  362. else
  363. bg = new CPicture(CGI->townh->factions[faction]->creatureBg120);
  364. anim = new CCreatureAnim(0, 0, cre->animDefName, Rect());
  365. anim->clipRect(cre->isDoubleWide() ? 170 : 150, 155, bg->pos.w, bg->pos.h);
  366. anim->startPreview(cre->hasBonusOfType(Bonus::SIEGE_WEAPON));
  367. amount = new CLabel(bg->pos.w, bg->pos.h, FONT_MEDIUM, BOTTOMRIGHT, Colors::WHITE);
  368. pos.w = bg->pos.w;
  369. pos.h = bg->pos.h;
  370. }
  371. void CCreaturePic::show(SDL_Surface * to)
  372. {
  373. // redraw everything in a proper order
  374. bg->showAll(to);
  375. anim->show(to);
  376. amount->showAll(to);
  377. }
  378. void CCreaturePic::setAmount(int newAmount)
  379. {
  380. if(newAmount != 0)
  381. amount->setText(boost::lexical_cast<std::string>(newAmount));
  382. else
  383. amount->setText("");
  384. }