GUIClasses.cpp 56 KB

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