GUIClasses.cpp 64 KB

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