GUIClasses.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  1. /*
  2. * GUIClasses.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 "GUIClasses.h"
  12. #include "CAdvmapInterface.h"
  13. #include "CCastleInterface.h"
  14. #include "CCreatureWindow.h"
  15. #include "CHeroWindow.h"
  16. #include "CreatureCostBox.h"
  17. #include "../CBitmapHandler.h"
  18. #include "../CGameInfo.h"
  19. #include "../CMessage.h"
  20. #include "../CMusicHandler.h"
  21. #include "../CPlayerInterface.h"
  22. #include "../CPreGame.h"
  23. #include "../CVideoHandler.h"
  24. #include "../Graphics.h"
  25. #include "../mapHandler.h"
  26. #include "../battle/CBattleInterfaceClasses.h"
  27. #include "../battle/CBattleInterface.h"
  28. #include "../battle/CCreatureAnimation.h"
  29. #include "../gui/CGuiHandler.h"
  30. #include "../gui/SDL_Extensions.h"
  31. #include "../gui/CCursorHandler.h"
  32. #include "../widgets/CComponent.h"
  33. #include "../widgets/MiscWidgets.h"
  34. #include "../windows/InfoWindows.h"
  35. #include "../../CCallback.h"
  36. #include "../lib/mapObjects/CGHeroInstance.h"
  37. #include "../lib/CArtHandler.h"
  38. #include "../lib/CBuildingHandler.h"
  39. #include "../lib/CConfigHandler.h"
  40. #include "../lib/CCreatureHandler.h"
  41. #include "../lib/CGameState.h"
  42. #include "../lib/CGeneralTextHandler.h"
  43. #include "../lib/CHeroHandler.h"
  44. #include "../lib/CModHandler.h"
  45. #include "../lib/CondSh.h"
  46. #include "../lib/CSkillHandler.h"
  47. #include "../lib/spells/CSpellHandler.h"
  48. #include "../lib/CStopWatch.h"
  49. #include "../lib/CTownHandler.h"
  50. #include "../lib/GameConstants.h"
  51. #include "../lib/HeroBonus.h"
  52. #include "../lib/mapping/CMap.h"
  53. #include "../lib/NetPacksBase.h"
  54. #include "../lib/StartInfo.h"
  55. using namespace CSDL_Ext;
  56. std::list<CFocusable*> CFocusable::focusables;
  57. CFocusable * CFocusable::inputWithFocus;
  58. #undef min
  59. #undef max
  60. CRecruitmentWindow::CCreatureCard::CCreatureCard(CRecruitmentWindow *window, const CCreature *crea, int totalAmount):
  61. CIntObject(LCLICK | RCLICK),
  62. parent(window),
  63. selected(false),
  64. creature(crea),
  65. amount(totalAmount)
  66. {
  67. OBJ_CONSTRUCTION_CAPTURING_ALL;
  68. pic = new CCreaturePic(1,1, creature, true, true);
  69. // 1 + 1 px for borders
  70. pos.w = pic->pos.w + 2;
  71. pos.h = pic->pos.h + 2;
  72. }
  73. void CRecruitmentWindow::CCreatureCard::select(bool on)
  74. {
  75. selected = on;
  76. redraw();
  77. }
  78. void CRecruitmentWindow::CCreatureCard::clickLeft(tribool down, bool previousState)
  79. {
  80. if (down)
  81. parent->select(this);
  82. }
  83. void CRecruitmentWindow::CCreatureCard::clickRight(tribool down, bool previousState)
  84. {
  85. if (down)
  86. GH.pushInt(new CStackWindow(creature, true));
  87. }
  88. void CRecruitmentWindow::CCreatureCard::showAll(SDL_Surface * to)
  89. {
  90. CIntObject::showAll(to);
  91. if (selected)
  92. drawBorder(to, pos, int3(248, 0, 0));
  93. else
  94. drawBorder(to, pos, int3(232, 212, 120));
  95. }
  96. void CRecruitmentWindow::select(CCreatureCard *card)
  97. {
  98. if (card == selected)
  99. return;
  100. if (selected)
  101. selected->select(false);
  102. selected = card;
  103. if (selected)
  104. selected->select(true);
  105. if (card)
  106. {
  107. si32 maxAmount = card->creature->maxAmount(LOCPLINT->cb->getResourceAmount());
  108. vstd::amin(maxAmount, card->amount);
  109. slider->setAmount(maxAmount);
  110. if(slider->getValue() != maxAmount)
  111. slider->moveTo(maxAmount);
  112. else // if slider already at 0 - emulate call to sliderMoved()
  113. sliderMoved(maxAmount);
  114. costPerTroopValue->createItems(card->creature->cost);
  115. totalCostValue->createItems(card->creature->cost);
  116. costPerTroopValue->set(card->creature->cost);
  117. totalCostValue->set(card->creature->cost * maxAmount);
  118. //Recruit %s
  119. title->setText(boost::str(boost::format(CGI->generaltexth->tcommands[21]) % card->creature->namePl));
  120. maxButton->block(maxAmount == 0);
  121. slider->block(maxAmount == 0);
  122. }
  123. }
  124. void CRecruitmentWindow::buy()
  125. {
  126. CreatureID crid = selected->creature->idNumber;
  127. SlotID dstslot = dst-> getSlotFor(crid);
  128. if(!dstslot.validSlot() && (selected->creature->warMachine == ArtifactID::NONE)) //no available slot
  129. {
  130. std::string txt;
  131. if(dst->ID == Obj::HERO)
  132. {
  133. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  134. boost::algorithm::replace_first(txt, "%s", slider->getValue() > 1 ? CGI->creh->creatures[crid]->namePl : CGI->creh->creatures[crid]->nameSing);
  135. }
  136. else
  137. {
  138. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  139. }
  140. LOCPLINT->showInfoDialog(txt);
  141. return;
  142. }
  143. onRecruit(crid, slider->getValue());
  144. if(level >= 0)
  145. close();
  146. }
  147. void CRecruitmentWindow::showAll(SDL_Surface * to)
  148. {
  149. CWindowObject::showAll(to);
  150. // recruit\total values
  151. drawBorder(to, pos.x + 172, pos.y + 222, 67, 42, int3(239,215,123));
  152. drawBorder(to, pos.x + 246, pos.y + 222, 67, 42, int3(239,215,123));
  153. //cost boxes
  154. drawBorder(to, pos.x + 64, pos.y + 222, 99, 76, int3(239,215,123));
  155. drawBorder(to, pos.x + 322, pos.y + 222, 99, 76, int3(239,215,123));
  156. //buttons borders
  157. drawBorder(to, pos.x + 133, pos.y + 312, 66, 34, int3(173,142,66));
  158. drawBorder(to, pos.x + 211, pos.y + 312, 66, 34, int3(173,142,66));
  159. drawBorder(to, pos.x + 289, pos.y + 312, 66, 34, int3(173,142,66));
  160. }
  161. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const std::function<void(CreatureID,int)> &Recruit, int y_offset):
  162. CWindowObject(PLAYER_COLORED, "TPRCRT"),
  163. onRecruit(Recruit),
  164. level(Level),
  165. dst(Dst),
  166. selected(nullptr),
  167. dwelling(Dwelling)
  168. {
  169. moveBy(Point(0, y_offset));
  170. OBJ_CONSTRUCTION_CAPTURING_ALL;
  171. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  172. slider = new CSlider(Point(176,279),135,std::bind(&CRecruitmentWindow::sliderMoved,this, _1),0,0,0,true);
  173. maxButton = new CButton(Point(134, 313), "IRCBTNS.DEF", CGI->generaltexth->zelp[553], std::bind(&CSlider::moveToMax,slider), SDLK_m);
  174. buyButton = new CButton(Point(212, 313), "IBY6432.DEF", CGI->generaltexth->zelp[554], std::bind(&CRecruitmentWindow::buy,this), SDLK_RETURN);
  175. cancelButton = new CButton(Point(290, 313), "ICN6432.DEF", CGI->generaltexth->zelp[555], std::bind(&CRecruitmentWindow::close,this), SDLK_ESCAPE);
  176. title = new CLabel(243, 32, FONT_BIG, CENTER, Colors::YELLOW);
  177. availableValue = new CLabel(205, 253, FONT_SMALL, CENTER, Colors::WHITE);
  178. toRecruitValue = new CLabel(279, 253, FONT_SMALL, CENTER, Colors::WHITE);
  179. costPerTroopValue = new CreatureCostBox(Rect(65, 222, 97, 74), CGI->generaltexth->allTexts[346]);
  180. totalCostValue = new CreatureCostBox(Rect(323, 222, 97, 74), CGI->generaltexth->allTexts[466]);
  181. new CLabel(205, 233, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[465]); //available t
  182. new CLabel(279, 233, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[16]); //recruit t
  183. availableCreaturesChanged();
  184. }
  185. void CRecruitmentWindow::availableCreaturesChanged()
  186. {
  187. OBJ_CONSTRUCTION_CAPTURING_ALL;
  188. size_t selectedIndex = 0;
  189. if (!cards.empty() && selected) // find position of selected item
  190. selectedIndex = std::find(cards.begin(), cards.end(), selected) - cards.begin();
  191. //deselect card
  192. select(nullptr);
  193. //delete old cards
  194. for(auto & card : cards)
  195. delete card;
  196. cards.clear();
  197. for(int i=0; i<dwelling->creatures.size(); i++)
  198. {
  199. //find appropriate level
  200. if(level >= 0 && i != level)
  201. continue;
  202. int amount = dwelling->creatures[i].first;
  203. //create new cards
  204. for(auto & creature : boost::adaptors::reverse(dwelling->creatures[i].second))
  205. cards.push_back(new CCreatureCard(this, CGI->creh->creatures[creature], amount));
  206. }
  207. assert(!cards.empty());
  208. const int creatureWidth = 102;
  209. //normal distance between cards - 18px
  210. int requiredSpace = 18;
  211. //maximum distance we can use without reaching window borders
  212. int availableSpace = pos.w - 50 - creatureWidth * cards.size();
  213. if (cards.size() > 1) // avoid division by zero
  214. availableSpace /= cards.size() - 1;
  215. else
  216. availableSpace = 0;
  217. assert(availableSpace >= 0);
  218. const int spaceBetween = std::min(requiredSpace, availableSpace);
  219. const int totalCreatureWidth = spaceBetween + creatureWidth;
  220. //now we know total amount of cards and can move them to correct position
  221. int curx = pos.w / 2 - (creatureWidth*cards.size()/2) - (spaceBetween*(cards.size()-1)/2);
  222. for(auto & card : cards)
  223. {
  224. card->moveBy(Point(curx, 64));
  225. curx += totalCreatureWidth;
  226. }
  227. //restore selection
  228. select(cards[selectedIndex]);
  229. if(slider->getValue() == slider->getAmount())
  230. slider->moveToMax();
  231. else // if slider already at 0 - emulate call to sliderMoved()
  232. sliderMoved(slider->getAmount());
  233. }
  234. void CRecruitmentWindow::sliderMoved(int to)
  235. {
  236. if (!selected)
  237. return;
  238. buyButton->block(!to);
  239. availableValue->setText(boost::lexical_cast<std::string>(selected->amount - to));
  240. toRecruitValue->setText(boost::lexical_cast<std::string>(to));
  241. totalCostValue->set(selected->creature->cost * to);
  242. }
  243. CSplitWindow::CSplitWindow(const CCreature * creature, std::function<void(int, int)> callback_,
  244. int leftMin_, int rightMin_, int leftAmount_, int rightAmount_):
  245. CWindowObject(PLAYER_COLORED, "GPUCRDIV"),
  246. callback(callback_),
  247. leftAmount(leftAmount_),
  248. rightAmount(rightAmount_),
  249. leftMin(leftMin_),
  250. rightMin(rightMin_),
  251. slider(nullptr)
  252. {
  253. OBJ_CONSTRUCTION_CAPTURING_ALL;
  254. int total = leftAmount + rightAmount;
  255. int leftMax = total - rightMin;
  256. int rightMax = total - leftMin;
  257. ok = new CButton(Point(20, 263), "IOK6432", CButton::tooltip(), std::bind(&CSplitWindow::apply, this), SDLK_RETURN);
  258. cancel = new CButton(Point(214, 263), "ICN6432", CButton::tooltip(), std::bind(&CSplitWindow::close, this), SDLK_ESCAPE);
  259. int sliderPositions = total - leftMin - rightMin;
  260. leftInput = new CTextInput(Rect(20, 218, 100, 36), FONT_BIG, std::bind(&CSplitWindow::setAmountText, this, _1, true));
  261. rightInput = new CTextInput(Rect(176, 218, 100, 36), FONT_BIG, std::bind(&CSplitWindow::setAmountText, this, _1, false));
  262. //add filters to allow only number input
  263. leftInput->filters += std::bind(&CTextInput::numberFilter, _1, _2, leftMin, leftMax);
  264. rightInput->filters += std::bind(&CTextInput::numberFilter, _1, _2, rightMin, rightMax);
  265. leftInput->setText(boost::lexical_cast<std::string>(leftAmount), false);
  266. rightInput->setText(boost::lexical_cast<std::string>(rightAmount), false);
  267. animLeft = new CCreaturePic(20, 54, creature, true, false);
  268. animRight = new CCreaturePic(177, 54,creature, true, false);
  269. slider = new CSlider(Point(21, 194), 257, std::bind(&CSplitWindow::sliderMoved, this, _1), 0, sliderPositions, rightAmount - rightMin, true);
  270. std::string title = CGI->generaltexth->allTexts[256];
  271. boost::algorithm::replace_first(title,"%s", creature->namePl);
  272. new CLabel(150, 34, FONT_BIG, CENTER, Colors::YELLOW, title);
  273. }
  274. void CSplitWindow::setAmountText(std::string text, bool left)
  275. {
  276. int amount = 0;
  277. if (text.length())
  278. {
  279. try
  280. {
  281. amount = boost::lexical_cast<int>(text);
  282. }
  283. catch(boost::bad_lexical_cast &)
  284. {
  285. amount = left ? leftAmount : rightAmount;
  286. }
  287. int total = leftAmount + rightAmount;
  288. if (amount > total)
  289. amount = total;
  290. }
  291. setAmount(amount, left);
  292. slider->moveTo(rightAmount - rightMin);
  293. }
  294. void CSplitWindow::setAmount(int value, bool left)
  295. {
  296. int total = leftAmount + rightAmount;
  297. leftAmount = left ? value : total - value;
  298. rightAmount = left ? total - value : value;
  299. leftInput->setText(boost::lexical_cast<std::string>(leftAmount));
  300. rightInput->setText(boost::lexical_cast<std::string>(rightAmount));
  301. }
  302. void CSplitWindow::apply()
  303. {
  304. callback(leftAmount, rightAmount);
  305. close();
  306. }
  307. void CSplitWindow::sliderMoved(int to)
  308. {
  309. setAmount(rightMin + to, false);
  310. }
  311. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, std::function<void(ui32)> callback):
  312. CWindowObject(PLAYER_COLORED, "LVLUPBKG"),
  313. cb(callback)
  314. {
  315. OBJ_CONSTRUCTION_CAPTURING_ALL;
  316. LOCPLINT->showingDialog->setn(true);
  317. new CAnimImage("PortraitsLarge", hero->portrait, 0, 170, 66);
  318. new CButton(Point(297, 413), "IOKAY", CButton::tooltip(), std::bind(&CLevelWindow::close, this), SDLK_RETURN);
  319. //%s has gained a level.
  320. new CLabel(192, 33, FONT_MEDIUM, CENTER, Colors::WHITE,
  321. boost::str(boost::format(CGI->generaltexth->allTexts[444]) % hero->name));
  322. //%s is now a level %d %s.
  323. new CLabel(192, 162, FONT_MEDIUM, CENTER, Colors::WHITE,
  324. boost::str(boost::format(CGI->generaltexth->allTexts[445]) % hero->name % hero->level % hero->type->heroClass->name));
  325. new CAnimImage("PSKIL42", pskill, 0, 174, 190);
  326. new CLabel(192, 253, FONT_MEDIUM, CENTER, Colors::WHITE,
  327. CGI->generaltexth->primarySkillNames[pskill] + " +1");
  328. if (!skills.empty())
  329. {
  330. std::vector<CSelectableComponent *> comps;
  331. for(auto & skill : skills)
  332. {
  333. comps.push_back(new CSelectableComponent(
  334. CComponent::secskill,
  335. skill,
  336. hero->getSecSkillLevel( SecondarySkill(skill) )+1,
  337. CComponent::medium));
  338. }
  339. box = new CComponentBox(comps, Rect(75, 300, pos.w - 150, 100));
  340. }
  341. else
  342. box = nullptr;
  343. }
  344. CLevelWindow::~CLevelWindow()
  345. {
  346. //FIXME: call callback if there was nothing to select?
  347. if (box && box->selectedIndex() != -1)
  348. cb(box->selectedIndex());
  349. LOCPLINT->showingDialog->setn(false);
  350. }
  351. static void setIntSetting(std::string group, std::string field, int value)
  352. {
  353. Settings entry = settings.write[group][field];
  354. entry->Float() = value;
  355. }
  356. static void setBoolSetting(std::string group, std::string field, bool value)
  357. {
  358. Settings fullscreen = settings.write[group][field];
  359. fullscreen->Bool() = value;
  360. }
  361. CSystemOptionsWindow::CSystemOptionsWindow():
  362. CWindowObject(PLAYER_COLORED, "SysOpBck"),
  363. onFullscreenChanged(settings.listen["video"]["fullscreen"])
  364. {
  365. OBJ_CONSTRUCTION_CAPTURING_ALL;
  366. title = new CLabel(242, 32, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[568]);
  367. const JsonNode & texts = CGI->generaltexth->localizedTexts["systemOptions"];
  368. //left window section
  369. leftGroup = new CLabelGroup(FONT_MEDIUM, CENTER, Colors::YELLOW);
  370. leftGroup->add(122, 64, CGI->generaltexth->allTexts[569]);
  371. leftGroup->add(122, 130, CGI->generaltexth->allTexts[570]);
  372. leftGroup->add(122, 196, CGI->generaltexth->allTexts[571]);
  373. leftGroup->add(122, 262, texts["resolutionButton"]["label"].String());
  374. leftGroup->add(122, 347, CGI->generaltexth->allTexts[394]);
  375. leftGroup->add(122, 412, CGI->generaltexth->allTexts[395]);
  376. //right section
  377. rightGroup = new CLabelGroup(FONT_MEDIUM, TOPLEFT, Colors::WHITE);
  378. rightGroup->add(282, 57, CGI->generaltexth->allTexts[572]);
  379. rightGroup->add(282, 89, CGI->generaltexth->allTexts[573]);
  380. rightGroup->add(282, 121, CGI->generaltexth->allTexts[574]);
  381. rightGroup->add(282, 153, CGI->generaltexth->allTexts[577]);
  382. rightGroup->add(282, 185, texts["creatureWindowButton"]["label"].String());
  383. rightGroup->add(282, 217, texts["fullscreenButton"]["label"].String());
  384. //setting up buttons
  385. load = new CButton (Point(246, 298), "SOLOAD.DEF", CGI->generaltexth->zelp[321], [&](){ bloadf(); }, SDLK_l);
  386. load->setImageOrder(1, 0, 2, 3);
  387. save = new CButton (Point(357, 298), "SOSAVE.DEF", CGI->generaltexth->zelp[322], [&](){ bsavef(); }, SDLK_s);
  388. save->setImageOrder(1, 0, 2, 3);
  389. restart = new CButton (Point(246, 357), "SORSTRT", CGI->generaltexth->zelp[323], [&](){ brestartf(); }, SDLK_r);
  390. restart->setImageOrder(1, 0, 2, 3);
  391. mainMenu = new CButton (Point(357, 357), "SOMAIN.DEF", CGI->generaltexth->zelp[320], [&](){ bmainmenuf(); }, SDLK_m);
  392. mainMenu->setImageOrder(1, 0, 2, 3);
  393. quitGame = new CButton (Point(246, 415), "soquit.def", CGI->generaltexth->zelp[324], [&](){ bquitf(); }, SDLK_q);
  394. quitGame->setImageOrder(1, 0, 2, 3);
  395. backToMap = new CButton ( Point(357, 415), "soretrn.def", CGI->generaltexth->zelp[325], [&](){ breturnf(); }, SDLK_RETURN);
  396. backToMap->setImageOrder(1, 0, 2, 3);
  397. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  398. heroMoveSpeed = new CToggleGroup(0);
  399. heroMoveSpeed->addToggle(1, new CToggleButton(Point( 28, 77), "sysopb1.def", CGI->generaltexth->zelp[349]));
  400. heroMoveSpeed->addToggle(2, new CToggleButton(Point( 76, 77), "sysopb2.def", CGI->generaltexth->zelp[350]));
  401. heroMoveSpeed->addToggle(4, new CToggleButton(Point(124, 77), "sysopb3.def", CGI->generaltexth->zelp[351]));
  402. heroMoveSpeed->addToggle(8, new CToggleButton(Point(172, 77), "sysopb4.def", CGI->generaltexth->zelp[352]));
  403. heroMoveSpeed->setSelected(settings["adventure"]["heroSpeed"].Float());
  404. heroMoveSpeed->addCallback(std::bind(&setIntSetting, "adventure", "heroSpeed", _1));
  405. enemyMoveSpeed = new CToggleGroup(0);
  406. enemyMoveSpeed->addToggle(2, new CToggleButton(Point( 28, 144), "sysopb5.def", CGI->generaltexth->zelp[353]));
  407. enemyMoveSpeed->addToggle(4, new CToggleButton(Point( 76, 144), "sysopb6.def", CGI->generaltexth->zelp[354]));
  408. enemyMoveSpeed->addToggle(8, new CToggleButton(Point(124, 144), "sysopb7.def", CGI->generaltexth->zelp[355]));
  409. enemyMoveSpeed->addToggle(0, new CToggleButton(Point(172, 144), "sysopb8.def", CGI->generaltexth->zelp[356]));
  410. enemyMoveSpeed->setSelected(settings["adventure"]["enemySpeed"].Float());
  411. enemyMoveSpeed->addCallback(std::bind(&setIntSetting, "adventure", "enemySpeed", _1));
  412. mapScrollSpeed = new CToggleGroup(0);
  413. mapScrollSpeed->addToggle(1, new CToggleButton(Point( 28, 210), "sysopb9.def", CGI->generaltexth->zelp[357]));
  414. mapScrollSpeed->addToggle(2, new CToggleButton(Point( 92, 210), "sysob10.def", CGI->generaltexth->zelp[358]));
  415. mapScrollSpeed->addToggle(4, new CToggleButton(Point(156, 210), "sysob11.def", CGI->generaltexth->zelp[359]));
  416. mapScrollSpeed->setSelected(settings["adventure"]["scrollSpeed"].Float());
  417. mapScrollSpeed->addCallback(std::bind(&setIntSetting, "adventure", "scrollSpeed", _1));
  418. musicVolume = new CVolumeSlider(Point(29, 359), "syslb.def", CCS->musich->getVolume(), &CGI->generaltexth->zelp[326]);
  419. musicVolume->addCallback(std::bind(&setIntSetting, "general", "music", _1));
  420. effectsVolume = new CVolumeSlider(Point(29, 425), "syslb.def", CCS->soundh->getVolume(), &CGI->generaltexth->zelp[336]);
  421. effectsVolume->addCallback(std::bind(&setIntSetting, "general", "sound", _1));
  422. showReminder = new CToggleButton(Point(246, 87), "sysopchk.def", CGI->generaltexth->zelp[361],
  423. [&] (bool value) { setBoolSetting("adventure", "heroReminder", value); });
  424. quickCombat = new CToggleButton(Point(246, 87+32), "sysopchk.def", CGI->generaltexth->zelp[362],
  425. [&] (bool value) { setBoolSetting("adventure", "quickCombat", value); });
  426. spellbookAnim = new CToggleButton(Point(246, 87+64), "sysopchk.def", CGI->generaltexth->zelp[364],
  427. [&] (bool value) { setBoolSetting("video", "spellbookAnimation", value); });
  428. fullscreen = new CToggleButton(Point(246, 215), "sysopchk.def", CButton::tooltip(texts["fullscreenButton"]),
  429. [&] (bool value) { setBoolSetting("video", "fullscreen", value); });
  430. showReminder->setSelected(settings["adventure"]["heroReminder"].Bool());
  431. quickCombat->setSelected(settings["adventure"]["quickCombat"].Bool());
  432. spellbookAnim->setSelected(settings["video"]["spellbookAnimation"].Bool());
  433. fullscreen->setSelected(settings["video"]["fullscreen"].Bool());
  434. onFullscreenChanged([&](const JsonNode &newState){ fullscreen->setSelected(newState.Bool());});
  435. gameResButton = new CButton(Point(28, 275),"buttons/resolution", CButton::tooltip(texts["resolutionButton"]),
  436. std::bind(&CSystemOptionsWindow::selectGameRes, this), SDLK_g);
  437. std::string resText;
  438. resText += boost::lexical_cast<std::string>(settings["video"]["screenRes"]["width"].Float());
  439. resText += "x";
  440. resText += boost::lexical_cast<std::string>(settings["video"]["screenRes"]["height"].Float());
  441. gameResLabel = new CLabel(170, 292, FONT_MEDIUM, CENTER, Colors::YELLOW, resText);
  442. }
  443. void CSystemOptionsWindow::selectGameRes()
  444. {
  445. std::vector<std::string> items;
  446. const JsonNode & texts = CGI->generaltexth->localizedTexts["systemOptions"]["resolutionMenu"];
  447. for( config::CConfigHandler::GuiOptionsMap::value_type& value : conf.guiOptions)
  448. {
  449. std::string resX = boost::lexical_cast<std::string>(value.first.first);
  450. std::string resY = boost::lexical_cast<std::string>(value.first.second);
  451. items.push_back(resX + 'x' + resY);
  452. }
  453. GH.pushInt(new CObjectListWindow(items, nullptr, texts["label"].String(), texts["help"].String(),
  454. std::bind(&CSystemOptionsWindow::setGameRes, this, _1)));
  455. }
  456. void CSystemOptionsWindow::setGameRes(int index)
  457. {
  458. auto iter = conf.guiOptions.begin();
  459. std::advance(iter, index);
  460. //do not set resolution to illegal one (0x0)
  461. assert(iter!=conf.guiOptions.end() && iter->first.first > 0 && iter->first.second > 0);
  462. Settings gameRes = settings.write["video"]["screenRes"];
  463. gameRes["width"].Float() = iter->first.first;
  464. gameRes["height"].Float() = iter->first.second;
  465. std::string resText;
  466. resText += boost::lexical_cast<std::string>(iter->first.first);
  467. resText += "x";
  468. resText += boost::lexical_cast<std::string>(iter->first.second);
  469. gameResLabel->setText(resText);
  470. }
  471. void CSystemOptionsWindow::bquitf()
  472. {
  473. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(SDL_USEREVENT, FORCE_QUIT); }, 0);
  474. }
  475. void CSystemOptionsWindow::breturnf()
  476. {
  477. GH.popIntTotally(this);
  478. }
  479. void CSystemOptionsWindow::bmainmenuf()
  480. {
  481. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], [this](){ closeAndPushEvent(SDL_USEREVENT, RETURN_TO_MAIN_MENU); }, 0);
  482. }
  483. void CSystemOptionsWindow::bloadf()
  484. {
  485. GH.popIntTotally(this);
  486. LOCPLINT->proposeLoadingGame();
  487. }
  488. void CSystemOptionsWindow::bsavef()
  489. {
  490. GH.popIntTotally(this);
  491. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  492. }
  493. void CSystemOptionsWindow::brestartf()
  494. {
  495. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[67], [this](){ closeAndPushEvent(SDL_USEREVENT, RESTART_GAME); }, 0);
  496. }
  497. void CSystemOptionsWindow::closeAndPushEvent(int eventType, int code)
  498. {
  499. GH.popIntTotally(this);
  500. GH.pushSDLEvent(eventType, code);
  501. }
  502. CTavernWindow::CTavernWindow(const CGObjectInstance *TavernObj):
  503. CWindowObject(PLAYER_COLORED, "TPTAVERN"),
  504. tavernObj(TavernObj)
  505. {
  506. OBJ_CONSTRUCTION_CAPTURING_ALL;
  507. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(TavernObj);
  508. if(h.size() < 2)
  509. h.resize(2, nullptr);
  510. h1 = new HeroPortrait(selected,0,72,299,h[0]);
  511. h2 = new HeroPortrait(selected,1,162,299,h[1]);
  512. selected = 0;
  513. if (!h[0])
  514. selected = 1;
  515. if (!h[0] && !h[1])
  516. selected = -1;
  517. oldSelected = -1;
  518. new CLabel(200, 35, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[37]);
  519. new CLabel(320, 328, FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(GameConstants::HERO_GOLD_COST));
  520. auto rumorText = boost::str(boost::format(CGI->generaltexth->allTexts[216]) % LOCPLINT->cb->getTavernRumor(tavernObj));
  521. new CTextBox(rumorText, Rect(32, 190, 330, 68), 0, FONT_SMALL, CENTER, Colors::WHITE);
  522. new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  523. cancel = new CButton(Point(310, 428), "ICANCEL.DEF", CButton::tooltip(CGI->generaltexth->tavernInfo[7]), std::bind(&CTavernWindow::close, this), SDLK_ESCAPE);
  524. recruit = new CButton(Point(272, 355), "TPTAV01.DEF", CButton::tooltip(), std::bind(&CTavernWindow::recruitb, this), SDLK_RETURN);
  525. thiefGuild = new CButton(Point(22, 428), "TPTAV02.DEF", CButton::tooltip(CGI->generaltexth->tavernInfo[5]), std::bind(&CTavernWindow::thievesguildb, this), SDLK_t);
  526. if(LOCPLINT->cb->getResourceAmount(Res::GOLD) < GameConstants::HERO_GOLD_COST) //not enough gold
  527. {
  528. recruit->addHoverText(CButton::NORMAL, CGI->generaltexth->tavernInfo[0]); //Cannot afford a Hero
  529. recruit->block(true);
  530. }
  531. else if(LOCPLINT->castleInt && LOCPLINT->cb->howManyHeroes(true) >= VLC->modh->settings.MAX_HEROES_AVAILABLE_PER_PLAYER)
  532. {
  533. //Cannot recruit. You already have %d Heroes.
  534. recruit->addHoverText(CButton::NORMAL, boost::str(boost::format(CGI->generaltexth->tavernInfo[1]) % LOCPLINT->cb->howManyHeroes(true)));
  535. recruit->block(true);
  536. }
  537. else if((!LOCPLINT->castleInt) && LOCPLINT->cb->howManyHeroes(false) >= VLC->modh->settings.MAX_HEROES_ON_MAP_PER_PLAYER)
  538. {
  539. //Cannot recruit. You already have %d Heroes.
  540. recruit->addHoverText(CButton::NORMAL, boost::str(boost::format(CGI->generaltexth->tavernInfo[1]) % LOCPLINT->cb->howManyHeroes(false)));
  541. recruit->block(true);
  542. }
  543. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  544. {
  545. recruit->addHoverText(CButton::NORMAL, CGI->generaltexth->tavernInfo[2]); //Cannot recruit. You already have a Hero in this town.
  546. recruit->block(true);
  547. }
  548. else
  549. {
  550. if(selected == -1)
  551. recruit->block(true);
  552. }
  553. if (LOCPLINT->castleInt)
  554. CCS->videoh->open(LOCPLINT->castleInt->town->town->clientInfo.tavernVideo);
  555. else
  556. CCS->videoh->open("TAVERN.BIK");
  557. }
  558. void CTavernWindow::recruitb()
  559. {
  560. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  561. const CGObjectInstance *obj = tavernObj;
  562. close();
  563. LOCPLINT->cb->recruitHero(obj, toBuy);
  564. }
  565. void CTavernWindow::thievesguildb()
  566. {
  567. GH.pushInt( new CThievesGuildWindow(tavernObj) );
  568. }
  569. CTavernWindow::~CTavernWindow()
  570. {
  571. CCS->videoh->close();
  572. }
  573. void CTavernWindow::show(SDL_Surface * to)
  574. {
  575. CWindowObject::show(to);
  576. CCS->videoh->update(pos.x+70, pos.y+56, to, true, false);
  577. if(selected >= 0)
  578. {
  579. HeroPortrait *sel = selected ? h2 : h1;
  580. if (selected != oldSelected && !recruit->isBlocked())
  581. {
  582. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  583. oldSelected = selected;
  584. //Recruit %s the %s
  585. recruit->addHoverText(CButton::NORMAL, boost::str(boost::format(CGI->generaltexth->tavernInfo[3]) % sel->h->name % sel->h->type->heroClass->name));
  586. }
  587. printAtMiddleWBLoc(sel->description, 146, 395, FONT_SMALL, 200, Colors::WHITE, to);
  588. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  589. }
  590. }
  591. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  592. {
  593. if(h && previousState && !down)
  594. *_sel = _id;
  595. }
  596. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  597. {
  598. if(h && down)
  599. {
  600. GH.pushInt(new CRClickPopupInt(new CHeroWindow(h), true));
  601. }
  602. }
  603. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  604. : h(H), _sel(&sel), _id(id)
  605. {
  606. addUsedEvents(LCLICK | RCLICK | HOVER);
  607. OBJ_CONSTRUCTION_CAPTURING_ALL;
  608. h = H;
  609. pos.x += x;
  610. pos.y += y;
  611. pos.w = 58;
  612. pos.h = 64;
  613. if(H)
  614. {
  615. hoverName = CGI->generaltexth->tavernInfo[4];
  616. boost::algorithm::replace_first(hoverName,"%s",H->name);
  617. int artifs = h->artifactsWorn.size() + h->artifactsInBackpack.size();
  618. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  619. if(vstd::contains(h->artifactsWorn, ArtifactPosition(i)))
  620. artifs--;
  621. description = CGI->generaltexth->allTexts[215];
  622. boost::algorithm::replace_first(description, "%s", h->name);
  623. boost::algorithm::replace_first(description, "%d", boost::lexical_cast<std::string>(h->level));
  624. boost::algorithm::replace_first(description, "%s", h->type->heroClass->name);
  625. boost::algorithm::replace_first(description, "%d", boost::lexical_cast<std::string>(artifs));
  626. new CAnimImage("portraitsLarge", h->portrait);
  627. }
  628. }
  629. void CTavernWindow::HeroPortrait::hover( bool on )
  630. {
  631. //Hoverable::hover(on);
  632. if(on)
  633. GH.statusbar->setText(hoverName);
  634. else
  635. GH.statusbar->clear();
  636. }
  637. void CExchangeWindow::questlog(int whichHero)
  638. {
  639. CCS->curh->dragAndDropCursor(nullptr);
  640. LOCPLINT->showQuestLog();
  641. }
  642. void CExchangeWindow::prepareBackground()
  643. {
  644. //printing heroes' names and levels
  645. auto genTitle = [](const CGHeroInstance *h)
  646. {
  647. return boost::str(boost::format(CGI->generaltexth->allTexts[138])
  648. % h->name % h->level % h->type->heroClass->name);
  649. };
  650. new CLabel(147, 25, FONT_SMALL, CENTER, Colors::WHITE, genTitle(heroInst[0]));
  651. new CLabel(653, 25, FONT_SMALL, CENTER, Colors::WHITE, genTitle(heroInst[1]));
  652. //printing primary skills
  653. for(int g=0; g<4; ++g)
  654. new CAnimImage("PSKIL32", g, 0, 385, 19 + 36*g);
  655. //heroes related thing
  656. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  657. {
  658. CHeroWithMaybePickedArtifact heroWArt = CHeroWithMaybePickedArtifact(this, heroInst[b]);
  659. //printing primary skills' amounts
  660. for(int m=0; m<GameConstants::PRIMARY_SKILLS; ++m)
  661. new CLabel(352 + 93 * b, 35 + 36 * m, FONT_SMALL, CENTER, Colors::WHITE,
  662. boost::lexical_cast<std::string>(heroWArt.getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(m))));
  663. //printing secondary skills
  664. for(int m=0; m<heroInst[b]->secSkills.size(); ++m)
  665. {
  666. int id = heroInst[b]->secSkills[m].first;
  667. int level = heroInst[b]->secSkills[m].second;
  668. new CAnimImage("SECSK32", id*3 + level + 2 , 0, 32 + 36 * m + 454 * b, 88);
  669. }
  670. //hero's specialty
  671. new CAnimImage("UN32", heroInst[b]->type->imageIndex, 0, 67 + 490*b, 45);
  672. //experience
  673. new CAnimImage("PSKIL32", 4, 0, 103 + 490*b, 45);
  674. new CLabel(119 + 490*b, 71, FONT_SMALL, CENTER, Colors::WHITE, makeNumberShort(heroInst[b]->exp));
  675. //mana points
  676. new CAnimImage("PSKIL32", 5, 0, 139 + 490*b, 45);
  677. new CLabel(155 + 490*b, 71, FONT_SMALL, CENTER, Colors::WHITE, makeNumberShort(heroInst[b]->mana));
  678. }
  679. //printing portraits
  680. new CAnimImage("PortraitsLarge", heroInst[0]->portrait, 0, 257, 13);
  681. new CAnimImage("PortraitsLarge", heroInst[1]->portrait, 0, 485, 13);
  682. }
  683. CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID queryID):
  684. CWindowObject(PLAYER_COLORED | BORDERED, "TRADE2")
  685. {
  686. OBJ_CONSTRUCTION_CAPTURING_ALL;
  687. heroInst[0] = LOCPLINT->cb->getHero(hero1);
  688. heroInst[1] = LOCPLINT->cb->getHero(hero2);
  689. prepareBackground();
  690. artifs[0] = new CArtifactsOfHero(Point(-334, 150));
  691. artifs[0]->commonInfo = std::make_shared<CArtifactsOfHero::SCommonPart>();
  692. artifs[0]->commonInfo->participants.insert(artifs[0]);
  693. artifs[0]->setHero(heroInst[0]);
  694. artifs[1] = new CArtifactsOfHero(Point(96, 150));
  695. artifs[1]->commonInfo = artifs[0]->commonInfo;
  696. artifs[1]->commonInfo->participants.insert(artifs[1]);
  697. artifs[1]->setHero(heroInst[1]);
  698. artSets.push_back(artifs[0]);
  699. artSets.push_back(artifs[1]);
  700. //primary skills
  701. for(int g=0; g<4; ++g)
  702. {
  703. //primary skill's clickable areas
  704. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  705. primSkillAreas[g]->pos = genRect(32, 140, pos.x + 329, pos.y + 19 + 36 * g);
  706. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  707. primSkillAreas[g]->type = g;
  708. primSkillAreas[g]->bonusValue = -1;
  709. primSkillAreas[g]->baseType = 0;
  710. primSkillAreas[g]->hoverText = CGI->generaltexth->heroscrn[1];
  711. boost::replace_first(primSkillAreas[g]->hoverText, "%s", CGI->generaltexth->primarySkillNames[g]);
  712. }
  713. //heroes related thing
  714. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  715. {
  716. //secondary skill's clickable areas
  717. for(int g=0; g<heroInst[b]->secSkills.size(); ++g)
  718. {
  719. int skill = heroInst[b]->secSkills[g].first,
  720. level = heroInst[b]->secSkills[g].second; // <1, 3>
  721. secSkillAreas[b].push_back(new LRClickableAreaWTextComp());
  722. secSkillAreas[b][g]->pos = genRect(32, 32, pos.x + 32 + g*36 + b*454 , pos.y + 88);
  723. secSkillAreas[b][g]->baseType = 1;
  724. secSkillAreas[b][g]->type = skill;
  725. secSkillAreas[b][g]->bonusValue = level;
  726. secSkillAreas[b][g]->text = CGI->skillh->skillInfo(skill, level);
  727. secSkillAreas[b][g]->hoverText = CGI->generaltexth->heroscrn[21];
  728. boost::algorithm::replace_first(secSkillAreas[b][g]->hoverText, "%s", CGI->generaltexth->levels[level - 1]);
  729. boost::algorithm::replace_first(secSkillAreas[b][g]->hoverText, "%s", CGI->skillh->skillName(skill));
  730. }
  731. portrait[b] = new CHeroArea(257 + 228*b, 13, heroInst[b]);
  732. specialty[b] = new LRClickableAreaWText();
  733. specialty[b]->pos = genRect(32, 32, pos.x + 69 + 490*b, pos.y + 45);
  734. specialty[b]->hoverText = CGI->generaltexth->heroscrn[27];
  735. specialty[b]->text = heroInst[b]->type->specDescr;
  736. experience[b] = new LRClickableAreaWText();
  737. experience[b]->pos = genRect(32, 32, pos.x + 105 + 490*b, pos.y + 45);
  738. experience[b]->hoverText = CGI->generaltexth->heroscrn[9];
  739. experience[b]->text = CGI->generaltexth->allTexts[2].c_str();
  740. boost::algorithm::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->level));
  741. boost::algorithm::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(heroInst[b]->level+1)));
  742. boost::algorithm::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->exp));
  743. spellPoints[b] = new LRClickableAreaWText();
  744. spellPoints[b]->pos = genRect(32, 32, pos.x + 141 + 490*b, pos.y + 45);
  745. spellPoints[b]->hoverText = CGI->generaltexth->heroscrn[22];
  746. spellPoints[b]->text = CGI->generaltexth->allTexts[205];
  747. boost::algorithm::replace_first(spellPoints[b]->text, "%s", heroInst[b]->name);
  748. boost::algorithm::replace_first(spellPoints[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->mana));
  749. boost::algorithm::replace_first(spellPoints[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->manaLimit()));
  750. //setting morale
  751. morale[b] = new MoraleLuckBox(true, genRect(32, 32, 176 + 490*b, 39), true);
  752. morale[b]->set(heroInst[b]);
  753. //setting luck
  754. luck[b] = new MoraleLuckBox(false, genRect(32, 32, 212 + 490*b, 39), true);
  755. luck[b]->set(heroInst[b]);
  756. }
  757. //buttons
  758. quit = new CButton(Point(732, 567), "IOKAY.DEF", CGI->generaltexth->zelp[600], std::bind(&CExchangeWindow::close, this), SDLK_RETURN);
  759. if(queryID.getNum() > 0)
  760. quit->addCallback([=](){ LOCPLINT->cb->selectionMade(0, queryID); });
  761. questlogButton[0] = new CButton(Point( 10, 44), "hsbtns4.def", CButton::tooltip(CGI->generaltexth->heroscrn[0]), std::bind(&CExchangeWindow::questlog,this, 0));
  762. questlogButton[1] = new CButton(Point(740, 44), "hsbtns4.def", CButton::tooltip(CGI->generaltexth->heroscrn[0]), std::bind(&CExchangeWindow::questlog,this, 1));
  763. Rect barRect(5, 578, 725, 18);
  764. ourBar = new CGStatusBar(new CPicture(*background, barRect, 5, 578, false));
  765. //garrison interface
  766. garr = new CGarrisonInt(69, 131, 4, Point(418,0), *background, Point(69,131), heroInst[0],heroInst[1], true, true);
  767. garr->addSplitBtn(new CButton( Point( 10, 132), "TSBTNS.DEF", CButton::tooltip(CGI->generaltexth->tcommands[3]), std::bind(&CGarrisonInt::splitClick, garr)));
  768. garr->addSplitBtn(new CButton( Point(740, 132), "TSBTNS.DEF", CButton::tooltip(CGI->generaltexth->tcommands[3]), std::bind(&CGarrisonInt::splitClick, garr)));
  769. }
  770. CExchangeWindow::~CExchangeWindow()
  771. {
  772. artifs[0]->commonInfo = nullptr;
  773. artifs[1]->commonInfo = nullptr;
  774. }
  775. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const std::function<void()> &onBuy):
  776. CWindowObject(PLAYER_COLORED, "TPSHIP")
  777. {
  778. OBJ_CONSTRUCTION_CAPTURING_ALL;
  779. bgWater = new CPicture("TPSHIPBK", 100, 69);
  780. std::string boatFilenames[3] = {"AB01_", "AB02_", "AB03_"};
  781. Point waterCenter = Point(bgWater->pos.x+bgWater->pos.w/2, bgWater->pos.y+bgWater->pos.h/2);
  782. bgShip = new CAnimImage(boatFilenames[boatType], 0, 7, 120, 96, CShowableAnim::USE_RLE);
  783. bgShip->center(waterCenter);
  784. // Create resource icons and costs.
  785. std::string goldValue = boost::lexical_cast<std::string>(cost[Res::GOLD]);
  786. std::string woodValue = boost::lexical_cast<std::string>(cost[Res::WOOD]);
  787. goldCost = new CLabel(118, 294, FONT_SMALL, CENTER, Colors::WHITE, goldValue);
  788. woodCost = new CLabel(212, 294, FONT_SMALL, CENTER, Colors::WHITE, woodValue);
  789. goldPic = new CAnimImage("RESOURCE", Res::GOLD, 0, 100, 244);
  790. woodPic = new CAnimImage("RESOURCE", Res::WOOD, 0, 196, 244);
  791. quit = new CButton( Point(224, 312), "ICANCEL", CButton::tooltip(CGI->generaltexth->allTexts[599]), std::bind(&CShipyardWindow::close, this), SDLK_RETURN);
  792. build = new CButton( Point( 42, 312), "IBUY30", CButton::tooltip(CGI->generaltexth->allTexts[598]), std::bind(&CShipyardWindow::close, this),SDLK_RETURN);
  793. build->addCallback(onBuy);
  794. for(Res::ERes i = Res::WOOD; i <= Res::GOLD; vstd::advance(i, 1))
  795. {
  796. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  797. {
  798. build->block(true);
  799. break;
  800. }
  801. }
  802. statusBar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  803. title = new CLabel(164, 27, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[13]);
  804. costLabel = new CLabel(164, 220, FONT_MEDIUM, CENTER, Colors::WHITE, CGI->generaltexth->jktexts[14]);
  805. }
  806. CPuzzleWindow::CPuzzleWindow(const int3 &GrailPos, double discoveredRatio):
  807. CWindowObject(PLAYER_COLORED | BORDERED, "PUZZLE"),
  808. grailPos(GrailPos),
  809. currentAlpha(SDL_ALPHA_OPAQUE)
  810. {
  811. OBJ_CONSTRUCTION_CAPTURING_ALL;
  812. CCS->soundh->playSound(soundBase::OBELISK);
  813. quitb = new CButton(Point(670, 538), "IOK6432.DEF", CButton::tooltip(CGI->generaltexth->allTexts[599]), std::bind(&CPuzzleWindow::close, this), SDLK_RETURN);
  814. quitb->assignedKeys.insert(SDLK_ESCAPE);
  815. quitb->borderColor = boost::make_optional(Colors::METALLIC_GOLD);
  816. new CPicture("PUZZLOGO", 607, 3);
  817. new CLabel(700, 95, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[463]);
  818. new CResDataBar("ARESBAR.bmp", 3, 575, 32, 2, 85, 85);
  819. int faction = LOCPLINT->cb->getStartInfo()->playerInfos.find(LOCPLINT->playerID)->second.castle;
  820. auto & puzzleMap = CGI->townh->factions[faction]->puzzleMap;
  821. for(auto & elem : puzzleMap)
  822. {
  823. const SPuzzleInfo & info = elem;
  824. auto piece = new CPicture(info.filename, info.x, info.y);
  825. //piece that will slowly disappear
  826. if(info.whenUncovered <= GameConstants::PUZZLE_MAP_PIECES * discoveredRatio)
  827. {
  828. piecesToRemove.push_back(piece);
  829. piece->needRefresh = true;
  830. piece->recActions = piece->recActions & ~SHOWALL;
  831. SDL_SetSurfaceBlendMode(piece->bg,SDL_BLENDMODE_BLEND);
  832. }
  833. }
  834. }
  835. void CPuzzleWindow::showAll(SDL_Surface * to)
  836. {
  837. int3 moveInt = int3(8, 9, 0);
  838. Rect mapRect = genRect(544, 591, pos.x + 8, pos.y + 7);
  839. int3 topTile = grailPos - moveInt;
  840. MapDrawingInfo info(topTile, &LOCPLINT->cb->getVisibilityMap(), &mapRect);
  841. info.puzzleMode = true;
  842. info.grailPos = grailPos;
  843. CGI->mh->drawTerrainRectNew(to, &info);
  844. CWindowObject::showAll(to);
  845. }
  846. void CPuzzleWindow::show(SDL_Surface * to)
  847. {
  848. static int animSpeed = 2;
  849. if (currentAlpha < animSpeed)
  850. {
  851. //animation done
  852. for(auto & piece : piecesToRemove)
  853. delete piece;
  854. piecesToRemove.clear();
  855. }
  856. else
  857. {
  858. //update disappearing puzzles
  859. for(auto & piece : piecesToRemove)
  860. piece->setAlpha(currentAlpha);
  861. currentAlpha -= animSpeed;
  862. }
  863. CWindowObject::show(to);
  864. }
  865. void CTransformerWindow::CItem::move()
  866. {
  867. if (left)
  868. moveBy(Point(289, 0));
  869. else
  870. moveBy(Point(-289, 0));
  871. left = !left;
  872. }
  873. void CTransformerWindow::CItem::clickLeft(tribool down, bool previousState)
  874. {
  875. if(previousState && (!down))
  876. {
  877. move();
  878. parent->showAll(screen2);
  879. }
  880. }
  881. void CTransformerWindow::CItem::update()
  882. {
  883. icon->setFrame(parent->army->getCreature(SlotID(id))->idNumber + 2);
  884. }
  885. CTransformerWindow::CItem::CItem(CTransformerWindow * parent, int size, int id):
  886. id(id), size(size), parent(parent)
  887. {
  888. OBJ_CONSTRUCTION_CAPTURING_ALL;
  889. addUsedEvents(LCLICK);
  890. left = true;
  891. pos.w = 58;
  892. pos.h = 64;
  893. pos.x += 45 + (id%3)*83 + id/6*83;
  894. pos.y += 109 + (id/3)*98;
  895. icon = new CAnimImage("TWCRPORT", parent->army->getCreature(SlotID(id))->idNumber + 2);
  896. new CLabel(28, 76,FONT_SMALL, CENTER, Colors::WHITE, boost::lexical_cast<std::string>(size));//stack size
  897. }
  898. void CTransformerWindow::makeDeal()
  899. {
  900. for (auto & elem : items)
  901. if (!elem->left)
  902. LOCPLINT->cb->trade(town, EMarketMode::CREATURE_UNDEAD, elem->id, 0, 0, hero);
  903. }
  904. void CTransformerWindow::addAll()
  905. {
  906. for (auto & elem : items)
  907. if (elem->left)
  908. elem->move();
  909. showAll(screen2);
  910. }
  911. void CTransformerWindow::updateGarrisons()
  912. {
  913. for(auto & item : items)
  914. {
  915. item->update();
  916. }
  917. }
  918. CTransformerWindow::CTransformerWindow(const CGHeroInstance * _hero, const CGTownInstance * _town):
  919. CWindowObject(PLAYER_COLORED, "SKTRNBK"),
  920. hero(_hero),
  921. town(_town)
  922. {
  923. OBJ_CONSTRUCTION_CAPTURING_ALL;
  924. if (hero)
  925. army = hero;
  926. else
  927. army = town;
  928. for (int i=0; i<GameConstants::ARMY_SIZE; i++ )
  929. if ( army->getCreature(SlotID(i)) )
  930. items.push_back(new CItem(this, army->getStackCount(SlotID(i)), i));
  931. all = new CButton(Point(146, 416), "ALTARMY.DEF", CGI->generaltexth->zelp[590], [&](){ addAll(); }, SDLK_a);
  932. convert = new CButton(Point(269, 416), "ALTSACR.DEF", CGI->generaltexth->zelp[591], [&](){ makeDeal(); }, SDLK_RETURN);
  933. cancel = new CButton(Point(392, 416), "ICANCEL.DEF", CGI->generaltexth->zelp[592], [&](){ close(); },SDLK_ESCAPE);
  934. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  935. new CLabel(153, 29,FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[485]);//holding area
  936. new CLabel(153+295, 29, FONT_SMALL, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[486]);//transformer
  937. new CTextBox(CGI->generaltexth->allTexts[487], Rect(26, 56, 255, 40), 0, FONT_MEDIUM, CENTER, Colors::YELLOW);//move creatures to create skeletons
  938. new CTextBox(CGI->generaltexth->allTexts[488], Rect(320, 56, 255, 40), 0, FONT_MEDIUM, CENTER, Colors::YELLOW);//creatures here will become skeletons
  939. }
  940. void CUniversityWindow::CItem::clickLeft(tribool down, bool previousState)
  941. {
  942. if(previousState && (!down))
  943. {
  944. if ( state() != 2 )
  945. return;
  946. auto win = new CUnivConfirmWindow(parent, ID, LOCPLINT->cb->getResourceAmount(Res::GOLD) >= 2000);
  947. GH.pushInt(win);
  948. }
  949. }
  950. void CUniversityWindow::CItem::clickRight(tribool down, bool previousState)
  951. {
  952. if(down)
  953. {
  954. CRClickPopup::createAndPush(CGI->skillh->skillInfo(ID, 1),
  955. new CComponent(CComponent::secskill, ID, 1));
  956. }
  957. }
  958. void CUniversityWindow::CItem::hover(bool on)
  959. {
  960. if (on)
  961. GH.statusbar->setText(CGI->skillh->skillName(ID));
  962. else
  963. GH.statusbar->clear();
  964. }
  965. int CUniversityWindow::CItem::state()
  966. {
  967. if (parent->hero->getSecSkillLevel(SecondarySkill(ID)))//hero know this skill
  968. return 1;
  969. if (!parent->hero->canLearnSkill())//can't learn more skills
  970. return 0;
  971. if (parent->hero->type->heroClass->secSkillProbability[ID]==0)//can't learn this skill (like necromancy for most of non-necros)
  972. return 0;
  973. return 2;
  974. }
  975. void CUniversityWindow::CItem::showAll(SDL_Surface * to)
  976. {
  977. CPicture * bar;
  978. switch (state())
  979. {
  980. case 0: bar = parent->red;
  981. break;
  982. case 1: bar = parent->yellow;
  983. break;
  984. case 2: bar = parent->green;
  985. break;
  986. default:bar = nullptr;
  987. break;
  988. }
  989. assert(bar);
  990. blitAtLoc(bar->bg, -28, -22, to);
  991. blitAtLoc(bar->bg, -28, 48, to);
  992. printAtMiddleLoc (CGI->skillh->skillName(ID), 22, -13, FONT_SMALL, Colors::WHITE,to);//Name
  993. printAtMiddleLoc (CGI->generaltexth->levels[0], 22, 57, FONT_SMALL, Colors::WHITE,to);//Level(always basic)
  994. CAnimImage::showAll(to);
  995. }
  996. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y):
  997. CAnimImage ("SECSKILL", _ID*3+3, 0, X, Y),
  998. ID(_ID),
  999. parent(_parent)
  1000. {
  1001. addUsedEvents(LCLICK | RCLICK | HOVER);
  1002. }
  1003. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market):
  1004. CWindowObject(PLAYER_COLORED, "UNIVERS1"),
  1005. hero(_hero),
  1006. market(_market)
  1007. {
  1008. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1009. green = new CPicture("UNIVGREN.PCX");
  1010. yellow = new CPicture("UNIVGOLD.PCX");//bars
  1011. red = new CPicture("UNIVRED.PCX");
  1012. green->recActions =
  1013. yellow->recActions =
  1014. red->recActions = DISPOSE;
  1015. CIntObject * titlePic = nullptr;
  1016. if (market->o->ID == Obj::TOWN)
  1017. titlePic = new CAnimImage(CGI->townh->factions[ETownType::CONFLUX]->town->clientInfo.buildingsIcons, BuildingID::MAGIC_UNIVERSITY);
  1018. else
  1019. titlePic = new CPicture("UNIVBLDG");
  1020. titlePic->center(Point(232 + pos.x, 76 + pos.y));
  1021. //Clerk speech
  1022. new CTextBox(CGI->generaltexth->allTexts[603], Rect(24, 129, 413, 70), 0, FONT_SMALL, CENTER, Colors::WHITE);
  1023. //University
  1024. new CLabel(231, 26, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[602]);
  1025. std::vector<int> list = market->availableItemsIds(EMarketMode::RESOURCE_SKILL);
  1026. assert(list.size() == 4);
  1027. for (int i=0; i<list.size(); i++)//prepare clickable items
  1028. items.push_back(new CItem(this, list[i], 54+i*104, 234));
  1029. cancel = new CButton(Point(200, 313), "IOKAY.DEF", CGI->generaltexth->zelp[632], [&](){ close(); }, SDLK_RETURN);
  1030. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1031. }
  1032. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * PARENT, int SKILL, bool available ):
  1033. CWindowObject(PLAYER_COLORED, "UNIVERS2.PCX"),
  1034. parent(PARENT)
  1035. {
  1036. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1037. std::string text = CGI->generaltexth->allTexts[608];
  1038. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  1039. boost::replace_first(text, "%s", CGI->skillh->skillName(SKILL));
  1040. boost::replace_first(text, "%d", "2000");
  1041. new CTextBox(text, Rect(24, 129, 413, 70), 0, FONT_SMALL, CENTER, Colors::WHITE);//Clerk speech
  1042. new CLabel(230, 37, FONT_SMALL, CENTER, Colors::WHITE, CGI->skillh->skillName(SKILL));//Skill name
  1043. new CAnimImage("SECSKILL", SKILL*3+3, 0, 211, 51);//skill
  1044. new CLabel(230, 107, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->levels[1]);//Skill level
  1045. new CAnimImage("RESOURCE", Res::GOLD, 0, 210, 210);//gold
  1046. new CLabel(230, 267, FONT_SMALL, CENTER, Colors::WHITE, "2000");//Cost
  1047. std::string hoverText = CGI->generaltexth->allTexts[609];
  1048. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->skillh->skillName(SKILL));
  1049. text = CGI->generaltexth->zelp[633].second;
  1050. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  1051. boost::replace_first(text, "%s", CGI->skillh->skillName(SKILL));
  1052. boost::replace_first(text, "%d", "2000");
  1053. confirm= new CButton(Point(148, 299), "IBY6432.DEF", CButton::tooltip(hoverText, text), [=](){makeDeal(SKILL);}, SDLK_RETURN);
  1054. confirm->block(!available);
  1055. cancel = new CButton(Point(252,299), "ICANCEL.DEF", CGI->generaltexth->zelp[631], [&](){ close(); }, SDLK_ESCAPE);
  1056. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1057. }
  1058. void CUnivConfirmWindow::makeDeal(int skill)
  1059. {
  1060. LOCPLINT->cb->trade(parent->market->o, EMarketMode::RESOURCE_SKILL, 6, skill, 1, parent->hero);
  1061. close();
  1062. }
  1063. CHillFortWindow::CHillFortWindow(const CGHeroInstance *visitor, const CGObjectInstance *object):
  1064. CWindowObject(PLAYER_COLORED, "APHLFTBK"),
  1065. fort(object),
  1066. hero(visitor)
  1067. {
  1068. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1069. new CLabel(325, 32, FONT_BIG, CENTER, Colors::YELLOW, fort->getObjectName());//Hill Fort
  1070. heroPic = new CHeroArea(30, 60, hero);
  1071. for (int i=0; i<resCount; i++)
  1072. {
  1073. totalIcons[i] = new CAnimImage("SMALRES", i, 0, 104 + 76 * i, 237);
  1074. totalLabels[i] = new CLabel(166 + 76 * i, 253, FONT_SMALL, BOTTOMRIGHT);
  1075. }
  1076. for (int i = 0; i < slotsCount; i++)
  1077. {
  1078. upgrade[i] = new CButton(Point(107 + i * 76, 171), "", CButton::tooltip(getTextForSlot(SlotID(i))), [=](){ makeDeal(SlotID(i)); }, SDLK_1 + i);
  1079. for (auto image : { "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF" })
  1080. upgrade[i]->addImage(image);
  1081. for(int j : {0,1})
  1082. {
  1083. slotIcons[i][j] = new CAnimImage("SMALRES", 0, 0, 104 + 76 * i, 128 + 20 * j);
  1084. slotLabels[i][j] = new CLabel(168 + 76 * i, 144 + 20 * j, FONT_SMALL, BOTTOMRIGHT);
  1085. }
  1086. }
  1087. upgradeAll = new CButton(Point(30, 231), "", CButton::tooltip(CGI->generaltexth->allTexts[432]), [&](){ makeDeal(SlotID(slotsCount));}, SDLK_0);
  1088. for (auto image : { "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF" })
  1089. upgradeAll->addImage(image);
  1090. quit = new CButton(Point(294, 275), "IOKAY.DEF", CButton::tooltip(), std::bind(&CHillFortWindow::close, this), SDLK_RETURN);
  1091. bar = new CGStatusBar(new CPicture(*background, Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1092. garr = new CGarrisonInt(108, 60, 18, Point(),background->bg,Point(108,60),hero,nullptr);
  1093. updateGarrisons();
  1094. }
  1095. void CHillFortWindow::updateGarrisons()
  1096. {
  1097. std::array<TResources, slotsCount> costs;// costs [slot ID] [resource ID] = resource count for upgrade
  1098. TResources totalSumm; // totalSum[resource ID] = value
  1099. totalSumm.resize(GameConstants::RESOURCE_QUANTITY);
  1100. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  1101. totalSumm[i]=0;
  1102. for (int i=0; i<slotsCount; i++)
  1103. {
  1104. costs[i].clear();
  1105. int newState = getState(SlotID(i));
  1106. if (newState != -1)
  1107. {
  1108. UpgradeInfo info;
  1109. LOCPLINT->cb->getUpgradeInfo(hero, SlotID(i), info);
  1110. if (info.newID.size())//we have upgrades here - update costs
  1111. {
  1112. costs[i] = info.cost[0] * hero->getStackCount(SlotID(i));
  1113. totalSumm += costs[i];
  1114. }
  1115. }
  1116. currState[i] = newState;
  1117. upgrade[i]->setIndex(currState[i] == -1 ? 0 : currState[i]);
  1118. upgrade[i]->block(currState[i] == -1);
  1119. upgrade[i]->addHoverText(CButton::NORMAL, getTextForSlot(SlotID(i)));
  1120. }
  1121. //"Upgrade all" slot
  1122. int newState = 2;
  1123. {
  1124. TResources myRes = LOCPLINT->cb->getResourceAmount();
  1125. bool allUpgraded = true;//All creatures are upgraded?
  1126. for (int i=0; i<slotsCount; i++)
  1127. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  1128. if (allUpgraded)
  1129. newState = 1;
  1130. if(!totalSumm.canBeAfforded(myRes))
  1131. newState = 0;
  1132. }
  1133. currState[slotsCount] = newState;
  1134. upgradeAll->setIndex(newState);
  1135. CWindowWithGarrison::updateGarrisons();
  1136. for (int i = 0; i < slotsCount; i++)
  1137. {
  1138. //hide all first
  1139. for(int j : {0,1})
  1140. {
  1141. slotIcons[i][j]->visible = false;
  1142. slotLabels[i][j]->setText("");
  1143. }
  1144. //if can upgrade or can not afford, draw cost
  1145. if (currState[i] == 0 || currState[i] == 2)
  1146. {
  1147. if (costs[i].nonZero())
  1148. {
  1149. //reverse iterator is used to display gold as first element
  1150. int j = 0;
  1151. for(int res = costs[i].size()-1; (res >= 0) && (j < 2); res--)
  1152. {
  1153. int val = costs[i][res];
  1154. if(!val)
  1155. continue;
  1156. slotIcons[i][j]->visible = true;
  1157. slotIcons[i][j]->setFrame(res);
  1158. slotLabels[i][j]->setText(boost::lexical_cast<std::string>(val));
  1159. j++;
  1160. }
  1161. }
  1162. else//free upgrade - print gold image and "Free" text
  1163. {
  1164. slotIcons[i][0]->visible = true;
  1165. slotIcons[i][0]->setFrame(Res::GOLD);
  1166. slotLabels[i][0]->setText(CGI->generaltexth->allTexts[344]);
  1167. }
  1168. }
  1169. }
  1170. for (int i = 0; i < resCount; i++)
  1171. {
  1172. if(totalSumm[i] == 0)
  1173. {
  1174. totalIcons[i]->visible = false;
  1175. totalLabels[i]->setText("");
  1176. }
  1177. else
  1178. {
  1179. totalIcons[i]->visible = true;
  1180. totalLabels[i]->setText(boost::lexical_cast<std::string>(totalSumm[i]));
  1181. }
  1182. }
  1183. }
  1184. void CHillFortWindow::makeDeal(SlotID slot)
  1185. {
  1186. assert(slot.getNum()>=0);
  1187. int offset = (slot.getNum() == slotsCount)?2:0;
  1188. switch (currState[slot.getNum()])
  1189. {
  1190. case 0:
  1191. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset],
  1192. std::vector<CComponent*>(), soundBase::sound_todo);
  1193. break;
  1194. case 1:
  1195. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset],
  1196. std::vector<CComponent*>(), soundBase::sound_todo);
  1197. break;
  1198. case 2:
  1199. for (int i=0; i<slotsCount; i++)
  1200. if ( slot.getNum() ==i || ( slot.getNum() == slotsCount && currState[i] == 2 ) )//this is activated slot or "upgrade all"
  1201. {
  1202. UpgradeInfo info;
  1203. LOCPLINT->cb->getUpgradeInfo(hero, SlotID(i), info);
  1204. LOCPLINT->cb->upgradeCreature(hero, SlotID(i), info.newID[0]);
  1205. }
  1206. break;
  1207. }
  1208. }
  1209. std::string CHillFortWindow::getTextForSlot(SlotID slot)
  1210. {
  1211. if ( !hero->getCreature(slot) )//we don`t have creature here
  1212. return "";
  1213. std::string str = CGI->generaltexth->allTexts[318];
  1214. int amount = hero->getStackCount(slot);
  1215. if ( amount == 1 )
  1216. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->nameSing);
  1217. else
  1218. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->namePl);
  1219. return str;
  1220. }
  1221. int CHillFortWindow::getState(SlotID slot)
  1222. {
  1223. TResources myRes = LOCPLINT->cb->getResourceAmount();
  1224. if (hero->slotEmpty(slot))//no creature here
  1225. return -1;
  1226. UpgradeInfo info;
  1227. LOCPLINT->cb->getUpgradeInfo(hero, slot, info);
  1228. if (!info.newID.size())//already upgraded
  1229. return 1;
  1230. if(!(info.cost[0] * hero->getStackCount(slot)).canBeAfforded(myRes))
  1231. return 0;
  1232. return 2;//can upgrade
  1233. }
  1234. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner):
  1235. CWindowObject(PLAYER_COLORED | BORDERED, "TpRank"),
  1236. owner(_owner)
  1237. {
  1238. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1239. type |= BLOCK_ADV_HOTKEYS;
  1240. SThievesGuildInfo tgi; //info to be displayed
  1241. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  1242. exitb = new CButton (Point(748, 556), "TPMAGE1", CButton::tooltip(CGI->generaltexth->allTexts[600]), [&](){ close();}, SDLK_RETURN);
  1243. exitb->assignedKeys.insert(SDLK_ESCAPE);
  1244. statusBar = new CGStatusBar(3, 555, "TStatBar.bmp", 742);
  1245. resdatabar = new CMinorResDataBar();
  1246. resdatabar->pos.x += pos.x;
  1247. resdatabar->pos.y += pos.y;
  1248. //data for information table:
  1249. // fields[row][column] = list of id's of players for this box
  1250. static std::vector< std::vector< PlayerColor > > SThievesGuildInfo::* fields[] =
  1251. { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  1252. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks,
  1253. &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army, &SThievesGuildInfo::income };
  1254. //printing texts & descriptions to background
  1255. for(int g=0; g<12; ++g)
  1256. {
  1257. int posY[] = {400, 460, 510};
  1258. int y;
  1259. if(g < 9)
  1260. y = 52 + 32*g;
  1261. else
  1262. y = posY[g-9];
  1263. std::string text = CGI->generaltexth->jktexts[24+g];
  1264. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  1265. new CLabel(135, y, FONT_MEDIUM, CENTER, Colors::YELLOW, text);
  1266. }
  1267. for(int g=1; g<tgi.playerColors.size(); ++g)
  1268. new CAnimImage("PRSTRIPS", g-1, 0, 250 + 66*g, 7);
  1269. for(int g=0; g<tgi.playerColors.size(); ++g)
  1270. new CLabel(283 + 66*g, 24, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[16+g]);
  1271. //printing flags
  1272. for(int g = 0; g < ARRAY_COUNT(fields); ++g) //by lines
  1273. {
  1274. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  1275. {
  1276. std::vector<PlayerColor> &players = (tgi .* fields[g])[b]; //get players with this place in this line
  1277. //position of box
  1278. int xpos = 259 + 66 * b;
  1279. int ypos = 41 + 32 * g;
  1280. size_t rowLength[2]; //size of each row
  1281. rowLength[0] = std::min<size_t>(players.size(), 4);
  1282. rowLength[1] = players.size() - rowLength[0];
  1283. for (size_t j=0; j< 2; j++)
  1284. {
  1285. // origin of this row | offset for 2nd row| shift right for short rows
  1286. //if we have 2 rows, start either from mid or beginning (depending on count), otherwise center the flags
  1287. int rowStartX = xpos + (j ? 6 + (rowLength[j] < 3 ? 12 : 0) : 24 - 6 * rowLength[j]);
  1288. int rowStartY = ypos + (j ? 4 : 0);
  1289. for (size_t i=0; i< rowLength[j]; i++)
  1290. {
  1291. new CAnimImage("itgflags", players[i + j*4].getNum(), 0, rowStartX + i*12, rowStartY);
  1292. }
  1293. }
  1294. }
  1295. }
  1296. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  1297. //printing best hero
  1298. int counter = 0;
  1299. for(auto & iter : tgi.colorToBestHero)
  1300. {
  1301. new CPicture(colorToBox[iter.first.getNum()], 253 + 66 * counter, 334);
  1302. if(iter.second.portrait >= 0)
  1303. {
  1304. new CAnimImage("PortraitsSmall", iter.second.portrait, 0, 260 + 66 * counter, 360);
  1305. //TODO: r-click info:
  1306. // - r-click on hero
  1307. // - r-click on primary skill label
  1308. if(iter.second.details)
  1309. {
  1310. new CTextBox(CGI->generaltexth->allTexts[184], Rect(260 + 66*counter, 396, 52, 64),
  1311. 0, FONT_TINY, TOPLEFT, Colors::WHITE);
  1312. for (int i=0; i<iter.second.details->primskills.size(); ++i)
  1313. {
  1314. new CLabel(310 + 66 * counter, 407 + 11*i, FONT_TINY, BOTTOMRIGHT, Colors::WHITE,
  1315. boost::lexical_cast<std::string>(iter.second.details->primskills[i]));
  1316. }
  1317. }
  1318. }
  1319. counter++;
  1320. }
  1321. //printing best creature
  1322. counter = 0;
  1323. for(auto & it : tgi.bestCreature)
  1324. {
  1325. if(it.second >= 0)
  1326. new CAnimImage("TWCRPORT", it.second+2, 0, 255 + 66 * counter, 479);
  1327. counter++;
  1328. }
  1329. //printing personality
  1330. counter = 0;
  1331. for(auto & it : tgi.personality)
  1332. {
  1333. std::string text;
  1334. if(it.second == EAiTactic::NONE)
  1335. {
  1336. text = CGI->generaltexth->arraytxt[172];
  1337. }
  1338. else if(it.second != EAiTactic::RANDOM)
  1339. {
  1340. text = CGI->generaltexth->arraytxt[168 + it.second];
  1341. }
  1342. new CLabel(283 + 66*counter, 459, FONT_SMALL, CENTER, Colors::WHITE, text);
  1343. counter++;
  1344. }
  1345. }
  1346. CObjectListWindow::CItem::CItem(CObjectListWindow *_parent, size_t _id, std::string _text):
  1347. parent(_parent),
  1348. index(_id)
  1349. {
  1350. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1351. border = new CPicture("TPGATES");
  1352. pos = border->pos;
  1353. addUsedEvents(LCLICK);
  1354. type |= REDRAW_PARENT;
  1355. text = new CLabel(pos.w/2, pos.h/2, FONT_SMALL, CENTER, Colors::WHITE, _text);
  1356. select(index == parent->selected);
  1357. }
  1358. void CObjectListWindow::CItem::select(bool on)
  1359. {
  1360. if (on)
  1361. border->recActions = 255;
  1362. else
  1363. border->recActions = ~(UPDATE | SHOWALL);
  1364. redraw();
  1365. }
  1366. void CObjectListWindow::CItem::clickLeft(tribool down, bool previousState)
  1367. {
  1368. if( previousState && !down)
  1369. parent->changeSelection(index);
  1370. }
  1371. CObjectListWindow::CObjectListWindow(const std::vector<int> &_items, CIntObject * titlePic, std::string _title, std::string _descr,
  1372. std::function<void(int)> Callback):
  1373. CWindowObject(PLAYER_COLORED, "TPGATE"),
  1374. onSelect(Callback),
  1375. selected(0)
  1376. {
  1377. items.reserve(_items.size());
  1378. for(int id : _items)
  1379. {
  1380. items.push_back(std::make_pair(id, CGI->mh->map->objects[id]->getObjectName()));
  1381. }
  1382. init(titlePic, _title, _descr);
  1383. }
  1384. CObjectListWindow::CObjectListWindow(const std::vector<std::string> &_items, CIntObject * titlePic, std::string _title, std::string _descr,
  1385. std::function<void(int)> Callback):
  1386. CWindowObject(PLAYER_COLORED, "TPGATE"),
  1387. onSelect(Callback),
  1388. selected(0)
  1389. {
  1390. items.reserve(_items.size());
  1391. for (size_t i=0; i<_items.size(); i++)
  1392. items.push_back(std::make_pair(int(i), _items[i]));
  1393. init(titlePic, _title, _descr);
  1394. }
  1395. void CObjectListWindow::init(CIntObject * titlePic, std::string _title, std::string _descr)
  1396. {
  1397. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1398. title = new CLabel(152, 27, FONT_BIG, CENTER, Colors::YELLOW, _title);
  1399. descr = new CLabel(145, 133, FONT_SMALL, CENTER, Colors::WHITE, _descr);
  1400. ok = new CButton(Point(15, 402), "IOKAY.DEF", CButton::tooltip(), std::bind(&CObjectListWindow::elementSelected, this), SDLK_RETURN);
  1401. ok->block(true);
  1402. exit = new CButton( Point(228, 402), "ICANCEL.DEF", CButton::tooltip(), std::bind(&CObjectListWindow::exitPressed, this), SDLK_ESCAPE);
  1403. if (titlePic)
  1404. {
  1405. addChild(titlePic);
  1406. titlePic->recActions = defActions;
  1407. titlePic->pos.x = pos.w/2 + pos.x - titlePic->pos.w/2;
  1408. titlePic->pos.y =75 + pos.y - titlePic->pos.h/2;
  1409. }
  1410. list = new CListBox(std::bind(&CObjectListWindow::genItem, this, _1), CListBox::DestroyFunc(),
  1411. Point(14, 151), Point(0, 25), 9, items.size(), 0, 1, Rect(262, -32, 256, 256) );
  1412. list->type |= REDRAW_PARENT;
  1413. }
  1414. CIntObject * CObjectListWindow::genItem(size_t index)
  1415. {
  1416. if (index < items.size())
  1417. return new CItem(this, index, items[index].second);
  1418. return nullptr;
  1419. }
  1420. void CObjectListWindow::elementSelected()
  1421. {
  1422. std::function<void(int)> toCall = onSelect;//save
  1423. int where = items[selected].first; //required variables
  1424. GH.popIntTotally(this);//then destroy window
  1425. toCall(where);//and send selected object
  1426. }
  1427. void CObjectListWindow::exitPressed()
  1428. {
  1429. std::function<void()> toCall = onExit;//save
  1430. GH.popIntTotally(this);//then destroy window
  1431. if(toCall)
  1432. toCall();
  1433. }
  1434. void CObjectListWindow::changeSelection(size_t which)
  1435. {
  1436. ok->block(false);
  1437. if (selected == which)
  1438. return;
  1439. std::list< CIntObject * > elements = list->getItems();
  1440. for(CIntObject * element : elements)
  1441. {
  1442. CItem *item;
  1443. if ( (item = dynamic_cast<CItem*>(element)) )
  1444. {
  1445. if (item->index == selected)
  1446. item->select(false);
  1447. if (item->index == which)
  1448. item->select(true);
  1449. }
  1450. }
  1451. selected = which;
  1452. }
  1453. void CObjectListWindow::keyPressed (const SDL_KeyboardEvent & key)
  1454. {
  1455. if(key.state != SDL_PRESSED)
  1456. return;
  1457. int sel = selected;
  1458. switch(key.keysym.sym)
  1459. {
  1460. break; case SDLK_UP:
  1461. sel -=1;
  1462. break; case SDLK_DOWN:
  1463. sel +=1;
  1464. break; case SDLK_PAGEUP:
  1465. sel -=9;
  1466. break; case SDLK_PAGEDOWN:
  1467. sel +=9;
  1468. break; case SDLK_HOME:
  1469. sel = 0;
  1470. break; case SDLK_END:
  1471. sel = items.size();
  1472. break; default:
  1473. return;
  1474. }
  1475. vstd::abetween(sel, 0, items.size()-1);
  1476. list->scrollTo(sel);
  1477. changeSelection(sel);
  1478. }