GUIClasses.cpp 66 KB

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