GUIClasses.cpp 59 KB

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