GUIClasses.cpp 64 KB

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