GUIClasses.cpp 67 KB

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