BattleInterfaceClasses.cpp 36 KB

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