GUIClasses.cpp 64 KB

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