BattleInterfaceClasses.cpp 36 KB

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