GUIClasses.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  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 "../CPlayerInterface.h"
  20. #include "../gui/CGuiHandler.h"
  21. #include "../gui/CursorHandler.h"
  22. #include "../gui/Shortcut.h"
  23. #include "../gui/WindowHandler.h"
  24. #include "../widgets/CComponent.h"
  25. #include "../widgets/CGarrisonInt.h"
  26. #include "../widgets/CreatureCostBox.h"
  27. #include "../widgets/CTextInput.h"
  28. #include "../widgets/Buttons.h"
  29. #include "../widgets/Slider.h"
  30. #include "../widgets/TextControls.h"
  31. #include "../widgets/ObjectLists.h"
  32. #include "../widgets/VideoWidget.h"
  33. #include "../render/Canvas.h"
  34. #include "../render/IRenderHandler.h"
  35. #include "../render/IImage.h"
  36. #include "../../CCallback.h"
  37. #include "../lib/mapObjectConstructors/CObjectClassesHandler.h"
  38. #include "../lib/mapObjectConstructors/CommonConstructors.h"
  39. #include "../lib/mapObjects/CGHeroInstance.h"
  40. #include "../lib/mapObjects/CGMarket.h"
  41. #include "../lib/mapObjects/CGTownInstance.h"
  42. #include "../lib/gameState/CGameState.h"
  43. #include "../lib/gameState/SThievesGuildInfo.h"
  44. #include "../lib/gameState/TavernHeroesPool.h"
  45. #include "../lib/texts/CGeneralTextHandler.h"
  46. #include "../lib/CHeroHandler.h"
  47. #include "../lib/GameSettings.h"
  48. #include "ConditionalWait.h"
  49. #include "../lib/CRandomGenerator.h"
  50. #include "../lib/CSkillHandler.h"
  51. #include "../lib/CSoundBase.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->mergeableStacks(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, creature.toCreature(), 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->setBusy();
  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. void CLevelWindow::close()
  346. {
  347. //FIXME: call callback if there was nothing to select?
  348. if (box && box->selectedIndex() != -1)
  349. cb(box->selectedIndex());
  350. LOCPLINT->showingDialog->setFree();
  351. CWindowObject::close();
  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. MetaString message;
  392. message.appendTextID("core.tvrninfo.1");
  393. message.replaceNumber(LOCPLINT->cb->howManyHeroes(true));
  394. //Cannot recruit. You already have %d Heroes.
  395. recruit->addHoverText(EButtonState::NORMAL, message.toString());
  396. recruit->block(true);
  397. }
  398. else if(LOCPLINT->cb->howManyHeroes(false) >= CGI->settings()->getInteger(EGameSettings::HEROES_PER_PLAYER_ON_MAP_CAP))
  399. {
  400. MetaString message;
  401. message.appendTextID("core.tvrninfo.1");
  402. message.replaceNumber(LOCPLINT->cb->howManyHeroes(false));
  403. recruit->addHoverText(EButtonState::NORMAL, message.toString());
  404. recruit->block(true);
  405. }
  406. else if(dynamic_cast<const CGTownInstance *>(TavernObj) && dynamic_cast<const CGTownInstance *>(TavernObj)->visitingHero)
  407. {
  408. recruit->addHoverText(EButtonState::NORMAL, CGI->generaltexth->tavernInfo[2]); //Cannot recruit. You already have a Hero in this town.
  409. recruit->block(true);
  410. }
  411. else
  412. {
  413. if(selected == -1)
  414. recruit->block(true);
  415. }
  416. if(LOCPLINT->castleInt)
  417. videoPlayer = std::make_shared<VideoWidget>(Point(70, 56), LOCPLINT->castleInt->town->town->clientInfo.tavernVideo, false);
  418. else if(const auto * townObj = dynamic_cast<const CGTownInstance *>(TavernObj))
  419. videoPlayer = std::make_shared<VideoWidget>(Point(70, 56), townObj->town->clientInfo.tavernVideo, false);
  420. else
  421. videoPlayer = std::make_shared<VideoWidget>(Point(70, 56), VideoPath::builtin("TAVERN.BIK"), false);
  422. addInvite();
  423. }
  424. void CTavernWindow::addInvite()
  425. {
  426. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  427. if(!VLC->settings()->getBoolean(EGameSettings::HEROES_TAVERN_INVITE))
  428. return;
  429. const auto & heroesPool = CSH->client->gameState()->heroesPool;
  430. for(auto & elem : heroesPool->unusedHeroesFromPool())
  431. {
  432. bool heroAvailable = heroesPool->isHeroAvailableFor(elem.first, tavernObj->getOwner());
  433. if(heroAvailable)
  434. inviteableHeroes[elem.first] = elem.second;
  435. }
  436. if(!inviteableHeroes.empty())
  437. {
  438. if(!heroToInvite)
  439. heroToInvite = (*RandomGeneratorUtil::nextItem(inviteableHeroes, CRandomGenerator::getDefault())).second;
  440. inviteHero = std::make_shared<CLabel>(170, 444, EFonts::FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("vcmi.tavernWindow.inviteHero"));
  441. inviteHeroImage = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsSmall"), (*CGI->heroh)[heroToInvite->getHeroType()]->imageIndex, 0, 245, 428);
  442. 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)); });
  443. }
  444. }
  445. void CTavernWindow::recruitb()
  446. {
  447. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  448. const CGObjectInstance *obj = tavernObj;
  449. LOCPLINT->cb->recruitHero(obj, toBuy, heroToInvite ? heroToInvite->getHeroType() : HeroTypeID::NONE);
  450. close();
  451. }
  452. void CTavernWindow::thievesguildb()
  453. {
  454. GH.windows().createAndPushWindow<CThievesGuildWindow>(tavernObj);
  455. }
  456. void CTavernWindow::close()
  457. {
  458. if (onWindowClosed)
  459. onWindowClosed();
  460. CStatusbarWindow::close();
  461. }
  462. void CTavernWindow::show(Canvas & to)
  463. {
  464. CWindowObject::show(to);
  465. if(selected >= 0)
  466. {
  467. auto sel = selected ? h2 : h1;
  468. if(selected != oldSelected)
  469. {
  470. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  471. oldSelected = selected;
  472. heroDescription->setText(sel->description);
  473. //Recruit %s the %s
  474. if (!recruit->isBlocked())
  475. recruit->addHoverText(EButtonState::NORMAL, boost::str(boost::format(CGI->generaltexth->tavernInfo[3]) % sel->h->getNameTranslated() % sel->h->getClassNameTranslated()));
  476. }
  477. to.drawBorder(Rect::createAround(sel->pos, 2), Colors::BRIGHT_YELLOW, 2);
  478. }
  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(
  537. pos.x,
  538. pos.y,
  539. ELEM_PER_LINES * 48,
  540. std::min((int)(inviteableHeroes.size() / ELEM_PER_LINES + (inviteableHeroes.size() % ELEM_PER_LINES != 0)), MAX_LINES) * 32
  541. );
  542. background = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, pos.w, pos.h));
  543. if(inviteableHeroes.size() / ELEM_PER_LINES > MAX_LINES)
  544. {
  545. pos.w += 16;
  546. slider = std::make_shared<CSlider>(Point(pos.w - 16, 0), pos.h, std::bind(&CTavernWindow::HeroSelector::sliderMove, this, _1), MAX_LINES, std::ceil((double)inviteableHeroes.size() / ELEM_PER_LINES), 0, Orientation::VERTICAL, CSlider::BROWN);
  547. slider->setPanningStep(32);
  548. slider->setScrollBounds(Rect(-pos.w + slider->pos.w, 0, pos.w, pos.h));
  549. }
  550. recreate();
  551. center();
  552. }
  553. void CTavernWindow::HeroSelector::sliderMove(int slidPos)
  554. {
  555. if(!slider)
  556. return; // ignore spurious call when slider is being created
  557. recreate();
  558. redraw();
  559. }
  560. void CTavernWindow::HeroSelector::recreate()
  561. {
  562. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  563. int sliderLine = slider ? slider->getValue() : 0;
  564. int x = 0;
  565. int y = -sliderLine;
  566. portraits.clear();
  567. portraitAreas.clear();
  568. for(auto & h : inviteableHeroes)
  569. {
  570. if(y >= 0 && y <= MAX_LINES - 1)
  571. {
  572. portraits.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsSmall"), (*CGI->heroh)[h.first]->imageIndex, 0, x * 48, y * 32));
  573. 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])); }));
  574. }
  575. if(x > 0 && x % 15 == 0)
  576. {
  577. x = 0;
  578. y++;
  579. }
  580. else
  581. x++;
  582. }
  583. }
  584. CShipyardWindow::CShipyardWindow(const TResources & cost, int state, BoatId boatType, const std::function<void()> & onBuy)
  585. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPSHIP"))
  586. {
  587. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  588. bgWater = std::make_shared<CPicture>(ImagePath::builtin("TPSHIPBK"), 100, 69);
  589. auto handler = CGI->objtypeh->getHandlerFor(Obj::BOAT, boatType);
  590. auto boatConstructor = std::dynamic_pointer_cast<const BoatInstanceConstructor>(handler);
  591. assert(boatConstructor);
  592. if (boatConstructor)
  593. {
  594. AnimationPath boatFilename = boatConstructor->getBoatAnimationName();
  595. Point waterCenter = Point(bgWater->pos.x+bgWater->pos.w/2, bgWater->pos.y+bgWater->pos.h/2);
  596. bgShip = std::make_shared<CAnimImage>(boatFilename, 0, 7, 120, 96, 0);
  597. bgShip->center(waterCenter);
  598. }
  599. // Create resource icons and costs.
  600. std::string goldValue = std::to_string(cost[EGameResID::GOLD]);
  601. std::string woodValue = std::to_string(cost[EGameResID::WOOD]);
  602. goldCost = std::make_shared<CLabel>(118, 294, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, goldValue);
  603. woodCost = std::make_shared<CLabel>(212, 294, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, woodValue);
  604. goldPic = std::make_shared<CAnimImage>(AnimationPath::builtin("RESOURCE"), GameResID(EGameResID::GOLD), 0, 100, 244);
  605. woodPic = std::make_shared<CAnimImage>(AnimationPath::builtin("RESOURCE"), GameResID(EGameResID::WOOD), 0, 196, 244);
  606. 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);
  607. 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);
  608. build->addCallback(onBuy);
  609. for(GameResID i = EGameResID::WOOD; i <= EGameResID::GOLD; ++i)
  610. {
  611. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  612. {
  613. build->block(true);
  614. break;
  615. }
  616. }
  617. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  618. title = std::make_shared<CLabel>(164, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[13]);
  619. costLabel = std::make_shared<CLabel>(164, 220, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->jktexts[14]);
  620. }
  621. void CTransformerWindow::CItem::move()
  622. {
  623. if(left)
  624. moveBy(Point(289, 0));
  625. else
  626. moveBy(Point(-289, 0));
  627. left = !left;
  628. }
  629. void CTransformerWindow::CItem::clickPressed(const Point & cursorPosition)
  630. {
  631. move();
  632. parent->redraw();
  633. }
  634. void CTransformerWindow::CItem::update()
  635. {
  636. icon->setFrame(parent->army->getCreature(SlotID(id))->getId() + 2);
  637. }
  638. CTransformerWindow::CItem::CItem(CTransformerWindow * parent_, int size_, int id_)
  639. : CIntObject(LCLICK),
  640. id(id_),
  641. size(size_),
  642. parent(parent_)
  643. {
  644. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  645. left = true;
  646. pos.w = 58;
  647. pos.h = 64;
  648. pos.x += 45 + (id%3)*83 + id/6*83;
  649. pos.y += 109 + (id/3)*98;
  650. icon = std::make_shared<CAnimImage>(AnimationPath::builtin("TWCRPORT"), parent->army->getCreature(SlotID(id))->getId() + 2);
  651. count = std::make_shared<CLabel>(28, 76,FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(size));
  652. }
  653. void CTransformerWindow::makeDeal()
  654. {
  655. for(auto & elem : items)
  656. {
  657. if(!elem->left)
  658. LOCPLINT->cb->trade(market, EMarketMode::CREATURE_UNDEAD, SlotID(elem->id), {}, {}, hero);
  659. }
  660. }
  661. void CTransformerWindow::addAll()
  662. {
  663. for(auto & elem : items)
  664. {
  665. if(elem->left)
  666. elem->move();
  667. }
  668. redraw();
  669. }
  670. void CTransformerWindow::updateGarrisons()
  671. {
  672. for(auto & item : items)
  673. item->update();
  674. }
  675. bool CTransformerWindow::holdsGarrison(const CArmedInstance * army)
  676. {
  677. return army == hero;
  678. }
  679. CTransformerWindow::CTransformerWindow(const IMarket * _market, const CGHeroInstance * _hero, const std::function<void()> & onWindowClosed)
  680. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("SKTRNBK")),
  681. hero(_hero),
  682. onWindowClosed(onWindowClosed),
  683. market(_market)
  684. {
  685. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  686. if(hero)
  687. army = hero;
  688. else
  689. army = dynamic_cast<const CArmedInstance *>(market); //for town only
  690. if(army)
  691. {
  692. for(int i = 0; i < GameConstants::ARMY_SIZE; i++)
  693. {
  694. if(army->getCreature(SlotID(i)))
  695. items.push_back(std::make_shared<CItem>(this, army->getStackCount(SlotID(i)), i));
  696. }
  697. }
  698. all = std::make_shared<CButton>(Point(146, 416), AnimationPath::builtin("ALTARMY.DEF"), CGI->generaltexth->zelp[590], [&](){ addAll(); }, EShortcut::RECRUITMENT_UPGRADE_ALL);
  699. convert = std::make_shared<CButton>(Point(269, 416), AnimationPath::builtin("ALTSACR.DEF"), CGI->generaltexth->zelp[591], [&](){ makeDeal(); }, EShortcut::GLOBAL_ACCEPT);
  700. cancel = std::make_shared<CButton>(Point(392, 416), AnimationPath::builtin("ICANCEL.DEF"), CGI->generaltexth->zelp[592], [&](){ close(); },EShortcut::GLOBAL_CANCEL);
  701. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  702. titleLeft = std::make_shared<CLabel>(153, 29,FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[485]);//holding area
  703. titleRight = std::make_shared<CLabel>(153+295, 29, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[486]);//transformer
  704. 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
  705. 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
  706. }
  707. void CTransformerWindow::close()
  708. {
  709. if (onWindowClosed)
  710. onWindowClosed();
  711. CStatusbarWindow::close();
  712. }
  713. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y)
  714. : CIntObject(LCLICK | SHOW_POPUP | HOVER),
  715. ID(_ID),
  716. parent(_parent)
  717. {
  718. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  719. pos.x += X;
  720. pos.y += Y;
  721. icon = std::make_shared<CAnimImage>(AnimationPath::builtin("SECSKILL"), _ID * 3 + 3, 0);
  722. pos.h = icon->pos.h;
  723. pos.w = icon->pos.w;
  724. update();
  725. }
  726. void CUniversityWindow::CItem::clickPressed(const Point & cursorPosition)
  727. {
  728. bool skillKnown = parent->hero->getSecSkillLevel(ID);
  729. bool canLearn = parent->hero->canLearnSkill(ID);
  730. if (!skillKnown && canLearn)
  731. GH.windows().createAndPushWindow<CUnivConfirmWindow>(parent, ID, LOCPLINT->cb->getResourceAmount(EGameResID::GOLD) >= 2000);
  732. }
  733. void CUniversityWindow::CItem::showPopupWindow(const Point & cursorPosition)
  734. {
  735. CRClickPopup::createAndPush(CGI->skillh->getByIndex(ID)->getDescriptionTranslated(1), std::make_shared<CComponent>(ComponentType::SEC_SKILL, ID, 1));
  736. }
  737. void CUniversityWindow::CItem::update()
  738. {
  739. bool skillKnown = parent->hero->getSecSkillLevel(ID);
  740. bool canLearn = parent->hero->canLearnSkill(ID);
  741. ImagePath image;
  742. if (skillKnown)
  743. image = ImagePath::builtin("UNIVGOLD");
  744. else if (canLearn)
  745. image = ImagePath::builtin("UNIVGREN");
  746. else
  747. image = ImagePath::builtin("UNIVRED");
  748. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  749. topBar = std::make_shared<CPicture>(image, Point(-28, -22));
  750. bottomBar = std::make_shared<CPicture>(image, Point(-28, 48));
  751. // needs to be on top of background bars
  752. name = std::make_shared<CLabel>(22, -13, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, ID.toEntity(VLC)->getNameTranslated());
  753. level = std::make_shared<CLabel>(22, 57, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->levels[0]);
  754. }
  755. void CUniversityWindow::CItem::hover(bool on)
  756. {
  757. if(on)
  758. GH.statusbar()->write(ID.toEntity(VLC)->getNameTranslated());
  759. else
  760. GH.statusbar()->clear();
  761. }
  762. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market, const std::function<void()> & onWindowClosed)
  763. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("UNIVERS1")),
  764. hero(_hero),
  765. onWindowClosed(onWindowClosed),
  766. market(_market)
  767. {
  768. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  769. std::string titleStr = CGI->generaltexth->allTexts[602];
  770. std::string speechStr = CGI->generaltexth->allTexts[603];
  771. if(auto town = dynamic_cast<const CGTownInstance *>(_market))
  772. {
  773. auto faction = town->town->faction->getId();
  774. auto bid = town->town->getSpecialBuilding(BuildingSubID::MAGIC_UNIVERSITY)->bid;
  775. titlePic = std::make_shared<CAnimImage>((*CGI->townh)[faction]->town->clientInfo.buildingsIcons, bid);
  776. }
  777. else if(auto uni = dynamic_cast<const CGUniversity *>(_market); uni->appearance)
  778. {
  779. titlePic = std::make_shared<CAnimImage>(uni->appearance->animationFile, 0);
  780. titleStr = uni->title;
  781. speechStr = uni->speech;
  782. }
  783. else
  784. {
  785. titlePic = std::make_shared<CPicture>(ImagePath::builtin("UNIVBLDG"));
  786. }
  787. titlePic->center(Point(232 + pos.x, 76 + pos.y));
  788. clerkSpeech = std::make_shared<CTextBox>(speechStr, Rect(24, 129, 413, 70), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  789. title = std::make_shared<CLabel>(231, 26, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, titleStr);
  790. std::vector<TradeItemBuy> goods = market->availableItemsIds(EMarketMode::RESOURCE_SKILL);
  791. for(int i=0; i<goods.size(); i++)//prepare clickable items
  792. items.push_back(std::make_shared<CItem>(this, goods[i].as<SecondarySkill>(), 54+i*104, 234));
  793. cancel = std::make_shared<CButton>(Point(200, 313), AnimationPath::builtin("IOKAY.DEF"), CGI->generaltexth->zelp[632], [&](){ close(); }, EShortcut::GLOBAL_ACCEPT);
  794. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  795. }
  796. void CUniversityWindow::close()
  797. {
  798. if (onWindowClosed)
  799. onWindowClosed();
  800. CStatusbarWindow::close();
  801. }
  802. void CUniversityWindow::updateSecondarySkills()
  803. {
  804. for (auto const & item : items)
  805. item->update();
  806. }
  807. void CUniversityWindow::makeDeal(SecondarySkill skill)
  808. {
  809. LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_SKILL, GameResID(GameResID::GOLD), skill, 1, hero);
  810. }
  811. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * owner_, SecondarySkill SKILL, bool available)
  812. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("UNIVERS2.PCX")),
  813. owner(owner_)
  814. {
  815. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  816. std::string text = CGI->generaltexth->allTexts[608];
  817. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  818. boost::replace_first(text, "%s", CGI->skillh->getByIndex(SKILL)->getNameTranslated());
  819. boost::replace_first(text, "%d", "2000");
  820. clerkSpeech = std::make_shared<CTextBox>(text, Rect(24, 129, 413, 70), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  821. name = std::make_shared<CLabel>(230, 37, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->skillh->getByIndex(SKILL)->getNameTranslated());
  822. icon = std::make_shared<CAnimImage>(AnimationPath::builtin("SECSKILL"), SKILL*3+3, 0, 211, 51);
  823. level = std::make_shared<CLabel>(230, 107, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->levels[1]);
  824. costIcon = std::make_shared<CAnimImage>(AnimationPath::builtin("RESOURCE"), GameResID(EGameResID::GOLD), 0, 210, 210);
  825. cost = std::make_shared<CLabel>(230, 267, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, "2000");
  826. std::string hoverText = CGI->generaltexth->allTexts[609];
  827. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->skillh->getByIndex(SKILL)->getNameTranslated());
  828. text = CGI->generaltexth->zelp[633].second;
  829. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  830. boost::replace_first(text, "%s", CGI->skillh->getByIndex(SKILL)->getNameTranslated());
  831. boost::replace_first(text, "%d", "2000");
  832. confirm = std::make_shared<CButton>(Point(148, 299), AnimationPath::builtin("IBY6432.DEF"), CButton::tooltip(hoverText, text), [=](){makeDeal(SKILL);}, EShortcut::GLOBAL_ACCEPT);
  833. confirm->block(!available);
  834. cancel = std::make_shared<CButton>(Point(252,299), AnimationPath::builtin("ICANCEL.DEF"), CGI->generaltexth->zelp[631], [&](){ close(); }, EShortcut::GLOBAL_CANCEL);
  835. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  836. }
  837. void CUnivConfirmWindow::makeDeal(SecondarySkill skill)
  838. {
  839. owner->makeDeal(skill);
  840. close();
  841. }
  842. CGarrisonWindow::CGarrisonWindow(const CArmedInstance * up, const CGHeroInstance * down, bool removableUnits)
  843. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("GARRISON"))
  844. {
  845. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  846. garr = std::make_shared<CGarrisonInt>(Point(92, 127), 4, Point(0,96), up, down, removableUnits);
  847. {
  848. auto split = std::make_shared<CButton>(Point(88, 314), AnimationPath::builtin("IDV6432.DEF"), CButton::tooltip(CGI->generaltexth->tcommands[3], ""), [this](){ garr->splitClick(); }, EShortcut::HERO_ARMY_SPLIT );
  849. garr->addSplitBtn(split);
  850. }
  851. quit = std::make_shared<CButton>(Point(399, 314), AnimationPath::builtin("IOK6432.DEF"), CButton::tooltip(CGI->generaltexth->tcommands[8], ""), [this](){ close(); }, EShortcut::GLOBAL_ACCEPT);
  852. std::string titleText;
  853. if(down->tempOwner == up->tempOwner)
  854. {
  855. titleText = CGI->generaltexth->allTexts[709];
  856. }
  857. else
  858. {
  859. //assume that this is joining monsters dialog
  860. if(up->Slots().size() > 0)
  861. {
  862. titleText = CGI->generaltexth->allTexts[35];
  863. boost::algorithm::replace_first(titleText, "%s", up->Slots().begin()->second->type->getNamePluralTranslated());
  864. }
  865. else
  866. {
  867. logGlobal->error("Invalid armed instance for garrison window.");
  868. }
  869. }
  870. title = std::make_shared<CLabel>(275, 30, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, titleText);
  871. banner = std::make_shared<CAnimImage>(AnimationPath::builtin("CREST58"), up->getOwner().getNum(), 0, 28, 124);
  872. portrait = std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsLarge"), down->getIconIndex(), 0, 29, 222);
  873. }
  874. void CGarrisonWindow::updateGarrisons()
  875. {
  876. garr->recreateSlots();
  877. }
  878. bool CGarrisonWindow::holdsGarrison(const CArmedInstance * army)
  879. {
  880. return garr->upperArmy() == army || garr->lowerArmy() == army;
  881. }
  882. CHillFortWindow::CHillFortWindow(const CGHeroInstance * visitor, const CGObjectInstance * object)
  883. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("APHLFTBK")),
  884. fort(object),
  885. hero(visitor)
  886. {
  887. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  888. title = std::make_shared<CLabel>(325, 32, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, fort->getObjectName());
  889. heroPic = std::make_shared<CHeroArea>(30, 60, hero);
  890. for(int i=0; i < resCount; i++)
  891. {
  892. totalIcons[i] = std::make_shared<CAnimImage>(AnimationPath::builtin("SMALRES"), i, 0, 104 + 76 * i, 237);
  893. totalLabels[i] = std::make_shared<CLabel>(166 + 76 * i, 253, FONT_SMALL, ETextAlignment::BOTTOMRIGHT);
  894. }
  895. for(int i = 0; i < slotsCount; i++)
  896. {
  897. 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));
  898. for(int j : {0,1})
  899. {
  900. slotIcons[i][j] = std::make_shared<CAnimImage>(AnimationPath::builtin("SMALRES"), 0, 0, 104 + 76 * i, 128 + 20 * j);
  901. slotLabels[i][j] = std::make_shared<CLabel>(168 + 76 * i, 144 + 20 * j, FONT_SMALL, ETextAlignment::BOTTOMRIGHT);
  902. }
  903. }
  904. 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);
  905. quit = std::make_shared<CButton>(Point(294, 275), AnimationPath::builtin("IOKAY.DEF"), CButton::tooltip(), [this](){close();}, EShortcut::GLOBAL_ACCEPT);
  906. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  907. garr = std::make_shared<CGarrisonInt>(Point(108, 60), 18, Point(), hero, nullptr);
  908. updateGarrisons();
  909. }
  910. bool CHillFortWindow::holdsGarrison(const CArmedInstance * army)
  911. {
  912. return hero == army;
  913. }
  914. void CHillFortWindow::updateGarrisons()
  915. {
  916. constexpr std::array slotImages = { "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF" };
  917. constexpr std::array allImages = { "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF" };
  918. std::array<TResources, slotsCount> costs;// costs [slot ID] [resource ID] = resource count for upgrade
  919. TResources totalSum; // totalSum[resource ID] = value
  920. for(int i=0; i<slotsCount; i++)
  921. {
  922. std::fill(costs[i].begin(), costs[i].end(), 0);
  923. int newState = getState(SlotID(i));
  924. if(newState != -1)
  925. {
  926. UpgradeInfo info;
  927. LOCPLINT->cb->fillUpgradeInfo(hero, SlotID(i), info);
  928. if(info.newID.size())//we have upgrades here - update costs
  929. {
  930. costs[i] = info.cost[0] * hero->getStackCount(SlotID(i));
  931. totalSum += costs[i];
  932. }
  933. }
  934. currState[i] = newState;
  935. upgrade[i]->setImage(AnimationPath::builtin(currState[i] == -1 ? slotImages[0] : slotImages[currState[i]]));
  936. upgrade[i]->block(currState[i] == -1);
  937. upgrade[i]->addHoverText(EButtonState::NORMAL, getTextForSlot(SlotID(i)));
  938. }
  939. //"Upgrade all" slot
  940. int newState = 2;
  941. {
  942. TResources myRes = LOCPLINT->cb->getResourceAmount();
  943. bool allUpgraded = true;//All creatures are upgraded?
  944. for(int i=0; i<slotsCount; i++)
  945. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  946. if(allUpgraded)
  947. newState = 1;
  948. if(!totalSum.canBeAfforded(myRes))
  949. newState = 0;
  950. }
  951. currState[slotsCount] = newState;
  952. upgradeAll->setImage(AnimationPath::builtin(allImages[newState]));
  953. garr->recreateSlots();
  954. for(int i = 0; i < slotsCount; i++)
  955. {
  956. //hide all first
  957. for(int j : {0,1})
  958. {
  959. slotIcons[i][j]->visible = false;
  960. slotLabels[i][j]->setText("");
  961. }
  962. //if can upgrade or can not afford, draw cost
  963. if(currState[i] == 0 || currState[i] == 2)
  964. {
  965. if(costs[i].nonZero())
  966. {
  967. //reverse iterator is used to display gold as first element
  968. int j = 0;
  969. for(int res = (int)costs[i].size()-1; (res >= 0) && (j < 2); res--)
  970. {
  971. int val = costs[i][res];
  972. if(!val)
  973. continue;
  974. slotIcons[i][j]->visible = true;
  975. slotIcons[i][j]->setFrame(res);
  976. slotLabels[i][j]->setText(std::to_string(val));
  977. j++;
  978. }
  979. }
  980. else//free upgrade - print gold image and "Free" text
  981. {
  982. slotIcons[i][0]->visible = true;
  983. slotIcons[i][0]->setFrame(GameResID(EGameResID::GOLD));
  984. slotLabels[i][0]->setText(CGI->generaltexth->allTexts[344]);
  985. }
  986. }
  987. }
  988. for(int i = 0; i < resCount; i++)
  989. {
  990. if(totalSum[i] == 0)
  991. {
  992. totalIcons[i]->visible = false;
  993. totalLabels[i]->setText("");
  994. }
  995. else
  996. {
  997. totalIcons[i]->visible = true;
  998. totalLabels[i]->setText(std::to_string(totalSum[i]));
  999. }
  1000. }
  1001. }
  1002. void CHillFortWindow::makeDeal(SlotID slot)
  1003. {
  1004. assert(slot.getNum()>=0);
  1005. int offset = (slot.getNum() == slotsCount)?2:0;
  1006. switch(currState[slot.getNum()])
  1007. {
  1008. case 0:
  1009. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset], std::vector<std::shared_ptr<CComponent>>(), soundBase::sound_todo);
  1010. break;
  1011. case 1:
  1012. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset], std::vector<std::shared_ptr<CComponent>>(), soundBase::sound_todo);
  1013. break;
  1014. case 2:
  1015. for(int i=0; i<slotsCount; i++)
  1016. {
  1017. if(slot.getNum() ==i || ( slot.getNum() == slotsCount && currState[i] == 2 ))//this is activated slot or "upgrade all"
  1018. {
  1019. UpgradeInfo info;
  1020. LOCPLINT->cb->fillUpgradeInfo(hero, SlotID(i), info);
  1021. LOCPLINT->cb->upgradeCreature(hero, SlotID(i), info.newID[0]);
  1022. }
  1023. }
  1024. break;
  1025. }
  1026. }
  1027. std::string CHillFortWindow::getTextForSlot(SlotID slot)
  1028. {
  1029. if(!hero->getCreature(slot))//we don`t have creature here
  1030. return "";
  1031. std::string str = CGI->generaltexth->allTexts[318];
  1032. int amount = hero->getStackCount(slot);
  1033. if(amount == 1)
  1034. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->getNameSingularTranslated());
  1035. else
  1036. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->getNamePluralTranslated());
  1037. return str;
  1038. }
  1039. int CHillFortWindow::getState(SlotID slot)
  1040. {
  1041. TResources myRes = LOCPLINT->cb->getResourceAmount();
  1042. if(hero->slotEmpty(slot))//no creature here
  1043. return -1;
  1044. UpgradeInfo info;
  1045. LOCPLINT->cb->fillUpgradeInfo(hero, slot, info);
  1046. if(!info.newID.size())//already upgraded
  1047. return 1;
  1048. if(!(info.cost[0] * hero->getStackCount(slot)).canBeAfforded(myRes))
  1049. return 0;
  1050. return 2;//can upgrade
  1051. }
  1052. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner):
  1053. CWindowObject(PLAYER_COLORED | BORDERED, ImagePath::builtin("TpRank")),
  1054. owner(_owner)
  1055. {
  1056. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1057. SThievesGuildInfo tgi; //info to be displayed
  1058. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  1059. exitb = std::make_shared<CButton>(Point(748, 556), AnimationPath::builtin("TPMAGE1"), CButton::tooltip(CGI->generaltexth->allTexts[600]), [&](){ close();}, EShortcut::GLOBAL_RETURN);
  1060. statusbar = CGStatusBar::create(3, 555, ImagePath::builtin("TStatBar.bmp"), 742);
  1061. resdatabar = std::make_shared<CMinorResDataBar>();
  1062. resdatabar->moveBy(pos.topLeft(), true);
  1063. //data for information table:
  1064. // fields[row][column] = list of id's of players for this box
  1065. constexpr std::vector< std::vector< PlayerColor > > SThievesGuildInfo::* fields[] =
  1066. { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  1067. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks,
  1068. &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army, &SThievesGuildInfo::income };
  1069. for(int g=0; g<12; ++g)
  1070. {
  1071. int posY[] = {400, 460, 510};
  1072. int y;
  1073. if(g < 9)
  1074. y = 52 + 32*g;
  1075. else
  1076. y = posY[g-9];
  1077. std::string text = CGI->generaltexth->jktexts[24+g];
  1078. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  1079. rowHeaders.push_back(std::make_shared<CLabel>(135, y, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, text));
  1080. }
  1081. for(int g=1; g<tgi.playerColors.size(); ++g)
  1082. columnBackgrounds.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PRSTRIPS"), g-1, 0, 250 + 66*g, 7));
  1083. for(int g=0; g<tgi.playerColors.size(); ++g)
  1084. columnHeaders.push_back(std::make_shared<CLabel>(283 + 66*g, 24, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[16+g]));
  1085. //printing flags
  1086. for(int g = 0; g < std::size(fields); ++g) //by lines
  1087. {
  1088. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  1089. {
  1090. std::vector<PlayerColor> &players = (tgi .* fields[g])[b]; //get players with this place in this line
  1091. //position of box
  1092. int xpos = 259 + 66 * b;
  1093. int ypos = 41 + 32 * g;
  1094. size_t rowLength[2]; //size of each row
  1095. rowLength[0] = std::min<size_t>(players.size(), 4);
  1096. rowLength[1] = players.size() - rowLength[0];
  1097. for(size_t j=0; j < 2; j++)
  1098. {
  1099. // origin of this row | offset for 2nd row| shift right for short rows
  1100. //if we have 2 rows, start either from mid or beginning (depending on count), otherwise center the flags
  1101. int rowStartX = xpos + (j ? 6 + ((int)rowLength[j] < 3 ? 12 : 0) : 24 - 6 * (int)rowLength[j]);
  1102. int rowStartY = ypos + (j ? 4 : 0);
  1103. for(size_t i=0; i < rowLength[j]; i++)
  1104. cells.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("itgflags"), players[i + j*4].getNum(), 0, rowStartX + (int)i*12, rowStartY));
  1105. }
  1106. }
  1107. }
  1108. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  1109. //printing best hero
  1110. int counter = 0;
  1111. for(auto & iter : tgi.colorToBestHero)
  1112. {
  1113. banners.push_back(std::make_shared<CPicture>(ImagePath::builtin(colorToBox[iter.first.getNum()]), 253 + 66 * counter, 334));
  1114. if(iter.second.portraitSource.isValid())
  1115. {
  1116. bestHeroes.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsSmall"), iter.second.getIconIndex(), 0, 260 + 66 * counter, 360));
  1117. //TODO: r-click info:
  1118. // - r-click on hero
  1119. // - r-click on primary skill label
  1120. if(iter.second.details)
  1121. {
  1122. primSkillHeaders.push_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[184], Rect(260 + 66*counter, 396, 52, 64),
  1123. 0, FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE));
  1124. for(int i=0; i<iter.second.details->primskills.size(); ++i)
  1125. {
  1126. primSkillValues.push_back(std::make_shared<CLabel>(310 + 66 * counter, 407 + 11*i, FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE,
  1127. std::to_string(iter.second.details->primskills[i])));
  1128. }
  1129. }
  1130. }
  1131. counter++;
  1132. }
  1133. //printing best creature
  1134. counter = 0;
  1135. for(auto & it : tgi.bestCreature)
  1136. {
  1137. if(it.second != CreatureID::NONE)
  1138. bestCreatures.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("TWCRPORT"), it.second+2, 0, 255 + 66 * counter, 479));
  1139. counter++;
  1140. }
  1141. //printing personality
  1142. counter = 0;
  1143. for(auto & it : tgi.personality)
  1144. {
  1145. std::string text;
  1146. if(it.second == EAiTactic::NONE)
  1147. {
  1148. text = CGI->generaltexth->arraytxt[172];
  1149. }
  1150. else if(it.second != EAiTactic::RANDOM)
  1151. {
  1152. text = CGI->generaltexth->arraytxt[168 + static_cast<int>(it.second)];
  1153. }
  1154. personalities.push_back(std::make_shared<CLabel>(283 + 66*counter, 459, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, text));
  1155. counter++;
  1156. }
  1157. }
  1158. CObjectListWindow::CItem::CItem(CObjectListWindow * _parent, size_t _id, std::string _text)
  1159. : CIntObject(LCLICK | DOUBLECLICK | RCLICK_POPUP),
  1160. parent(_parent),
  1161. index(_id)
  1162. {
  1163. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1164. if(parent->images.size() > index)
  1165. icon = std::make_shared<CPicture>(parent->images[index], Point(1, 1));
  1166. border = std::make_shared<CPicture>(ImagePath::builtin("TPGATES"));
  1167. pos = border->pos;
  1168. setRedrawParent(true);
  1169. text = std::make_shared<CLabel>(pos.w/2, pos.h/2, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, _text);
  1170. select(index == parent->selected);
  1171. }
  1172. void CObjectListWindow::CItem::select(bool on)
  1173. {
  1174. ui8 mask = UPDATE | SHOWALL;
  1175. if(on)
  1176. border->recActions |= mask;
  1177. else
  1178. border->recActions &= ~mask;
  1179. redraw();//???
  1180. }
  1181. void CObjectListWindow::CItem::clickPressed(const Point & cursorPosition)
  1182. {
  1183. parent->changeSelection(index);
  1184. if(parent->onClicked)
  1185. parent->onClicked(index);
  1186. }
  1187. void CObjectListWindow::CItem::clickDouble(const Point & cursorPosition)
  1188. {
  1189. if (parent->selected != index)
  1190. {
  1191. clickPressed(cursorPosition);
  1192. return;
  1193. }
  1194. parent->elementSelected();
  1195. }
  1196. void CObjectListWindow::CItem::showPopupWindow(const Point & cursorPosition)
  1197. {
  1198. if(parent->onPopup)
  1199. parent->onPopup(index);
  1200. }
  1201. 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)
  1202. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPGATE")),
  1203. onSelect(Callback),
  1204. selected(initialSelection),
  1205. images(images)
  1206. {
  1207. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1208. items.reserve(_items.size());
  1209. for(int id : _items)
  1210. {
  1211. items.push_back(std::make_pair(id, LOCPLINT->cb->getObjInstance(ObjectInstanceID(id))->getObjectName()));
  1212. }
  1213. init(titleWidget_, _title, _descr);
  1214. }
  1215. 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)
  1216. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPGATE")),
  1217. onSelect(Callback),
  1218. selected(initialSelection),
  1219. images(images)
  1220. {
  1221. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1222. items.reserve(_items.size());
  1223. for(size_t i=0; i<_items.size(); i++)
  1224. items.push_back(std::make_pair(int(i), _items[i]));
  1225. init(titleWidget_, _title, _descr);
  1226. }
  1227. void CObjectListWindow::init(std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr)
  1228. {
  1229. titleWidget = titleWidget_;
  1230. title = std::make_shared<CLabel>(152, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, _title);
  1231. descr = std::make_shared<CLabel>(145, 133, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, _descr);
  1232. exit = std::make_shared<CButton>( Point(228, 402), AnimationPath::builtin("ICANCEL.DEF"), CButton::tooltip(), std::bind(&CObjectListWindow::exitPressed, this), EShortcut::GLOBAL_CANCEL);
  1233. if(titleWidget)
  1234. {
  1235. addChild(titleWidget.get());
  1236. titleWidget->recActions = 255-DISPOSE;
  1237. titleWidget->pos.x = pos.w/2 + pos.x - titleWidget->pos.w/2;
  1238. titleWidget->pos.y =75 + pos.y - titleWidget->pos.h/2;
  1239. }
  1240. list = std::make_shared<CListBox>(std::bind(&CObjectListWindow::genItem, this, _1),
  1241. Point(14, 151), Point(0, 25), 9, items.size(), 0, 1, Rect(262, -32, 256, 256) );
  1242. list->setRedrawParent(true);
  1243. ok = std::make_shared<CButton>(Point(15, 402), AnimationPath::builtin("IOKAY.DEF"), CButton::tooltip(), std::bind(&CObjectListWindow::elementSelected, this), EShortcut::GLOBAL_ACCEPT);
  1244. ok->block(!list->size());
  1245. }
  1246. std::shared_ptr<CIntObject> CObjectListWindow::genItem(size_t index)
  1247. {
  1248. if(index < items.size())
  1249. return std::make_shared<CItem>(this, index, items[index].second);
  1250. return std::shared_ptr<CIntObject>();
  1251. }
  1252. void CObjectListWindow::elementSelected()
  1253. {
  1254. std::function<void(int)> toCall = onSelect;//save
  1255. int where = items[selected].first; //required variables
  1256. close();//then destroy window
  1257. toCall(where);//and send selected object
  1258. }
  1259. void CObjectListWindow::exitPressed()
  1260. {
  1261. std::function<void()> toCall = onExit;//save
  1262. close();//then destroy window
  1263. if(toCall)
  1264. toCall();
  1265. }
  1266. void CObjectListWindow::changeSelection(size_t which)
  1267. {
  1268. ok->block(false);
  1269. if(selected == which)
  1270. return;
  1271. for(std::shared_ptr<CIntObject> element : list->getItems())
  1272. {
  1273. CItem * item = dynamic_cast<CItem*>(element.get());
  1274. if(item)
  1275. {
  1276. if(item->index == selected)
  1277. item->select(false);
  1278. if(item->index == which)
  1279. item->select(true);
  1280. }
  1281. }
  1282. selected = which;
  1283. }
  1284. void CObjectListWindow::keyPressed(EShortcut key)
  1285. {
  1286. int sel = static_cast<int>(selected);
  1287. switch(key)
  1288. {
  1289. break; case EShortcut::MOVE_UP:
  1290. sel -=1;
  1291. break; case EShortcut::MOVE_DOWN:
  1292. sel +=1;
  1293. break; case EShortcut::MOVE_PAGE_UP:
  1294. sel -=9;
  1295. break; case EShortcut::MOVE_PAGE_DOWN:
  1296. sel +=9;
  1297. break; case EShortcut::MOVE_FIRST:
  1298. sel = 0;
  1299. break; case EShortcut::MOVE_LAST:
  1300. sel = static_cast<int>(items.size());
  1301. break; default:
  1302. return;
  1303. }
  1304. vstd::abetween<int>(sel, 0, items.size()-1);
  1305. list->scrollTo(sel);
  1306. changeSelection(sel);
  1307. }