GUIClasses.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  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/GameSettings.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_CAPTURING(255-DISPOSE);
  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 = CGI->settings()->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_CAPTURING(255-DISPOSE);
  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_CUSTOM_CAPTURING(255-DISPOSE);
  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_CAPTURING(255-DISPOSE);
  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_CAPTURING(255-DISPOSE);
  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_CAPTURING(255-DISPOSE);
  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) >= CGI->settings()->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) >= CGI->settings()->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_CAPTURING(255-DISPOSE);
  429. if(!VLC->settings()->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_CAPTURING(255-DISPOSE);
  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. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  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. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  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_CAPTURING(255-DISPOSE);
  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<CAnimImage>(boatFilename, 0, 7, 120, 96, 0);
  599. bgShip->center(waterCenter);
  600. }
  601. // Create resource icons and costs.
  602. std::string goldValue = std::to_string(cost[EGameResID::GOLD]);
  603. std::string woodValue = std::to_string(cost[EGameResID::WOOD]);
  604. goldCost = std::make_shared<CLabel>(118, 294, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, goldValue);
  605. woodCost = std::make_shared<CLabel>(212, 294, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, woodValue);
  606. goldPic = std::make_shared<CAnimImage>(AnimationPath::builtin("RESOURCE"), GameResID(EGameResID::GOLD), 0, 100, 244);
  607. woodPic = std::make_shared<CAnimImage>(AnimationPath::builtin("RESOURCE"), GameResID(EGameResID::WOOD), 0, 196, 244);
  608. 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);
  609. 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);
  610. build->addCallback(onBuy);
  611. for(GameResID i = EGameResID::WOOD; i <= EGameResID::GOLD; ++i)
  612. {
  613. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  614. {
  615. build->block(true);
  616. break;
  617. }
  618. }
  619. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  620. title = std::make_shared<CLabel>(164, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[13]);
  621. costLabel = std::make_shared<CLabel>(164, 220, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->jktexts[14]);
  622. }
  623. void CTransformerWindow::CItem::move()
  624. {
  625. if(left)
  626. moveBy(Point(289, 0));
  627. else
  628. moveBy(Point(-289, 0));
  629. left = !left;
  630. }
  631. void CTransformerWindow::CItem::clickPressed(const Point & cursorPosition)
  632. {
  633. move();
  634. parent->redraw();
  635. }
  636. void CTransformerWindow::CItem::update()
  637. {
  638. icon->setFrame(parent->army->getCreature(SlotID(id))->getId() + 2);
  639. }
  640. CTransformerWindow::CItem::CItem(CTransformerWindow * parent_, int size_, int id_)
  641. : CIntObject(LCLICK),
  642. id(id_),
  643. size(size_),
  644. parent(parent_)
  645. {
  646. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  647. left = true;
  648. pos.w = 58;
  649. pos.h = 64;
  650. pos.x += 45 + (id%3)*83 + id/6*83;
  651. pos.y += 109 + (id/3)*98;
  652. icon = std::make_shared<CAnimImage>(AnimationPath::builtin("TWCRPORT"), parent->army->getCreature(SlotID(id))->getId() + 2);
  653. count = std::make_shared<CLabel>(28, 76,FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(size));
  654. }
  655. void CTransformerWindow::makeDeal()
  656. {
  657. for(auto & elem : items)
  658. {
  659. if(!elem->left)
  660. LOCPLINT->cb->trade(market, EMarketMode::CREATURE_UNDEAD, SlotID(elem->id), {}, {}, hero);
  661. }
  662. }
  663. void CTransformerWindow::addAll()
  664. {
  665. for(auto & elem : items)
  666. {
  667. if(elem->left)
  668. elem->move();
  669. }
  670. redraw();
  671. }
  672. void CTransformerWindow::updateGarrisons()
  673. {
  674. for(auto & item : items)
  675. item->update();
  676. }
  677. bool CTransformerWindow::holdsGarrison(const CArmedInstance * army)
  678. {
  679. return army == hero;
  680. }
  681. CTransformerWindow::CTransformerWindow(const IMarket * _market, const CGHeroInstance * _hero, const std::function<void()> & onWindowClosed)
  682. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("SKTRNBK")),
  683. hero(_hero),
  684. onWindowClosed(onWindowClosed),
  685. market(_market)
  686. {
  687. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  688. if(hero)
  689. army = hero;
  690. else
  691. army = dynamic_cast<const CArmedInstance *>(market); //for town only
  692. if(army)
  693. {
  694. for(int i = 0; i < GameConstants::ARMY_SIZE; i++)
  695. {
  696. if(army->getCreature(SlotID(i)))
  697. items.push_back(std::make_shared<CItem>(this, army->getStackCount(SlotID(i)), i));
  698. }
  699. }
  700. all = std::make_shared<CButton>(Point(146, 416), AnimationPath::builtin("ALTARMY.DEF"), CGI->generaltexth->zelp[590], [&](){ addAll(); }, EShortcut::RECRUITMENT_UPGRADE_ALL);
  701. convert = std::make_shared<CButton>(Point(269, 416), AnimationPath::builtin("ALTSACR.DEF"), CGI->generaltexth->zelp[591], [&](){ makeDeal(); }, EShortcut::GLOBAL_ACCEPT);
  702. cancel = std::make_shared<CButton>(Point(392, 416), AnimationPath::builtin("ICANCEL.DEF"), CGI->generaltexth->zelp[592], [&](){ close(); },EShortcut::GLOBAL_CANCEL);
  703. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  704. titleLeft = std::make_shared<CLabel>(153, 29,FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[485]);//holding area
  705. titleRight = std::make_shared<CLabel>(153+295, 29, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[486]);//transformer
  706. 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
  707. 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
  708. }
  709. void CTransformerWindow::close()
  710. {
  711. if (onWindowClosed)
  712. onWindowClosed();
  713. CStatusbarWindow::close();
  714. }
  715. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y)
  716. : CIntObject(LCLICK | SHOW_POPUP | HOVER),
  717. ID(_ID),
  718. parent(_parent)
  719. {
  720. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  721. pos.x += X;
  722. pos.y += Y;
  723. icon = std::make_shared<CAnimImage>(AnimationPath::builtin("SECSKILL"), _ID * 3 + 3, 0);
  724. pos.h = icon->pos.h;
  725. pos.w = icon->pos.w;
  726. update();
  727. }
  728. void CUniversityWindow::CItem::clickPressed(const Point & cursorPosition)
  729. {
  730. bool skillKnown = parent->hero->getSecSkillLevel(ID);
  731. bool canLearn = parent->hero->canLearnSkill(ID);
  732. if (!skillKnown && canLearn)
  733. GH.windows().createAndPushWindow<CUnivConfirmWindow>(parent, ID, LOCPLINT->cb->getResourceAmount(EGameResID::GOLD) >= 2000);
  734. }
  735. void CUniversityWindow::CItem::showPopupWindow(const Point & cursorPosition)
  736. {
  737. CRClickPopup::createAndPush(CGI->skillh->getByIndex(ID)->getDescriptionTranslated(1), std::make_shared<CComponent>(ComponentType::SEC_SKILL, ID, 1));
  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_CAPTURING(255-DISPOSE);
  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. void CUniversityWindow::CItem::hover(bool on)
  758. {
  759. if(on)
  760. GH.statusbar()->write(ID.toEntity(VLC)->getNameTranslated());
  761. else
  762. GH.statusbar()->clear();
  763. }
  764. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market, const std::function<void()> & onWindowClosed)
  765. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("UNIVERS1")),
  766. hero(_hero),
  767. onWindowClosed(onWindowClosed),
  768. market(_market)
  769. {
  770. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  771. std::string titleStr = CGI->generaltexth->allTexts[602];
  772. std::string speechStr = CGI->generaltexth->allTexts[603];
  773. if(auto town = dynamic_cast<const CGTownInstance *>(_market))
  774. {
  775. auto faction = town->town->faction->getId();
  776. auto bid = town->town->getSpecialBuilding(BuildingSubID::MAGIC_UNIVERSITY)->bid;
  777. titlePic = std::make_shared<CAnimImage>((*CGI->townh)[faction]->town->clientInfo.buildingsIcons, bid);
  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, 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_CAPTURING(255-DISPOSE);
  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_CAPTURING(255-DISPOSE);
  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_CAPTURING(255-DISPOSE);
  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. updateGarrisons();
  911. }
  912. bool CHillFortWindow::holdsGarrison(const CArmedInstance * army)
  913. {
  914. return hero == army;
  915. }
  916. void CHillFortWindow::updateGarrisons()
  917. {
  918. constexpr std::array slotImages = { "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF" };
  919. constexpr std::array allImages = { "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF" };
  920. std::array<TResources, slotsCount> costs;// costs [slot ID] [resource ID] = resource count for upgrade
  921. TResources totalSum; // totalSum[resource ID] = value
  922. for(int i=0; i<slotsCount; i++)
  923. {
  924. std::fill(costs[i].begin(), costs[i].end(), 0);
  925. int newState = getState(SlotID(i));
  926. if(newState != -1)
  927. {
  928. UpgradeInfo info;
  929. LOCPLINT->cb->fillUpgradeInfo(hero, SlotID(i), info);
  930. if(info.newID.size())//we have upgrades here - update costs
  931. {
  932. costs[i] = info.cost[0] * hero->getStackCount(SlotID(i));
  933. totalSum += costs[i];
  934. }
  935. }
  936. currState[i] = newState;
  937. upgrade[i]->setImage(AnimationPath::builtin(currState[i] == -1 ? slotImages[0] : slotImages[currState[i]]));
  938. upgrade[i]->block(currState[i] == -1);
  939. upgrade[i]->addHoverText(EButtonState::NORMAL, getTextForSlot(SlotID(i)));
  940. }
  941. //"Upgrade all" slot
  942. int newState = 2;
  943. {
  944. TResources myRes = LOCPLINT->cb->getResourceAmount();
  945. bool allUpgraded = true;//All creatures are upgraded?
  946. for(int i=0; i<slotsCount; i++)
  947. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  948. if(allUpgraded)
  949. newState = 1;
  950. if(!totalSum.canBeAfforded(myRes))
  951. newState = 0;
  952. }
  953. currState[slotsCount] = newState;
  954. upgradeAll->setImage(AnimationPath::builtin(allImages[newState]));
  955. garr->recreateSlots();
  956. for(int i = 0; i < slotsCount; i++)
  957. {
  958. //hide all first
  959. for(int j : {0,1})
  960. {
  961. slotIcons[i][j]->visible = false;
  962. slotLabels[i][j]->setText("");
  963. }
  964. //if can upgrade or can not afford, draw cost
  965. if(currState[i] == 0 || currState[i] == 2)
  966. {
  967. if(costs[i].nonZero())
  968. {
  969. //reverse iterator is used to display gold as first element
  970. int j = 0;
  971. for(int res = (int)costs[i].size()-1; (res >= 0) && (j < 2); res--)
  972. {
  973. int val = costs[i][res];
  974. if(!val)
  975. continue;
  976. slotIcons[i][j]->visible = true;
  977. slotIcons[i][j]->setFrame(res);
  978. slotLabels[i][j]->setText(std::to_string(val));
  979. j++;
  980. }
  981. }
  982. else//free upgrade - print gold image and "Free" text
  983. {
  984. slotIcons[i][0]->visible = true;
  985. slotIcons[i][0]->setFrame(GameResID(EGameResID::GOLD));
  986. slotLabels[i][0]->setText(CGI->generaltexth->allTexts[344]);
  987. }
  988. }
  989. }
  990. for(int i = 0; i < resCount; i++)
  991. {
  992. if(totalSum[i] == 0)
  993. {
  994. totalIcons[i]->visible = false;
  995. totalLabels[i]->setText("");
  996. }
  997. else
  998. {
  999. totalIcons[i]->visible = true;
  1000. totalLabels[i]->setText(std::to_string(totalSum[i]));
  1001. }
  1002. }
  1003. }
  1004. void CHillFortWindow::makeDeal(SlotID slot)
  1005. {
  1006. assert(slot.getNum()>=0);
  1007. int offset = (slot.getNum() == slotsCount)?2:0;
  1008. switch(currState[slot.getNum()])
  1009. {
  1010. case 0:
  1011. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset], std::vector<std::shared_ptr<CComponent>>(), soundBase::sound_todo);
  1012. break;
  1013. case 1:
  1014. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset], std::vector<std::shared_ptr<CComponent>>(), soundBase::sound_todo);
  1015. break;
  1016. case 2:
  1017. for(int i=0; i<slotsCount; i++)
  1018. {
  1019. if(slot.getNum() ==i || ( slot.getNum() == slotsCount && currState[i] == 2 ))//this is activated slot or "upgrade all"
  1020. {
  1021. UpgradeInfo info;
  1022. LOCPLINT->cb->fillUpgradeInfo(hero, SlotID(i), info);
  1023. LOCPLINT->cb->upgradeCreature(hero, SlotID(i), info.newID[0]);
  1024. }
  1025. }
  1026. break;
  1027. }
  1028. }
  1029. std::string CHillFortWindow::getTextForSlot(SlotID slot)
  1030. {
  1031. if(!hero->getCreature(slot))//we don`t have creature here
  1032. return "";
  1033. std::string str = CGI->generaltexth->allTexts[318];
  1034. int amount = hero->getStackCount(slot);
  1035. if(amount == 1)
  1036. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->getNameSingularTranslated());
  1037. else
  1038. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->getNamePluralTranslated());
  1039. return str;
  1040. }
  1041. int CHillFortWindow::getState(SlotID slot)
  1042. {
  1043. TResources myRes = LOCPLINT->cb->getResourceAmount();
  1044. if(hero->slotEmpty(slot))//no creature here
  1045. return -1;
  1046. UpgradeInfo info;
  1047. LOCPLINT->cb->fillUpgradeInfo(hero, slot, info);
  1048. if(!info.newID.size())//already upgraded
  1049. return 1;
  1050. if(!(info.cost[0] * hero->getStackCount(slot)).canBeAfforded(myRes))
  1051. return 0;
  1052. return 2;//can upgrade
  1053. }
  1054. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner):
  1055. CWindowObject(PLAYER_COLORED | BORDERED, ImagePath::builtin("TpRank")),
  1056. owner(_owner)
  1057. {
  1058. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1059. SThievesGuildInfo tgi; //info to be displayed
  1060. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  1061. exitb = std::make_shared<CButton>(Point(748, 556), AnimationPath::builtin("TPMAGE1"), CButton::tooltip(CGI->generaltexth->allTexts[600]), [&](){ close();}, EShortcut::GLOBAL_RETURN);
  1062. statusbar = CGStatusBar::create(3, 555, ImagePath::builtin("TStatBar.bmp"), 742);
  1063. resdatabar = std::make_shared<CMinorResDataBar>();
  1064. resdatabar->moveBy(pos.topLeft(), true);
  1065. //data for information table:
  1066. // fields[row][column] = list of id's of players for this box
  1067. constexpr std::vector< std::vector< PlayerColor > > SThievesGuildInfo::* fields[] =
  1068. { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  1069. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks,
  1070. &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army, &SThievesGuildInfo::income };
  1071. for(int g=0; g<12; ++g)
  1072. {
  1073. int posY[] = {400, 460, 510};
  1074. int y;
  1075. if(g < 9)
  1076. y = 52 + 32*g;
  1077. else
  1078. y = posY[g-9];
  1079. std::string text = CGI->generaltexth->jktexts[24+g];
  1080. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  1081. rowHeaders.push_back(std::make_shared<CLabel>(135, y, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, text));
  1082. }
  1083. for(int g=1; g<tgi.playerColors.size(); ++g)
  1084. columnBackgrounds.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PRSTRIPS"), g-1, 0, 250 + 66*g, 7));
  1085. for(int g=0; g<tgi.playerColors.size(); ++g)
  1086. columnHeaders.push_back(std::make_shared<CLabel>(283 + 66*g, 24, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[16+g]));
  1087. //printing flags
  1088. for(int g = 0; g < std::size(fields); ++g) //by lines
  1089. {
  1090. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  1091. {
  1092. std::vector<PlayerColor> &players = (tgi .* fields[g])[b]; //get players with this place in this line
  1093. //position of box
  1094. int xpos = 259 + 66 * b;
  1095. int ypos = 41 + 32 * g;
  1096. size_t rowLength[2]; //size of each row
  1097. rowLength[0] = std::min<size_t>(players.size(), 4);
  1098. rowLength[1] = players.size() - rowLength[0];
  1099. for(size_t j=0; j < 2; j++)
  1100. {
  1101. // origin of this row | offset for 2nd row| shift right for short rows
  1102. //if we have 2 rows, start either from mid or beginning (depending on count), otherwise center the flags
  1103. int rowStartX = xpos + (j ? 6 + ((int)rowLength[j] < 3 ? 12 : 0) : 24 - 6 * (int)rowLength[j]);
  1104. int rowStartY = ypos + (j ? 4 : 0);
  1105. for(size_t i=0; i < rowLength[j]; i++)
  1106. cells.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("itgflags"), players[i + j*4].getNum(), 0, rowStartX + (int)i*12, rowStartY));
  1107. }
  1108. }
  1109. }
  1110. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  1111. //printing best hero
  1112. int counter = 0;
  1113. for(auto & iter : tgi.colorToBestHero)
  1114. {
  1115. banners.push_back(std::make_shared<CPicture>(ImagePath::builtin(colorToBox[iter.first.getNum()]), 253 + 66 * counter, 334));
  1116. if(iter.second.portraitSource.isValid())
  1117. {
  1118. bestHeroes.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsSmall"), iter.second.getIconIndex(), 0, 260 + 66 * counter, 360));
  1119. //TODO: r-click info:
  1120. // - r-click on hero
  1121. // - r-click on primary skill label
  1122. if(iter.second.details)
  1123. {
  1124. primSkillHeaders.push_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[184], Rect(260 + 66*counter, 396, 52, 64),
  1125. 0, FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE));
  1126. for(int i=0; i<iter.second.details->primskills.size(); ++i)
  1127. {
  1128. primSkillValues.push_back(std::make_shared<CLabel>(310 + 66 * counter, 407 + 11*i, FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE,
  1129. std::to_string(iter.second.details->primskills[i])));
  1130. }
  1131. }
  1132. }
  1133. counter++;
  1134. }
  1135. //printing best creature
  1136. counter = 0;
  1137. for(auto & it : tgi.bestCreature)
  1138. {
  1139. if(it.second != CreatureID::NONE)
  1140. bestCreatures.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("TWCRPORT"), it.second+2, 0, 255 + 66 * counter, 479));
  1141. counter++;
  1142. }
  1143. //printing personality
  1144. counter = 0;
  1145. for(auto & it : tgi.personality)
  1146. {
  1147. std::string text;
  1148. if(it.second == EAiTactic::NONE)
  1149. {
  1150. text = CGI->generaltexth->arraytxt[172];
  1151. }
  1152. else if(it.second != EAiTactic::RANDOM)
  1153. {
  1154. text = CGI->generaltexth->arraytxt[168 + static_cast<int>(it.second)];
  1155. }
  1156. personalities.push_back(std::make_shared<CLabel>(283 + 66*counter, 459, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, text));
  1157. counter++;
  1158. }
  1159. }
  1160. CObjectListWindow::CItem::CItem(CObjectListWindow * _parent, size_t _id, std::string _text)
  1161. : CIntObject(LCLICK | DOUBLECLICK | RCLICK_POPUP),
  1162. parent(_parent),
  1163. index(_id)
  1164. {
  1165. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1166. if(parent->images.size() > index)
  1167. icon = std::make_shared<CPicture>(parent->images[index], Point(1, 1));
  1168. border = std::make_shared<CPicture>(ImagePath::builtin("TPGATES"));
  1169. pos = border->pos;
  1170. setRedrawParent(true);
  1171. text = std::make_shared<CLabel>(pos.w/2, pos.h/2, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, _text);
  1172. select(index == parent->selected);
  1173. }
  1174. void CObjectListWindow::CItem::select(bool on)
  1175. {
  1176. ui8 mask = UPDATE | SHOWALL;
  1177. if(on)
  1178. border->recActions |= mask;
  1179. else
  1180. border->recActions &= ~mask;
  1181. redraw();//???
  1182. }
  1183. void CObjectListWindow::CItem::clickPressed(const Point & cursorPosition)
  1184. {
  1185. parent->changeSelection(index);
  1186. if(parent->onClicked)
  1187. parent->onClicked(index);
  1188. }
  1189. void CObjectListWindow::CItem::clickDouble(const Point & cursorPosition)
  1190. {
  1191. if (parent->selected != index)
  1192. {
  1193. clickPressed(cursorPosition);
  1194. return;
  1195. }
  1196. parent->elementSelected();
  1197. }
  1198. void CObjectListWindow::CItem::showPopupWindow(const Point & cursorPosition)
  1199. {
  1200. if(parent->onPopup)
  1201. parent->onPopup(index);
  1202. }
  1203. 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)
  1204. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPGATE")),
  1205. onSelect(Callback),
  1206. selected(initialSelection),
  1207. images(images)
  1208. {
  1209. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1210. items.reserve(_items.size());
  1211. for(int id : _items)
  1212. {
  1213. items.push_back(std::make_pair(id, LOCPLINT->cb->getObjInstance(ObjectInstanceID(id))->getObjectName()));
  1214. }
  1215. init(titleWidget_, _title, _descr);
  1216. }
  1217. 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)
  1218. : CWindowObject(PLAYER_COLORED, ImagePath::builtin("TPGATE")),
  1219. onSelect(Callback),
  1220. selected(initialSelection),
  1221. images(images)
  1222. {
  1223. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1224. items.reserve(_items.size());
  1225. for(size_t i=0; i<_items.size(); i++)
  1226. items.push_back(std::make_pair(int(i), _items[i]));
  1227. init(titleWidget_, _title, _descr);
  1228. }
  1229. void CObjectListWindow::init(std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr)
  1230. {
  1231. titleWidget = titleWidget_;
  1232. title = std::make_shared<CLabel>(152, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, _title);
  1233. descr = std::make_shared<CLabel>(145, 133, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, _descr);
  1234. exit = std::make_shared<CButton>( Point(228, 402), AnimationPath::builtin("ICANCEL.DEF"), CButton::tooltip(), std::bind(&CObjectListWindow::exitPressed, this), EShortcut::GLOBAL_CANCEL);
  1235. if(titleWidget)
  1236. {
  1237. addChild(titleWidget.get());
  1238. titleWidget->recActions = 255-DISPOSE;
  1239. titleWidget->pos.x = pos.w/2 + pos.x - titleWidget->pos.w/2;
  1240. titleWidget->pos.y =75 + pos.y - titleWidget->pos.h/2;
  1241. }
  1242. list = std::make_shared<CListBox>(std::bind(&CObjectListWindow::genItem, this, _1),
  1243. Point(14, 151), Point(0, 25), 9, items.size(), 0, 1, Rect(262, -32, 256, 256) );
  1244. list->setRedrawParent(true);
  1245. ok = std::make_shared<CButton>(Point(15, 402), AnimationPath::builtin("IOKAY.DEF"), CButton::tooltip(), std::bind(&CObjectListWindow::elementSelected, this), EShortcut::GLOBAL_ACCEPT);
  1246. ok->block(!list->size());
  1247. }
  1248. std::shared_ptr<CIntObject> CObjectListWindow::genItem(size_t index)
  1249. {
  1250. if(index < items.size())
  1251. return std::make_shared<CItem>(this, index, items[index].second);
  1252. return std::shared_ptr<CIntObject>();
  1253. }
  1254. void CObjectListWindow::elementSelected()
  1255. {
  1256. std::function<void(int)> toCall = onSelect;//save
  1257. int where = items[selected].first; //required variables
  1258. close();//then destroy window
  1259. toCall(where);//and send selected object
  1260. }
  1261. void CObjectListWindow::exitPressed()
  1262. {
  1263. std::function<void()> toCall = onExit;//save
  1264. close();//then destroy window
  1265. if(toCall)
  1266. toCall();
  1267. }
  1268. void CObjectListWindow::changeSelection(size_t which)
  1269. {
  1270. ok->block(false);
  1271. if(selected == which)
  1272. return;
  1273. for(std::shared_ptr<CIntObject> element : list->getItems())
  1274. {
  1275. CItem * item = dynamic_cast<CItem*>(element.get());
  1276. if(item)
  1277. {
  1278. if(item->index == selected)
  1279. item->select(false);
  1280. if(item->index == which)
  1281. item->select(true);
  1282. }
  1283. }
  1284. selected = which;
  1285. }
  1286. void CObjectListWindow::keyPressed(EShortcut key)
  1287. {
  1288. int sel = static_cast<int>(selected);
  1289. switch(key)
  1290. {
  1291. break; case EShortcut::MOVE_UP:
  1292. sel -=1;
  1293. break; case EShortcut::MOVE_DOWN:
  1294. sel +=1;
  1295. break; case EShortcut::MOVE_PAGE_UP:
  1296. sel -=9;
  1297. break; case EShortcut::MOVE_PAGE_DOWN:
  1298. sel +=9;
  1299. break; case EShortcut::MOVE_FIRST:
  1300. sel = 0;
  1301. break; case EShortcut::MOVE_LAST:
  1302. sel = static_cast<int>(items.size());
  1303. break; default:
  1304. return;
  1305. }
  1306. vstd::abetween<int>(sel, 0, items.size()-1);
  1307. list->scrollTo(sel);
  1308. changeSelection(sel);
  1309. }