BattleInterfaceClasses.cpp 36 KB

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