GUIClasses.cpp 67 KB

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