GUIClasses.cpp 62 KB

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