GUIClasses.cpp 63 KB

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