GUIClasses.cpp 65 KB

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