GUIClasses.cpp 62 KB

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