GUIClasses.cpp 64 KB

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