GUIClasses.cpp 54 KB

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