GUIClasses.cpp 64 KB

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