GUIClasses.cpp 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934
  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 "../CMusicHandler.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../CVideoHandler.h"
  20. #include "../CServerHandler.h"
  21. #include "../battle/BattleInterfaceClasses.h"
  22. #include "../battle/BattleInterface.h"
  23. #include "../gui/CGuiHandler.h"
  24. #include "../gui/CursorHandler.h"
  25. #include "../gui/TextAlignment.h"
  26. #include "../gui/Shortcut.h"
  27. #include "../gui/WindowHandler.h"
  28. #include "../widgets/CComponent.h"
  29. #include "../widgets/MiscWidgets.h"
  30. #include "../widgets/CreatureCostBox.h"
  31. #include "../widgets/Buttons.h"
  32. #include "../widgets/Slider.h"
  33. #include "../widgets/TextControls.h"
  34. #include "../widgets/ObjectLists.h"
  35. #include "../lobby/CSavingScreen.h"
  36. #include "../render/Canvas.h"
  37. #include "../render/CAnimation.h"
  38. #include "../CMT.h"
  39. #include "../../CCallback.h"
  40. #include "../lib/mapObjectConstructors/AObjectTypeHandler.h"
  41. #include "../lib/mapObjectConstructors/CObjectClassesHandler.h"
  42. #include "../lib/mapObjectConstructors/CommonConstructors.h"
  43. #include "../lib/mapObjects/CGHeroInstance.h"
  44. #include "../lib/mapObjects/CGMarket.h"
  45. #include "../lib/ArtifactUtils.h"
  46. #include "../lib/mapObjects/CGTownInstance.h"
  47. #include "../lib/mapObjects/ObjectTemplate.h"
  48. #include "../lib/gameState/CGameState.h"
  49. #include "../lib/gameState/InfoAboutArmy.h"
  50. #include "../lib/gameState/SThievesGuildInfo.h"
  51. #include "../lib/CArtHandler.h"
  52. #include "../lib/CBuildingHandler.h"
  53. #include "../lib/CConfigHandler.h"
  54. #include "../lib/CCreatureHandler.h"
  55. #include "../lib/CGeneralTextHandler.h"
  56. #include "../lib/CHeroHandler.h"
  57. #include "../lib/CModHandler.h"
  58. #include "../lib/GameSettings.h"
  59. #include "../lib/CondSh.h"
  60. #include "../lib/CSkillHandler.h"
  61. #include "../lib/spells/CSpellHandler.h"
  62. #include "../lib/filesystem/Filesystem.h"
  63. #include "../lib/CStopWatch.h"
  64. #include "../lib/CTownHandler.h"
  65. #include "../lib/GameConstants.h"
  66. #include "../lib/bonuses/Bonus.h"
  67. #include "../lib/NetPacksBase.h"
  68. #include "../lib/StartInfo.h"
  69. #include "../lib/TextOperations.h"
  70. CRecruitmentWindow::CCreatureCard::CCreatureCard(CRecruitmentWindow * window, const CCreature * crea, int totalAmount)
  71. : CIntObject(LCLICK | SHOW_POPUP),
  72. parent(window),
  73. selected(false),
  74. creature(crea),
  75. amount(totalAmount)
  76. {
  77. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  78. animation = std::make_shared<CCreaturePic>(1, 1, creature, true, true);
  79. // 1 + 1 px for borders
  80. pos.w = animation->pos.w + 2;
  81. pos.h = animation->pos.h + 2;
  82. }
  83. void CRecruitmentWindow::CCreatureCard::select(bool on)
  84. {
  85. selected = on;
  86. redraw();
  87. }
  88. void CRecruitmentWindow::CCreatureCard::clickPressed(const Point & cursorPosition)
  89. {
  90. parent->select(this->shared_from_this());
  91. }
  92. void CRecruitmentWindow::CCreatureCard::showPopupWindow(const Point & cursorPosition)
  93. {
  94. GH.windows().createAndPushWindow<CStackWindow>(creature, true);
  95. }
  96. void CRecruitmentWindow::CCreatureCard::showAll(Canvas & to)
  97. {
  98. CIntObject::showAll(to);
  99. if(selected)
  100. to.drawBorder(pos, Colors::RED);
  101. else
  102. to.drawBorder(pos, Colors::YELLOW);
  103. }
  104. void CRecruitmentWindow::select(std::shared_ptr<CCreatureCard> card)
  105. {
  106. if(card == selected)
  107. return;
  108. if(selected)
  109. selected->select(false);
  110. selected = card;
  111. if(selected)
  112. selected->select(true);
  113. if(card)
  114. {
  115. si32 maxAmount = card->creature->maxAmount(LOCPLINT->cb->getResourceAmount());
  116. vstd::amin(maxAmount, card->amount);
  117. slider->setAmount(maxAmount);
  118. if(slider->getValue() != maxAmount)
  119. slider->scrollTo(maxAmount);
  120. else // if slider already at 0 - emulate call to sliderMoved()
  121. sliderMoved(maxAmount);
  122. costPerTroopValue->createItems(card->creature->getFullRecruitCost());
  123. totalCostValue->createItems(card->creature->getFullRecruitCost());
  124. costPerTroopValue->set(card->creature->getFullRecruitCost());
  125. totalCostValue->set(card->creature->getFullRecruitCost() * maxAmount);
  126. //Recruit %s
  127. title->setText(boost::str(boost::format(CGI->generaltexth->tcommands[21]) % card->creature->getNamePluralTranslated()));
  128. maxButton->block(maxAmount == 0);
  129. slider->block(maxAmount == 0);
  130. }
  131. }
  132. void CRecruitmentWindow::buy()
  133. {
  134. CreatureID crid = selected->creature->getId();
  135. SlotID dstslot = dst->getSlotFor(crid);
  136. if(!dstslot.validSlot() && (selected->creature->warMachine == ArtifactID::NONE)) //no available slot
  137. {
  138. std::pair<SlotID, SlotID> toMerge;
  139. bool allowMerge = CGI->settings()->getBoolean(EGameSettings::DWELLINGS_ACCUMULATE_WHEN_OWNED);
  140. if (allowMerge && dst->mergableStacks(toMerge))
  141. {
  142. LOCPLINT->cb->mergeStacks( dst, dst, toMerge.first, toMerge.second);
  143. }
  144. else
  145. {
  146. std::string txt;
  147. if(dst->ID == Obj::HERO)
  148. {
  149. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  150. boost::algorithm::replace_first(txt, "%s", slider->getValue() > 1 ? CGI->creh->objects[crid]->getNamePluralTranslated() : CGI->creh->objects[crid]->getNameSingularTranslated());
  151. }
  152. else
  153. {
  154. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  155. }
  156. LOCPLINT->showInfoDialog(txt);
  157. return;
  158. }
  159. }
  160. onRecruit(crid, slider->getValue());
  161. if(level >= 0)
  162. close();
  163. }
  164. void CRecruitmentWindow::showAll(Canvas & to)
  165. {
  166. CWindowObject::showAll(to);
  167. Rect(172, 222, 67, 42) + pos.topLeft();
  168. // recruit\total values
  169. to.drawBorder(Rect(172, 222, 67, 42) + pos.topLeft(), Colors::YELLOW);
  170. to.drawBorder(Rect(246, 222, 67, 42) + pos.topLeft(), Colors::YELLOW);
  171. //cost boxes
  172. to.drawBorder(Rect( 64, 222, 99, 76) + pos.topLeft(), Colors::YELLOW);
  173. to.drawBorder(Rect(322, 222, 99, 76) + pos.topLeft(), Colors::YELLOW);
  174. //buttons borders
  175. to.drawBorder(Rect(133, 312, 66, 34) + pos.topLeft(), Colors::METALLIC_GOLD);
  176. to.drawBorder(Rect(211, 312, 66, 34) + pos.topLeft(), Colors::METALLIC_GOLD);
  177. to.drawBorder(Rect(289, 312, 66, 34) + pos.topLeft(), Colors::METALLIC_GOLD);
  178. }
  179. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling * Dwelling, int Level, const CArmedInstance * Dst, const std::function<void(CreatureID,int)> & Recruit, int y_offset):
  180. CStatusbarWindow(PLAYER_COLORED, "TPRCRT"),
  181. onRecruit(Recruit),
  182. level(Level),
  183. dst(Dst),
  184. selected(nullptr),
  185. dwelling(Dwelling)
  186. {
  187. moveBy(Point(0, y_offset));
  188. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  189. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  190. slider = std::make_shared<CSlider>(Point(176, 279), 135, std::bind(&CRecruitmentWindow::sliderMoved, this, _1), 0, 0, 0, Orientation::HORIZONTAL);
  191. maxButton = std::make_shared<CButton>(Point(134, 313), "IRCBTNS.DEF", CGI->generaltexth->zelp[553], std::bind(&CSlider::scrollToMax, slider), EShortcut::RECRUITMENT_MAX);
  192. buyButton = std::make_shared<CButton>(Point(212, 313), "IBY6432.DEF", CGI->generaltexth->zelp[554], std::bind(&CRecruitmentWindow::buy, this), EShortcut::GLOBAL_ACCEPT);
  193. cancelButton = std::make_shared<CButton>(Point(290, 313), "ICN6432.DEF", CGI->generaltexth->zelp[555], std::bind(&CRecruitmentWindow::close, this), EShortcut::GLOBAL_CANCEL);
  194. title = std::make_shared<CLabel>(243, 32, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW);
  195. availableValue = std::make_shared<CLabel>(205, 253, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  196. toRecruitValue = std::make_shared<CLabel>(279, 253, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  197. costPerTroopValue = std::make_shared<CreatureCostBox>(Rect(65, 222, 97, 74), CGI->generaltexth->allTexts[346]);
  198. totalCostValue = std::make_shared<CreatureCostBox>(Rect(323, 222, 97, 74), CGI->generaltexth->allTexts[466]);
  199. availableTitle = std::make_shared<CLabel>(205, 233, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[465]);
  200. toRecruitTitle = std::make_shared<CLabel>(279, 233, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[16]);
  201. availableCreaturesChanged();
  202. }
  203. void CRecruitmentWindow::availableCreaturesChanged()
  204. {
  205. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  206. size_t selectedIndex = 0;
  207. if(!cards.empty() && selected) // find position of selected item
  208. selectedIndex = std::find(cards.begin(), cards.end(), selected) - cards.begin();
  209. select(nullptr);
  210. cards.clear();
  211. for(int i=0; i<dwelling->creatures.size(); i++)
  212. {
  213. //find appropriate level
  214. if(level >= 0 && i != level)
  215. continue;
  216. int amount = dwelling->creatures[i].first;
  217. //create new cards
  218. for(auto & creature : boost::adaptors::reverse(dwelling->creatures[i].second))
  219. cards.push_back(std::make_shared<CCreatureCard>(this, CGI->creh->objects[creature], amount));
  220. }
  221. const int creatureWidth = 102;
  222. //normal distance between cards - 18px
  223. int requiredSpace = 18;
  224. //maximum distance we can use without reaching window borders
  225. int availableSpace = pos.w - 50 - creatureWidth * (int)cards.size();
  226. if (cards.size() > 1) // avoid division by zero
  227. availableSpace /= (int)cards.size() - 1;
  228. else
  229. availableSpace = 0;
  230. assert(availableSpace >= 0);
  231. const int spaceBetween = std::min(requiredSpace, availableSpace);
  232. const int totalCreatureWidth = spaceBetween + creatureWidth;
  233. //now we know total amount of cards and can move them to correct position
  234. int curx = pos.w / 2 - (creatureWidth*(int)cards.size()/2) - (spaceBetween*((int)cards.size()-1)/2);
  235. for(auto & card : cards)
  236. {
  237. card->moveBy(Point(curx, 64));
  238. curx += totalCreatureWidth;
  239. }
  240. //restore selection
  241. select(cards[selectedIndex]);
  242. if(slider->getValue() == slider->getAmount())
  243. slider->scrollToMax();
  244. else // if slider already at 0 - emulate call to sliderMoved()
  245. sliderMoved(slider->getAmount());
  246. }
  247. void CRecruitmentWindow::sliderMoved(int to)
  248. {
  249. if(!selected)
  250. return;
  251. buyButton->block(!to);
  252. availableValue->setText(std::to_string(selected->amount - to));
  253. toRecruitValue->setText(std::to_string(to));
  254. totalCostValue->set(selected->creature->getFullRecruitCost() * to);
  255. }
  256. CSplitWindow::CSplitWindow(const CCreature * creature, std::function<void(int, int)> callback_, int leftMin_, int rightMin_, int leftAmount_, int rightAmount_)
  257. : CWindowObject(PLAYER_COLORED, "GPUCRDIV"),
  258. callback(callback_),
  259. leftAmount(leftAmount_),
  260. rightAmount(rightAmount_),
  261. leftMin(leftMin_),
  262. rightMin(rightMin_)
  263. {
  264. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  265. int total = leftAmount + rightAmount;
  266. int leftMax = total - rightMin;
  267. int rightMax = total - leftMin;
  268. ok = std::make_shared<CButton>(Point(20, 263), "IOK6432", CButton::tooltip(), std::bind(&CSplitWindow::apply, this), EShortcut::GLOBAL_ACCEPT);
  269. cancel = std::make_shared<CButton>(Point(214, 263), "ICN6432", CButton::tooltip(), std::bind(&CSplitWindow::close, this), EShortcut::GLOBAL_CANCEL);
  270. int sliderPosition = total - leftMin - rightMin;
  271. leftInput = std::make_shared<CTextInput>(Rect(20, 218, 100, 36), FONT_BIG, std::bind(&CSplitWindow::setAmountText, this, _1, true));
  272. rightInput = std::make_shared<CTextInput>(Rect(176, 218, 100, 36), FONT_BIG, std::bind(&CSplitWindow::setAmountText, this, _1, false));
  273. //add filters to allow only number input
  274. leftInput->filters += std::bind(&CTextInput::numberFilter, _1, _2, leftMin, leftMax);
  275. rightInput->filters += std::bind(&CTextInput::numberFilter, _1, _2, rightMin, rightMax);
  276. leftInput->setText(std::to_string(leftAmount), false);
  277. rightInput->setText(std::to_string(rightAmount), false);
  278. animLeft = std::make_shared<CCreaturePic>(20, 54, creature, true, false);
  279. animRight = std::make_shared<CCreaturePic>(177, 54,creature, true, false);
  280. slider = std::make_shared<CSlider>(Point(21, 194), 257, std::bind(&CSplitWindow::sliderMoved, this, _1), 0, sliderPosition, rightAmount - rightMin, Orientation::HORIZONTAL);
  281. std::string titleStr = CGI->generaltexth->allTexts[256];
  282. boost::algorithm::replace_first(titleStr,"%s", creature->getNamePluralTranslated());
  283. title = std::make_shared<CLabel>(150, 34, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, titleStr);
  284. }
  285. void CSplitWindow::setAmountText(std::string text, bool left)
  286. {
  287. int amount = 0;
  288. if(text.length())
  289. {
  290. try
  291. {
  292. amount = boost::lexical_cast<int>(text);
  293. }
  294. catch(boost::bad_lexical_cast &)
  295. {
  296. amount = left ? leftAmount : rightAmount;
  297. }
  298. int total = leftAmount + rightAmount;
  299. if(amount > total)
  300. amount = total;
  301. }
  302. setAmount(amount, left);
  303. slider->scrollTo(rightAmount - rightMin);
  304. }
  305. void CSplitWindow::setAmount(int value, bool left)
  306. {
  307. int total = leftAmount + rightAmount;
  308. leftAmount = left ? value : total - value;
  309. rightAmount = left ? total - value : value;
  310. leftInput->setText(std::to_string(leftAmount));
  311. rightInput->setText(std::to_string(rightAmount));
  312. }
  313. void CSplitWindow::apply()
  314. {
  315. callback(leftAmount, rightAmount);
  316. close();
  317. }
  318. void CSplitWindow::sliderMoved(int to)
  319. {
  320. setAmount(rightMin + to, false);
  321. }
  322. CLevelWindow::CLevelWindow(const CGHeroInstance * hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> & skills, std::function<void(ui32)> callback)
  323. : CWindowObject(PLAYER_COLORED, "LVLUPBKG"),
  324. cb(callback)
  325. {
  326. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  327. LOCPLINT->showingDialog->setn(true);
  328. if(!skills.empty())
  329. {
  330. std::vector<std::shared_ptr<CSelectableComponent>> comps;
  331. for(auto & skill : skills)
  332. {
  333. auto comp = std::make_shared<CSelectableComponent>(CComponent::secskill, skill, hero->getSecSkillLevel(SecondarySkill(skill))+1, CComponent::medium);
  334. comps.push_back(comp);
  335. }
  336. box = std::make_shared<CComponentBox>(comps, Rect(75, 300, pos.w - 150, 100));
  337. }
  338. portrait = std::make_shared<CAnimImage>("PortraitsLarge", hero->portrait, 0, 170, 66);
  339. ok = std::make_shared<CButton>(Point(297, 413), "IOKAY", CButton::tooltip(), std::bind(&CLevelWindow::close, this), EShortcut::GLOBAL_ACCEPT);
  340. //%s has gained a level.
  341. mainTitle = std::make_shared<CLabel>(192, 33, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, boost::str(boost::format(CGI->generaltexth->allTexts[444]) % hero->getNameTranslated()));
  342. //%s is now a level %d %s.
  343. std::string levelTitleText = CGI->generaltexth->translate("core.genrltxt.445");
  344. boost::replace_first(levelTitleText, "%s", hero->getNameTranslated());
  345. boost::replace_first(levelTitleText, "%d", std::to_string(hero->level));
  346. boost::replace_first(levelTitleText, "%s", hero->type->heroClass->getNameTranslated());
  347. levelTitle = std::make_shared<CLabel>(192, 162, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, levelTitleText);
  348. skillIcon = std::make_shared<CAnimImage>("PSKIL42", pskill, 0, 174, 190);
  349. skillValue = std::make_shared<CLabel>(192, 253, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->primarySkillNames[pskill] + " +1");
  350. }
  351. CLevelWindow::~CLevelWindow()
  352. {
  353. //FIXME: call callback if there was nothing to select?
  354. if (box && box->selectedIndex() != -1)
  355. cb(box->selectedIndex());
  356. LOCPLINT->showingDialog->setn(false);
  357. }
  358. CTavernWindow::CTavernWindow(const CGObjectInstance * TavernObj)
  359. : CStatusbarWindow(PLAYER_COLORED, "TPTAVERN"),
  360. tavernObj(TavernObj)
  361. {
  362. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  363. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(TavernObj);
  364. if(h.size() < 2)
  365. h.resize(2, nullptr);
  366. selected = 0;
  367. if(!h[0])
  368. selected = 1;
  369. if(!h[0] && !h[1])
  370. selected = -1;
  371. oldSelected = -1;
  372. h1 = std::make_shared<HeroPortrait>(selected, 0, 72, 299, h[0]);
  373. h2 = std::make_shared<HeroPortrait>(selected, 1, 162, 299, h[1]);
  374. title = std::make_shared<CLabel>(197, 32, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[37]);
  375. cost = std::make_shared<CLabel>(320, 328, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(GameConstants::HERO_GOLD_COST));
  376. heroDescription = std::make_shared<CTextBox>("", Rect(30, 373, 233, 35), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  377. heroesForHire = std::make_shared<CLabel>(145, 283, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[38]);
  378. auto rumorText = boost::str(boost::format(CGI->generaltexth->allTexts[216]) % LOCPLINT->cb->getTavernRumor(tavernObj));
  379. rumor = std::make_shared<CTextBox>(rumorText, Rect(32, 188, 330, 66), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  380. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  381. cancel = std::make_shared<CButton>(Point(310, 428), "ICANCEL.DEF", CButton::tooltip(CGI->generaltexth->tavernInfo[7]), std::bind(&CTavernWindow::close, this), EShortcut::GLOBAL_CANCEL);
  382. recruit = std::make_shared<CButton>(Point(272, 355), "TPTAV01.DEF", CButton::tooltip(), std::bind(&CTavernWindow::recruitb, this), EShortcut::GLOBAL_ACCEPT);
  383. thiefGuild = std::make_shared<CButton>(Point(22, 428), "TPTAV02.DEF", CButton::tooltip(CGI->generaltexth->tavernInfo[5]), std::bind(&CTavernWindow::thievesguildb, this), EShortcut::ADVENTURE_THIEVES_GUILD);
  384. if(LOCPLINT->cb->getResourceAmount(EGameResID::GOLD) < GameConstants::HERO_GOLD_COST) //not enough gold
  385. {
  386. recruit->addHoverText(CButton::NORMAL, CGI->generaltexth->tavernInfo[0]); //Cannot afford a Hero
  387. recruit->block(true);
  388. }
  389. else if(LOCPLINT->cb->howManyHeroes(true) >= CGI->settings()->getInteger(EGameSettings::HEROES_PER_PLAYER_TOTAL_CAP))
  390. {
  391. //Cannot recruit. You already have %d Heroes.
  392. recruit->addHoverText(CButton::NORMAL, boost::str(boost::format(CGI->generaltexth->tavernInfo[1]) % LOCPLINT->cb->howManyHeroes(true)));
  393. recruit->block(true);
  394. }
  395. else if(LOCPLINT->cb->howManyHeroes(false) >= CGI->settings()->getInteger(EGameSettings::HEROES_PER_PLAYER_ON_MAP_CAP))
  396. {
  397. //Cannot recruit. You already have %d Heroes.
  398. recruit->addHoverText(CButton::NORMAL, boost::str(boost::format(CGI->generaltexth->tavernInfo[1]) % LOCPLINT->cb->howManyHeroes(false)));
  399. recruit->block(true);
  400. }
  401. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  402. {
  403. recruit->addHoverText(CButton::NORMAL, CGI->generaltexth->tavernInfo[2]); //Cannot recruit. You already have a Hero in this town.
  404. recruit->block(true);
  405. }
  406. else
  407. {
  408. if(selected == -1)
  409. recruit->block(true);
  410. }
  411. if(LOCPLINT->castleInt)
  412. CCS->videoh->open(LOCPLINT->castleInt->town->town->clientInfo.tavernVideo);
  413. else
  414. CCS->videoh->open("TAVERN.BIK");
  415. }
  416. void CTavernWindow::recruitb()
  417. {
  418. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  419. const CGObjectInstance *obj = tavernObj;
  420. close();
  421. LOCPLINT->cb->recruitHero(obj, toBuy);
  422. }
  423. void CTavernWindow::thievesguildb()
  424. {
  425. GH.windows().createAndPushWindow<CThievesGuildWindow>(tavernObj);
  426. }
  427. CTavernWindow::~CTavernWindow()
  428. {
  429. CCS->videoh->close();
  430. }
  431. void CTavernWindow::show(Canvas & to)
  432. {
  433. CWindowObject::show(to);
  434. if(selected >= 0)
  435. {
  436. auto sel = selected ? h2 : h1;
  437. if(selected != oldSelected)
  438. {
  439. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  440. oldSelected = selected;
  441. heroDescription->setText(sel->description);
  442. //Recruit %s the %s
  443. if (!recruit->isBlocked())
  444. recruit->addHoverText(CButton::NORMAL, boost::str(boost::format(CGI->generaltexth->tavernInfo[3]) % sel->h->getNameTranslated() % sel->h->type->heroClass->getNameTranslated()));
  445. }
  446. to.drawBorder(Rect::createAround(sel->pos, 2), Colors::BRIGHT_YELLOW, 2);
  447. }
  448. CCS->videoh->update(pos.x+70, pos.y+56, to.getInternalSurface(), true, false);
  449. }
  450. void CTavernWindow::HeroPortrait::clickPressed(const Point & cursorPosition)
  451. {
  452. if(h)
  453. *_sel = _id;
  454. }
  455. void CTavernWindow::HeroPortrait::showPopupWindow(const Point & cursorPosition)
  456. {
  457. if(h)
  458. GH.windows().createAndPushWindow<CRClickPopupInt>(std::make_shared<CHeroWindow>(h));
  459. }
  460. CTavernWindow::HeroPortrait::HeroPortrait(int & sel, int id, int x, int y, const CGHeroInstance * H)
  461. : CIntObject(LCLICK | SHOW_POPUP | HOVER),
  462. h(H), _sel(&sel), _id(id)
  463. {
  464. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  465. h = H;
  466. pos.x += x;
  467. pos.y += y;
  468. pos.w = 58;
  469. pos.h = 64;
  470. if(H)
  471. {
  472. hoverName = CGI->generaltexth->tavernInfo[4];
  473. boost::algorithm::replace_first(hoverName,"%s",H->getNameTranslated());
  474. int artifs = (int)h->artifactsWorn.size() + (int)h->artifactsInBackpack.size();
  475. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  476. if(vstd::contains(h->artifactsWorn, ArtifactPosition(i)))
  477. artifs--;
  478. description = CGI->generaltexth->allTexts[215];
  479. boost::algorithm::replace_first(description, "%s", h->getNameTranslated());
  480. boost::algorithm::replace_first(description, "%d", std::to_string(h->level));
  481. boost::algorithm::replace_first(description, "%s", h->type->heroClass->getNameTranslated());
  482. boost::algorithm::replace_first(description, "%d", std::to_string(artifs));
  483. portrait = std::make_shared<CAnimImage>("portraitsLarge", h->portrait);
  484. }
  485. }
  486. void CTavernWindow::HeroPortrait::hover(bool on)
  487. {
  488. //Hoverable::hover(on);
  489. if(on)
  490. GH.statusbar()->write(hoverName);
  491. else
  492. GH.statusbar()->clear();
  493. }
  494. static const std::string QUICK_EXCHANGE_MOD_PREFIX = "quick-exchange";
  495. static const std::string QUICK_EXCHANGE_BG = QUICK_EXCHANGE_MOD_PREFIX + "/TRADEQE";
  496. static bool isQuickExchangeLayoutAvailable()
  497. {
  498. return CResourceHandler::get()->existsResource(ResourceID(std::string("SPRITES/") + QUICK_EXCHANGE_BG, EResType::IMAGE));
  499. }
  500. // Runs a task asynchronously with gamestate locking and waitTillRealize set to true
  501. class GsThread
  502. {
  503. private:
  504. std::function<void()> action;
  505. std::shared_ptr<CCallback> cb;
  506. public:
  507. static void run(std::function<void()> action)
  508. {
  509. std::shared_ptr<GsThread> instance(new GsThread(action));
  510. boost::thread(std::bind(&GsThread::staticRun, instance));
  511. }
  512. private:
  513. GsThread(std::function<void()> action)
  514. :action(action), cb(LOCPLINT->cb)
  515. {
  516. }
  517. static void staticRun(std::shared_ptr<GsThread> instance)
  518. {
  519. instance->run();
  520. }
  521. void run()
  522. {
  523. boost::shared_lock<boost::shared_mutex> gsLock(CGameState::mutex);
  524. auto originalWaitTillRealize = cb->waitTillRealize;
  525. auto originalUnlockGsWhenWating = cb->unlockGsWhenWaiting;
  526. cb->waitTillRealize = true;
  527. cb->unlockGsWhenWaiting = true;
  528. action();
  529. cb->waitTillRealize = originalWaitTillRealize;
  530. cb->unlockGsWhenWaiting = originalUnlockGsWhenWating;
  531. }
  532. };
  533. CExchangeController::CExchangeController(CExchangeWindow * view, ObjectInstanceID hero1, ObjectInstanceID hero2)
  534. :left(LOCPLINT->cb->getHero(hero1)), right(LOCPLINT->cb->getHero(hero2)), cb(LOCPLINT->cb), view(view)
  535. {
  536. }
  537. std::function<void()> CExchangeController::onMoveArmyToLeft()
  538. {
  539. return [&]() { moveArmy(false); };
  540. }
  541. std::function<void()> CExchangeController::onMoveArmyToRight()
  542. {
  543. return [&]() { moveArmy(true); };
  544. }
  545. std::vector<CArtifactInstance *> getBackpackArts(const CGHeroInstance * hero)
  546. {
  547. std::vector<CArtifactInstance *> result;
  548. for(auto slot : hero->artifactsInBackpack)
  549. {
  550. result.push_back(slot.artifact);
  551. }
  552. return result;
  553. }
  554. std::function<void()> CExchangeController::onSwapArtifacts()
  555. {
  556. return [&]()
  557. {
  558. GsThread::run([=]
  559. {
  560. cb->bulkMoveArtifacts(left->id, right->id, true);
  561. });
  562. };
  563. }
  564. std::function<void()> CExchangeController::onMoveArtifactsToLeft()
  565. {
  566. return [&]() { moveArtifacts(false); };
  567. }
  568. std::function<void()> CExchangeController::onMoveArtifactsToRight()
  569. {
  570. return [&]() { moveArtifacts(true); };
  571. }
  572. std::vector<std::pair<SlotID, CStackInstance *>> getStacks(const CArmedInstance * source)
  573. {
  574. auto slots = source->Slots();
  575. return std::vector<std::pair<SlotID, CStackInstance *>>(slots.begin(), slots.end());
  576. }
  577. std::function<void()> CExchangeController::onSwapArmy()
  578. {
  579. return [&]()
  580. {
  581. GsThread::run([=]
  582. {
  583. if(left->tempOwner != cb->getMyColor()
  584. || right->tempOwner != cb->getMyColor())
  585. {
  586. return;
  587. }
  588. auto leftSlots = getStacks(left);
  589. auto rightSlots = getStacks(right);
  590. auto i = leftSlots.begin(), j = rightSlots.begin();
  591. for(; i != leftSlots.end() && j != rightSlots.end(); i++, j++)
  592. {
  593. cb->swapCreatures(left, right, i->first, j->first);
  594. }
  595. if(i != leftSlots.end())
  596. {
  597. for(; i != leftSlots.end(); i++)
  598. {
  599. cb->swapCreatures(left, right, i->first, right->getFreeSlot());
  600. }
  601. }
  602. else if(j != rightSlots.end())
  603. {
  604. for(; j != rightSlots.end(); j++)
  605. {
  606. cb->swapCreatures(left, right, left->getFreeSlot(), j->first);
  607. }
  608. }
  609. });
  610. };
  611. }
  612. std::function<void()> CExchangeController::onMoveStackToLeft(SlotID slotID)
  613. {
  614. return [=]()
  615. {
  616. if(right->tempOwner != cb->getMyColor())
  617. {
  618. return;
  619. }
  620. moveStack(right, left, slotID);
  621. };
  622. }
  623. std::function<void()> CExchangeController::onMoveStackToRight(SlotID slotID)
  624. {
  625. return [=]()
  626. {
  627. if(left->tempOwner != cb->getMyColor())
  628. {
  629. return;
  630. }
  631. moveStack(left, right, slotID);
  632. };
  633. }
  634. void CExchangeController::moveStack(
  635. const CGHeroInstance * source,
  636. const CGHeroInstance * target,
  637. SlotID sourceSlot)
  638. {
  639. auto creature = source->getCreature(sourceSlot);
  640. SlotID targetSlot = target->getSlotFor(creature);
  641. if(targetSlot.validSlot())
  642. {
  643. if(source->stacksCount() == 1 && source->needsLastStack())
  644. {
  645. cb->splitStack(
  646. source,
  647. target,
  648. sourceSlot,
  649. targetSlot,
  650. target->getStackCount(targetSlot) + source->getStackCount(sourceSlot) - 1);
  651. }
  652. else
  653. {
  654. cb->mergeOrSwapStacks(source, target, sourceSlot, targetSlot);
  655. }
  656. }
  657. }
  658. void CExchangeController::moveArmy(bool leftToRight)
  659. {
  660. const CGHeroInstance * source = leftToRight ? left : right;
  661. const CGHeroInstance * target = leftToRight ? right : left;
  662. const CGarrisonSlot * selection = this->view->getSelectedSlotID();
  663. SlotID slot;
  664. if(source->tempOwner != cb->getMyColor())
  665. {
  666. return;
  667. }
  668. if(selection && selection->our() && selection->getObj() == source)
  669. {
  670. slot = selection->getSlot();
  671. }
  672. else
  673. {
  674. auto weakestSlot = vstd::minElementByFun(
  675. source->Slots(),
  676. [](const std::pair<SlotID, CStackInstance *> & s) -> int
  677. {
  678. return s.second->getCreatureID().toCreature()->getAIValue();
  679. });
  680. slot = weakestSlot->first;
  681. }
  682. cb->bulkMoveArmy(source->id, target->id, slot);
  683. }
  684. void CExchangeController::moveArtifacts(bool leftToRight)
  685. {
  686. const CGHeroInstance * source = leftToRight ? left : right;
  687. const CGHeroInstance * target = leftToRight ? right : left;
  688. if(source->tempOwner != cb->getMyColor())
  689. {
  690. return;
  691. }
  692. GsThread::run([=]
  693. {
  694. cb->bulkMoveArtifacts(source->id, target->id, false);
  695. });
  696. }
  697. void CExchangeController::moveArtifact(
  698. const CGHeroInstance * source,
  699. const CGHeroInstance * target,
  700. ArtifactPosition srcPosition)
  701. {
  702. auto srcLocation = ArtifactLocation(source, srcPosition);
  703. auto dstLocation = ArtifactLocation(target,
  704. ArtifactUtils::getArtAnyPosition(target, source->getArt(srcPosition)->getTypeId()));
  705. cb->swapArtifacts(srcLocation, dstLocation);
  706. }
  707. CExchangeWindow::CExchangeWindow(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID queryID)
  708. : CStatusbarWindow(PLAYER_COLORED | BORDERED, isQuickExchangeLayoutAvailable() ? QUICK_EXCHANGE_BG : "TRADE2"),
  709. controller(this, hero1, hero2),
  710. moveStackLeftButtons(),
  711. moveStackRightButtons()
  712. {
  713. const bool qeLayout = isQuickExchangeLayoutAvailable();
  714. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  715. heroInst[0] = LOCPLINT->cb->getHero(hero1);
  716. heroInst[1] = LOCPLINT->cb->getHero(hero2);
  717. auto genTitle = [](const CGHeroInstance * h)
  718. {
  719. boost::format fmt(CGI->generaltexth->allTexts[138]);
  720. fmt % h->getNameTranslated() % h->level % h->type->heroClass->getNameTranslated();
  721. return boost::str(fmt);
  722. };
  723. titles[0] = std::make_shared<CLabel>(147, 25, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, genTitle(heroInst[0]));
  724. titles[1] = std::make_shared<CLabel>(653, 25, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, genTitle(heroInst[1]));
  725. auto PSKIL32 = std::make_shared<CAnimation>("PSKIL32");
  726. PSKIL32->preload();
  727. auto SECSK32 = std::make_shared<CAnimation>("SECSK32");
  728. for(int g = 0; g < 4; ++g)
  729. {
  730. if (qeLayout)
  731. primSkillImages.push_back(std::make_shared<CAnimImage>(PSKIL32, g, Rect(389, 12 + 26 * g, 22, 22)));
  732. else
  733. primSkillImages.push_back(std::make_shared<CAnimImage>(PSKIL32, g, 0, 385, 19 + 36 * g));
  734. }
  735. for(int leftRight : {0, 1})
  736. {
  737. const CGHeroInstance * hero = heroInst.at(leftRight);
  738. for(int m=0; m<GameConstants::PRIMARY_SKILLS; ++m)
  739. primSkillValues[leftRight].push_back(std::make_shared<CLabel>(352 + (qeLayout ? 96 : 93) * leftRight, (qeLayout ? 22 : 35) + (qeLayout ? 26 : 36) * m, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE));
  740. for(int m=0; m < hero->secSkills.size(); ++m)
  741. secSkillIcons[leftRight].push_back(std::make_shared<CAnimImage>(SECSK32, 0, 0, 32 + 36 * m + 454 * leftRight, qeLayout ? 83 : 88));
  742. specImages[leftRight] = std::make_shared<CAnimImage>("UN32", hero->type->imageIndex, 0, 67 + 490 * leftRight, qeLayout ? 41 : 45);
  743. expImages[leftRight] = std::make_shared<CAnimImage>(PSKIL32, 4, 0, 103 + 490 * leftRight, qeLayout ? 41 : 45);
  744. expValues[leftRight] = std::make_shared<CLabel>(119 + 490 * leftRight, qeLayout ? 66 : 71, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  745. manaImages[leftRight] = std::make_shared<CAnimImage>(PSKIL32, 5, 0, 139 + 490 * leftRight, qeLayout ? 41 : 45);
  746. manaValues[leftRight] = std::make_shared<CLabel>(155 + 490 * leftRight, qeLayout ? 66 : 71, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  747. }
  748. portraits[0] = std::make_shared<CAnimImage>("PortraitsLarge", heroInst[0]->portrait, 0, 257, 13);
  749. portraits[1] = std::make_shared<CAnimImage>("PortraitsLarge", heroInst[1]->portrait, 0, 485, 13);
  750. artifs[0] = std::make_shared<CArtifactsOfHeroMain>(Point(-334, 150));
  751. artifs[0]->setHero(heroInst[0]);
  752. artifs[1] = std::make_shared<CArtifactsOfHeroMain>(Point(98, 150));
  753. artifs[1]->setHero(heroInst[1]);
  754. addSet(artifs[0]);
  755. addSet(artifs[1]);
  756. for(int g=0; g<4; ++g)
  757. {
  758. primSkillAreas.push_back(std::make_shared<LRClickableAreaWTextComp>());
  759. if (qeLayout)
  760. primSkillAreas[g]->pos = Rect(Point(pos.x + 324, pos.y + 12 + 26 * g), Point(152, 22));
  761. else
  762. primSkillAreas[g]->pos = Rect(Point(pos.x + 329, pos.y + 19 + 36 * g), Point(140, 32));
  763. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  764. primSkillAreas[g]->type = g;
  765. primSkillAreas[g]->bonusValue = 0;
  766. primSkillAreas[g]->baseType = 0;
  767. primSkillAreas[g]->hoverText = CGI->generaltexth->heroscrn[1];
  768. boost::replace_first(primSkillAreas[g]->hoverText, "%s", CGI->generaltexth->primarySkillNames[g]);
  769. }
  770. //heroes related thing
  771. for(int b=0; b < heroInst.size(); b++)
  772. {
  773. const CGHeroInstance * hero = heroInst.at(b);
  774. //secondary skill's clickable areas
  775. for(int g=0; g<hero->secSkills.size(); ++g)
  776. {
  777. int skill = hero->secSkills[g].first,
  778. level = hero->secSkills[g].second; // <1, 3>
  779. secSkillAreas[b].push_back(std::make_shared<LRClickableAreaWTextComp>());
  780. secSkillAreas[b][g]->pos = Rect(Point(pos.x + 32 + g * 36 + b * 454 , pos.y + (qeLayout ? 83 : 88)), Point(32, 32) );
  781. secSkillAreas[b][g]->baseType = 1;
  782. secSkillAreas[b][g]->type = skill;
  783. secSkillAreas[b][g]->bonusValue = level;
  784. secSkillAreas[b][g]->text = CGI->skillh->getByIndex(skill)->getDescriptionTranslated(level);
  785. secSkillAreas[b][g]->hoverText = CGI->generaltexth->heroscrn[21];
  786. boost::algorithm::replace_first(secSkillAreas[b][g]->hoverText, "%s", CGI->generaltexth->levels[level - 1]);
  787. boost::algorithm::replace_first(secSkillAreas[b][g]->hoverText, "%s", CGI->skillh->getByIndex(skill)->getNameTranslated());
  788. }
  789. heroAreas[b] = std::make_shared<CHeroArea>(257 + 228*b, 13, hero);
  790. specialtyAreas[b] = std::make_shared<LRClickableAreaWText>();
  791. specialtyAreas[b]->pos = Rect(Point(pos.x + 69 + 490 * b, pos.y + (qeLayout ? 41 : 45)), Point(32, 32));
  792. specialtyAreas[b]->hoverText = CGI->generaltexth->heroscrn[27];
  793. specialtyAreas[b]->text = hero->type->getSpecialtyDescriptionTranslated();
  794. experienceAreas[b] = std::make_shared<LRClickableAreaWText>();
  795. experienceAreas[b]->pos = Rect(Point(pos.x + 105 + 490 * b, pos.y + (qeLayout ? 41 : 45)), Point(32, 32));
  796. experienceAreas[b]->hoverText = CGI->generaltexth->heroscrn[9];
  797. experienceAreas[b]->text = CGI->generaltexth->allTexts[2];
  798. boost::algorithm::replace_first(experienceAreas[b]->text, "%d", std::to_string(hero->level));
  799. boost::algorithm::replace_first(experienceAreas[b]->text, "%d", std::to_string(CGI->heroh->reqExp(hero->level+1)));
  800. boost::algorithm::replace_first(experienceAreas[b]->text, "%d", std::to_string(hero->exp));
  801. spellPointsAreas[b] = std::make_shared<LRClickableAreaWText>();
  802. spellPointsAreas[b]->pos = Rect(Point(pos.x + 141 + 490 * b, pos.y + (qeLayout ? 41 : 45)), Point(32, 32));
  803. spellPointsAreas[b]->hoverText = CGI->generaltexth->heroscrn[22];
  804. spellPointsAreas[b]->text = CGI->generaltexth->allTexts[205];
  805. boost::algorithm::replace_first(spellPointsAreas[b]->text, "%s", hero->getNameTranslated());
  806. boost::algorithm::replace_first(spellPointsAreas[b]->text, "%d", std::to_string(hero->mana));
  807. boost::algorithm::replace_first(spellPointsAreas[b]->text, "%d", std::to_string(hero->manaLimit()));
  808. morale[b] = std::make_shared<MoraleLuckBox>(true, Rect(Point(176 + 490 * b, 39), Point(32, 32)), true);
  809. luck[b] = std::make_shared<MoraleLuckBox>(false, Rect(Point(212 + 490 * b, 39), Point(32, 32)), true);
  810. }
  811. quit = std::make_shared<CButton>(Point(732, 567), "IOKAY.DEF", CGI->generaltexth->zelp[600], std::bind(&CExchangeWindow::close, this), EShortcut::GLOBAL_ACCEPT);
  812. if(queryID.getNum() > 0)
  813. quit->addCallback([=](){ LOCPLINT->cb->selectionMade(0, queryID); });
  814. questlogButton[0] = std::make_shared<CButton>(Point( 10, qeLayout ? 39 : 44), "hsbtns4.def", CButton::tooltip(CGI->generaltexth->heroscrn[0]), std::bind(&CExchangeWindow::questlog, this, 0));
  815. questlogButton[1] = std::make_shared<CButton>(Point(740, qeLayout ? 39 : 44), "hsbtns4.def", CButton::tooltip(CGI->generaltexth->heroscrn[0]), std::bind(&CExchangeWindow::questlog, this, 1));
  816. Rect barRect(5, 578, 725, 18);
  817. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), barRect, 5, 578));
  818. //garrison interface
  819. garr = std::make_shared<CGarrisonInt>(69, qeLayout ? 122 : 131, 4, Point(418,0), heroInst[0], heroInst[1], true, true);
  820. auto splitButtonCallback = [&](){ garr->splitClick(); };
  821. garr->addSplitBtn(std::make_shared<CButton>( Point( 10, qeLayout ? 122 : 132), "TSBTNS.DEF", CButton::tooltip(CGI->generaltexth->tcommands[3]), splitButtonCallback));
  822. garr->addSplitBtn(std::make_shared<CButton>( Point(744, qeLayout ? 122 : 132), "TSBTNS.DEF", CButton::tooltip(CGI->generaltexth->tcommands[3]), splitButtonCallback));
  823. if(qeLayout)
  824. {
  825. moveAllGarrButtonLeft = std::make_shared<CButton>(Point(325, 118), QUICK_EXCHANGE_MOD_PREFIX + "/armRight.DEF", CButton::tooltip(CGI->generaltexth->qeModCommands[1]), controller.onMoveArmyToRight());
  826. echangeGarrButton = std::make_shared<CButton>(Point(377, 118), QUICK_EXCHANGE_MOD_PREFIX + "/swapAll.DEF", CButton::tooltip(CGI->generaltexth->qeModCommands[2]), controller.onSwapArmy());
  827. moveAllGarrButtonRight = std::make_shared<CButton>(Point(425, 118), QUICK_EXCHANGE_MOD_PREFIX + "/armLeft.DEF", CButton::tooltip(CGI->generaltexth->qeModCommands[1]), controller.onMoveArmyToLeft());
  828. moveArtifactsButtonLeft = std::make_shared<CButton>(Point(325, 154), QUICK_EXCHANGE_MOD_PREFIX + "/artRight.DEF", CButton::tooltip(CGI->generaltexth->qeModCommands[3]), controller.onMoveArtifactsToRight());
  829. echangeArtifactsButton = std::make_shared<CButton>(Point(377, 154), QUICK_EXCHANGE_MOD_PREFIX + "/swapAll.DEF", CButton::tooltip(CGI->generaltexth->qeModCommands[4]), controller.onSwapArtifacts());
  830. moveArtifactsButtonRight = std::make_shared<CButton>(Point(425, 154), QUICK_EXCHANGE_MOD_PREFIX + "/artLeft.DEF", CButton::tooltip(CGI->generaltexth->qeModCommands[3]), controller.onMoveArtifactsToLeft());
  831. for(int i = 0; i < GameConstants::ARMY_SIZE; i++)
  832. {
  833. moveStackLeftButtons.push_back(
  834. std::make_shared<CButton>(
  835. Point(484 + 35 * i, 154),
  836. QUICK_EXCHANGE_MOD_PREFIX + "/unitLeft.DEF",
  837. CButton::tooltip(CGI->generaltexth->qeModCommands[1]),
  838. controller.onMoveStackToLeft(SlotID(i))));
  839. moveStackRightButtons.push_back(
  840. std::make_shared<CButton>(
  841. Point(66 + 35 * i, 154),
  842. QUICK_EXCHANGE_MOD_PREFIX + "/unitRight.DEF",
  843. CButton::tooltip(CGI->generaltexth->qeModCommands[1]),
  844. controller.onMoveStackToRight(SlotID(i))));
  845. }
  846. }
  847. updateWidgets();
  848. }
  849. const CGarrisonSlot * CExchangeWindow::getSelectedSlotID() const
  850. {
  851. return garr->getSelection();
  852. }
  853. void CExchangeWindow::updateGarrisons()
  854. {
  855. garr->recreateSlots();
  856. updateWidgets();
  857. }
  858. void CExchangeWindow::questlog(int whichHero)
  859. {
  860. CCS->curh->dragAndDropCursor(nullptr);
  861. LOCPLINT->showQuestLog();
  862. }
  863. void CExchangeWindow::updateWidgets()
  864. {
  865. for(size_t leftRight : {0, 1})
  866. {
  867. const CGHeroInstance * hero = heroInst.at(leftRight);
  868. for(int m=0; m<GameConstants::PRIMARY_SKILLS; ++m)
  869. {
  870. auto value = heroInst[leftRight]->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(m));
  871. primSkillValues[leftRight][m]->setText(std::to_string(value));
  872. }
  873. for(int m=0; m < hero->secSkills.size(); ++m)
  874. {
  875. int id = hero->secSkills[m].first;
  876. int level = hero->secSkills[m].second;
  877. secSkillIcons[leftRight][m]->setFrame(2 + id * 3 + level);
  878. }
  879. expValues[leftRight]->setText(TextOperations::formatMetric(hero->exp, 3));
  880. manaValues[leftRight]->setText(TextOperations::formatMetric(hero->mana, 3));
  881. morale[leftRight]->set(hero);
  882. luck[leftRight]->set(hero);
  883. }
  884. }
  885. CShipyardWindow::CShipyardWindow(const TResources & cost, int state, BoatId boatType, const std::function<void()> & onBuy)
  886. : CStatusbarWindow(PLAYER_COLORED, "TPSHIP")
  887. {
  888. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  889. bgWater = std::make_shared<CPicture>("TPSHIPBK", 100, 69);
  890. auto handler = CGI->objtypeh->getHandlerFor(Obj::BOAT, boatType);
  891. auto boatConstructor = std::dynamic_pointer_cast<const BoatInstanceConstructor>(handler);
  892. assert(boatConstructor);
  893. if (boatConstructor)
  894. {
  895. std::string boatFilename = boatConstructor->getBoatAnimationName();
  896. Point waterCenter = Point(bgWater->pos.x+bgWater->pos.w/2, bgWater->pos.y+bgWater->pos.h/2);
  897. bgShip = std::make_shared<CAnimImage>(boatFilename, 0, 7, 120, 96, 0);
  898. bgShip->center(waterCenter);
  899. }
  900. // Create resource icons and costs.
  901. std::string goldValue = std::to_string(cost[EGameResID::GOLD]);
  902. std::string woodValue = std::to_string(cost[EGameResID::WOOD]);
  903. goldCost = std::make_shared<CLabel>(118, 294, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, goldValue);
  904. woodCost = std::make_shared<CLabel>(212, 294, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, woodValue);
  905. goldPic = std::make_shared<CAnimImage>("RESOURCE",GameResID(EGameResID::GOLD), 0, 100, 244);
  906. woodPic = std::make_shared<CAnimImage>("RESOURCE", GameResID(EGameResID::WOOD), 0, 196, 244);
  907. quit = std::make_shared<CButton>(Point(224, 312), "ICANCEL", CButton::tooltip(CGI->generaltexth->allTexts[599]), std::bind(&CShipyardWindow::close, this), EShortcut::GLOBAL_CANCEL);
  908. build = std::make_shared<CButton>(Point(42, 312), "IBUY30", CButton::tooltip(CGI->generaltexth->allTexts[598]), std::bind(&CShipyardWindow::close, this), EShortcut::GLOBAL_ACCEPT);
  909. build->addCallback(onBuy);
  910. for(GameResID i = EGameResID::WOOD; i <= EGameResID::GOLD; ++i)
  911. {
  912. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  913. {
  914. build->block(true);
  915. break;
  916. }
  917. }
  918. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  919. title = std::make_shared<CLabel>(164, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[13]);
  920. costLabel = std::make_shared<CLabel>(164, 220, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->jktexts[14]);
  921. }
  922. void CTransformerWindow::CItem::move()
  923. {
  924. if(left)
  925. moveBy(Point(289, 0));
  926. else
  927. moveBy(Point(-289, 0));
  928. left = !left;
  929. }
  930. void CTransformerWindow::CItem::clickPressed(const Point & cursorPosition)
  931. {
  932. move();
  933. parent->redraw();
  934. }
  935. void CTransformerWindow::CItem::update()
  936. {
  937. icon->setFrame(parent->army->getCreature(SlotID(id))->getId() + 2);
  938. }
  939. CTransformerWindow::CItem::CItem(CTransformerWindow * parent_, int size_, int id_)
  940. : CIntObject(LCLICK),
  941. id(id_),
  942. size(size_),
  943. parent(parent_)
  944. {
  945. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  946. left = true;
  947. pos.w = 58;
  948. pos.h = 64;
  949. pos.x += 45 + (id%3)*83 + id/6*83;
  950. pos.y += 109 + (id/3)*98;
  951. icon = std::make_shared<CAnimImage>("TWCRPORT", parent->army->getCreature(SlotID(id))->getId() + 2);
  952. count = std::make_shared<CLabel>(28, 76,FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, std::to_string(size));
  953. }
  954. void CTransformerWindow::makeDeal()
  955. {
  956. for(auto & elem : items)
  957. {
  958. if(!elem->left)
  959. LOCPLINT->cb->trade(market, EMarketMode::CREATURE_UNDEAD, elem->id, 0, 0, hero);
  960. }
  961. }
  962. void CTransformerWindow::addAll()
  963. {
  964. for(auto & elem : items)
  965. {
  966. if(elem->left)
  967. elem->move();
  968. }
  969. redraw();
  970. }
  971. void CTransformerWindow::updateGarrisons()
  972. {
  973. for(auto & item : items)
  974. item->update();
  975. }
  976. CTransformerWindow::CTransformerWindow(const IMarket * _market, const CGHeroInstance * _hero)
  977. : CStatusbarWindow(PLAYER_COLORED, "SKTRNBK"),
  978. hero(_hero),
  979. market(_market)
  980. {
  981. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  982. if(hero)
  983. army = hero;
  984. else
  985. army = dynamic_cast<const CArmedInstance *>(market); //for town only
  986. if(army)
  987. {
  988. for(int i = 0; i < GameConstants::ARMY_SIZE; i++)
  989. {
  990. if(army->getCreature(SlotID(i)))
  991. items.push_back(std::make_shared<CItem>(this, army->getStackCount(SlotID(i)), i));
  992. }
  993. }
  994. all = std::make_shared<CButton>(Point(146, 416), "ALTARMY.DEF", CGI->generaltexth->zelp[590], [&](){ addAll(); }, EShortcut::RECRUITMENT_UPGRADE_ALL);
  995. convert = std::make_shared<CButton>(Point(269, 416), "ALTSACR.DEF", CGI->generaltexth->zelp[591], [&](){ makeDeal(); }, EShortcut::GLOBAL_ACCEPT);
  996. cancel = std::make_shared<CButton>(Point(392, 416), "ICANCEL.DEF", CGI->generaltexth->zelp[592], [&](){ close(); },EShortcut::GLOBAL_CANCEL);
  997. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  998. titleLeft = std::make_shared<CLabel>(153, 29,FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[485]);//holding area
  999. titleRight = std::make_shared<CLabel>(153+295, 29, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[486]);//transformer
  1000. 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
  1001. 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
  1002. }
  1003. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y)
  1004. : CIntObject(LCLICK | SHOW_POPUP | HOVER),
  1005. ID(_ID),
  1006. parent(_parent)
  1007. {
  1008. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1009. pos.x += X;
  1010. pos.y += Y;
  1011. topBar = std::make_shared<CAnimImage>(parent->bars, 0, 0, -28, -22);
  1012. bottomBar = std::make_shared<CAnimImage>(parent->bars, 0, 0, -28, 48);
  1013. icon = std::make_shared<CAnimImage>("SECSKILL", _ID * 3 + 3, 0);
  1014. name = std::make_shared<CLabel>(22, -13, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->skillh->getByIndex(ID)->getNameTranslated());
  1015. level = std::make_shared<CLabel>(22, 57, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->levels[0]);
  1016. pos.h = icon->pos.h;
  1017. pos.w = icon->pos.w;
  1018. }
  1019. void CUniversityWindow::CItem::clickPressed(const Point & cursorPosition)
  1020. {
  1021. if(state() == 2)
  1022. GH.windows().createAndPushWindow<CUnivConfirmWindow>(parent, ID, LOCPLINT->cb->getResourceAmount(EGameResID::GOLD) >= 2000);
  1023. }
  1024. void CUniversityWindow::CItem::showPopupWindow(const Point & cursorPosition)
  1025. {
  1026. CRClickPopup::createAndPush(CGI->skillh->getByIndex(ID)->getDescriptionTranslated(1), std::make_shared<CComponent>(CComponent::secskill, ID, 1));
  1027. }
  1028. void CUniversityWindow::CItem::hover(bool on)
  1029. {
  1030. if(on)
  1031. GH.statusbar()->write(CGI->skillh->getByIndex(ID)->getNameTranslated());
  1032. else
  1033. GH.statusbar()->clear();
  1034. }
  1035. int CUniversityWindow::CItem::state()
  1036. {
  1037. if(parent->hero->getSecSkillLevel(SecondarySkill(ID)))//hero know this skill
  1038. return 1;
  1039. if(!parent->hero->canLearnSkill(SecondarySkill(ID)))//can't learn more skills
  1040. return 0;
  1041. return 2;
  1042. }
  1043. void CUniversityWindow::CItem::showAll(Canvas & to)
  1044. {
  1045. //TODO: update when state actually changes
  1046. auto stateIndex = state();
  1047. topBar->setFrame(stateIndex);
  1048. bottomBar->setFrame(stateIndex);
  1049. CIntObject::showAll(to);
  1050. }
  1051. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market)
  1052. : CStatusbarWindow(PLAYER_COLORED, "UNIVERS1"),
  1053. hero(_hero),
  1054. market(_market)
  1055. {
  1056. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1057. bars = std::make_shared<CAnimation>();
  1058. bars->setCustom("UNIVRED", 0, 0);
  1059. bars->setCustom("UNIVGOLD", 1, 0);
  1060. bars->setCustom("UNIVGREN", 2, 0);
  1061. bars->preload();
  1062. std::string titleStr = CGI->generaltexth->allTexts[602];
  1063. std::string speechStr = CGI->generaltexth->allTexts[603];
  1064. if(auto town = dynamic_cast<const CGTownInstance *>(_market))
  1065. {
  1066. auto faction = town->town->faction->getId();
  1067. auto bid = town->town->getSpecialBuilding(BuildingSubID::MAGIC_UNIVERSITY)->bid;
  1068. titlePic = std::make_shared<CAnimImage>((*CGI->townh)[faction]->town->clientInfo.buildingsIcons, bid);
  1069. }
  1070. else if(auto uni = dynamic_cast<const CGUniversity *>(_market); uni->appearance)
  1071. {
  1072. titlePic = std::make_shared<CAnimImage>(uni->appearance->animationFile, 0);
  1073. titleStr = uni->title;
  1074. speechStr = uni->speech;
  1075. }
  1076. else
  1077. {
  1078. titlePic = std::make_shared<CPicture>("UNIVBLDG");
  1079. }
  1080. titlePic->center(Point(232 + pos.x, 76 + pos.y));
  1081. clerkSpeech = std::make_shared<CTextBox>(speechStr, Rect(24, 129, 413, 70), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  1082. title = std::make_shared<CLabel>(231, 26, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, titleStr);
  1083. std::vector<int> goods = market->availableItemsIds(EMarketMode::RESOURCE_SKILL);
  1084. for(int i=0; i<goods.size(); i++)//prepare clickable items
  1085. items.push_back(std::make_shared<CItem>(this, goods[i], 54+i*104, 234));
  1086. cancel = std::make_shared<CButton>(Point(200, 313), "IOKAY.DEF", CGI->generaltexth->zelp[632], [&](){ close(); }, EShortcut::GLOBAL_ACCEPT);
  1087. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1088. }
  1089. void CUniversityWindow::makeDeal(int skill)
  1090. {
  1091. LOCPLINT->cb->trade(market, EMarketMode::RESOURCE_SKILL, 6, skill, 1, hero);
  1092. }
  1093. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * owner_, int SKILL, bool available)
  1094. : CStatusbarWindow(PLAYER_COLORED, "UNIVERS2.PCX"),
  1095. owner(owner_)
  1096. {
  1097. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1098. std::string text = CGI->generaltexth->allTexts[608];
  1099. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  1100. boost::replace_first(text, "%s", CGI->skillh->getByIndex(SKILL)->getNameTranslated());
  1101. boost::replace_first(text, "%d", "2000");
  1102. clerkSpeech = std::make_shared<CTextBox>(text, Rect(24, 129, 413, 70), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  1103. name = std::make_shared<CLabel>(230, 37, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->skillh->getByIndex(SKILL)->getNameTranslated());
  1104. icon = std::make_shared<CAnimImage>("SECSKILL", SKILL*3+3, 0, 211, 51);
  1105. level = std::make_shared<CLabel>(230, 107, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->levels[1]);
  1106. costIcon = std::make_shared<CAnimImage>("RESOURCE", GameResID(EGameResID::GOLD), 0, 210, 210);
  1107. cost = std::make_shared<CLabel>(230, 267, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, "2000");
  1108. std::string hoverText = CGI->generaltexth->allTexts[609];
  1109. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->skillh->getByIndex(SKILL)->getNameTranslated());
  1110. text = CGI->generaltexth->zelp[633].second;
  1111. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  1112. boost::replace_first(text, "%s", CGI->skillh->getByIndex(SKILL)->getNameTranslated());
  1113. boost::replace_first(text, "%d", "2000");
  1114. confirm = std::make_shared<CButton>(Point(148, 299), "IBY6432.DEF", CButton::tooltip(hoverText, text), [=](){makeDeal(SKILL);}, EShortcut::GLOBAL_ACCEPT);
  1115. confirm->block(!available);
  1116. cancel = std::make_shared<CButton>(Point(252,299), "ICANCEL.DEF", CGI->generaltexth->zelp[631], [&](){ close(); }, EShortcut::GLOBAL_CANCEL);
  1117. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1118. }
  1119. void CUnivConfirmWindow::makeDeal(int skill)
  1120. {
  1121. owner->makeDeal(skill);
  1122. close();
  1123. }
  1124. CGarrisonWindow::CGarrisonWindow(const CArmedInstance * up, const CGHeroInstance * down, bool removableUnits)
  1125. : CWindowObject(PLAYER_COLORED, "GARRISON")
  1126. {
  1127. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1128. garr = std::make_shared<CGarrisonInt>(92, 127, 4, Point(0,96), up, down, removableUnits);
  1129. {
  1130. auto split = std::make_shared<CButton>(Point(88, 314), "IDV6432.DEF", CButton::tooltip(CGI->generaltexth->tcommands[3], ""), [&](){ garr->splitClick(); } );
  1131. garr->addSplitBtn(split);
  1132. }
  1133. quit = std::make_shared<CButton>(Point(399, 314), "IOK6432.DEF", CButton::tooltip(CGI->generaltexth->tcommands[8], ""), [&](){ close(); }, EShortcut::GLOBAL_ACCEPT);
  1134. std::string titleText;
  1135. if(down->tempOwner == up->tempOwner)
  1136. {
  1137. titleText = CGI->generaltexth->allTexts[709];
  1138. }
  1139. else
  1140. {
  1141. //assume that this is joining monsters dialog
  1142. if(up->Slots().size() > 0)
  1143. {
  1144. titleText = CGI->generaltexth->allTexts[35];
  1145. boost::algorithm::replace_first(titleText, "%s", up->Slots().begin()->second->type->getNamePluralTranslated());
  1146. }
  1147. else
  1148. {
  1149. logGlobal->error("Invalid armed instance for garrison window.");
  1150. }
  1151. }
  1152. title = std::make_shared<CLabel>(275, 30, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, titleText);
  1153. banner = std::make_shared<CAnimImage>("CREST58", up->getOwner().getNum(), 0, 28, 124);
  1154. portrait = std::make_shared<CAnimImage>("PortraitsLarge", down->portrait, 0, 29, 222);
  1155. }
  1156. void CGarrisonWindow::updateGarrisons()
  1157. {
  1158. garr->recreateSlots();
  1159. }
  1160. CHillFortWindow::CHillFortWindow(const CGHeroInstance * visitor, const CGObjectInstance * object)
  1161. : CStatusbarWindow(PLAYER_COLORED, "APHLFTBK"),
  1162. fort(object),
  1163. hero(visitor)
  1164. {
  1165. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1166. title = std::make_shared<CLabel>(325, 32, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, fort->getObjectName());
  1167. heroPic = std::make_shared<CHeroArea>(30, 60, hero);
  1168. for(int i=0; i < resCount; i++)
  1169. {
  1170. totalIcons[i] = std::make_shared<CAnimImage>("SMALRES", i, 0, 104 + 76 * i, 237);
  1171. totalLabels[i] = std::make_shared<CLabel>(166 + 76 * i, 253, FONT_SMALL, ETextAlignment::BOTTOMRIGHT);
  1172. }
  1173. for(int i = 0; i < slotsCount; i++)
  1174. {
  1175. upgrade[i] = std::make_shared<CButton>(Point(107 + i * 76, 171), "", CButton::tooltip(getTextForSlot(SlotID(i))), [=](){ makeDeal(SlotID(i)); }, vstd::next(EShortcut::SELECT_INDEX_1, i));
  1176. for(auto image : { "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF" })
  1177. upgrade[i]->addImage(image);
  1178. for(int j : {0,1})
  1179. {
  1180. slotIcons[i][j] = std::make_shared<CAnimImage>("SMALRES", 0, 0, 104 + 76 * i, 128 + 20 * j);
  1181. slotLabels[i][j] = std::make_shared<CLabel>(168 + 76 * i, 144 + 20 * j, FONT_SMALL, ETextAlignment::BOTTOMRIGHT);
  1182. }
  1183. }
  1184. upgradeAll = std::make_shared<CButton>(Point(30, 231), "", CButton::tooltip(CGI->generaltexth->allTexts[432]), [&](){ makeDeal(SlotID(slotsCount));}, EShortcut::RECRUITMENT_UPGRADE_ALL);
  1185. for(auto image : { "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF" })
  1186. upgradeAll->addImage(image);
  1187. quit = std::make_shared<CButton>(Point(294, 275), "IOKAY.DEF", CButton::tooltip(), std::bind(&CHillFortWindow::close, this), EShortcut::GLOBAL_ACCEPT);
  1188. statusbar = CGStatusBar::create(std::make_shared<CPicture>(background->getSurface(), Rect(8, pos.h - 26, pos.w - 16, 19), 8, pos.h - 26));
  1189. garr = std::make_shared<CGarrisonInt>(108, 60, 18, Point(), hero, nullptr);
  1190. updateGarrisons();
  1191. }
  1192. void CHillFortWindow::updateGarrisons()
  1193. {
  1194. std::array<TResources, slotsCount> costs;// costs [slot ID] [resource ID] = resource count for upgrade
  1195. TResources totalSum; // totalSum[resource ID] = value
  1196. for(int i=0; i<slotsCount; i++)
  1197. {
  1198. std::fill(costs[i].begin(), costs[i].end(), 0);
  1199. int newState = getState(SlotID(i));
  1200. if(newState != -1)
  1201. {
  1202. UpgradeInfo info;
  1203. LOCPLINT->cb->fillUpgradeInfo(hero, SlotID(i), info);
  1204. if(info.newID.size())//we have upgrades here - update costs
  1205. {
  1206. costs[i] = info.cost[0] * hero->getStackCount(SlotID(i));
  1207. totalSum += costs[i];
  1208. }
  1209. }
  1210. currState[i] = newState;
  1211. upgrade[i]->setIndex(currState[i] == -1 ? 0 : currState[i]);
  1212. upgrade[i]->block(currState[i] == -1);
  1213. upgrade[i]->addHoverText(CButton::NORMAL, getTextForSlot(SlotID(i)));
  1214. }
  1215. //"Upgrade all" slot
  1216. int newState = 2;
  1217. {
  1218. TResources myRes = LOCPLINT->cb->getResourceAmount();
  1219. bool allUpgraded = true;//All creatures are upgraded?
  1220. for(int i=0; i<slotsCount; i++)
  1221. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  1222. if(allUpgraded)
  1223. newState = 1;
  1224. if(!totalSum.canBeAfforded(myRes))
  1225. newState = 0;
  1226. }
  1227. currState[slotsCount] = newState;
  1228. upgradeAll->setIndex(newState);
  1229. garr->recreateSlots();
  1230. for(int i = 0; i < slotsCount; i++)
  1231. {
  1232. //hide all first
  1233. for(int j : {0,1})
  1234. {
  1235. slotIcons[i][j]->visible = false;
  1236. slotLabels[i][j]->setText("");
  1237. }
  1238. //if can upgrade or can not afford, draw cost
  1239. if(currState[i] == 0 || currState[i] == 2)
  1240. {
  1241. if(costs[i].nonZero())
  1242. {
  1243. //reverse iterator is used to display gold as first element
  1244. int j = 0;
  1245. for(int res = (int)costs[i].size()-1; (res >= 0) && (j < 2); res--)
  1246. {
  1247. int val = costs[i][res];
  1248. if(!val)
  1249. continue;
  1250. slotIcons[i][j]->visible = true;
  1251. slotIcons[i][j]->setFrame(res);
  1252. slotLabels[i][j]->setText(std::to_string(val));
  1253. j++;
  1254. }
  1255. }
  1256. else//free upgrade - print gold image and "Free" text
  1257. {
  1258. slotIcons[i][0]->visible = true;
  1259. slotIcons[i][0]->setFrame(GameResID(EGameResID::GOLD));
  1260. slotLabels[i][0]->setText(CGI->generaltexth->allTexts[344]);
  1261. }
  1262. }
  1263. }
  1264. for(int i = 0; i < resCount; i++)
  1265. {
  1266. if(totalSum[i] == 0)
  1267. {
  1268. totalIcons[i]->visible = false;
  1269. totalLabels[i]->setText("");
  1270. }
  1271. else
  1272. {
  1273. totalIcons[i]->visible = true;
  1274. totalLabels[i]->setText(std::to_string(totalSum[i]));
  1275. }
  1276. }
  1277. }
  1278. void CHillFortWindow::makeDeal(SlotID slot)
  1279. {
  1280. assert(slot.getNum()>=0);
  1281. int offset = (slot.getNum() == slotsCount)?2:0;
  1282. switch(currState[slot.getNum()])
  1283. {
  1284. case 0:
  1285. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset], std::vector<std::shared_ptr<CComponent>>(), soundBase::sound_todo);
  1286. break;
  1287. case 1:
  1288. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset], std::vector<std::shared_ptr<CComponent>>(), soundBase::sound_todo);
  1289. break;
  1290. case 2:
  1291. for(int i=0; i<slotsCount; i++)
  1292. {
  1293. if(slot.getNum() ==i || ( slot.getNum() == slotsCount && currState[i] == 2 ))//this is activated slot or "upgrade all"
  1294. {
  1295. UpgradeInfo info;
  1296. LOCPLINT->cb->fillUpgradeInfo(hero, SlotID(i), info);
  1297. LOCPLINT->cb->upgradeCreature(hero, SlotID(i), info.newID[0]);
  1298. }
  1299. }
  1300. break;
  1301. }
  1302. }
  1303. std::string CHillFortWindow::getTextForSlot(SlotID slot)
  1304. {
  1305. if(!hero->getCreature(slot))//we don`t have creature here
  1306. return "";
  1307. std::string str = CGI->generaltexth->allTexts[318];
  1308. int amount = hero->getStackCount(slot);
  1309. if(amount == 1)
  1310. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->getNameSingularTranslated());
  1311. else
  1312. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->getNamePluralTranslated());
  1313. return str;
  1314. }
  1315. int CHillFortWindow::getState(SlotID slot)
  1316. {
  1317. TResources myRes = LOCPLINT->cb->getResourceAmount();
  1318. if(hero->slotEmpty(slot))//no creature here
  1319. return -1;
  1320. UpgradeInfo info;
  1321. LOCPLINT->cb->fillUpgradeInfo(hero, slot, info);
  1322. if(!info.newID.size())//already upgraded
  1323. return 1;
  1324. if(!(info.cost[0] * hero->getStackCount(slot)).canBeAfforded(myRes))
  1325. return 0;
  1326. return 2;//can upgrade
  1327. }
  1328. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner):
  1329. CStatusbarWindow(PLAYER_COLORED | BORDERED, "TpRank"),
  1330. owner(_owner)
  1331. {
  1332. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1333. SThievesGuildInfo tgi; //info to be displayed
  1334. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  1335. exitb = std::make_shared<CButton>(Point(748, 556), "TPMAGE1", CButton::tooltip(CGI->generaltexth->allTexts[600]), [&](){ close();}, EShortcut::GLOBAL_RETURN);
  1336. statusbar = CGStatusBar::create(3, 555, "TStatBar.bmp", 742);
  1337. resdatabar = std::make_shared<CMinorResDataBar>();
  1338. resdatabar->moveBy(pos.topLeft(), true);
  1339. //data for information table:
  1340. // fields[row][column] = list of id's of players for this box
  1341. static std::vector< std::vector< PlayerColor > > SThievesGuildInfo::* fields[] =
  1342. { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  1343. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks,
  1344. &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army, &SThievesGuildInfo::income };
  1345. for(int g=0; g<12; ++g)
  1346. {
  1347. int posY[] = {400, 460, 510};
  1348. int y;
  1349. if(g < 9)
  1350. y = 52 + 32*g;
  1351. else
  1352. y = posY[g-9];
  1353. std::string text = CGI->generaltexth->jktexts[24+g];
  1354. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  1355. rowHeaders.push_back(std::make_shared<CLabel>(135, y, FONT_MEDIUM, ETextAlignment::CENTER, Colors::YELLOW, text));
  1356. }
  1357. auto PRSTRIPS = std::make_shared<CAnimation>("PRSTRIPS");
  1358. PRSTRIPS->preload();
  1359. for(int g=1; g<tgi.playerColors.size(); ++g)
  1360. columnBackgrounds.push_back(std::make_shared<CAnimImage>(PRSTRIPS, g-1, 0, 250 + 66*g, 7));
  1361. for(int g=0; g<tgi.playerColors.size(); ++g)
  1362. columnHeaders.push_back(std::make_shared<CLabel>(283 + 66*g, 24, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->jktexts[16+g]));
  1363. auto itgflags = std::make_shared<CAnimation>("itgflags");
  1364. itgflags->preload();
  1365. //printing flags
  1366. for(int g = 0; g < std::size(fields); ++g) //by lines
  1367. {
  1368. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  1369. {
  1370. std::vector<PlayerColor> &players = (tgi .* fields[g])[b]; //get players with this place in this line
  1371. //position of box
  1372. int xpos = 259 + 66 * b;
  1373. int ypos = 41 + 32 * g;
  1374. size_t rowLength[2]; //size of each row
  1375. rowLength[0] = std::min<size_t>(players.size(), 4);
  1376. rowLength[1] = players.size() - rowLength[0];
  1377. for(size_t j=0; j < 2; j++)
  1378. {
  1379. // origin of this row | offset for 2nd row| shift right for short rows
  1380. //if we have 2 rows, start either from mid or beginning (depending on count), otherwise center the flags
  1381. int rowStartX = xpos + (j ? 6 + ((int)rowLength[j] < 3 ? 12 : 0) : 24 - 6 * (int)rowLength[j]);
  1382. int rowStartY = ypos + (j ? 4 : 0);
  1383. for(size_t i=0; i < rowLength[j]; i++)
  1384. cells.push_back(std::make_shared<CAnimImage>(itgflags, players[i + j*4].getNum(), 0, rowStartX + (int)i*12, rowStartY));
  1385. }
  1386. }
  1387. }
  1388. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  1389. //printing best hero
  1390. int counter = 0;
  1391. for(auto & iter : tgi.colorToBestHero)
  1392. {
  1393. banners.push_back(std::make_shared<CPicture>(colorToBox[iter.first.getNum()], 253 + 66 * counter, 334));
  1394. if(iter.second.portrait >= 0)
  1395. {
  1396. bestHeroes.push_back(std::make_shared<CAnimImage>("PortraitsSmall", iter.second.portrait, 0, 260 + 66 * counter, 360));
  1397. //TODO: r-click info:
  1398. // - r-click on hero
  1399. // - r-click on primary skill label
  1400. if(iter.second.details)
  1401. {
  1402. primSkillHeaders.push_back(std::make_shared<CTextBox>(CGI->generaltexth->allTexts[184], Rect(260 + 66*counter, 396, 52, 64),
  1403. 0, FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE));
  1404. for(int i=0; i<iter.second.details->primskills.size(); ++i)
  1405. {
  1406. primSkillValues.push_back(std::make_shared<CLabel>(310 + 66 * counter, 407 + 11*i, FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE,
  1407. std::to_string(iter.second.details->primskills[i])));
  1408. }
  1409. }
  1410. }
  1411. counter++;
  1412. }
  1413. //printing best creature
  1414. counter = 0;
  1415. for(auto & it : tgi.bestCreature)
  1416. {
  1417. if(it.second >= 0)
  1418. bestCreatures.push_back(std::make_shared<CAnimImage>("TWCRPORT", it.second+2, 0, 255 + 66 * counter, 479));
  1419. counter++;
  1420. }
  1421. //printing personality
  1422. counter = 0;
  1423. for(auto & it : tgi.personality)
  1424. {
  1425. std::string text;
  1426. if(it.second == EAiTactic::NONE)
  1427. {
  1428. text = CGI->generaltexth->arraytxt[172];
  1429. }
  1430. else if(it.second != EAiTactic::RANDOM)
  1431. {
  1432. text = CGI->generaltexth->arraytxt[168 + it.second];
  1433. }
  1434. personalities.push_back(std::make_shared<CLabel>(283 + 66*counter, 459, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, text));
  1435. counter++;
  1436. }
  1437. }
  1438. CObjectListWindow::CItem::CItem(CObjectListWindow * _parent, size_t _id, std::string _text)
  1439. : CIntObject(LCLICK | DOUBLECLICK),
  1440. parent(_parent),
  1441. index(_id)
  1442. {
  1443. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1444. border = std::make_shared<CPicture>("TPGATES");
  1445. pos = border->pos;
  1446. setRedrawParent(true);
  1447. text = std::make_shared<CLabel>(pos.w/2, pos.h/2, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, _text);
  1448. select(index == parent->selected);
  1449. }
  1450. void CObjectListWindow::CItem::select(bool on)
  1451. {
  1452. ui8 mask = UPDATE | SHOWALL;
  1453. if(on)
  1454. border->recActions |= mask;
  1455. else
  1456. border->recActions &= ~mask;
  1457. redraw();//???
  1458. }
  1459. void CObjectListWindow::CItem::clickPressed(const Point & cursorPosition)
  1460. {
  1461. parent->changeSelection(index);
  1462. }
  1463. void CObjectListWindow::CItem::clickDouble(const Point & cursorPosition)
  1464. {
  1465. parent->elementSelected();
  1466. }
  1467. 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)
  1468. : CWindowObject(PLAYER_COLORED, "TPGATE"),
  1469. onSelect(Callback),
  1470. selected(initialSelection)
  1471. {
  1472. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1473. items.reserve(_items.size());
  1474. for(int id : _items)
  1475. {
  1476. items.push_back(std::make_pair(id, LOCPLINT->cb->getObjInstance(ObjectInstanceID(id))->getObjectName()));
  1477. }
  1478. init(titleWidget_, _title, _descr);
  1479. }
  1480. 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)
  1481. : CWindowObject(PLAYER_COLORED, "TPGATE"),
  1482. onSelect(Callback),
  1483. selected(initialSelection)
  1484. {
  1485. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  1486. items.reserve(_items.size());
  1487. for(size_t i=0; i<_items.size(); i++)
  1488. items.push_back(std::make_pair(int(i), _items[i]));
  1489. init(titleWidget_, _title, _descr);
  1490. }
  1491. void CObjectListWindow::init(std::shared_ptr<CIntObject> titleWidget_, std::string _title, std::string _descr)
  1492. {
  1493. titleWidget = titleWidget_;
  1494. title = std::make_shared<CLabel>(152, 27, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, _title);
  1495. descr = std::make_shared<CLabel>(145, 133, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, _descr);
  1496. exit = std::make_shared<CButton>( Point(228, 402), "ICANCEL.DEF", CButton::tooltip(), std::bind(&CObjectListWindow::exitPressed, this), EShortcut::GLOBAL_CANCEL);
  1497. if(titleWidget)
  1498. {
  1499. addChild(titleWidget.get());
  1500. titleWidget->recActions = 255-DISPOSE;
  1501. titleWidget->pos.x = pos.w/2 + pos.x - titleWidget->pos.w/2;
  1502. titleWidget->pos.y =75 + pos.y - titleWidget->pos.h/2;
  1503. }
  1504. list = std::make_shared<CListBox>(std::bind(&CObjectListWindow::genItem, this, _1),
  1505. Point(14, 151), Point(0, 25), 9, items.size(), 0, 1, Rect(262, -32, 256, 256) );
  1506. list->setRedrawParent(true);
  1507. ok = std::make_shared<CButton>(Point(15, 402), "IOKAY.DEF", CButton::tooltip(), std::bind(&CObjectListWindow::elementSelected, this), EShortcut::GLOBAL_ACCEPT);
  1508. ok->block(!list->size());
  1509. }
  1510. std::shared_ptr<CIntObject> CObjectListWindow::genItem(size_t index)
  1511. {
  1512. if(index < items.size())
  1513. return std::make_shared<CItem>(this, index, items[index].second);
  1514. return std::shared_ptr<CIntObject>();
  1515. }
  1516. void CObjectListWindow::elementSelected()
  1517. {
  1518. std::function<void(int)> toCall = onSelect;//save
  1519. int where = items[selected].first; //required variables
  1520. close();//then destroy window
  1521. toCall(where);//and send selected object
  1522. }
  1523. void CObjectListWindow::exitPressed()
  1524. {
  1525. std::function<void()> toCall = onExit;//save
  1526. close();//then destroy window
  1527. if(toCall)
  1528. toCall();
  1529. }
  1530. void CObjectListWindow::changeSelection(size_t which)
  1531. {
  1532. ok->block(false);
  1533. if(selected == which)
  1534. return;
  1535. for(std::shared_ptr<CIntObject> element : list->getItems())
  1536. {
  1537. CItem * item = dynamic_cast<CItem*>(element.get());
  1538. if(item)
  1539. {
  1540. if(item->index == selected)
  1541. item->select(false);
  1542. if(item->index == which)
  1543. item->select(true);
  1544. }
  1545. }
  1546. selected = which;
  1547. }
  1548. void CObjectListWindow::keyPressed (EShortcut key)
  1549. {
  1550. int sel = static_cast<int>(selected);
  1551. switch(key)
  1552. {
  1553. break; case EShortcut::MOVE_UP:
  1554. sel -=1;
  1555. break; case EShortcut::MOVE_DOWN:
  1556. sel +=1;
  1557. break; case EShortcut::MOVE_PAGE_UP:
  1558. sel -=9;
  1559. break; case EShortcut::MOVE_PAGE_DOWN:
  1560. sel +=9;
  1561. break; case EShortcut::MOVE_FIRST:
  1562. sel = 0;
  1563. break; case EShortcut::MOVE_LAST:
  1564. sel = static_cast<int>(items.size());
  1565. break; default:
  1566. return;
  1567. }
  1568. vstd::abetween<int>(sel, 0, items.size()-1);
  1569. list->scrollTo(sel);
  1570. changeSelection(sel);
  1571. }