GUIClasses.cpp 62 KB

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