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