GUIClasses.cpp 61 KB

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