GUIClasses.cpp 55 KB

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