GUIClasses.cpp 67 KB

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