GUIClasses.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924
  1. /*
  2. * GUIClasses.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "GUIClasses.h"
  12. #include "CCastleInterface.h"
  13. #include "CCreatureWindow.h"
  14. #include "CHeroWindow.h"
  15. #include "InfoWindows.h"
  16. #include "../CGameInfo.h"
  17. #include "../CMusicHandler.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../CVideoHandler.h"
  20. #include "../CServerHandler.h"
  21. #include "../battle/BattleInterfaceClasses.h"
  22. #include "../battle/BattleInterface.h"
  23. #include "../gui/CGuiHandler.h"
  24. #include "../gui/CursorHandler.h"
  25. #include "../gui/TextAlignment.h"
  26. #include "../gui/Shortcut.h"
  27. #include "../gui/WindowHandler.h"
  28. #include "../widgets/CComponent.h"
  29. #include "../widgets/MiscWidgets.h"
  30. #include "../widgets/CreatureCostBox.h"
  31. #include "../widgets/Buttons.h"
  32. #include "../widgets/Slider.h"
  33. #include "../widgets/TextControls.h"
  34. #include "../widgets/ObjectLists.h"
  35. #include "../lobby/CSavingScreen.h"
  36. #include "../render/Canvas.h"
  37. #include "../render/CAnimation.h"
  38. #include "../CMT.h"
  39. #include "../../CCallback.h"
  40. #include "../lib/mapObjects/CGHeroInstance.h"
  41. #include "../lib/mapObjects/CGMarket.h"
  42. #include "../lib/ArtifactUtils.h"
  43. #include "../lib/mapObjects/CGTownInstance.h"
  44. #include "../lib/mapObjects/ObjectTemplate.h"
  45. #include "../lib/CArtHandler.h"
  46. #include "../lib/CBuildingHandler.h"
  47. #include "../lib/CConfigHandler.h"
  48. #include "../lib/CCreatureHandler.h"
  49. #include "../lib/CGameState.h"
  50. #include "../lib/CGeneralTextHandler.h"
  51. #include "../lib/CHeroHandler.h"
  52. #include "../lib/CModHandler.h"
  53. #include "../lib/GameSettings.h"
  54. #include "../lib/CondSh.h"
  55. #include "../lib/CSkillHandler.h"
  56. #include "../lib/spells/CSpellHandler.h"
  57. #include "../lib/filesystem/Filesystem.h"
  58. #include "../lib/CStopWatch.h"
  59. #include "../lib/CTownHandler.h"
  60. #include "../lib/GameConstants.h"
  61. #include "../lib/bonuses/Bonus.h"
  62. #include "../lib/NetPacksBase.h"
  63. #include "../lib/StartInfo.h"
  64. #include "../lib/TextOperations.h"
  65. CRecruitmentWindow::CCreatureCard::CCreatureCard(CRecruitmentWindow * window, const CCreature * crea, int totalAmount)
  66. : CIntObject(LCLICK | RCLICK),
  67. parent(window),
  68. selected(false),
  69. creature(crea),
  70. amount(totalAmount)
  71. {
  72. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  73. animation = std::make_shared<CCreaturePic>(1, 1, creature, true, true);
  74. // 1 + 1 px for borders
  75. pos.w = animation->pos.w + 2;
  76. pos.h = animation->pos.h + 2;
  77. }
  78. void CRecruitmentWindow::CCreatureCard::select(bool on)
  79. {
  80. selected = on;
  81. redraw();
  82. }
  83. void CRecruitmentWindow::CCreatureCard::clickLeft(tribool down, bool previousState)
  84. {
  85. if(down)
  86. parent->select(this->shared_from_this());
  87. }
  88. void CRecruitmentWindow::CCreatureCard::clickRight(tribool down, bool previousState)
  89. {
  90. if(down)
  91. GH.windows().createAndPushWindow<CStackWindow>(creature, true);
  92. }
  93. void CRecruitmentWindow::CCreatureCard::showAll(Canvas & to)
  94. {
  95. CIntObject::showAll(to);
  96. if(selected)
  97. to.drawBorder(pos, Colors::RED);
  98. else
  99. to.drawBorder(pos, Colors::YELLOW);
  100. }
  101. void CRecruitmentWindow::select(std::shared_ptr<CCreatureCard> card)
  102. {
  103. if(card == selected)
  104. return;
  105. if(selected)
  106. selected->select(false);
  107. selected = card;
  108. if(selected)
  109. selected->select(true);
  110. if(card)
  111. {
  112. si32 maxAmount = card->creature->maxAmount(LOCPLINT->cb->getResourceAmount());
  113. vstd::amin(maxAmount, card->amount);
  114. slider->setAmount(maxAmount);
  115. if(slider->getValue() != maxAmount)
  116. slider->scrollTo(maxAmount);
  117. else // if slider already at 0 - emulate call to sliderMoved()
  118. sliderMoved(maxAmount);
  119. costPerTroopValue->createItems(card->creature->getFullRecruitCost());
  120. totalCostValue->createItems(card->creature->getFullRecruitCost());
  121. costPerTroopValue->set(card->creature->getFullRecruitCost());
  122. totalCostValue->set(card->creature->getFullRecruitCost() * maxAmount);
  123. //Recruit %s
  124. title->setText(boost::str(boost::format(CGI->generaltexth->tcommands[21]) % card->creature->getNamePluralTranslated()));
  125. maxButton->block(maxAmount == 0);
  126. slider->block(maxAmount == 0);
  127. }
  128. }
  129. void CRecruitmentWindow::buy()
  130. {
  131. CreatureID crid = selected->creature->getId();
  132. SlotID dstslot = dst-> getSlotFor(crid);
  133. if(!dstslot.validSlot() && (selected->creature->warMachine == ArtifactID::NONE)) //no available slot
  134. {
  135. std::string txt;
  136. if(dst->ID == Obj::HERO)
  137. {
  138. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  139. boost::algorithm::replace_first(txt, "%s", slider->getValue() > 1 ? CGI->creh->objects[crid]->getNamePluralTranslated() : CGI->creh->objects[crid]->getNameSingularTranslated());
  140. }
  141. else
  142. {
  143. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  144. }
  145. LOCPLINT->showInfoDialog(txt);
  146. return;
  147. }
  148. onRecruit(crid, slider->getValue());
  149. if(level >= 0)
  150. close();
  151. }
  152. void CRecruitmentWindow::showAll(Canvas & to)
  153. {
  154. CWindowObject::showAll(to);
  155. Rect(172, 222, 67, 42) + pos.topLeft();
  156. // recruit\total values
  157. to.drawBorder(Rect(172, 222, 67, 42) + pos.topLeft(), Colors::YELLOW);
  158. to.drawBorder(Rect(246, 222, 67, 42) + pos.topLeft(), Colors::YELLOW);
  159. //cost boxes
  160. to.drawBorder(Rect( 64, 222, 99, 76) + pos.topLeft(), Colors::YELLOW);
  161. to.drawBorder(Rect(322, 222, 99, 76) + pos.topLeft(), Colors::YELLOW);
  162. //buttons borders
  163. to.drawBorder(Rect(133, 312, 66, 34) + pos.topLeft(), Colors::METALLIC_GOLD);
  164. to.drawBorder(Rect(211, 312, 66, 34) + pos.topLeft(), Colors::METALLIC_GOLD);
  165. to.drawBorder(Rect(289, 312, 66, 34) + pos.topLeft(), Colors::METALLIC_GOLD);
  166. }
  167. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling * Dwelling, int Level, const CArmedInstance * Dst, const std::function<void(CreatureID,int)> & Recruit, int y_offset):
  168. CStatusbarWindow(PLAYER_COLORED, "TPRCRT"),
  169. onRecruit(Recruit),
  170. level(Level),
  171. dst(Dst),
  172. selected(nullptr),
  173. dwelling(Dwelling)
  174. {
  175. moveBy(Point(0, y_offset));
  176. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  177. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  178. slider = std::make_shared<CSlider>(Point(176,279),135,std::bind(&CRecruitmentWindow::sliderMoved,this, _1),0,0,0,true);
  179. maxButton = std::make_shared<CButton>(Point(134, 313), "IRCBTNS.DEF", CGI->generaltexth->zelp[553], std::bind(&CSlider::scrollToMax, slider), EShortcut::RECRUITMENT_MAX);
  180. buyButton = std::make_shared<CButton>(Point(212, 313), "IBY6432.DEF", CGI->generaltexth->zelp[554], std::bind(&CRecruitmentWindow::buy, this), EShortcut::GLOBAL_ACCEPT);
  181. cancelButton = std::make_shared<CButton>(Point(290, 313), "ICN6432.DEF", CGI->generaltexth->zelp[555], std::bind(&CRecruitmentWindow::close, this), EShortcut::GLOBAL_CANCEL);
  182. title = std::make_shared<CLabel>(243, 32, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW);
  183. availableValue = std::make_shared<CLabel>(205, 253, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  184. toRecruitValue = std::make_shared<CLabel>(279, 253, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  185. costPerTroopValue = std::make_shared<CreatureCostBox>(Rect(65, 222, 97, 74), CGI->generaltexth->allTexts[346]);
  186. totalCostValue = std::make_shared<CreatureCostBox>(Rect(323, 222, 97, 74), CGI->generaltexth->allTexts[466]);
  187. availableTitle = std::make_shared<CLabel>(205, 233, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[465]);
  188. toRecruitTitle = std::make_shared<CLabel>(279, 233, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[16]);
  189. availableCreaturesChanged();
  190. }
  191. void CRecruitmentWindow::availableCreaturesChanged()
  192. {
  193. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  194. size_t selectedIndex = 0;
  195. if(!cards.empty() && selected) // find position of selected item
  196. selectedIndex = std::find(cards.begin(), cards.end(), selected) - cards.begin();
  197. select(nullptr);
  198. cards.clear();
  199. for(int i=0; i<dwelling->creatures.size(); i++)
  200. {
  201. //find appropriate level
  202. if(level >= 0 && i != level)
  203. continue;
  204. int amount = dwelling->creatures[i].first;
  205. //create new cards
  206. for(auto & creature : boost::adaptors::reverse(dwelling->creatures[i].second))
  207. cards.push_back(std::make_shared<CCreatureCard>(this, CGI->creh->objects[creature], amount));
  208. }
  209. const int creatureWidth = 102;
  210. //normal distance between cards - 18px
  211. int requiredSpace = 18;
  212. //maximum distance we can use without reaching window borders
  213. int availableSpace = pos.w - 50 - creatureWidth * (int)cards.size();
  214. if (cards.size() > 1) // avoid division by zero
  215. availableSpace /= (int)cards.size() - 1;
  216. else
  217. availableSpace = 0;
  218. assert(availableSpace >= 0);
  219. const int spaceBetween = std::min(requiredSpace, availableSpace);
  220. const int totalCreatureWidth = spaceBetween + creatureWidth;
  221. //now we know total amount of cards and can move them to correct position
  222. int curx = pos.w / 2 - (creatureWidth*(int)cards.size()/2) - (spaceBetween*((int)cards.size()-1)/2);
  223. for(auto & card : cards)
  224. {
  225. card->moveBy(Point(curx, 64));
  226. curx += totalCreatureWidth;
  227. }
  228. //restore selection
  229. select(cards[selectedIndex]);
  230. if(slider->getValue() == slider->getAmount())
  231. slider->scrollToMax();
  232. else // if slider already at 0 - emulate call to sliderMoved()
  233. sliderMoved(slider->getAmount());
  234. }
  235. void CRecruitmentWindow::sliderMoved(int to)
  236. {
  237. if(!selected)
  238. return;
  239. buyButton->block(!to);
  240. availableValue->setText(std::to_string(selected->amount - to));
  241. toRecruitValue->setText(std::to_string(to));
  242. totalCostValue->set(selected->creature->getFullRecruitCost() * to);
  243. }
  244. CSplitWindow::CSplitWindow(const CCreature * creature, std::function<void(int, int)> callback_, int leftMin_, int rightMin_, int leftAmount_, int rightAmount_)
  245. : CWindowObject(PLAYER_COLORED, "GPUCRDIV"),
  246. callback(callback_),
  247. leftAmount(leftAmount_),
  248. rightAmount(rightAmount_),
  249. leftMin(leftMin_),
  250. rightMin(rightMin_)
  251. {
  252. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  253. int total = leftAmount + rightAmount;
  254. int leftMax = total - rightMin;
  255. int rightMax = total - leftMin;
  256. ok = std::make_shared<CButton>(Point(20, 263), "IOK6432", CButton::tooltip(), std::bind(&CSplitWindow::apply, this), EShortcut::GLOBAL_ACCEPT);
  257. cancel = std::make_shared<CButton>(Point(214, 263), "ICN6432", CButton::tooltip(), std::bind(&CSplitWindow::close, this), EShortcut::GLOBAL_CANCEL);
  258. int sliderPosition = total - leftMin - rightMin;
  259. leftInput = std::make_shared<CTextInput>(Rect(20, 218, 100, 36), FONT_BIG, std::bind(&CSplitWindow::setAmountText, this, _1, true));
  260. rightInput = std::make_shared<CTextInput>(Rect(176, 218, 100, 36), FONT_BIG, std::bind(&CSplitWindow::setAmountText, this, _1, false));
  261. //add filters to allow only number input
  262. leftInput->filters += std::bind(&CTextInput::numberFilter, _1, _2, leftMin, leftMax);
  263. rightInput->filters += std::bind(&CTextInput::numberFilter, _1, _2, rightMin, rightMax);
  264. leftInput->setText(std::to_string(leftAmount), false);
  265. rightInput->setText(std::to_string(rightAmount), false);
  266. animLeft = std::make_shared<CCreaturePic>(20, 54, creature, true, false);
  267. animRight = std::make_shared<CCreaturePic>(177, 54,creature, true, false);
  268. slider = std::make_shared<CSlider>(Point(21, 194), 257, std::bind(&CSplitWindow::sliderMoved, this, _1), 0, sliderPosition, rightAmount - rightMin, true);
  269. std::string titleStr = CGI->generaltexth->allTexts[256];
  270. boost::algorithm::replace_first(titleStr,"%s", creature->getNamePluralTranslated());
  271. title = std::make_shared<CLabel>(150, 34, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, titleStr);
  272. }
  273. void CSplitWindow::setAmountText(std::string text, bool left)
  274. {
  275. int amount = 0;
  276. if(text.length())
  277. {
  278. try
  279. {
  280. amount = boost::lexical_cast<int>(text);
  281. }
  282. catch(boost::bad_lexical_cast &)
  283. {
  284. amount = left ? leftAmount : rightAmount;
  285. }
  286. int total = leftAmount + rightAmount;
  287. if(amount > total)
  288. amount = total;
  289. }
  290. setAmount(amount, left);
  291. slider->scrollTo(rightAmount - rightMin);
  292. }
  293. void CSplitWindow::setAmount(int value, bool left)
  294. {
  295. int total = leftAmount + rightAmount;
  296. leftAmount = left ? value : total - value;
  297. rightAmount = left ? total - value : value;
  298. leftInput->setText(std::to_string(leftAmount));
  299. rightInput->setText(std::to_string(rightAmount));
  300. }
  301. void CSplitWindow::apply()
  302. {
  303. callback(leftAmount, rightAmount);
  304. close();
  305. }
  306. void CSplitWindow::sliderMoved(int to)
  307. {
  308. setAmount(rightMin + to, false);
  309. }
  310. CLevelWindow::CLevelWindow(const CGHeroInstance * hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> & skills, std::function<void(ui32)> callback)
  311. : CWindowObject(PLAYER_COLORED, "LVLUPBKG"),
  312. cb(callback)
  313. {
  314. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  315. LOCPLINT->showingDialog->setn(true);
  316. if(!skills.empty())
  317. {
  318. std::vector<std::shared_ptr<CSelectableComponent>> comps;
  319. for(auto & skill : skills)
  320. {
  321. auto comp = std::make_shared<CSelectableComponent>(CComponent::secskill, skill, hero->getSecSkillLevel(SecondarySkill(skill))+1, CComponent::medium);
  322. comps.push_back(comp);
  323. }
  324. box = std::make_shared<CComponentBox>(comps, Rect(75, 300, pos.w - 150, 100));
  325. }
  326. portrait = std::make_shared<CAnimImage>("PortraitsLarge", hero->portrait, 0, 170, 66);
  327. ok = std::make_shared<CButton>(Point(297, 413), "IOKAY", CButton::tooltip(), std::bind(&CLevelWindow::close, this), EShortcut::GLOBAL_ACCEPT);
  328. //%s has gained a level.
  329. mainTitle = std::make_shared<CLabel>(192, 33, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, boost::str(boost::format(CGI->generaltexth->allTexts[444]) % hero->getNameTranslated()));
  330. //%s is now a level %d %s.
  331. std::string levelTitleText = CGI->generaltexth->translate("core.genrltxt.445");
  332. boost::replace_first(levelTitleText, "%s", hero->getNameTranslated());
  333. boost::replace_first(levelTitleText, "%d", std::to_string(hero->level));
  334. boost::replace_first(levelTitleText, "%s", hero->type->heroClass->getNameTranslated());
  335. levelTitle = std::make_shared<CLabel>(192, 162, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, levelTitleText);
  336. skillIcon = std::make_shared<CAnimImage>("PSKIL42", pskill, 0, 174, 190);
  337. skillValue = std::make_shared<CLabel>(192, 253, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->primarySkillNames[pskill] + " +1");
  338. }
  339. CLevelWindow::~CLevelWindow()
  340. {
  341. //FIXME: call callback if there was nothing to select?
  342. if (box && box->selectedIndex() != -1)
  343. cb(box->selectedIndex());
  344. LOCPLINT->showingDialog->setn(false);
  345. }
  346. CTavernWindow::CTavernWindow(const CGObjectInstance * TavernObj)
  347. : CStatusbarWindow(PLAYER_COLORED, "TPTAVERN"),
  348. tavernObj(TavernObj)
  349. {
  350. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  351. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(TavernObj);
  352. if(h.size() < 2)
  353. h.resize(2, nullptr);
  354. selected = 0;
  355. if(!h[0])
  356. selected = 1;
  357. if(!h[0] && !h[1])
  358. selected = -1;
  359. oldSelected = -1;
  360. h1 = std::make_shared<HeroPortrait>(selected, 0, 72, 299, h[0]);
  361. h2 = std::make_shared<HeroPortrait>(selected, 1, 162, 299, h[1]);
  362. title = std::make_shared<CLabel>(197, 32, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[37]);
  363. cost = std::make_shared<CLabel>(320, 328, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(GameConstants::HERO_GOLD_COST));
  364. heroDescription = std::make_shared<CTextBox>("", Rect(30, 373, 233, 35), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  365. heroesForHire = std::make_shared<CLabel>(145, 283, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[38]);
  366. auto rumorText = boost::str(boost::format(CGI->generaltexth->allTexts[216]) % LOCPLINT->cb->getTavernRumor(tavernObj));
  367. rumor = std::make_shared<CTextBox>(rumorText, Rect(32, 188, 330, 66), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  368. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  369. cancel = std::make_shared<CButton>(Point(310, 428), "ICANCEL.DEF", CButton::tooltip(CGI->generaltexth->tavernInfo[7]), std::bind(&CTavernWindow::close, this), EShortcut::GLOBAL_CANCEL);
  370. recruit = std::make_shared<CButton>(Point(272, 355), "TPTAV01.DEF", CButton::tooltip(), std::bind(&CTavernWindow::recruitb, this), EShortcut::GLOBAL_ACCEPT);
  371. thiefGuild = std::make_shared<CButton>(Point(22, 428), "TPTAV02.DEF", CButton::tooltip(CGI->generaltexth->tavernInfo[5]), std::bind(&CTavernWindow::thievesguildb, this), EShortcut::ADVENTURE_THIEVES_GUILD);
  372. if(LOCPLINT->cb->getResourceAmount(EGameResID::GOLD) < GameConstants::HERO_GOLD_COST) //not enough gold
  373. {
  374. recruit->addHoverText(CButton::NORMAL, CGI->generaltexth->tavernInfo[0]); //Cannot afford a Hero
  375. recruit->block(true);
  376. }
  377. else if(LOCPLINT->cb->howManyHeroes(true) >= CGI->settings()->getInteger(EGameSettings::HEROES_PER_PLAYER_TOTAL_CAP))
  378. {
  379. //Cannot recruit. You already have %d Heroes.
  380. recruit->addHoverText(CButton::NORMAL, boost::str(boost::format(CGI->generaltexth->tavernInfo[1]) % LOCPLINT->cb->howManyHeroes(true)));
  381. recruit->block(true);
  382. }
  383. else if(LOCPLINT->cb->howManyHeroes(false) >= CGI->settings()->getInteger(EGameSettings::HEROES_PER_PLAYER_ON_MAP_CAP))
  384. {
  385. //Cannot recruit. You already have %d Heroes.
  386. recruit->addHoverText(CButton::NORMAL, boost::str(boost::format(CGI->generaltexth->tavernInfo[1]) % LOCPLINT->cb->howManyHeroes(false)));
  387. recruit->block(true);
  388. }
  389. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  390. {
  391. recruit->addHoverText(CButton::NORMAL, CGI->generaltexth->tavernInfo[2]); //Cannot recruit. You already have a Hero in this town.
  392. recruit->block(true);
  393. }
  394. else
  395. {
  396. if(selected == -1)
  397. recruit->block(true);
  398. }
  399. if(LOCPLINT->castleInt)
  400. CCS->videoh->open(LOCPLINT->castleInt->town->town->clientInfo.tavernVideo);
  401. else
  402. CCS->videoh->open("TAVERN.BIK");
  403. }
  404. void CTavernWindow::recruitb()
  405. {
  406. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  407. const CGObjectInstance *obj = tavernObj;
  408. close();
  409. LOCPLINT->cb->recruitHero(obj, toBuy);
  410. }
  411. void CTavernWindow::thievesguildb()
  412. {
  413. GH.windows().createAndPushWindow<CThievesGuildWindow>(tavernObj);
  414. }
  415. CTavernWindow::~CTavernWindow()
  416. {
  417. CCS->videoh->close();
  418. }
  419. void CTavernWindow::show(Canvas & to)
  420. {
  421. CWindowObject::show(to);
  422. if(selected >= 0)
  423. {
  424. auto sel = selected ? h2 : h1;
  425. if(selected != oldSelected)
  426. {
  427. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  428. oldSelected = selected;
  429. heroDescription->setText(sel->description);
  430. //Recruit %s the %s
  431. if (!recruit->isBlocked())
  432. recruit->addHoverText(CButton::NORMAL, boost::str(boost::format(CGI->generaltexth->tavernInfo[3]) % sel->h->getNameTranslated() % sel->h->type->heroClass->getNameTranslated()));
  433. }
  434. to.drawBorder(Rect::createAround(sel->pos, 2), Colors::BRIGHT_YELLOW, 2);
  435. }
  436. CCS->videoh->update(pos.x+70, pos.y+56, to.getInternalSurface(), true, false);
  437. }
  438. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  439. {
  440. if(h && previousState && !down)
  441. *_sel = _id;
  442. }
  443. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  444. {
  445. if(h && down)
  446. GH.windows().createAndPushWindow<CRClickPopupInt>(std::make_shared<CHeroWindow>(h));
  447. }
  448. CTavernWindow::HeroPortrait::HeroPortrait(int & sel, int id, int x, int y, const CGHeroInstance * H)
  449. : CIntObject(LCLICK | RCLICK | HOVER),
  450. h(H), _sel(&sel), _id(id)
  451. {
  452. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  453. h = H;
  454. pos.x += x;
  455. pos.y += y;
  456. pos.w = 58;
  457. pos.h = 64;
  458. if(H)
  459. {
  460. hoverName = CGI->generaltexth->tavernInfo[4];
  461. boost::algorithm::replace_first(hoverName,"%s",H->getNameTranslated());
  462. int artifs = (int)h->artifactsWorn.size() + (int)h->artifactsInBackpack.size();
  463. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  464. if(vstd::contains(h->artifactsWorn, ArtifactPosition(i)))
  465. artifs--;
  466. description = CGI->generaltexth->allTexts[215];
  467. boost::algorithm::replace_first(description, "%s", h->getNameTranslated());
  468. boost::algorithm::replace_first(description, "%d", std::to_string(h->level));
  469. boost::algorithm::replace_first(description, "%s", h->type->heroClass->getNameTranslated());
  470. boost::algorithm::replace_first(description, "%d", std::to_string(artifs));
  471. portrait = std::make_shared<CAnimImage>("portraitsLarge", h->portrait);
  472. }
  473. }
  474. void CTavernWindow::HeroPortrait::hover(bool on)
  475. {
  476. //Hoverable::hover(on);
  477. if(on)
  478. GH.statusbar()->write(hoverName);
  479. else
  480. GH.statusbar()->clear();
  481. }
  482. static const std::string QUICK_EXCHANGE_MOD_PREFIX = "quick-exchange";
  483. static const std::string QUICK_EXCHANGE_BG = QUICK_EXCHANGE_MOD_PREFIX + "/TRADEQE";
  484. static bool isQuickExchangeLayoutAvailable()
  485. {
  486. return CResourceHandler::get()->existsResource(ResourceID(std::string("SPRITES/") + QUICK_EXCHANGE_BG, EResType::IMAGE));
  487. }
  488. // Runs a task asynchronously with gamestate locking and waitTillRealize set to true
  489. class GsThread
  490. {
  491. private:
  492. std::function<void()> action;
  493. std::shared_ptr<CCallback> cb;
  494. public:
  495. static void run(std::function<void()> action)
  496. {
  497. std::shared_ptr<GsThread> instance(new GsThread(action));
  498. boost::thread(std::bind(&GsThread::staticRun, instance));
  499. }
  500. private:
  501. GsThread(std::function<void()> action)
  502. :action(action), cb(LOCPLINT->cb)
  503. {
  504. }
  505. static void staticRun(std::shared_ptr<GsThread> instance)
  506. {
  507. instance->run();
  508. }
  509. void run()
  510. {
  511. boost::shared_lock<boost::shared_mutex> gsLock(CGameState::mutex);
  512. auto originalWaitTillRealize = cb->waitTillRealize;
  513. auto originalUnlockGsWhenWating = cb->unlockGsWhenWaiting;
  514. cb->waitTillRealize = true;
  515. cb->unlockGsWhenWaiting = true;
  516. action();
  517. cb->waitTillRealize = originalWaitTillRealize;
  518. cb->unlockGsWhenWaiting = originalUnlockGsWhenWating;
  519. }
  520. };
  521. CExchangeController::CExchangeController(CExchangeWindow * view, ObjectInstanceID hero1, ObjectInstanceID hero2)
  522. :left(LOCPLINT->cb->getHero(hero1)), right(LOCPLINT->cb->getHero(hero2)), cb(LOCPLINT->cb), view(view)
  523. {
  524. }
  525. std::function<void()> CExchangeController::onMoveArmyToLeft()
  526. {
  527. return [&]() { moveArmy(false); };
  528. }
  529. std::function<void()> CExchangeController::onMoveArmyToRight()
  530. {
  531. return [&]() { moveArmy(true); };
  532. }
  533. std::vector<CArtifactInstance *> getBackpackArts(const CGHeroInstance * hero)
  534. {
  535. std::vector<CArtifactInstance *> result;
  536. for(auto slot : hero->artifactsInBackpack)
  537. {
  538. result.push_back(slot.artifact);
  539. }
  540. return result;
  541. }
  542. std::function<void()> CExchangeController::onSwapArtifacts()
  543. {
  544. return [&]()
  545. {
  546. GsThread::run([=]
  547. {
  548. cb->bulkMoveArtifacts(left->id, right->id, true);
  549. });
  550. };
  551. }
  552. std::function<void()> CExchangeController::onMoveArtifactsToLeft()
  553. {
  554. return [&]() { moveArtifacts(false); };
  555. }
  556. std::function<void()> CExchangeController::onMoveArtifactsToRight()
  557. {
  558. return [&]() { moveArtifacts(true); };
  559. }
  560. std::vector<std::pair<SlotID, CStackInstance *>> getStacks(const CArmedInstance * source)
  561. {
  562. auto slots = source->Slots();
  563. return std::vector<std::pair<SlotID, CStackInstance *>>(slots.begin(), slots.end());
  564. }
  565. std::function<void()> CExchangeController::onSwapArmy()
  566. {
  567. return [&]()
  568. {
  569. GsThread::run([=]
  570. {
  571. if(left->tempOwner != cb->getMyColor()
  572. || right->tempOwner != cb->getMyColor())
  573. {
  574. return;
  575. }
  576. auto leftSlots = getStacks(left);
  577. auto rightSlots = getStacks(right);
  578. auto i = leftSlots.begin(), j = rightSlots.begin();
  579. for(; i != leftSlots.end() && j != rightSlots.end(); i++, j++)
  580. {
  581. cb->swapCreatures(left, right, i->first, j->first);
  582. }
  583. if(i != leftSlots.end())
  584. {
  585. for(; i != leftSlots.end(); i++)
  586. {
  587. cb->swapCreatures(left, right, i->first, right->getFreeSlot());
  588. }
  589. }
  590. else if(j != rightSlots.end())
  591. {
  592. for(; j != rightSlots.end(); j++)
  593. {
  594. cb->swapCreatures(left, right, left->getFreeSlot(), j->first);
  595. }
  596. }
  597. });
  598. };
  599. }
  600. std::function<void()> CExchangeController::onMoveStackToLeft(SlotID slotID)
  601. {
  602. return [=]()
  603. {
  604. if(right->tempOwner != cb->getMyColor())
  605. {
  606. return;
  607. }
  608. moveStack(right, left, slotID);
  609. };
  610. }
  611. std::function<void()> CExchangeController::onMoveStackToRight(SlotID slotID)
  612. {
  613. return [=]()
  614. {
  615. if(left->tempOwner != cb->getMyColor())
  616. {
  617. return;
  618. }
  619. moveStack(left, right, slotID);
  620. };
  621. }
  622. void CExchangeController::moveStack(
  623. const CGHeroInstance * source,
  624. const CGHeroInstance * target,
  625. SlotID sourceSlot)
  626. {
  627. auto creature = source->getCreature(sourceSlot);
  628. SlotID targetSlot = target->getSlotFor(creature);
  629. if(targetSlot.validSlot())
  630. {
  631. if(source->stacksCount() == 1 && source->needsLastStack())
  632. {
  633. cb->splitStack(
  634. source,
  635. target,
  636. sourceSlot,
  637. targetSlot,
  638. target->getStackCount(targetSlot) + source->getStackCount(sourceSlot) - 1);
  639. }
  640. else
  641. {
  642. cb->mergeOrSwapStacks(source, target, sourceSlot, targetSlot);
  643. }
  644. }
  645. }
  646. void CExchangeController::moveArmy(bool leftToRight)
  647. {
  648. const CGHeroInstance * source = leftToRight ? left : right;
  649. const CGHeroInstance * target = leftToRight ? right : left;
  650. const CGarrisonSlot * selection = this->view->getSelectedSlotID();
  651. SlotID slot;
  652. if(source->tempOwner != cb->getMyColor())
  653. {
  654. return;
  655. }
  656. if(selection && selection->our() && selection->getObj() == source)
  657. {
  658. slot = selection->getSlot();
  659. }
  660. else
  661. {
  662. auto weakestSlot = vstd::minElementByFun(
  663. source->Slots(),
  664. [](const std::pair<SlotID, CStackInstance *> & s) -> int
  665. {
  666. return s.second->getCreatureID().toCreature()->getAIValue();
  667. });
  668. slot = weakestSlot->first;
  669. }
  670. cb->bulkMoveArmy(source->id, target->id, slot);
  671. }
  672. void CExchangeController::moveArtifacts(bool leftToRight)
  673. {
  674. const CGHeroInstance * source = leftToRight ? left : right;
  675. const CGHeroInstance * target = leftToRight ? right : left;
  676. if(source->tempOwner != cb->getMyColor())
  677. {
  678. return;
  679. }
  680. GsThread::run([=]
  681. {
  682. cb->bulkMoveArtifacts(source->id, target->id, false);
  683. });
  684. }
  685. void CExchangeController::moveArtifact(
  686. const CGHeroInstance * source,
  687. const CGHeroInstance * target,
  688. ArtifactPosition srcPosition)
  689. {
  690. auto srcLocation = ArtifactLocation(source, srcPosition);
  691. auto dstLocation = ArtifactLocation(target,
  692. ArtifactUtils::getArtAnyPosition(target, source->getArt(srcPosition)->getTypeId()));
  693. cb->swapArtifacts(srcLocation, dstLocation);
  694. }
  695. CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID queryID)
  696. : CStatusbarWindow(PLAYER_COLORED | BORDERED, isQuickExchangeLayoutAvailable() ? QUICK_EXCHANGE_BG : "TRADE2"),
  697. controller(this, hero1, hero2),
  698. moveStackLeftButtons(),
  699. moveStackRightButtons()
  700. {
  701. const bool qeLayout = isQuickExchangeLayoutAvailable();
  702. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  703. heroInst[0] = LOCPLINT->cb->getHero(hero1);
  704. heroInst[1] = LOCPLINT->cb->getHero(hero2);
  705. auto genTitle = [](const CGHeroInstance * h)
  706. {
  707. boost::format fmt(CGI->generaltexth->allTexts[138]);
  708. fmt % h->getNameTranslated() % h->level % h->type->heroClass->getNameTranslated();
  709. return boost::str(fmt);
  710. };
  711. titles[0] = std::make_shared<CLabel>(147, 25, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, genTitle(heroInst[0]));
  712. titles[1] = std::make_shared<CLabel>(653, 25, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, genTitle(heroInst[1]));
  713. auto PSKIL32 = std::make_shared<CAnimation>("PSKIL32");
  714. PSKIL32->preload();
  715. auto SECSK32 = std::make_shared<CAnimation>("SECSK32");
  716. for(int g = 0; g < 4; ++g)
  717. {
  718. if (qeLayout)
  719. primSkillImages.push_back(std::make_shared<CAnimImage>(PSKIL32, g, Rect(389, 12 + 26 * g, 22, 22)));
  720. else
  721. primSkillImages.push_back(std::make_shared<CAnimImage>(PSKIL32, g, 0, 385, 19 + 36 * g));
  722. }
  723. for(int leftRight : {0, 1})
  724. {
  725. const CGHeroInstance * hero = heroInst.at(leftRight);
  726. herosWArt[leftRight] = std::make_shared<CHeroWithMaybePickedArtifact>(this, hero);
  727. for(int m=0; m<GameConstants::PRIMARY_SKILLS; ++m)
  728. primSkillValues[leftRight].push_back(std::make_shared<CLabel>(352 + (qeLayout ? 96 : 93) * leftRight, (qeLayout ? 22 : 35) + (qeLayout ? 26 : 36) * m, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE));
  729. for(int m=0; m < hero->secSkills.size(); ++m)
  730. secSkillIcons[leftRight].push_back(std::make_shared<CAnimImage>(SECSK32, 0, 0, 32 + 36 * m + 454 * leftRight, qeLayout ? 83 : 88));
  731. specImages[leftRight] = std::make_shared<CAnimImage>("UN32", hero->type->imageIndex, 0, 67 + 490 * leftRight, qeLayout ? 41 : 45);
  732. expImages[leftRight] = std::make_shared<CAnimImage>(PSKIL32, 4, 0, 103 + 490 * leftRight, qeLayout ? 41 : 45);
  733. expValues[leftRight] = std::make_shared<CLabel>(119 + 490 * leftRight, qeLayout ? 66 : 71, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  734. manaImages[leftRight] = std::make_shared<CAnimImage>(PSKIL32, 5, 0, 139 + 490 * leftRight, qeLayout ? 41 : 45);
  735. manaValues[leftRight] = std::make_shared<CLabel>(155 + 490 * leftRight, qeLayout ? 66 : 71, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  736. }
  737. portraits[0] = std::make_shared<CAnimImage>("PortraitsLarge", heroInst[0]->portrait, 0, 257, 13);
  738. portraits[1] = std::make_shared<CAnimImage>("PortraitsLarge", heroInst[1]->portrait, 0, 485, 13);
  739. artifs[0] = std::make_shared<CArtifactsOfHeroMain>(Point(-334, 150));
  740. artifs[0]->setHero(heroInst[0]);
  741. artifs[1] = std::make_shared<CArtifactsOfHeroMain>(Point(98, 150));
  742. artifs[1]->setHero(heroInst[1]);
  743. addSet(artifs[0]);
  744. addSet(artifs[1]);
  745. for(int g=0; g<4; ++g)
  746. {
  747. primSkillAreas.push_back(std::make_shared<LRClickableAreaWTextComp>());
  748. if (qeLayout)
  749. primSkillAreas[g]->pos = Rect(Point(pos.x + 324, pos.y + 12 + 26 * g), Point(152, 22));
  750. else
  751. primSkillAreas[g]->pos = Rect(Point(pos.x + 329, pos.y + 19 + 36 * g), Point(140, 32));
  752. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  753. primSkillAreas[g]->type = g;
  754. primSkillAreas[g]->bonusValue = 0;
  755. primSkillAreas[g]->baseType = 0;
  756. primSkillAreas[g]->hoverText = CGI->generaltexth->heroscrn[1];
  757. boost::replace_first(primSkillAreas[g]->hoverText, "%s", CGI->generaltexth->primarySkillNames[g]);
  758. }
  759. //heroes related thing
  760. for(int b=0; b < heroInst.size(); b++)
  761. {
  762. const CGHeroInstance * hero = heroInst.at(b);
  763. //secondary skill's clickable areas
  764. for(int g=0; g<hero->secSkills.size(); ++g)
  765. {
  766. int skill = hero->secSkills[g].first,
  767. level = hero->secSkills[g].second; // <1, 3>
  768. secSkillAreas[b].push_back(std::make_shared<LRClickableAreaWTextComp>());
  769. secSkillAreas[b][g]->pos = Rect(Point(pos.x + 32 + g * 36 + b * 454 , pos.y + (qeLayout ? 83 : 88)), Point(32, 32) );
  770. secSkillAreas[b][g]->baseType = 1;
  771. secSkillAreas[b][g]->type = skill;
  772. secSkillAreas[b][g]->bonusValue = level;
  773. secSkillAreas[b][g]->text = CGI->skillh->getByIndex(skill)->getDescriptionTranslated(level);
  774. secSkillAreas[b][g]->hoverText = CGI->generaltexth->heroscrn[21];
  775. boost::algorithm::replace_first(secSkillAreas[b][g]->hoverText, "%s", CGI->generaltexth->levels[level - 1]);
  776. boost::algorithm::replace_first(secSkillAreas[b][g]->hoverText, "%s", CGI->skillh->getByIndex(skill)->getNameTranslated());
  777. }
  778. heroAreas[b] = std::make_shared<CHeroArea>(257 + 228*b, 13, hero);
  779. specialtyAreas[b] = std::make_shared<LRClickableAreaWText>();
  780. specialtyAreas[b]->pos = Rect(Point(pos.x + 69 + 490 * b, pos.y + (qeLayout ? 41 : 45)), Point(32, 32));
  781. specialtyAreas[b]->hoverText = CGI->generaltexth->heroscrn[27];
  782. specialtyAreas[b]->text = hero->type->getSpecialtyDescriptionTranslated();
  783. experienceAreas[b] = std::make_shared<LRClickableAreaWText>();
  784. experienceAreas[b]->pos = Rect(Point(pos.x + 105 + 490 * b, pos.y + (qeLayout ? 41 : 45)), Point(32, 32));
  785. experienceAreas[b]->hoverText = CGI->generaltexth->heroscrn[9];
  786. experienceAreas[b]->text = CGI->generaltexth->allTexts[2];
  787. boost::algorithm::replace_first(experienceAreas[b]->text, "%d", std::to_string(hero->level));
  788. boost::algorithm::replace_first(experienceAreas[b]->text, "%d", std::to_string(CGI->heroh->reqExp(hero->level+1)));
  789. boost::algorithm::replace_first(experienceAreas[b]->text, "%d", std::to_string(hero->exp));
  790. spellPointsAreas[b] = std::make_shared<LRClickableAreaWText>();
  791. spellPointsAreas[b]->pos = Rect(Point(pos.x + 141 + 490 * b, pos.y + (qeLayout ? 41 : 45)), Point(32, 32));
  792. spellPointsAreas[b]->hoverText = CGI->generaltexth->heroscrn[22];
  793. spellPointsAreas[b]->text = CGI->generaltexth->allTexts[205];
  794. boost::algorithm::replace_first(spellPointsAreas[b]->text, "%s", hero->getNameTranslated());
  795. boost::algorithm::replace_first(spellPointsAreas[b]->text, "%d", std::to_string(hero->mana));
  796. boost::algorithm::replace_first(spellPointsAreas[b]->text, "%d", std::to_string(hero->manaLimit()));
  797. morale[b] = std::make_shared<MoraleLuckBox>(true, Rect(Point(176 + 490 * b, 39), Point(32, 32)), true);
  798. luck[b] = std::make_shared<MoraleLuckBox>(false, Rect(Point(212 + 490 * b, 39), Point(32, 32)), true);
  799. }
  800. quit = std::make_shared<CButton>(Point(732, 567), "IOKAY.DEF", CGI->generaltexth->zelp[600], std::bind(&CExchangeWindow::close, this), EShortcut::GLOBAL_ACCEPT);
  801. if(queryID.getNum() > 0)
  802. quit->addCallback([=](){ LOCPLINT->cb->selectionMade(0, queryID); });
  803. questlogButton[0] = std::make_shared<CButton>(Point( 10, qeLayout ? 39 : 44), "hsbtns4.def", CButton::tooltip(CGI->generaltexth->heroscrn[0]), std::bind(&CExchangeWindow::questlog, this, 0));
  804. questlogButton[1] = std::make_shared<CButton>(Point(740, qeLayout ? 39 : 44), "hsbtns4.def", CButton::tooltip(CGI->generaltexth->heroscrn[0]), std::bind(&CExchangeWindow::questlog, this, 1));
  805. Rect barRect(5, 578, 725, 18);
  806. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), barRect, 5, 578));
  807. //garrison interface
  808. garr = std::make_shared<CGarrisonInt>(69, qeLayout ? 122 : 131, 4, Point(418,0), heroInst[0], heroInst[1], true, true);
  809. auto splitButtonCallback = [&](){ garr->splitClick(); };
  810. garr->addSplitBtn(std::make_shared<CButton>( Point( 10, qeLayout ? 122 : 132), "TSBTNS.DEF", CButton::tooltip(CGI->generaltexth->tcommands[3]), splitButtonCallback));
  811. garr->addSplitBtn(std::make_shared<CButton>( Point(744, qeLayout ? 122 : 132), "TSBTNS.DEF", CButton::tooltip(CGI->generaltexth->tcommands[3]), splitButtonCallback));
  812. if(qeLayout)
  813. {
  814. moveAllGarrButtonLeft = std::make_shared<CButton>(Point(325, 118), QUICK_EXCHANGE_MOD_PREFIX + "/armRight.DEF", CButton::tooltip(CGI->generaltexth->qeModCommands[1]), controller.onMoveArmyToRight());
  815. echangeGarrButton = std::make_shared<CButton>(Point(377, 118), QUICK_EXCHANGE_MOD_PREFIX + "/swapAll.DEF", CButton::tooltip(CGI->generaltexth->qeModCommands[2]), controller.onSwapArmy());
  816. moveAllGarrButtonRight = std::make_shared<CButton>(Point(425, 118), QUICK_EXCHANGE_MOD_PREFIX + "/armLeft.DEF", CButton::tooltip(CGI->generaltexth->qeModCommands[1]), controller.onMoveArmyToLeft());
  817. moveArtifactsButtonLeft = std::make_shared<CButton>(Point(325, 154), QUICK_EXCHANGE_MOD_PREFIX + "/artRight.DEF", CButton::tooltip(CGI->generaltexth->qeModCommands[3]), controller.onMoveArtifactsToRight());
  818. echangeArtifactsButton = std::make_shared<CButton>(Point(377, 154), QUICK_EXCHANGE_MOD_PREFIX + "/swapAll.DEF", CButton::tooltip(CGI->generaltexth->qeModCommands[4]), controller.onSwapArtifacts());
  819. moveArtifactsButtonRight = std::make_shared<CButton>(Point(425, 154), QUICK_EXCHANGE_MOD_PREFIX + "/artLeft.DEF", CButton::tooltip(CGI->generaltexth->qeModCommands[3]), controller.onMoveArtifactsToLeft());
  820. for(int i = 0; i < GameConstants::ARMY_SIZE; i++)
  821. {
  822. moveStackLeftButtons.push_back(
  823. std::make_shared<CButton>(
  824. Point(484 + 35 * i, 154),
  825. QUICK_EXCHANGE_MOD_PREFIX + "/unitLeft.DEF",
  826. CButton::tooltip(CGI->generaltexth->qeModCommands[1]),
  827. controller.onMoveStackToLeft(SlotID(i))));
  828. moveStackRightButtons.push_back(
  829. std::make_shared<CButton>(
  830. Point(66 + 35 * i, 154),
  831. QUICK_EXCHANGE_MOD_PREFIX + "/unitRight.DEF",
  832. CButton::tooltip(CGI->generaltexth->qeModCommands[1]),
  833. controller.onMoveStackToRight(SlotID(i))));
  834. }
  835. }
  836. updateWidgets();
  837. }
  838. const CGarrisonSlot * CExchangeWindow::getSelectedSlotID() const
  839. {
  840. return garr->getSelection();
  841. }
  842. void CExchangeWindow::updateGarrisons()
  843. {
  844. garr->recreateSlots();
  845. updateWidgets();
  846. }
  847. void CExchangeWindow::questlog(int whichHero)
  848. {
  849. CCS->curh->dragAndDropCursor(nullptr);
  850. LOCPLINT->showQuestLog();
  851. }
  852. void CExchangeWindow::updateWidgets()
  853. {
  854. for(size_t leftRight : {0, 1})
  855. {
  856. const CGHeroInstance * hero = heroInst.at(leftRight);
  857. for(int m=0; m<GameConstants::PRIMARY_SKILLS; ++m)
  858. {
  859. auto value = herosWArt[leftRight]->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(m));
  860. primSkillValues[leftRight][m]->setText(std::to_string(value));
  861. }
  862. for(int m=0; m < hero->secSkills.size(); ++m)
  863. {
  864. int id = hero->secSkills[m].first;
  865. int level = hero->secSkills[m].second;
  866. secSkillIcons[leftRight][m]->setFrame(2 + id * 3 + level);
  867. }
  868. expValues[leftRight]->setText(TextOperations::formatMetric(hero->exp, 3));
  869. manaValues[leftRight]->setText(TextOperations::formatMetric(hero->mana, 3));
  870. morale[leftRight]->set(hero);
  871. luck[leftRight]->set(hero);
  872. }
  873. }
  874. CShipyardWindow::CShipyardWindow(const TResources & cost, int state, BoatId boatType, const std::function<void()> & onBuy)
  875. : CStatusbarWindow(PLAYER_COLORED, "TPSHIP")
  876. {
  877. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  878. bgWater = std::make_shared<CPicture>("TPSHIPBK", 100, 69);
  879. std::string boatFilenames[3] = {"AB01_", "AB02_", "AB03_"};
  880. Point waterCenter = Point(bgWater->pos.x+bgWater->pos.w/2, bgWater->pos.y+bgWater->pos.h/2);
  881. bgShip = std::make_shared<CAnimImage>(boatFilenames[boatType.getNum()], 0, 7, 120, 96, 0);
  882. bgShip->center(waterCenter);
  883. // Create resource icons and costs.
  884. std::string goldValue = std::to_string(cost[EGameResID::GOLD]);
  885. std::string woodValue = std::to_string(cost[EGameResID::WOOD]);
  886. goldCost = std::make_shared<CLabel>(118, 294, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, goldValue);
  887. woodCost = std::make_shared<CLabel>(212, 294, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, woodValue);
  888. goldPic = std::make_shared<CAnimImage>("RESOURCE",GameResID(EGameResID::GOLD), 0, 100, 244);
  889. woodPic = std::make_shared<CAnimImage>("RESOURCE", GameResID(EGameResID::WOOD), 0, 196, 244);
  890. quit = std::make_shared<CButton>(Point(224, 312), "ICANCEL", CButton::tooltip(CGI->generaltexth->allTexts[599]), std::bind(&CShipyardWindow::close, this), EShortcut::GLOBAL_CANCEL);
  891. build = std::make_shared<CButton>(Point(42, 312), "IBUY30", CButton::tooltip(CGI->generaltexth->allTexts[598]), std::bind(&CShipyardWindow::close, this), EShortcut::GLOBAL_ACCEPT);
  892. build->addCallback(onBuy);
  893. for(GameResID i = EGameResID::WOOD; i <= EGameResID::GOLD; ++i)
  894. {
  895. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  896. {
  897. build->block(true);
  898. break;
  899. }
  900. }
  901. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  902. title = std::make_shared<CLabel>(164, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[13]);
  903. costLabel = std::make_shared<CLabel>(164, 220, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->jktexts[14]);
  904. }
  905. void CTransformerWindow::CItem::move()
  906. {
  907. if(left)
  908. moveBy(Point(289, 0));
  909. else
  910. moveBy(Point(-289, 0));
  911. left = !left;
  912. }
  913. void CTransformerWindow::CItem::clickLeft(tribool down, bool previousState)
  914. {
  915. if(previousState && (!down))
  916. {
  917. move();
  918. parent->redraw();
  919. }
  920. }
  921. void CTransformerWindow::CItem::update()
  922. {
  923. icon->setFrame(parent->army->getCreature(SlotID(id))->getId() + 2);
  924. }
  925. CTransformerWindow::CItem::CItem(CTransformerWindow * parent_, int size_, int id_)
  926. : CIntObject(LCLICK),
  927. id(id_),
  928. size(size_),
  929. parent(parent_)
  930. {
  931. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  932. left = true;
  933. pos.w = 58;
  934. pos.h = 64;
  935. pos.x += 45 + (id%3)*83 + id/6*83;
  936. pos.y += 109 + (id/3)*98;
  937. icon = std::make_shared<CAnimImage>("TWCRPORT", parent->army->getCreature(SlotID(id))->getId() + 2);
  938. count = std::make_shared<CLabel>(28, 76,FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(size));
  939. }
  940. void CTransformerWindow::makeDeal()
  941. {
  942. for(auto & elem : items)
  943. {
  944. if(!elem->left)
  945. LOCPLINT->cb->trade(market, EMarketMode::CREATURE_UNDEAD, elem->id, 0, 0, hero);
  946. }
  947. }
  948. void CTransformerWindow::addAll()
  949. {
  950. for(auto & elem : items)
  951. {
  952. if(elem->left)
  953. elem->move();
  954. }
  955. redraw();
  956. }
  957. void CTransformerWindow::updateGarrisons()
  958. {
  959. for(auto & item : items)
  960. item->update();
  961. }
  962. CTransformerWindow::CTransformerWindow(const IMarket * _market, const CGHeroInstance * _hero)
  963. : CStatusbarWindow(PLAYER_COLORED, "SKTRNBK"),
  964. hero(_hero),
  965. market(_market)
  966. {
  967. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  968. if(hero)
  969. army = hero;
  970. else
  971. army = dynamic_cast<const CArmedInstance *>(market); //for town only
  972. if(army)
  973. {
  974. for(int i = 0; i < GameConstants::ARMY_SIZE; i++)
  975. {
  976. if(army->getCreature(SlotID(i)))
  977. items.push_back(std::make_shared<CItem>(this, army->getStackCount(SlotID(i)), i));
  978. }
  979. }
  980. all = std::make_shared<CButton>(Point(146, 416), "ALTARMY.DEF", CGI->generaltexth->zelp[590], [&](){ addAll(); }, EShortcut::RECRUITMENT_UPGRADE_ALL);
  981. convert = std::make_shared<CButton>(Point(269, 416), "ALTSACR.DEF", CGI->generaltexth->zelp[591], [&](){ makeDeal(); }, EShortcut::GLOBAL_ACCEPT);
  982. cancel = std::make_shared<CButton>(Point(392, 416), "ICANCEL.DEF", CGI->generaltexth->zelp[592], [&](){ close(); },EShortcut::GLOBAL_CANCEL);
  983. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  984. titleLeft = std::make_shared<CLabel>(153, 29,FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[485]);//holding area
  985. titleRight = std::make_shared<CLabel>(153+295, 29, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[486]);//transformer
  986. helpLeft = std::make_shared<CTextBox>(CGI->generaltexth->allTexts[487], Rect(26, 56, 255, 40), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW);//move creatures to create skeletons
  987. helpRight = std::make_shared<CTextBox>(CGI->generaltexth->allTexts[488], Rect(320, 56, 255, 40), 0, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW);//creatures here will become skeletons
  988. }
  989. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y)
  990. : CIntObject(LCLICK | RCLICK | HOVER),
  991. ID(_ID),
  992. parent(_parent)
  993. {
  994. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  995. pos.x += X;
  996. pos.y += Y;
  997. topBar = std::make_shared<CAnimImage>(parent->bars, 0, 0, -28, -22);
  998. bottomBar = std::make_shared<CAnimImage>(parent->bars, 0, 0, -28, 48);
  999. icon = std::make_shared<CAnimImage>("SECSKILL", _ID * 3 + 3, 0);
  1000. name = std::make_shared<CLabel>(22, -13, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->skillh->getByIndex(ID)->getNameTranslated());
  1001. level = std::make_shared<CLabel>(22, 57, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->levels[0]);
  1002. pos.h = icon->pos.h;
  1003. pos.w = icon->pos.w;
  1004. }
  1005. void CUniversityWindow::CItem::clickLeft(tribool down, bool previousState)
  1006. {
  1007. if(previousState && (!down))
  1008. {
  1009. if(state() == 2)
  1010. GH.windows().createAndPushWindow<CUnivConfirmWindow>(parent, ID, LOCPLINT->cb->getResourceAmount(EGameResID::GOLD) >= 2000);
  1011. }
  1012. }
  1013. void CUniversityWindow::CItem::clickRight(tribool down, bool previousState)
  1014. {
  1015. if(down)
  1016. {
  1017. CRClickPopup::createAndPush(CGI->skillh->getByIndex(ID)->getDescriptionTranslated(1), std::make_shared<CComponent>(CComponent::secskill, ID, 1));
  1018. }
  1019. }
  1020. void CUniversityWindow::CItem::hover(bool on)
  1021. {
  1022. if(on)
  1023. GH.statusbar()->write(CGI->skillh->getByIndex(ID)->getNameTranslated());
  1024. else
  1025. GH.statusbar()->clear();
  1026. }
  1027. int CUniversityWindow::CItem::state()
  1028. {
  1029. if(parent->hero->getSecSkillLevel(SecondarySkill(ID)))//hero know this skill
  1030. return 1;
  1031. if(!parent->hero->canLearnSkill(SecondarySkill(ID)))//can't learn more skills
  1032. return 0;
  1033. return 2;
  1034. }
  1035. void CUniversityWindow::CItem::showAll(Canvas & to)
  1036. {
  1037. //TODO: update when state actually changes
  1038. auto stateIndex = state();
  1039. topBar->setFrame(stateIndex);
  1040. bottomBar->setFrame(stateIndex);
  1041. CIntObject::showAll(to);
  1042. }
  1043. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market)
  1044. : CStatusbarWindow(PLAYER_COLORED, "UNIVERS1"),
  1045. hero(_hero),
  1046. market(_market)
  1047. {
  1048. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1049. bars = std::make_shared<CAnimation>();
  1050. bars->setCustom("UNIVRED", 0, 0);
  1051. bars->setCustom("UNIVGOLD", 1, 0);
  1052. bars->setCustom("UNIVGREN", 2, 0);
  1053. bars->preload();
  1054. std::string titleStr = CGI->generaltexth->allTexts[602];
  1055. std::string speechStr = CGI->generaltexth->allTexts[603];
  1056. if(auto town = dynamic_cast<const CGTownInstance *>(_market))
  1057. {
  1058. auto faction = town->town->faction->getId();
  1059. auto bid = town->town->getSpecialBuilding(BuildingSubID::MAGIC_UNIVERSITY)->bid;
  1060. titlePic = std::make_shared<CAnimImage>((*CGI->townh)[faction]->town->clientInfo.buildingsIcons, bid);
  1061. }
  1062. else if(auto uni = dynamic_cast<const CGUniversity *>(_market); uni->appearance)
  1063. {
  1064. titlePic = std::make_shared<CAnimImage>(uni->appearance->animationFile, 0);
  1065. titleStr = uni->title;
  1066. speechStr = uni->speech;
  1067. }
  1068. else
  1069. {
  1070. titlePic = std::make_shared<CPicture>("UNIVBLDG");
  1071. }
  1072. titlePic->center(Point(232 + pos.x, 76 + pos.y));
  1073. clerkSpeech = std::make_shared<CTextBox>(speechStr, Rect(24, 129, 413, 70), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  1074. title = std::make_shared<CLabel>(231, 26, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, titleStr);
  1075. std::vector<int> goods = market->availableItemsIds(EMarketMode::RESOURCE_SKILL);
  1076. for(int i=0; i<goods.size(); i++)//prepare clickable items
  1077. items.push_back(std::make_shared<CItem>(this, goods[i], 54+i*104, 234));
  1078. cancel = std::make_shared<CButton>(Point(200, 313), "IOKAY.DEF", CGI->generaltexth->zelp[632], [&](){ close(); }, EShortcut::GLOBAL_ACCEPT);
  1079. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1080. }
  1081. void CUniversityWindow::makeDeal(int skill)
  1082. {
  1083. LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_SKILL, 6, skill, 1, hero);
  1084. }
  1085. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * owner_, int SKILL, bool available)
  1086. : CStatusbarWindow(PLAYER_COLORED, "UNIVERS2.PCX"),
  1087. owner(owner_)
  1088. {
  1089. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1090. std::string text = CGI->generaltexth->allTexts[608];
  1091. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  1092. boost::replace_first(text, "%s", CGI->skillh->getByIndex(SKILL)->getNameTranslated());
  1093. boost::replace_first(text, "%d", "2000");
  1094. clerkSpeech = std::make_shared<CTextBox>(text, Rect(24, 129, 413, 70), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  1095. name = std::make_shared<CLabel>(230, 37, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->skillh->getByIndex(SKILL)->getNameTranslated());
  1096. icon = std::make_shared<CAnimImage>("SECSKILL", SKILL*3+3, 0, 211, 51);
  1097. level = std::make_shared<CLabel>(230, 107, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->levels[1]);
  1098. costIcon = std::make_shared<CAnimImage>("RESOURCE", GameResID(EGameResID::GOLD), 0, 210, 210);
  1099. cost = std::make_shared<CLabel>(230, 267, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, "2000");
  1100. std::string hoverText = CGI->generaltexth->allTexts[609];
  1101. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->skillh->getByIndex(SKILL)->getNameTranslated());
  1102. text = CGI->generaltexth->zelp[633].second;
  1103. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  1104. boost::replace_first(text, "%s", CGI->skillh->getByIndex(SKILL)->getNameTranslated());
  1105. boost::replace_first(text, "%d", "2000");
  1106. confirm = std::make_shared<CButton>(Point(148, 299), "IBY6432.DEF", CButton::tooltip(hoverText, text), [=](){makeDeal(SKILL);}, EShortcut::GLOBAL_ACCEPT);
  1107. confirm->block(!available);
  1108. cancel = std::make_shared<CButton>(Point(252,299), "ICANCEL.DEF", CGI->generaltexth->zelp[631], [&](){ close(); }, EShortcut::GLOBAL_CANCEL);
  1109. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1110. }
  1111. void CUnivConfirmWindow::makeDeal(int skill)
  1112. {
  1113. owner->makeDeal(skill);
  1114. close();
  1115. }
  1116. CGarrisonWindow::CGarrisonWindow(const CArmedInstance * up, const CGHeroInstance * down, bool removableUnits)
  1117. : CWindowObject(PLAYER_COLORED, "GARRISON")
  1118. {
  1119. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1120. garr = std::make_shared<CGarrisonInt>(92, 127, 4, Point(0,96), up, down, removableUnits);
  1121. {
  1122. auto split = std::make_shared<CButton>(Point(88, 314), "IDV6432.DEF", CButton::tooltip(CGI->generaltexth->tcommands[3], ""), [&](){ garr->splitClick(); } );
  1123. garr->addSplitBtn(split);
  1124. }
  1125. quit = std::make_shared<CButton>(Point(399, 314), "IOK6432.DEF", CButton::tooltip(CGI->generaltexth->tcommands[8], ""), [&](){ close(); }, EShortcut::GLOBAL_ACCEPT);
  1126. std::string titleText;
  1127. if(down->tempOwner == up->tempOwner)
  1128. {
  1129. titleText = CGI->generaltexth->allTexts[709];
  1130. }
  1131. else
  1132. {
  1133. //assume that this is joining monsters dialog
  1134. if(up->Slots().size() > 0)
  1135. {
  1136. titleText = CGI->generaltexth->allTexts[35];
  1137. boost::algorithm::replace_first(titleText, "%s", up->Slots().begin()->second->type->getNamePluralTranslated());
  1138. }
  1139. else
  1140. {
  1141. logGlobal->error("Invalid armed instance for garrison window.");
  1142. }
  1143. }
  1144. title = std::make_shared<CLabel>(275, 30, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, titleText);
  1145. banner = std::make_shared<CAnimImage>("CREST58", up->getOwner().getNum(), 0, 28, 124);
  1146. portrait = std::make_shared<CAnimImage>("PortraitsLarge", down->portrait, 0, 29, 222);
  1147. }
  1148. void CGarrisonWindow::updateGarrisons()
  1149. {
  1150. garr->recreateSlots();
  1151. }
  1152. CHillFortWindow::CHillFortWindow(const CGHeroInstance * visitor, const CGObjectInstance * object)
  1153. : CStatusbarWindow(PLAYER_COLORED, "APHLFTBK"),
  1154. fort(object),
  1155. hero(visitor)
  1156. {
  1157. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1158. title = std::make_shared<CLabel>(325, 32, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, fort->getObjectName());
  1159. heroPic = std::make_shared<CHeroArea>(30, 60, hero);
  1160. for(int i=0; i < resCount; i++)
  1161. {
  1162. totalIcons[i] = std::make_shared<CAnimImage>("SMALRES", i, 0, 104 + 76 * i, 237);
  1163. totalLabels[i] = std::make_shared<CLabel>(166 + 76 * i, 253, FONT_SMALL, ETextAlignment::BOTTOMRIGHT);
  1164. }
  1165. for(int i = 0; i < slotsCount; i++)
  1166. {
  1167. upgrade[i] = std::make_shared<CButton>(Point(107 + i * 76, 171), "", CButton::tooltip(getTextForSlot(SlotID(i))), [=](){ makeDeal(SlotID(i)); }, vstd::next(EShortcut::SELECT_INDEX_1, i));
  1168. for(auto image : { "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF" })
  1169. upgrade[i]->addImage(image);
  1170. for(int j : {0,1})
  1171. {
  1172. slotIcons[i][j] = std::make_shared<CAnimImage>("SMALRES", 0, 0, 104 + 76 * i, 128 + 20 * j);
  1173. slotLabels[i][j] = std::make_shared<CLabel>(168 + 76 * i, 144 + 20 * j, FONT_SMALL, ETextAlignment::BOTTOMRIGHT);
  1174. }
  1175. }
  1176. upgradeAll = std::make_shared<CButton>(Point(30, 231), "", CButton::tooltip(CGI->generaltexth->allTexts[432]), [&](){ makeDeal(SlotID(slotsCount));}, EShortcut::RECRUITMENT_UPGRADE_ALL);
  1177. for(auto image : { "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF" })
  1178. upgradeAll->addImage(image);
  1179. quit = std::make_shared<CButton>(Point(294, 275), "IOKAY.DEF", CButton::tooltip(), std::bind(&CHillFortWindow::close, this), EShortcut::GLOBAL_ACCEPT);
  1180. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1181. garr = std::make_shared<CGarrisonInt>(108, 60, 18, Point(), hero, nullptr);
  1182. updateGarrisons();
  1183. }
  1184. void CHillFortWindow::updateGarrisons()
  1185. {
  1186. std::array<TResources, slotsCount> costs;// costs [slot ID] [resource ID] = resource count for upgrade
  1187. TResources totalSum; // totalSum[resource ID] = value
  1188. for(int i=0; i<slotsCount; i++)
  1189. {
  1190. std::fill(costs[i].begin(), costs[i].end(), 0);
  1191. int newState = getState(SlotID(i));
  1192. if(newState != -1)
  1193. {
  1194. UpgradeInfo info;
  1195. LOCPLINT->cb->fillUpgradeInfo(hero, SlotID(i), info);
  1196. if(info.newID.size())//we have upgrades here - update costs
  1197. {
  1198. costs[i] = info.cost[0] * hero->getStackCount(SlotID(i));
  1199. totalSum += costs[i];
  1200. }
  1201. }
  1202. currState[i] = newState;
  1203. upgrade[i]->setIndex(currState[i] == -1 ? 0 : currState[i]);
  1204. upgrade[i]->block(currState[i] == -1);
  1205. upgrade[i]->addHoverText(CButton::NORMAL, getTextForSlot(SlotID(i)));
  1206. }
  1207. //"Upgrade all" slot
  1208. int newState = 2;
  1209. {
  1210. TResources myRes = LOCPLINT->cb->getResourceAmount();
  1211. bool allUpgraded = true;//All creatures are upgraded?
  1212. for(int i=0; i<slotsCount; i++)
  1213. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  1214. if(allUpgraded)
  1215. newState = 1;
  1216. if(!totalSum.canBeAfforded(myRes))
  1217. newState = 0;
  1218. }
  1219. currState[slotsCount] = newState;
  1220. upgradeAll->setIndex(newState);
  1221. garr->recreateSlots();
  1222. for(int i = 0; i < slotsCount; i++)
  1223. {
  1224. //hide all first
  1225. for(int j : {0,1})
  1226. {
  1227. slotIcons[i][j]->visible = false;
  1228. slotLabels[i][j]->setText("");
  1229. }
  1230. //if can upgrade or can not afford, draw cost
  1231. if(currState[i] == 0 || currState[i] == 2)
  1232. {
  1233. if(costs[i].nonZero())
  1234. {
  1235. //reverse iterator is used to display gold as first element
  1236. int j = 0;
  1237. for(int res = (int)costs[i].size()-1; (res >= 0) && (j < 2); res--)
  1238. {
  1239. int val = costs[i][res];
  1240. if(!val)
  1241. continue;
  1242. slotIcons[i][j]->visible = true;
  1243. slotIcons[i][j]->setFrame(res);
  1244. slotLabels[i][j]->setText(std::to_string(val));
  1245. j++;
  1246. }
  1247. }
  1248. else//free upgrade - print gold image and "Free" text
  1249. {
  1250. slotIcons[i][0]->visible = true;
  1251. slotIcons[i][0]->setFrame(GameResID(EGameResID::GOLD));
  1252. slotLabels[i][0]->setText(CGI->generaltexth->allTexts[344]);
  1253. }
  1254. }
  1255. }
  1256. for(int i = 0; i < resCount; i++)
  1257. {
  1258. if(totalSum[i] == 0)
  1259. {
  1260. totalIcons[i]->visible = false;
  1261. totalLabels[i]->setText("");
  1262. }
  1263. else
  1264. {
  1265. totalIcons[i]->visible = true;
  1266. totalLabels[i]->setText(std::to_string(totalSum[i]));
  1267. }
  1268. }
  1269. }
  1270. void CHillFortWindow::makeDeal(SlotID slot)
  1271. {
  1272. assert(slot.getNum()>=0);
  1273. int offset = (slot.getNum() == slotsCount)?2:0;
  1274. switch(currState[slot.getNum()])
  1275. {
  1276. case 0:
  1277. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset], std::vector<std::shared_ptr<CComponent>>(), soundBase::sound_todo);
  1278. break;
  1279. case 1:
  1280. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset], std::vector<std::shared_ptr<CComponent>>(), soundBase::sound_todo);
  1281. break;
  1282. case 2:
  1283. for(int i=0; i<slotsCount; i++)
  1284. {
  1285. if(slot.getNum() ==i || ( slot.getNum() == slotsCount && currState[i] == 2 ))//this is activated slot or "upgrade all"
  1286. {
  1287. UpgradeInfo info;
  1288. LOCPLINT->cb->fillUpgradeInfo(hero, SlotID(i), info);
  1289. LOCPLINT->cb->upgradeCreature(hero, SlotID(i), info.newID[0]);
  1290. }
  1291. }
  1292. break;
  1293. }
  1294. }
  1295. std::string CHillFortWindow::getTextForSlot(SlotID slot)
  1296. {
  1297. if(!hero->getCreature(slot))//we don`t have creature here
  1298. return "";
  1299. std::string str = CGI->generaltexth->allTexts[318];
  1300. int amount = hero->getStackCount(slot);
  1301. if(amount == 1)
  1302. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->getNameSingularTranslated());
  1303. else
  1304. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->getNamePluralTranslated());
  1305. return str;
  1306. }
  1307. int CHillFortWindow::getState(SlotID slot)
  1308. {
  1309. TResources myRes = LOCPLINT->cb->getResourceAmount();
  1310. if(hero->slotEmpty(slot))//no creature here
  1311. return -1;
  1312. UpgradeInfo info;
  1313. LOCPLINT->cb->fillUpgradeInfo(hero, slot, info);
  1314. if(!info.newID.size())//already upgraded
  1315. return 1;
  1316. if(!(info.cost[0] * hero->getStackCount(slot)).canBeAfforded(myRes))
  1317. return 0;
  1318. return 2;//can upgrade
  1319. }
  1320. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner):
  1321. CStatusbarWindow(PLAYER_COLORED | BORDERED, "TpRank"),
  1322. owner(_owner)
  1323. {
  1324. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1325. SThievesGuildInfo tgi; //info to be displayed
  1326. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  1327. exitb = std::make_shared<CButton>(Point(748, 556), "TPMAGE1", CButton::tooltip(CGI->generaltexth->allTexts[600]), [&](){ close();}, EShortcut::GLOBAL_RETURN);
  1328. statusbar = CGStatusBar::create(3, 555, "TStatBar.bmp", 742);
  1329. resdatabar = std::make_shared<CMinorResDataBar>();
  1330. resdatabar->moveBy(pos.topLeft(), true);
  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. 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. rowHeaders.push_back(std::make_shared<CLabel>(135, y, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, text));
  1348. }
  1349. auto PRSTRIPS = std::make_shared<CAnimation>("PRSTRIPS");
  1350. PRSTRIPS->preload();
  1351. for(int g=1; g<tgi.playerColors.size(); ++g)
  1352. columnBackgrounds.push_back(std::make_shared<CAnimImage>(PRSTRIPS, g-1, 0, 250 + 66*g, 7));
  1353. for(int g=0; g<tgi.playerColors.size(); ++g)
  1354. columnHeaders.push_back(std::make_shared<CLabel>(283 + 66*g, 24, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[16+g]));
  1355. auto itgflags = std::make_shared<CAnimation>("itgflags");
  1356. itgflags->preload();
  1357. //printing flags
  1358. for(int g = 0; g < std::size(fields); ++g) //by lines
  1359. {
  1360. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  1361. {
  1362. std::vector<PlayerColor> &players = (tgi .* fields[g])[b]; //get players with this place in this line
  1363. //position of box
  1364. int xpos = 259 + 66 * b;
  1365. int ypos = 41 + 32 * g;
  1366. size_t rowLength[2]; //size of each row
  1367. rowLength[0] = std::min<size_t>(players.size(), 4);
  1368. rowLength[1] = players.size() - rowLength[0];
  1369. for(size_t j=0; j < 2; j++)
  1370. {
  1371. // origin of this row | offset for 2nd row| shift right for short rows
  1372. //if we have 2 rows, start either from mid or beginning (depending on count), otherwise center the flags
  1373. int rowStartX = xpos + (j ? 6 + ((int)rowLength[j] < 3 ? 12 : 0) : 24 - 6 * (int)rowLength[j]);
  1374. int rowStartY = ypos + (j ? 4 : 0);
  1375. for(size_t i=0; i < rowLength[j]; i++)
  1376. cells.push_back(std::make_shared<CAnimImage>(itgflags, players[i + j*4].getNum(), 0, rowStartX + (int)i*12, rowStartY));
  1377. }
  1378. }
  1379. }
  1380. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  1381. //printing best hero
  1382. int counter = 0;
  1383. for(auto & iter : tgi.colorToBestHero)
  1384. {
  1385. banners.push_back(std::make_shared<CPicture>(colorToBox[iter.first.getNum()], 253 + 66 * counter, 334));
  1386. if(iter.second.portrait >= 0)
  1387. {
  1388. bestHeroes.push_back(std::make_shared<CAnimImage>("PortraitsSmall", iter.second.portrait, 0, 260 + 66 * counter, 360));
  1389. //TODO: r-click info:
  1390. // - r-click on hero
  1391. // - r-click on primary skill label
  1392. if(iter.second.details)
  1393. {
  1394. primSkillHeaders.push_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[184], Rect(260 + 66*counter, 396, 52, 64),
  1395. 0, FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE));
  1396. for(int i=0; i<iter.second.details->primskills.size(); ++i)
  1397. {
  1398. primSkillValues.push_back(std::make_shared<CLabel>(310 + 66 * counter, 407 + 11*i, FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE,
  1399. std::to_string(iter.second.details->primskills[i])));
  1400. }
  1401. }
  1402. }
  1403. counter++;
  1404. }
  1405. //printing best creature
  1406. counter = 0;
  1407. for(auto & it : tgi.bestCreature)
  1408. {
  1409. if(it.second >= 0)
  1410. bestCreatures.push_back(std::make_shared<CAnimImage>("TWCRPORT", it.second+2, 0, 255 + 66 * counter, 479));
  1411. counter++;
  1412. }
  1413. //printing personality
  1414. counter = 0;
  1415. for(auto & it : tgi.personality)
  1416. {
  1417. std::string text;
  1418. if(it.second == EAiTactic::NONE)
  1419. {
  1420. text = CGI->generaltexth->arraytxt[172];
  1421. }
  1422. else if(it.second != EAiTactic::RANDOM)
  1423. {
  1424. text = CGI->generaltexth->arraytxt[168 + it.second];
  1425. }
  1426. personalities.push_back(std::make_shared<CLabel>(283 + 66*counter, 459, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, text));
  1427. counter++;
  1428. }
  1429. }
  1430. CObjectListWindow::CItem::CItem(CObjectListWindow * _parent, size_t _id, std::string _text)
  1431. : CIntObject(LCLICK | DOUBLECLICK),
  1432. parent(_parent),
  1433. index(_id)
  1434. {
  1435. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1436. border = std::make_shared<CPicture>("TPGATES");
  1437. pos = border->pos;
  1438. type |= REDRAW_PARENT;
  1439. text = std::make_shared<CLabel>(pos.w/2, pos.h/2, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, _text);
  1440. select(index == parent->selected);
  1441. }
  1442. void CObjectListWindow::CItem::select(bool on)
  1443. {
  1444. ui8 mask = UPDATE | SHOWALL;
  1445. if(on)
  1446. border->recActions |= mask;
  1447. else
  1448. border->recActions &= ~mask;
  1449. redraw();//???
  1450. }
  1451. void CObjectListWindow::CItem::clickLeft(tribool down, bool previousState)
  1452. {
  1453. if( previousState && !down)
  1454. parent->changeSelection(index);
  1455. }
  1456. void CObjectListWindow::CItem::clickDouble()
  1457. {
  1458. parent->elementSelected();
  1459. }
  1460. CObjectListWindow::CObjectListWindow(const std::vector<int> & _items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection)
  1461. : CWindowObject(PLAYER_COLORED, "TPGATE"),
  1462. onSelect(Callback),
  1463. selected(initialSelection)
  1464. {
  1465. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1466. items.reserve(_items.size());
  1467. for(int id : _items)
  1468. {
  1469. items.push_back(std::make_pair(id, LOCPLINT->cb->getObjInstance(ObjectInstanceID(id))->getObjectName()));
  1470. }
  1471. init(titleWidget_, _title, _descr);
  1472. }
  1473. CObjectListWindow::CObjectListWindow(const std::vector<std::string> & _items, std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr, std::function<void(int)> Callback, size_t initialSelection)
  1474. : CWindowObject(PLAYER_COLORED, "TPGATE"),
  1475. onSelect(Callback),
  1476. selected(initialSelection)
  1477. {
  1478. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1479. items.reserve(_items.size());
  1480. for(size_t i=0; i<_items.size(); i++)
  1481. items.push_back(std::make_pair(int(i), _items[i]));
  1482. init(titleWidget_, _title, _descr);
  1483. }
  1484. void CObjectListWindow::init(std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr)
  1485. {
  1486. titleWidget = titleWidget_;
  1487. title = std::make_shared<CLabel>(152, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, _title);
  1488. descr = std::make_shared<CLabel>(145, 133, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, _descr);
  1489. exit = std::make_shared<CButton>( Point(228, 402), "ICANCEL.DEF", CButton::tooltip(), std::bind(&CObjectListWindow::exitPressed, this), EShortcut::GLOBAL_CANCEL);
  1490. if(titleWidget)
  1491. {
  1492. addChild(titleWidget.get());
  1493. titleWidget->recActions = 255-DISPOSE;
  1494. titleWidget->pos.x = pos.w/2 + pos.x - titleWidget->pos.w/2;
  1495. titleWidget->pos.y =75 + pos.y - titleWidget->pos.h/2;
  1496. }
  1497. list = std::make_shared<CListBox>(std::bind(&CObjectListWindow::genItem, this, _1),
  1498. Point(14, 151), Point(0, 25), 9, items.size(), 0, 1, Rect(262, -32, 256, 256) );
  1499. list->type |= REDRAW_PARENT;
  1500. ok = std::make_shared<CButton>(Point(15, 402), "IOKAY.DEF", CButton::tooltip(), std::bind(&CObjectListWindow::elementSelected, this), EShortcut::GLOBAL_ACCEPT);
  1501. ok->block(!list->size());
  1502. }
  1503. std::shared_ptr<CIntObject> CObjectListWindow::genItem(size_t index)
  1504. {
  1505. if(index < items.size())
  1506. return std::make_shared<CItem>(this, index, items[index].second);
  1507. return std::shared_ptr<CIntObject>();
  1508. }
  1509. void CObjectListWindow::elementSelected()
  1510. {
  1511. std::function<void(int)> toCall = onSelect;//save
  1512. int where = items[selected].first; //required variables
  1513. close();//then destroy window
  1514. toCall(where);//and send selected object
  1515. }
  1516. void CObjectListWindow::exitPressed()
  1517. {
  1518. std::function<void()> toCall = onExit;//save
  1519. close();//then destroy window
  1520. if(toCall)
  1521. toCall();
  1522. }
  1523. void CObjectListWindow::changeSelection(size_t which)
  1524. {
  1525. ok->block(false);
  1526. if(selected == which)
  1527. return;
  1528. for(std::shared_ptr<CIntObject> element : list->getItems())
  1529. {
  1530. CItem * item = dynamic_cast<CItem*>(element.get());
  1531. if(item)
  1532. {
  1533. if(item->index == selected)
  1534. item->select(false);
  1535. if(item->index == which)
  1536. item->select(true);
  1537. }
  1538. }
  1539. selected = which;
  1540. }
  1541. void CObjectListWindow::keyPressed (EShortcut key)
  1542. {
  1543. int sel = static_cast<int>(selected);
  1544. switch(key)
  1545. {
  1546. break; case EShortcut::MOVE_UP:
  1547. sel -=1;
  1548. break; case EShortcut::MOVE_DOWN:
  1549. sel +=1;
  1550. break; case EShortcut::MOVE_PAGE_UP:
  1551. sel -=9;
  1552. break; case EShortcut::MOVE_PAGE_DOWN:
  1553. sel +=9;
  1554. break; case EShortcut::MOVE_FIRST:
  1555. sel = 0;
  1556. break; case EShortcut::MOVE_LAST:
  1557. sel = static_cast<int>(items.size());
  1558. break; default:
  1559. return;
  1560. }
  1561. vstd::abetween<int>(sel, 0, items.size()-1);
  1562. list->scrollTo(sel);
  1563. changeSelection(sel);
  1564. }