GUIClasses.cpp 56 KB

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