GUIClasses.cpp 67 KB

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