GUIClasses.cpp 66 KB

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