GUIClasses.cpp 61 KB

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