GUIClasses.cpp 60 KB

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