BattleInterfaceClasses.cpp 36 KB

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