GUIClasses.cpp 67 KB

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