BattleInterfaceClasses.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * BattleInterfaceClasses.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 "BattleInterfaceClasses.h"
  12. #include "BattleInterface.h"
  13. #include "BattleActionsController.h"
  14. #include "BattleRenderer.h"
  15. #include "BattleSiegeController.h"
  16. #include "BattleFieldController.h"
  17. #include "BattleStacksController.h"
  18. #include "BattleWindow.h"
  19. #include "../CGameInfo.h"
  20. #include "../CMusicHandler.h"
  21. #include "../CPlayerInterface.h"
  22. #include "../CVideoHandler.h"
  23. #include "../gui/CursorHandler.h"
  24. #include "../gui/CGuiHandler.h"
  25. #include "../gui/Shortcut.h"
  26. #include "../gui/MouseButton.h"
  27. #include "../gui/WindowHandler.h"
  28. #include "../render/Canvas.h"
  29. #include "../render/IImage.h"
  30. #include "../render/IFont.h"
  31. #include "../render/Graphics.h"
  32. #include "../widgets/Buttons.h"
  33. #include "../widgets/Images.h"
  34. #include "../widgets/TextControls.h"
  35. #include "../widgets/MiscWidgets.h"
  36. #include "../windows/CMessage.h"
  37. #include "../windows/CCreatureWindow.h"
  38. #include "../windows/CSpellWindow.h"
  39. #include "../render/CAnimation.h"
  40. #include "../render/IRenderHandler.h"
  41. #include "../adventureMap/CInGameConsole.h"
  42. #include "../../CCallback.h"
  43. #include "../../lib/CStack.h"
  44. #include "../../lib/CConfigHandler.h"
  45. #include "../../lib/CCreatureHandler.h"
  46. #include "../../lib/gameState/InfoAboutArmy.h"
  47. #include "../../lib/CGeneralTextHandler.h"
  48. #include "../../lib/CTownHandler.h"
  49. #include "../../lib/CHeroHandler.h"
  50. #include "../../lib/StartInfo.h"
  51. #include "../../lib/CondSh.h"
  52. #include "../../lib/mapObjects/CGTownInstance.h"
  53. #include "../../lib/networkPacks/PacksForClientBattle.h"
  54. #include "../../lib/TextOperations.h"
  55. void BattleConsole::showAll(Canvas & to)
  56. {
  57. CIntObject::showAll(to);
  58. Point line1 (pos.x + pos.w/2, pos.y + 8);
  59. Point line2 (pos.x + pos.w/2, pos.y + 24);
  60. auto visibleText = getVisibleText();
  61. if(visibleText.size() > 0)
  62. to.drawText(line1, FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, visibleText[0]);
  63. if(visibleText.size() > 1)
  64. to.drawText(line2, FONT_SMALL, Colors::WHITE, ETextAlignment::CENTER, visibleText[1]);
  65. }
  66. std::vector<std::string> BattleConsole::getVisibleText()
  67. {
  68. // high priority texts that hide battle log entries
  69. for(const auto & text : {consoleText, hoverText})
  70. {
  71. if (text.empty())
  72. continue;
  73. auto result = CMessage::breakText(text, pos.w, FONT_SMALL);
  74. if(result.size() > 2)
  75. result.resize(2);
  76. return result;
  77. }
  78. // log is small enough to fit entirely - display it as such
  79. if (logEntries.size() < 3)
  80. return logEntries;
  81. return { logEntries[scrollPosition - 1], logEntries[scrollPosition] };
  82. }
  83. std::vector<std::string> BattleConsole::splitText(const std::string &text)
  84. {
  85. std::vector<std::string> lines;
  86. std::vector<std::string> output;
  87. boost::split(lines, text, boost::is_any_of("\n"));
  88. for(const auto & line : lines)
  89. {
  90. if (graphics->fonts[FONT_SMALL]->getStringWidth(text) < pos.w)
  91. {
  92. output.push_back(line);
  93. }
  94. else
  95. {
  96. std::vector<std::string> substrings = CMessage::breakText(line, pos.w, FONT_SMALL);
  97. output.insert(output.end(), substrings.begin(), substrings.end());
  98. }
  99. }
  100. return output;
  101. }
  102. bool BattleConsole::addText(const std::string & text)
  103. {
  104. logGlobal->trace("CBattleConsole message: %s", text);
  105. auto newLines = splitText(text);
  106. logEntries.insert(logEntries.end(), newLines.begin(), newLines.end());
  107. scrollPosition = (int)logEntries.size()-1;
  108. redraw();
  109. return true;
  110. }
  111. void BattleConsole::scrollUp(ui32 by)
  112. {
  113. if(scrollPosition > static_cast<int>(by))
  114. scrollPosition -= by;
  115. redraw();
  116. }
  117. void BattleConsole::scrollDown(ui32 by)
  118. {
  119. if(scrollPosition + by < logEntries.size())
  120. scrollPosition += by;
  121. redraw();
  122. }
  123. BattleConsole::BattleConsole(std::shared_ptr<CPicture> backgroundSource, const Point & objectPos, const Point & imagePos, const Point &size)
  124. : scrollPosition(-1)
  125. , enteringText(false)
  126. {
  127. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  128. pos += objectPos;
  129. pos.w = size.x;
  130. pos.h = size.y;
  131. background = std::make_shared<CPicture>(backgroundSource->getSurface(), Rect(imagePos, size), 0, 0 );
  132. }
  133. void BattleConsole::deactivate()
  134. {
  135. if (enteringText)
  136. LOCPLINT->cingconsole->endEnteringText(false);
  137. CIntObject::deactivate();
  138. }
  139. void BattleConsole::setEnteringMode(bool on)
  140. {
  141. consoleText.clear();
  142. if (on)
  143. {
  144. assert(enteringText == false);
  145. GH.startTextInput(pos);
  146. }
  147. else
  148. {
  149. assert(enteringText == true);
  150. GH.stopTextInput();
  151. }
  152. enteringText = on;
  153. redraw();
  154. }
  155. void BattleConsole::setEnteredText(const std::string & text)
  156. {
  157. assert(enteringText == true);
  158. consoleText = text;
  159. redraw();
  160. }
  161. void BattleConsole::write(const std::string & Text)
  162. {
  163. hoverText = Text;
  164. redraw();
  165. }
  166. void BattleConsole::clearIfMatching(const std::string & Text)
  167. {
  168. if (hoverText == Text)
  169. clear();
  170. }
  171. void BattleConsole::clear()
  172. {
  173. write({});
  174. }
  175. const CGHeroInstance * BattleHero::instance()
  176. {
  177. return hero;
  178. }
  179. void BattleHero::tick(uint32_t msPassed)
  180. {
  181. size_t groupIndex = static_cast<size_t>(phase);
  182. float timePassed = msPassed / 1000.f;
  183. flagCurrentFrame += currentSpeed * timePassed;
  184. currentFrame += currentSpeed * timePassed;
  185. if(flagCurrentFrame >= flagAnimation->size(0))
  186. flagCurrentFrame -= flagAnimation->size(0);
  187. if(currentFrame >= animation->size(groupIndex))
  188. {
  189. currentFrame -= animation->size(groupIndex);
  190. switchToNextPhase();
  191. }
  192. }
  193. void BattleHero::render(Canvas & canvas)
  194. {
  195. size_t groupIndex = static_cast<size_t>(phase);
  196. auto flagFrame = flagAnimation->getImage(flagCurrentFrame, 0, true);
  197. auto heroFrame = animation->getImage(currentFrame, groupIndex, true);
  198. Point heroPosition = pos.center() - parent->pos.topLeft() - heroFrame->dimensions() / 2;
  199. Point flagPosition = pos.center() - parent->pos.topLeft() - flagFrame->dimensions() / 2;
  200. if(defender)
  201. flagPosition += Point(-4, -41);
  202. else
  203. flagPosition += Point(4, -41);
  204. canvas.draw(flagFrame, flagPosition);
  205. canvas.draw(heroFrame, heroPosition);
  206. }
  207. void BattleHero::pause()
  208. {
  209. currentSpeed = 0.f;
  210. }
  211. void BattleHero::play()
  212. {
  213. //H3 speed: 10 fps ( 100 ms per frame)
  214. currentSpeed = 10.f;
  215. }
  216. float BattleHero::getFrame() const
  217. {
  218. return currentFrame;
  219. }
  220. void BattleHero::collectRenderableObjects(BattleRenderer & renderer)
  221. {
  222. auto hex = defender ? BattleHex(GameConstants::BFIELD_WIDTH-1) : BattleHex(0);
  223. renderer.insert(EBattleFieldLayer::HEROES, hex, [this](BattleRenderer::RendererRef canvas)
  224. {
  225. render(canvas);
  226. });
  227. }
  228. void BattleHero::onPhaseFinished(const std::function<void()> & callback)
  229. {
  230. phaseFinishedCallback = callback;
  231. }
  232. void BattleHero::setPhase(EHeroAnimType newPhase)
  233. {
  234. nextPhase = newPhase;
  235. switchToNextPhase(); //immediately switch to next phase and then restore idling phase
  236. nextPhase = EHeroAnimType::HOLDING;
  237. }
  238. void BattleHero::heroLeftClicked()
  239. {
  240. if(owner.actionsController->spellcastingModeActive()) //we are casting a spell
  241. return;
  242. if(!hero || !owner.makingTurn())
  243. return;
  244. if(owner.getBattle()->battleCanCastSpell(hero, spells::Mode::HERO) == ESpellCastProblem::OK) //check conditions
  245. {
  246. CCS->curh->set(Cursor::Map::POINTER);
  247. GH.windows().createAndPushWindow<CSpellWindow>(hero, owner.getCurrentPlayerInterface());
  248. }
  249. }
  250. void BattleHero::heroRightClicked()
  251. {
  252. if(settings["battle"]["stickyHeroInfoWindows"].Bool())
  253. return;
  254. Point windowPosition;
  255. if(GH.screenDimensions().x < 1000)
  256. {
  257. windowPosition.x = (!defender) ? owner.fieldController->pos.left() + 1 : owner.fieldController->pos.right() - 79;
  258. windowPosition.y = owner.fieldController->pos.y + 135;
  259. }
  260. else
  261. {
  262. windowPosition.x = (!defender) ? owner.fieldController->pos.left() - 93 : owner.fieldController->pos.right() + 15;
  263. windowPosition.y = owner.fieldController->pos.y;
  264. }
  265. InfoAboutHero targetHero;
  266. if(owner.makingTurn() || settings["session"]["spectate"].Bool())
  267. {
  268. auto h = defender ? owner.defendingHeroInstance : owner.attackingHeroInstance;
  269. targetHero.initFromHero(h, InfoAboutHero::EInfoLevel::INBATTLE);
  270. GH.windows().createAndPushWindow<HeroInfoWindow>(targetHero, &windowPosition);
  271. }
  272. }
  273. void BattleHero::switchToNextPhase()
  274. {
  275. phase = nextPhase;
  276. currentFrame = 0.f;
  277. auto copy = phaseFinishedCallback;
  278. phaseFinishedCallback.clear();
  279. copy();
  280. }
  281. BattleHero::BattleHero(const BattleInterface & owner, const CGHeroInstance * hero, bool defender):
  282. defender(defender),
  283. hero(hero),
  284. owner(owner),
  285. phase(EHeroAnimType::HOLDING),
  286. nextPhase(EHeroAnimType::HOLDING),
  287. currentSpeed(0.f),
  288. currentFrame(0.f),
  289. flagCurrentFrame(0.f)
  290. {
  291. AnimationPath animationPath;
  292. if(!hero->type->battleImage.empty())
  293. animationPath = hero->type->battleImage;
  294. else
  295. if(hero->gender == EHeroGender::FEMALE)
  296. animationPath = hero->type->heroClass->imageBattleFemale;
  297. else
  298. animationPath = hero->type->heroClass->imageBattleMale;
  299. animation = GH.renderHandler().loadAnimation(animationPath);
  300. animation->preload();
  301. pos.w = 64;
  302. pos.h = 136;
  303. pos.x = owner.fieldController->pos.x + (defender ? (owner.fieldController->pos.w - pos.w) : 0);
  304. pos.y = owner.fieldController->pos.y;
  305. if(defender)
  306. animation->verticalFlip();
  307. if(defender)
  308. flagAnimation = GH.renderHandler().loadAnimation(AnimationPath::builtin("CMFLAGR"));
  309. else
  310. flagAnimation = GH.renderHandler().loadAnimation(AnimationPath::builtin("CMFLAGL"));
  311. flagAnimation->preload();
  312. flagAnimation->playerColored(hero->tempOwner);
  313. switchToNextPhase();
  314. play();
  315. addUsedEvents(TIME);
  316. }
  317. HeroInfoBasicPanel::HeroInfoBasicPanel(const InfoAboutHero & hero, Point * position, bool initializeBackground)
  318. : CIntObject(0)
  319. {
  320. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  321. if (position != nullptr)
  322. moveTo(*position);
  323. if(initializeBackground)
  324. {
  325. background = std::make_shared<CPicture>(ImagePath::builtin("CHRPOP"));
  326. background->getSurface()->setBlitMode(EImageBlitMode::OPAQUE);
  327. background->colorize(hero.owner);
  328. }
  329. initializeData(hero);
  330. }
  331. void HeroInfoBasicPanel::initializeData(const InfoAboutHero & hero)
  332. {
  333. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  334. auto attack = hero.details->primskills[0];
  335. auto defense = hero.details->primskills[1];
  336. auto power = hero.details->primskills[2];
  337. auto knowledge = hero.details->primskills[3];
  338. auto morale = hero.details->morale;
  339. auto luck = hero.details->luck;
  340. auto currentSpellPoints = hero.details->mana;
  341. auto maxSpellPoints = hero.details->manaLimit;
  342. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsLarge"), hero.getIconIndex(), 0, 10, 6));
  343. //primary stats
  344. labels.push_back(std::make_shared<CLabel>(9, 75, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[380] + ":"));
  345. labels.push_back(std::make_shared<CLabel>(9, 87, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[381] + ":"));
  346. labels.push_back(std::make_shared<CLabel>(9, 99, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[382] + ":"));
  347. labels.push_back(std::make_shared<CLabel>(9, 111, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[383] + ":"));
  348. labels.push_back(std::make_shared<CLabel>(69, 87, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(attack)));
  349. labels.push_back(std::make_shared<CLabel>(69, 99, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(defense)));
  350. labels.push_back(std::make_shared<CLabel>(69, 111, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(power)));
  351. labels.push_back(std::make_shared<CLabel>(69, 123, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(knowledge)));
  352. //morale+luck
  353. labels.push_back(std::make_shared<CLabel>(9, 131, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[384] + ":"));
  354. labels.push_back(std::make_shared<CLabel>(9, 143, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[385] + ":"));
  355. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("IMRL22"), morale + 3, 0, 47, 131));
  356. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("ILCK22"), luck + 3, 0, 47, 143));
  357. //spell points
  358. labels.push_back(std::make_shared<CLabel>(39, 174, EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[387]));
  359. labels.push_back(std::make_shared<CLabel>(39, 186, EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, std::to_string(currentSpellPoints) + "/" + std::to_string(maxSpellPoints)));
  360. }
  361. void HeroInfoBasicPanel::update(const InfoAboutHero & updatedInfo)
  362. {
  363. icons.clear();
  364. labels.clear();
  365. initializeData(updatedInfo);
  366. }
  367. void HeroInfoBasicPanel::show(Canvas & to)
  368. {
  369. showAll(to);
  370. CIntObject::show(to);
  371. }
  372. StackInfoBasicPanel::StackInfoBasicPanel(const CStack * stack, bool initializeBackground)
  373. : CIntObject(0)
  374. {
  375. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  376. if(initializeBackground)
  377. {
  378. background = std::make_shared<CPicture>(ImagePath::builtin("CCRPOP"));
  379. background->pos.y += 37;
  380. background->getSurface()->setBlitMode(EImageBlitMode::OPAQUE);
  381. background->colorize(stack->getOwner());
  382. background2 = std::make_shared<CPicture>(ImagePath::builtin("CHRPOP"));
  383. background2->getSurface()->setBlitMode(EImageBlitMode::OPAQUE);
  384. background2->colorize(stack->getOwner());
  385. }
  386. initializeData(stack);
  387. }
  388. void StackInfoBasicPanel::initializeData(const CStack * stack)
  389. {
  390. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  391. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("TWCRPORT"), stack->creatureId() + 2, 0, 10, 6));
  392. labels.push_back(std::make_shared<CLabel>(10 + 58, 6 + 64, FONT_MEDIUM, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, TextOperations::formatMetric(stack->getCount(), 4)));
  393. auto attack = std::to_string(CGI->creatures()->getByIndex(stack->creatureIndex())->getAttack(stack->isShooter())) + "(" + std::to_string(stack->getAttack(stack->isShooter())) + ")";
  394. auto defense = std::to_string(CGI->creatures()->getByIndex(stack->creatureIndex())->getDefense(stack->isShooter())) + "(" + std::to_string(stack->getDefense(stack->isShooter())) + ")";
  395. auto damage = std::to_string(CGI->creatures()->getByIndex(stack->creatureIndex())->getMinDamage(stack->isShooter())) + "-" + std::to_string(stack->getMaxDamage(stack->isShooter()));
  396. auto health = CGI->creatures()->getByIndex(stack->creatureIndex())->getMaxHealth();
  397. auto morale = stack->moraleVal();
  398. auto luck = stack->luckVal();
  399. auto killed = stack->getKilled();
  400. auto healthRemaining = TextOperations::formatMetric(std::max(stack->getAvailableHealth() - (stack->getCount() - 1) * health, (si64)0), 4);
  401. //primary stats*/
  402. labels.push_back(std::make_shared<CLabel>(9, 75, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[380] + ":"));
  403. labels.push_back(std::make_shared<CLabel>(9, 87, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[381] + ":"));
  404. labels.push_back(std::make_shared<CLabel>(9, 99, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[386] + ":"));
  405. labels.push_back(std::make_shared<CLabel>(9, 111, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[389] + ":"));
  406. labels.push_back(std::make_shared<CLabel>(69, 87, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, attack));
  407. labels.push_back(std::make_shared<CLabel>(69, 99, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, defense));
  408. labels.push_back(std::make_shared<CLabel>(69, 111, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, damage));
  409. labels.push_back(std::make_shared<CLabel>(69, 123, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(health)));
  410. //morale+luck
  411. labels.push_back(std::make_shared<CLabel>(9, 131, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[384] + ":"));
  412. labels.push_back(std::make_shared<CLabel>(9, 143, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[385] + ":"));
  413. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("IMRL22"), morale + 3, 0, 47, 131));
  414. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("ILCK22"), luck + 3, 0, 47, 143));
  415. //extra information
  416. labels.push_back(std::make_shared<CLabel>(9, 168, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, VLC->generaltexth->translate("vcmi.battleWindow.killed") + ":"));
  417. labels.push_back(std::make_shared<CLabel>(9, 180, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[389] + ":"));
  418. labels.push_back(std::make_shared<CLabel>(69, 180, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(killed)));
  419. labels.push_back(std::make_shared<CLabel>(69, 192, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, healthRemaining));
  420. //spells
  421. static const Point firstPos(15, 206); // position of 1st spell box
  422. static const Point offset(0, 38); // offset of each spell box from previous
  423. for(int i = 0; i < 3; i++)
  424. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("SpellInt"), 78, 0, firstPos.x + offset.x * i, firstPos.y + offset.y * i));
  425. int printed=0; //how many effect pics have been printed
  426. std::vector<SpellID> spells = stack->activeSpells();
  427. for(SpellID effect : spells)
  428. {
  429. //not all effects have graphics (for eg. Acid Breath)
  430. //for modded spells iconEffect is added to SpellInt.def
  431. const bool hasGraphics = (effect < SpellID::THUNDERBOLT) || (effect >= SpellID::AFTER_LAST);
  432. if (hasGraphics)
  433. {
  434. //FIXME: support permanent duration
  435. int duration = stack->getFirstBonus(Selector::source(BonusSource::SPELL_EFFECT, BonusSourceID(effect)))->turnsRemain;
  436. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("SpellInt"), effect + 1, 0, firstPos.x + offset.x * printed, firstPos.y + offset.y * printed));
  437. if(settings["general"]["enableUiEnhancements"].Bool())
  438. labels.push_back(std::make_shared<CLabel>(firstPos.x + offset.x * printed + 46, firstPos.y + offset.y * printed + 36, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(duration)));
  439. if(++printed >= 3 || (printed == 2 && spells.size() > 3)) // interface limit reached
  440. break;
  441. }
  442. }
  443. if(spells.size() == 0)
  444. labelsMultiline.push_back(std::make_shared<CMultiLineLabel>(Rect(firstPos.x, firstPos.y, 48, 36), EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[674]));
  445. if(spells.size() > 3)
  446. labelsMultiline.push_back(std::make_shared<CMultiLineLabel>(Rect(firstPos.x + offset.x * 2, firstPos.y + offset.y * 2 - 4, 48, 36), EFonts::FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, "..."));
  447. }
  448. void StackInfoBasicPanel::update(const CStack * updatedInfo)
  449. {
  450. icons.clear();
  451. labels.clear();
  452. labelsMultiline.clear();
  453. initializeData(updatedInfo);
  454. }
  455. void StackInfoBasicPanel::show(Canvas & to)
  456. {
  457. showAll(to);
  458. CIntObject::show(to);
  459. }
  460. HeroInfoWindow::HeroInfoWindow(const InfoAboutHero & hero, Point * position)
  461. : CWindowObject(RCLICK_POPUP | SHADOW_DISABLED, ImagePath::builtin("CHRPOP"))
  462. {
  463. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  464. if (position != nullptr)
  465. moveTo(*position);
  466. background->colorize(hero.owner); //maybe add this functionality to base class?
  467. content = std::make_shared<HeroInfoBasicPanel>(hero, nullptr, false);
  468. }
  469. BattleResultWindow::BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner, bool allowReplay)
  470. : owner(_owner), currentVideo(BattleResultVideo::NONE)
  471. {
  472. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  473. background = std::make_shared<CPicture>(ImagePath::builtin("CPRESULT"));
  474. background->colorize(owner.playerID);
  475. pos = center(background->pos);
  476. exit = std::make_shared<CButton>(Point(384, 505), AnimationPath::builtin("iok6432.def"), std::make_pair("", ""), [&](){ bExitf();}, EShortcut::GLOBAL_ACCEPT);
  477. exit->setBorderColor(Colors::METALLIC_GOLD);
  478. if(allowReplay || owner.cb->getStartInfo()->extraOptionsInfo.unlimitedReplay)
  479. {
  480. repeat = std::make_shared<CButton>(Point(24, 505), AnimationPath::builtin("icn6432.def"), std::make_pair("", ""), [&](){ bRepeatf();}, EShortcut::GLOBAL_CANCEL);
  481. repeat->setBorderColor(Colors::METALLIC_GOLD);
  482. labels.push_back(std::make_shared<CLabel>(232, 520, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->translate("vcmi.battleResultsWindow.applyResultsLabel")));
  483. }
  484. if(br.winner == 0) //attacker won
  485. {
  486. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  487. }
  488. else
  489. {
  490. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  491. }
  492. if(br.winner == 1)
  493. {
  494. labels.push_back(std::make_shared<CLabel>(412, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  495. }
  496. else
  497. {
  498. labels.push_back(std::make_shared<CLabel>(408, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  499. }
  500. labels.push_back(std::make_shared<CLabel>(232, 302, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[407]));
  501. labels.push_back(std::make_shared<CLabel>(232, 332, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[408]));
  502. labels.push_back(std::make_shared<CLabel>(232, 428, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[409]));
  503. std::string sideNames[2] = {"N/A", "N/A"};
  504. for(int i = 0; i < 2; i++)
  505. {
  506. auto heroInfo = owner.cb->getBattle(br.battleID)->battleGetHeroInfo(i);
  507. const int xs[] = {21, 392};
  508. if(heroInfo.portraitSource.isValid()) //attacking hero
  509. {
  510. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("PortraitsLarge"), heroInfo.getIconIndex(), 0, xs[i], 38));
  511. sideNames[i] = heroInfo.name;
  512. }
  513. else
  514. {
  515. auto stacks = owner.cb->getBattle(br.battleID)->battleGetAllStacks();
  516. vstd::erase_if(stacks, [i](const CStack * stack) //erase stack of other side and not coming from garrison
  517. {
  518. return stack->unitSide() != i || !stack->base;
  519. });
  520. auto best = vstd::maxElementByFun(stacks, [](const CStack * stack)
  521. {
  522. return stack->unitType()->getAIValue();
  523. });
  524. if(best != stacks.end()) //should be always but to be safe...
  525. {
  526. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("TWCRPORT"), (*best)->unitType()->getIconIndex(), 0, xs[i], 38));
  527. sideNames[i] = (*best)->unitType()->getNamePluralTranslated();
  528. }
  529. }
  530. }
  531. //printing attacker and defender's names
  532. labels.push_back(std::make_shared<CLabel>(89, 37, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, sideNames[0]));
  533. labels.push_back(std::make_shared<CLabel>(381, 53, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, sideNames[1]));
  534. //printing casualties
  535. for(int step = 0; step < 2; ++step)
  536. {
  537. if(br.casualties[step].size()==0)
  538. {
  539. labels.push_back(std::make_shared<CLabel>(235, 360 + 97 * step, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[523]));
  540. }
  541. else
  542. {
  543. int xPos = 235 - ((int)br.casualties[step].size()*32 + ((int)br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  544. int yPos = 344 + step * 97;
  545. for(auto & elem : br.casualties[step])
  546. {
  547. auto creature = CGI->creatures()->getByIndex(elem.first);
  548. if (creature->getId() == CreatureID::ARROW_TOWERS )
  549. continue; // do not show destroyed towers in battle results
  550. icons.push_back(std::make_shared<CAnimImage>(AnimationPath::builtin("CPRSMALL"), creature->getIconIndex(), 0, xPos, yPos));
  551. std::ostringstream amount;
  552. amount<<elem.second;
  553. labels.push_back(std::make_shared<CLabel>(xPos + 16, yPos + 42, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, amount.str()));
  554. xPos += 42;
  555. }
  556. }
  557. }
  558. //printing result description
  559. bool weAreAttacker = !(owner.cb->getBattle(br.battleID)->battleGetMySide());
  560. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  561. {
  562. int text = 304;
  563. currentVideo = BattleResultVideo::WIN;
  564. switch(br.result)
  565. {
  566. case EBattleResult::NORMAL:
  567. if(owner.cb->getBattle(br.battleID)->battleGetDefendedTown() && !weAreAttacker)
  568. currentVideo = BattleResultVideo::WIN_SIEGE;
  569. break;
  570. case EBattleResult::ESCAPE:
  571. text = 303;
  572. break;
  573. case EBattleResult::SURRENDER:
  574. text = 302;
  575. break;
  576. default:
  577. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  578. break;
  579. }
  580. playVideo();
  581. std::string str = CGI->generaltexth->allTexts[text];
  582. const CGHeroInstance * ourHero = owner.cb->getBattle(br.battleID)->battleGetMyHero();
  583. if (ourHero)
  584. {
  585. str += CGI->generaltexth->allTexts[305];
  586. boost::algorithm::replace_first(str, "%s", ourHero->getNameTranslated());
  587. boost::algorithm::replace_first(str, "%d", std::to_string(br.exp[weAreAttacker ? 0 : 1]));
  588. }
  589. description = std::make_shared<CTextBox>(str, Rect(69, 203, 330, 68), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  590. }
  591. else // we lose
  592. {
  593. int text = 311;
  594. currentVideo = BattleResultVideo::DEFEAT;
  595. switch(br.result)
  596. {
  597. case EBattleResult::NORMAL:
  598. if(owner.cb->getBattle(br.battleID)->battleGetDefendedTown() && !weAreAttacker)
  599. currentVideo = BattleResultVideo::DEFEAT_SIEGE;
  600. break;
  601. case EBattleResult::ESCAPE:
  602. currentVideo = BattleResultVideo::RETREAT;
  603. text = 310;
  604. break;
  605. case EBattleResult::SURRENDER:
  606. currentVideo = BattleResultVideo::SURRENDER;
  607. text = 309;
  608. break;
  609. default:
  610. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  611. break;
  612. }
  613. playVideo();
  614. labels.push_back(std::make_shared<CLabel>(235, 235, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[text]));
  615. }
  616. }
  617. void BattleResultWindow::activate()
  618. {
  619. owner.showingDialog->set(true);
  620. CIntObject::activate();
  621. }
  622. void BattleResultWindow::show(Canvas & to)
  623. {
  624. CIntObject::show(to);
  625. CCS->videoh->update(pos.x + 107, pos.y + 70, to.getInternalSurface(), true, false,
  626. [&]()
  627. {
  628. playVideo(true);
  629. });
  630. }
  631. void BattleResultWindow::playVideo(bool startLoop)
  632. {
  633. AudioPath musicName = AudioPath();
  634. VideoPath videoName = VideoPath();
  635. if(!startLoop)
  636. {
  637. switch(currentVideo)
  638. {
  639. case BattleResultVideo::WIN:
  640. musicName = AudioPath::builtin("Music/Win Battle");
  641. videoName = VideoPath::builtin("WIN3.BIK");
  642. break;
  643. case BattleResultVideo::SURRENDER:
  644. musicName = AudioPath::builtin("Music/Surrender Battle");
  645. videoName = VideoPath::builtin("SURRENDER.BIK");
  646. break;
  647. case BattleResultVideo::RETREAT:
  648. musicName = AudioPath::builtin("Music/Retreat Battle");
  649. videoName = VideoPath::builtin("RTSTART.BIK");
  650. break;
  651. case BattleResultVideo::DEFEAT:
  652. musicName = AudioPath::builtin("Music/LoseCombat");
  653. videoName = VideoPath::builtin("LBSTART.BIK");
  654. break;
  655. case BattleResultVideo::DEFEAT_SIEGE:
  656. musicName = AudioPath::builtin("Music/LoseCastle");
  657. videoName = VideoPath::builtin("LOSECSTL.BIK");
  658. break;
  659. case BattleResultVideo::WIN_SIEGE:
  660. musicName = AudioPath::builtin("Music/Defend Castle");
  661. videoName = VideoPath::builtin("DEFENDALL.BIK");
  662. break;
  663. }
  664. }
  665. else
  666. {
  667. switch(currentVideo)
  668. {
  669. case BattleResultVideo::RETREAT:
  670. currentVideo = BattleResultVideo::RETREAT_LOOP;
  671. videoName = VideoPath::builtin("RTLOOP.BIK");
  672. break;
  673. case BattleResultVideo::DEFEAT:
  674. currentVideo = BattleResultVideo::DEFEAT_LOOP;
  675. videoName = VideoPath::builtin("LBLOOP.BIK");
  676. break;
  677. case BattleResultVideo::DEFEAT_SIEGE:
  678. currentVideo = BattleResultVideo::DEFEAT_SIEGE_LOOP;
  679. videoName = VideoPath::builtin("LOSECSLP.BIK");
  680. break;
  681. case BattleResultVideo::WIN_SIEGE:
  682. currentVideo = BattleResultVideo::WIN_SIEGE_LOOP;
  683. videoName = VideoPath::builtin("DEFENDLOOP.BIK");
  684. break;
  685. }
  686. }
  687. if(musicName != AudioPath())
  688. CCS->musich->playMusic(musicName, false, true);
  689. if(videoName != VideoPath())
  690. CCS->videoh->open(videoName);
  691. }
  692. void BattleResultWindow::buttonPressed(int button)
  693. {
  694. if (resultCallback)
  695. resultCallback(button);
  696. CPlayerInterface &intTmp = owner; //copy reference because "this" will be destructed soon
  697. close();
  698. if(GH.windows().topWindow<BattleWindow>())
  699. GH.windows().popWindows(1); //pop battle interface if present
  700. //Result window and battle interface are gone. We requested all dialogs to be closed before opening the battle,
  701. //so we can be sure that there is no dialogs left on GUI stack.
  702. intTmp.showingDialog->setn(false);
  703. CCS->videoh->close();
  704. }
  705. void BattleResultWindow::bExitf()
  706. {
  707. buttonPressed(0);
  708. }
  709. void BattleResultWindow::bRepeatf()
  710. {
  711. buttonPressed(1);
  712. }
  713. StackQueue::StackQueue(bool Embedded, BattleInterface & owner)
  714. : embedded(Embedded),
  715. owner(owner)
  716. {
  717. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  718. uint32_t queueSize = QUEUE_SIZE_BIG;
  719. if(embedded)
  720. {
  721. int32_t queueSmallOutsideYOffset = 65;
  722. bool queueSmallOutside = settings["battle"]["queueSmallOutside"].Bool() && (pos.y - queueSmallOutsideYOffset) >= 0;
  723. queueSize = std::clamp(static_cast<int>(settings["battle"]["queueSmallSlots"].Float()), 1, queueSmallOutside ? GH.screenDimensions().x / 41 : 19);
  724. pos.w = queueSize * 41;
  725. pos.h = 49;
  726. pos.x += parent->pos.w/2 - pos.w/2;
  727. pos.y += queueSmallOutside ? -queueSmallOutsideYOffset : 10;
  728. icons = GH.renderHandler().loadAnimation(AnimationPath::builtin("CPRSMALL"));
  729. stateIcons = GH.renderHandler().loadAnimation(AnimationPath::builtin("VCMI/BATTLEQUEUE/STATESSMALL"));
  730. }
  731. else
  732. {
  733. pos.w = 800;
  734. pos.h = 85;
  735. pos.x += 0;
  736. pos.y -= pos.h;
  737. background = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK"), Rect(0, 0, pos.w, pos.h));
  738. icons = GH.renderHandler().loadAnimation(AnimationPath::builtin("TWCRPORT"));
  739. stateIcons = GH.renderHandler().loadAnimation(AnimationPath::builtin("VCMI/BATTLEQUEUE/STATESSMALL"));
  740. //TODO: where use big icons?
  741. //stateIcons = GH.renderHandler().loadAnimation("VCMI/BATTLEQUEUE/STATESBIG");
  742. }
  743. stateIcons->preload();
  744. stackBoxes.resize(queueSize);
  745. for (int i = 0; i < stackBoxes.size(); i++)
  746. {
  747. stackBoxes[i] = std::make_shared<StackBox>(this);
  748. stackBoxes[i]->moveBy(Point(1 + (embedded ? 41 : 80) * i, 0));
  749. }
  750. }
  751. void StackQueue::show(Canvas & to)
  752. {
  753. if (embedded)
  754. showAll(to);
  755. CIntObject::show(to);
  756. }
  757. void StackQueue::update()
  758. {
  759. std::vector<battle::Units> queueData;
  760. owner.getBattle()->battleGetTurnOrder(queueData, stackBoxes.size(), 0);
  761. size_t boxIndex = 0;
  762. ui32 tmpTurn = -1;
  763. for(size_t turn = 0; turn < queueData.size() && boxIndex < stackBoxes.size(); turn++)
  764. {
  765. for(size_t unitIndex = 0; unitIndex < queueData[turn].size() && boxIndex < stackBoxes.size(); boxIndex++, unitIndex++)
  766. {
  767. ui32 currentTurn = owner.round + turn;
  768. stackBoxes[boxIndex]->setUnit(queueData[turn][unitIndex], turn, tmpTurn != currentTurn && owner.round != 0 && (!embedded || tmpTurn != -1) ? (std::optional<ui32>)currentTurn : std::nullopt);
  769. tmpTurn = currentTurn;
  770. }
  771. }
  772. for(; boxIndex < stackBoxes.size(); boxIndex++)
  773. stackBoxes[boxIndex]->setUnit(nullptr);
  774. }
  775. int32_t StackQueue::getSiegeShooterIconID()
  776. {
  777. return owner.siegeController->getSiegedTown()->town->faction->getIndex();
  778. }
  779. std::optional<uint32_t> StackQueue::getHoveredUnitIdIfAny() const
  780. {
  781. for(const auto & stackBox : stackBoxes)
  782. {
  783. if(stackBox->isHovered())
  784. {
  785. return stackBox->getBoundUnitID();
  786. }
  787. }
  788. return std::nullopt;
  789. }
  790. StackQueue::StackBox::StackBox(StackQueue * owner):
  791. CIntObject(SHOW_POPUP | HOVER), owner(owner)
  792. {
  793. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  794. background = std::make_shared<CPicture>(ImagePath::builtin(owner->embedded ? "StackQueueSmall" : "StackQueueLarge"));
  795. pos.w = background->pos.w;
  796. pos.h = background->pos.h;
  797. if(owner->embedded)
  798. {
  799. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 5, 2);
  800. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 7, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  801. roundRect = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, 2, 48), ColorRGBA(0, 0, 0, 255), ColorRGBA(0, 255, 0, 255));
  802. }
  803. else
  804. {
  805. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 9, 1);
  806. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 8, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  807. roundRect = std::make_shared<TransparentFilledRectangle>(Rect(0, 0, 15, 18), ColorRGBA(0, 0, 0, 255), ColorRGBA(241, 216, 120, 255));
  808. round = std::make_shared<CLabel>(4, 2, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE);
  809. int icon_x = pos.w - 17;
  810. int icon_y = pos.h - 18;
  811. stateIcon = std::make_shared<CAnimImage>(owner->stateIcons, 0, 0, icon_x, icon_y);
  812. stateIcon->visible = false;
  813. }
  814. roundRect->disable();
  815. }
  816. void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn, std::optional<ui32> currentTurn)
  817. {
  818. if(unit)
  819. {
  820. boundUnitID = unit->unitId();
  821. background->colorize(unit->unitOwner());
  822. icon->visible = true;
  823. // temporary code for mod compatibility:
  824. // first, set icon that should definitely exist (arrow tower icon in base vcmi mod)
  825. // second, try to switch to icon that should be provided by mod
  826. // if mod is not up to date and does have arrow tower icon yet - second setFrame call will fail and retain previously set image
  827. // for 1.2 release & later next line should be moved into 'else' block
  828. icon->setFrame(unit->creatureIconIndex(), 0);
  829. if (unit->unitType()->getId() == CreatureID::ARROW_TOWERS)
  830. icon->setFrame(owner->getSiegeShooterIconID(), 1);
  831. roundRect->setEnabled(currentTurn.has_value());
  832. if(!owner->embedded)
  833. round->setEnabled(currentTurn.has_value());
  834. amount->setText(TextOperations::formatMetric(unit->getCount(), 4));
  835. if(currentTurn && !owner->embedded)
  836. {
  837. std::string tmp = std::to_string(*currentTurn);
  838. int len = graphics->fonts[FONT_SMALL]->getStringWidth(tmp);
  839. roundRect->pos.w = len + 6;
  840. round->setText(tmp);
  841. }
  842. if(stateIcon)
  843. {
  844. if(unit->defended((int)turn) || (turn > 0 && unit->defended((int)turn - 1)))
  845. {
  846. stateIcon->setFrame(0, 0);
  847. stateIcon->visible = true;
  848. }
  849. else if(unit->waited((int)turn))
  850. {
  851. stateIcon->setFrame(1, 0);
  852. stateIcon->visible = true;
  853. }
  854. else
  855. {
  856. stateIcon->visible = false;
  857. }
  858. }
  859. }
  860. else
  861. {
  862. boundUnitID = std::nullopt;
  863. background->colorize(PlayerColor::NEUTRAL);
  864. icon->visible = false;
  865. icon->setFrame(0);
  866. amount->setText("");
  867. if(stateIcon)
  868. stateIcon->visible = false;
  869. }
  870. }
  871. std::optional<uint32_t> StackQueue::StackBox::getBoundUnitID() const
  872. {
  873. return boundUnitID;
  874. }
  875. bool StackQueue::StackBox::isBoundUnitHighlighted() const
  876. {
  877. auto unitIdsToHighlight = owner->owner.stacksController->getHoveredStacksUnitIds();
  878. return vstd::contains(unitIdsToHighlight, getBoundUnitID());
  879. }
  880. void StackQueue::StackBox::showAll(Canvas & to)
  881. {
  882. CIntObject::showAll(to);
  883. if(isBoundUnitHighlighted())
  884. to.drawBorder(background->pos, Colors::CYAN, 2);
  885. }
  886. void StackQueue::StackBox::show(Canvas & to)
  887. {
  888. CIntObject::show(to);
  889. if(isBoundUnitHighlighted())
  890. to.drawBorder(background->pos, Colors::CYAN, 2);
  891. }
  892. void StackQueue::StackBox::showPopupWindow(const Point & cursorPosition)
  893. {
  894. auto stacks = owner->owner.getBattle()->battleGetAllStacks();
  895. for(const CStack * stack : stacks)
  896. if(boundUnitID.has_value() && stack->unitId() == *boundUnitID)
  897. GH.windows().createAndPushWindow<CStackWindow>(stack, true);
  898. }