BattleInterfaceClasses.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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 "../render/Canvas.h"
  26. #include "../render/IImage.h"
  27. #include "../widgets/Buttons.h"
  28. #include "../widgets/Images.h"
  29. #include "../widgets/TextControls.h"
  30. #include "../windows/CMessage.h"
  31. #include "../windows/CSpellWindow.h"
  32. #include "../render/CAnimation.h"
  33. #include "../adventureMap/CInGameConsole.h"
  34. #include "../renderSDL/SDL_Extensions.h"
  35. #include "../../CCallback.h"
  36. #include "../../lib/CStack.h"
  37. #include "../../lib/CConfigHandler.h"
  38. #include "../../lib/CCreatureHandler.h"
  39. #include "../../lib/CGameState.h"
  40. #include "../../lib/CGeneralTextHandler.h"
  41. #include "../../lib/CTownHandler.h"
  42. #include "../../lib/CHeroHandler.h"
  43. #include "../../lib/NetPacks.h"
  44. #include "../../lib/StartInfo.h"
  45. #include "../../lib/CondSh.h"
  46. #include "../../lib/mapObjects/CGTownInstance.h"
  47. #include "../../lib/TextOperations.h"
  48. void BattleConsole::showAll(SDL_Surface * to)
  49. {
  50. CIntObject::showAll(to);
  51. Point line1 (pos.x + pos.w/2, pos.y + 8);
  52. Point line2 (pos.x + pos.w/2, pos.y + 24);
  53. auto visibleText = getVisibleText();
  54. if(visibleText.size() > 0)
  55. graphics->fonts[FONT_SMALL]->renderTextCenter(to, visibleText[0], Colors::WHITE, line1);
  56. if(visibleText.size() > 1)
  57. graphics->fonts[FONT_SMALL]->renderTextCenter(to, visibleText[1], Colors::WHITE, line2);
  58. }
  59. std::vector<std::string> BattleConsole::getVisibleText()
  60. {
  61. // high priority texts that hide battle log entries
  62. for (auto const & text : {consoleText, hoverText} )
  63. {
  64. if (text.empty())
  65. continue;
  66. auto result = CMessage::breakText(text, pos.w, FONT_SMALL);
  67. if(result.size() > 2)
  68. result.resize(2);
  69. return result;
  70. }
  71. // log is small enough to fit entirely - display it as such
  72. if (logEntries.size() < 3)
  73. return logEntries;
  74. return { logEntries[scrollPosition - 1], logEntries[scrollPosition] };
  75. }
  76. std::vector<std::string> BattleConsole::splitText(const std::string &text)
  77. {
  78. std::vector<std::string> lines;
  79. std::vector<std::string> output;
  80. boost::split(lines, text, boost::is_any_of("\n"));
  81. for (auto const & line : lines)
  82. {
  83. if (graphics->fonts[FONT_SMALL]->getStringWidth(text) < pos.w)
  84. {
  85. output.push_back(line);
  86. }
  87. else
  88. {
  89. std::vector<std::string> substrings = CMessage::breakText(line, pos.w, FONT_SMALL);
  90. output.insert(output.end(), substrings.begin(), substrings.end());
  91. }
  92. }
  93. return output;
  94. }
  95. bool BattleConsole::addText(const std::string & text)
  96. {
  97. logGlobal->trace("CBattleConsole message: %s", text);
  98. auto newLines = splitText(text);
  99. logEntries.insert(logEntries.end(), newLines.begin(), newLines.end());
  100. scrollPosition = (int)logEntries.size()-1;
  101. redraw();
  102. return true;
  103. }
  104. void BattleConsole::scrollUp(ui32 by)
  105. {
  106. if(scrollPosition > static_cast<int>(by))
  107. scrollPosition -= by;
  108. redraw();
  109. }
  110. void BattleConsole::scrollDown(ui32 by)
  111. {
  112. if(scrollPosition + by < logEntries.size())
  113. scrollPosition += by;
  114. redraw();
  115. }
  116. BattleConsole::BattleConsole(std::shared_ptr<CPicture> backgroundSource, const Point & objectPos, const Point & imagePos, const Point &size)
  117. : scrollPosition(-1)
  118. , enteringText(false)
  119. {
  120. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  121. pos += objectPos;
  122. pos.w = size.x;
  123. pos.h = size.y;
  124. background = std::make_shared<CPicture>(backgroundSource->getSurface(), Rect(imagePos, size), 0, 0 );
  125. }
  126. void BattleConsole::deactivate()
  127. {
  128. if (enteringText)
  129. LOCPLINT->cingconsole->endEnteringText(false);
  130. CIntObject::deactivate();
  131. }
  132. void BattleConsole::setEnteringMode(bool on)
  133. {
  134. consoleText.clear();
  135. if (on)
  136. {
  137. assert(enteringText == false);
  138. GH.startTextInput(pos);
  139. }
  140. else
  141. {
  142. assert(enteringText == true);
  143. GH.stopTextInput();
  144. }
  145. enteringText = on;
  146. redraw();
  147. }
  148. void BattleConsole::setEnteredText(const std::string & text)
  149. {
  150. assert(enteringText == true);
  151. consoleText = text;
  152. redraw();
  153. }
  154. void BattleConsole::write(const std::string & Text)
  155. {
  156. hoverText = Text;
  157. redraw();
  158. }
  159. void BattleConsole::clearIfMatching(const std::string & Text)
  160. {
  161. if (hoverText == Text)
  162. clear();
  163. }
  164. void BattleConsole::clear()
  165. {
  166. write({});
  167. }
  168. const CGHeroInstance * BattleHero::instance()
  169. {
  170. return hero;
  171. }
  172. void BattleHero::render(Canvas & canvas)
  173. {
  174. size_t groupIndex = static_cast<size_t>(phase);
  175. auto flagFrame = flagAnimation->getImage(flagCurrentFrame, 0, true);
  176. auto heroFrame = animation->getImage(currentFrame, groupIndex, true);
  177. Point heroPosition = pos.center() - parent->pos.topLeft() - heroFrame->dimensions() / 2;
  178. Point flagPosition = pos.center() - parent->pos.topLeft() - flagFrame->dimensions() / 2;
  179. if(defender)
  180. flagPosition += Point(-4, -41);
  181. else
  182. flagPosition += Point(4, -41);
  183. canvas.draw(flagFrame, flagPosition);
  184. canvas.draw(heroFrame, heroPosition);
  185. float timePassed = float(GH.mainFPSmng->getElapsedMilliseconds()) / 1000.f;
  186. flagCurrentFrame += currentSpeed * timePassed;
  187. currentFrame += currentSpeed * timePassed;
  188. if(flagCurrentFrame >= flagAnimation->size(0))
  189. flagCurrentFrame -= flagAnimation->size(0);
  190. if(currentFrame >= animation->size(groupIndex))
  191. {
  192. currentFrame -= animation->size(groupIndex);
  193. switchToNextPhase();
  194. }
  195. }
  196. void BattleHero::pause()
  197. {
  198. currentSpeed = 0.f;
  199. }
  200. void BattleHero::play()
  201. {
  202. //H3 speed: 10 fps ( 100 ms per frame)
  203. currentSpeed = 10.f;
  204. }
  205. float BattleHero::getFrame() const
  206. {
  207. return currentFrame;
  208. }
  209. void BattleHero::collectRenderableObjects(BattleRenderer & renderer)
  210. {
  211. auto hex = defender ? BattleHex(GameConstants::BFIELD_WIDTH-1) : BattleHex(0);
  212. renderer.insert(EBattleFieldLayer::HEROES, hex, [this](BattleRenderer::RendererRef canvas)
  213. {
  214. render(canvas);
  215. });
  216. }
  217. void BattleHero::onPhaseFinished(const std::function<void()> & callback)
  218. {
  219. phaseFinishedCallback = callback;
  220. }
  221. void BattleHero::setPhase(EHeroAnimType newPhase)
  222. {
  223. nextPhase = newPhase;
  224. switchToNextPhase(); //immediately switch to next phase and then restore idling phase
  225. nextPhase = EHeroAnimType::HOLDING;
  226. }
  227. void BattleHero::heroLeftClicked()
  228. {
  229. if(owner.actionsController->spellcastingModeActive()) //we are casting a spell
  230. return;
  231. if(!hero || !owner.makingTurn())
  232. return;
  233. if(owner.getCurrentPlayerInterface()->cb->battleCanCastSpell(hero, spells::Mode::HERO) == ESpellCastProblem::OK) //check conditions
  234. {
  235. CCS->curh->set(Cursor::Map::POINTER);
  236. GH.pushIntT<CSpellWindow>(hero, owner.getCurrentPlayerInterface());
  237. }
  238. }
  239. void BattleHero::heroRightClicked()
  240. {
  241. Point windowPosition;
  242. windowPosition.x = (!defender) ? owner.fieldController->pos.left() + 1 : owner.fieldController->pos.right() - 79;
  243. windowPosition.y = owner.fieldController->pos.y + 135;
  244. InfoAboutHero targetHero;
  245. if(owner.makingTurn() || settings["session"]["spectate"].Bool())
  246. {
  247. auto h = defender ? owner.defendingHeroInstance : owner.attackingHeroInstance;
  248. targetHero.initFromHero(h, InfoAboutHero::EInfoLevel::INBATTLE);
  249. GH.pushIntT<HeroInfoWindow>(targetHero, &windowPosition);
  250. }
  251. }
  252. void BattleHero::switchToNextPhase()
  253. {
  254. phase = nextPhase;
  255. currentFrame = 0.f;
  256. auto copy = phaseFinishedCallback;
  257. phaseFinishedCallback.clear();
  258. copy();
  259. }
  260. BattleHero::BattleHero(const BattleInterface & owner, const CGHeroInstance * hero, bool defender):
  261. defender(defender),
  262. hero(hero),
  263. owner(owner),
  264. phase(EHeroAnimType::HOLDING),
  265. nextPhase(EHeroAnimType::HOLDING),
  266. currentSpeed(0.f),
  267. currentFrame(0.f),
  268. flagCurrentFrame(0.f)
  269. {
  270. std::string animationPath;
  271. if(!hero->type->battleImage.empty())
  272. animationPath = hero->type->battleImage;
  273. else
  274. if(hero->sex)
  275. animationPath = hero->type->heroClass->imageBattleFemale;
  276. else
  277. animationPath = hero->type->heroClass->imageBattleMale;
  278. animation = std::make_shared<CAnimation>(animationPath);
  279. animation->preload();
  280. pos.w = 64;
  281. pos.h = 136;
  282. pos.x = owner.fieldController->pos.x + (defender ? (owner.fieldController->pos.w - pos.w) : 0);
  283. pos.y = owner.fieldController->pos.y;
  284. if(defender)
  285. animation->verticalFlip();
  286. if(defender)
  287. flagAnimation = std::make_shared<CAnimation>("CMFLAGR");
  288. else
  289. flagAnimation = std::make_shared<CAnimation>("CMFLAGL");
  290. flagAnimation->preload();
  291. flagAnimation->playerColored(hero->tempOwner);
  292. switchToNextPhase();
  293. play();
  294. }
  295. HeroInfoWindow::HeroInfoWindow(const InfoAboutHero & hero, Point * position)
  296. : CWindowObject(RCLICK_POPUP | SHADOW_DISABLED, "CHRPOP")
  297. {
  298. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  299. if (position != nullptr)
  300. moveTo(*position);
  301. background->colorize(hero.owner); //maybe add this functionality to base class?
  302. auto attack = hero.details->primskills[0];
  303. auto defense = hero.details->primskills[1];
  304. auto power = hero.details->primskills[2];
  305. auto knowledge = hero.details->primskills[3];
  306. auto morale = hero.details->morale;
  307. auto luck = hero.details->luck;
  308. auto currentSpellPoints = hero.details->mana;
  309. auto maxSpellPoints = hero.details->manaLimit;
  310. icons.push_back(std::make_shared<CAnimImage>("PortraitsLarge", hero.portrait, 0, 10, 6));
  311. //primary stats
  312. labels.push_back(std::make_shared<CLabel>(9, 75, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[380] + ":"));
  313. labels.push_back(std::make_shared<CLabel>(9, 87, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[381] + ":"));
  314. labels.push_back(std::make_shared<CLabel>(9, 99, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[382] + ":"));
  315. labels.push_back(std::make_shared<CLabel>(9, 111, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[383] + ":"));
  316. labels.push_back(std::make_shared<CLabel>(69, 87, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(attack)));
  317. labels.push_back(std::make_shared<CLabel>(69, 99, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(defense)));
  318. labels.push_back(std::make_shared<CLabel>(69, 111, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(power)));
  319. labels.push_back(std::make_shared<CLabel>(69, 123, EFonts::FONT_TINY, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, std::to_string(knowledge)));
  320. //morale+luck
  321. labels.push_back(std::make_shared<CLabel>(9, 131, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[384] + ":"));
  322. labels.push_back(std::make_shared<CLabel>(9, 143, EFonts::FONT_TINY, ETextAlignment::TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[385] + ":"));
  323. icons.push_back(std::make_shared<CAnimImage>("IMRL22", morale + 3, 0, 47, 131));
  324. icons.push_back(std::make_shared<CAnimImage>("ILCK22", luck + 3, 0, 47, 143));
  325. //spell points
  326. labels.push_back(std::make_shared<CLabel>(39, 174, EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[387]));
  327. labels.push_back(std::make_shared<CLabel>(39, 186, EFonts::FONT_TINY, ETextAlignment::CENTER, Colors::WHITE, std::to_string(currentSpellPoints) + "/" + std::to_string(maxSpellPoints)));
  328. }
  329. BattleResultWindow::BattleResultWindow(const BattleResult & br, CPlayerInterface & _owner)
  330. : owner(_owner)
  331. {
  332. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  333. background = std::make_shared<CPicture>("CPRESULT");
  334. background->colorize(owner.playerID);
  335. pos = center(background->pos);
  336. exit = std::make_shared<CButton>(Point(384, 505), "iok6432.def", std::make_pair("", ""), [&](){ bExitf();}, SDLK_RETURN);
  337. exit->setBorderColor(Colors::METALLIC_GOLD);
  338. if(br.winner == 0) //attacker won
  339. {
  340. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  341. }
  342. else
  343. {
  344. labels.push_back(std::make_shared<CLabel>(59, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  345. }
  346. if(br.winner == 1)
  347. {
  348. labels.push_back(std::make_shared<CLabel>(412, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]));
  349. }
  350. else
  351. {
  352. labels.push_back(std::make_shared<CLabel>(408, 124, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]));
  353. }
  354. labels.push_back(std::make_shared<CLabel>(232, 302, FONT_BIG, ETextAlignment::CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[407]));
  355. labels.push_back(std::make_shared<CLabel>(232, 332, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[408]));
  356. labels.push_back(std::make_shared<CLabel>(232, 428, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[409]));
  357. std::string sideNames[2] = {"N/A", "N/A"};
  358. for(int i = 0; i < 2; i++)
  359. {
  360. auto heroInfo = owner.cb->battleGetHeroInfo(i);
  361. const int xs[] = {21, 392};
  362. if(heroInfo.portrait >= 0) //attacking hero
  363. {
  364. icons.push_back(std::make_shared<CAnimImage>("PortraitsLarge", heroInfo.portrait, 0, xs[i], 38));
  365. sideNames[i] = heroInfo.name;
  366. }
  367. else
  368. {
  369. auto stacks = owner.cb->battleGetAllStacks();
  370. vstd::erase_if(stacks, [i](const CStack * stack) //erase stack of other side and not coming from garrison
  371. {
  372. return stack->side != i || !stack->base;
  373. });
  374. auto best = vstd::maxElementByFun(stacks, [](const CStack * stack)
  375. {
  376. return stack->type->getAIValue();
  377. });
  378. if(best != stacks.end()) //should be always but to be safe...
  379. {
  380. icons.push_back(std::make_shared<CAnimImage>("TWCRPORT", (*best)->type->getIconIndex(), 0, xs[i], 38));
  381. sideNames[i] = (*best)->type->getNamePluralTranslated();
  382. }
  383. }
  384. }
  385. //printing attacker and defender's names
  386. labels.push_back(std::make_shared<CLabel>(89, 37, FONT_SMALL, ETextAlignment::TOPLEFT, Colors::WHITE, sideNames[0]));
  387. labels.push_back(std::make_shared<CLabel>(381, 53, FONT_SMALL, ETextAlignment::BOTTOMRIGHT, Colors::WHITE, sideNames[1]));
  388. //printing casualties
  389. for(int step = 0; step < 2; ++step)
  390. {
  391. if(br.casualties[step].size()==0)
  392. {
  393. labels.push_back(std::make_shared<CLabel>(235, 360 + 97 * step, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[523]));
  394. }
  395. else
  396. {
  397. int xPos = 235 - ((int)br.casualties[step].size()*32 + ((int)br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  398. int yPos = 344 + step * 97;
  399. for(auto & elem : br.casualties[step])
  400. {
  401. auto creature = CGI->creatures()->getByIndex(elem.first);
  402. if (creature->getId() == CreatureID::ARROW_TOWERS )
  403. continue; // do not show destroyed towers in battle results
  404. icons.push_back(std::make_shared<CAnimImage>("CPRSMALL", creature->getIconIndex(), 0, xPos, yPos));
  405. std::ostringstream amount;
  406. amount<<elem.second;
  407. labels.push_back(std::make_shared<CLabel>(xPos + 16, yPos + 42, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, amount.str()));
  408. xPos += 42;
  409. }
  410. }
  411. }
  412. //printing result description
  413. bool weAreAttacker = !(owner.cb->battleGetMySide());
  414. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  415. {
  416. int text = 304;
  417. switch(br.result)
  418. {
  419. case BattleResult::NORMAL:
  420. break;
  421. case BattleResult::ESCAPE:
  422. text = 303;
  423. break;
  424. case BattleResult::SURRENDER:
  425. text = 302;
  426. break;
  427. default:
  428. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  429. break;
  430. }
  431. CCS->musich->playMusic("Music/Win Battle", false, true);
  432. CCS->videoh->open("WIN3.BIK");
  433. std::string str = CGI->generaltexth->allTexts[text];
  434. const CGHeroInstance * ourHero = owner.cb->battleGetMyHero();
  435. if (ourHero)
  436. {
  437. str += CGI->generaltexth->allTexts[305];
  438. boost::algorithm::replace_first(str, "%s", ourHero->getNameTranslated());
  439. boost::algorithm::replace_first(str, "%d", std::to_string(br.exp[weAreAttacker ? 0 : 1]));
  440. }
  441. description = std::make_shared<CTextBox>(str, Rect(69, 203, 330, 68), 0, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  442. }
  443. else // we lose
  444. {
  445. int text = 311;
  446. std::string musicName = "Music/LoseCombat";
  447. std::string videoName = "LBSTART.BIK";
  448. switch(br.result)
  449. {
  450. case BattleResult::NORMAL:
  451. break;
  452. case BattleResult::ESCAPE:
  453. musicName = "Music/Retreat Battle";
  454. videoName = "RTSTART.BIK";
  455. text = 310;
  456. break;
  457. case BattleResult::SURRENDER:
  458. musicName = "Music/Surrender Battle";
  459. videoName = "SURRENDER.BIK";
  460. text = 309;
  461. break;
  462. default:
  463. logGlobal->error("Invalid battle result code %d. Assumed normal.", static_cast<int>(br.result));
  464. break;
  465. }
  466. CCS->musich->playMusic(musicName, false, true);
  467. CCS->videoh->open(videoName);
  468. labels.push_back(std::make_shared<CLabel>(235, 235, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE, CGI->generaltexth->allTexts[text]));
  469. }
  470. }
  471. void BattleResultWindow::activate()
  472. {
  473. owner.showingDialog->set(true);
  474. CIntObject::activate();
  475. }
  476. void BattleResultWindow::show(SDL_Surface * to)
  477. {
  478. CIntObject::show(to);
  479. CCS->videoh->update(pos.x + 107, pos.y + 70, to, true, false);
  480. }
  481. void BattleResultWindow::bExitf()
  482. {
  483. CPlayerInterface &intTmp = owner; //copy reference because "this" will be destructed soon
  484. close();
  485. if(dynamic_cast<BattleWindow*>(GH.topInt().get()))
  486. GH.popInts(1); //pop battle interface if present
  487. //Result window and battle interface are gone. We requested all dialogs to be closed before opening the battle,
  488. //so we can be sure that there is no dialogs left on GUI stack.
  489. intTmp.showingDialog->setn(false);
  490. CCS->videoh->close();
  491. }
  492. StackQueue::StackQueue(bool Embedded, BattleInterface & owner)
  493. : embedded(Embedded),
  494. owner(owner)
  495. {
  496. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  497. if(embedded)
  498. {
  499. pos.w = QUEUE_SIZE * 41;
  500. pos.h = 49;
  501. pos.x += parent->pos.w/2 - pos.w/2;
  502. pos.y += 10;
  503. icons = std::make_shared<CAnimation>("CPRSMALL");
  504. stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESSMALL");
  505. }
  506. else
  507. {
  508. pos.w = 800;
  509. pos.h = 85;
  510. pos.x += 0;
  511. pos.y -= pos.h;
  512. background = std::make_shared<CFilledTexture>("DIBOXBCK", Rect(0, 0, pos.w, pos.h));
  513. icons = std::make_shared<CAnimation>("TWCRPORT");
  514. stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESSMALL");
  515. //TODO: where use big icons?
  516. //stateIcons = std::make_shared<CAnimation>("VCMI/BATTLEQUEUE/STATESBIG");
  517. }
  518. stateIcons->preload();
  519. stackBoxes.resize(QUEUE_SIZE);
  520. for (int i = 0; i < stackBoxes.size(); i++)
  521. {
  522. stackBoxes[i] = std::make_shared<StackBox>(this);
  523. stackBoxes[i]->moveBy(Point(1 + (embedded ? 41 : 80) * i, 0));
  524. }
  525. }
  526. void StackQueue::show(SDL_Surface * to)
  527. {
  528. auto unitIdsToHighlight = owner.stacksController->getHoveredStacksUnitIds();
  529. for(auto & stackBox : stackBoxes)
  530. {
  531. bool isBoundUnitCurrentlyHovered = vstd::contains(unitIdsToHighlight, stackBox->getBoundUnitID());
  532. stackBox->toggleHighlight(isBoundUnitCurrentlyHovered);
  533. }
  534. if (embedded)
  535. showAll(to);
  536. CIntObject::show(to);
  537. }
  538. void StackQueue::update()
  539. {
  540. std::vector<battle::Units> queueData;
  541. owner.getCurrentPlayerInterface()->cb->battleGetTurnOrder(queueData, stackBoxes.size(), 0);
  542. size_t boxIndex = 0;
  543. for(size_t turn = 0; turn < queueData.size() && boxIndex < stackBoxes.size(); turn++)
  544. {
  545. for(size_t unitIndex = 0; unitIndex < queueData[turn].size() && boxIndex < stackBoxes.size(); boxIndex++, unitIndex++)
  546. stackBoxes[boxIndex]->setUnit(queueData[turn][unitIndex], turn);
  547. }
  548. for(; boxIndex < stackBoxes.size(); boxIndex++)
  549. stackBoxes[boxIndex]->setUnit(nullptr);
  550. }
  551. int32_t StackQueue::getSiegeShooterIconID()
  552. {
  553. return owner.siegeController->getSiegedTown()->town->faction->getIndex();
  554. }
  555. boost::optional<uint32_t> StackQueue::getHoveredUnitIdIfAny() const
  556. {
  557. for(const auto & stackBox : stackBoxes)
  558. {
  559. if(stackBox->hovered || stackBox->mouseState(MouseButton::RIGHT))
  560. {
  561. return stackBox->getBoundUnitID();
  562. }
  563. }
  564. return boost::none;
  565. }
  566. StackQueue::StackBox::StackBox(StackQueue * owner):
  567. CIntObject(RCLICK | HOVER), owner(owner)
  568. {
  569. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  570. background = std::make_shared<CPicture>(owner->embedded ? "StackQueueSmall" : "StackQueueLarge");
  571. pos.w = background->pos.w;
  572. pos.h = background->pos.h;
  573. if(owner->embedded)
  574. {
  575. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 5, 2);
  576. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 7, FONT_SMALL, ETextAlignment::CENTER, Colors::WHITE);
  577. }
  578. else
  579. {
  580. icon = std::make_shared<CAnimImage>(owner->icons, 0, 0, 9, 1);
  581. amount = std::make_shared<CLabel>(pos.w/2, pos.h - 8, FONT_MEDIUM, ETextAlignment::CENTER, Colors::WHITE);
  582. int icon_x = pos.w - 17;
  583. int icon_y = pos.h - 18;
  584. stateIcon = std::make_shared<CAnimImage>(owner->stateIcons, 0, 0, icon_x, icon_y);
  585. stateIcon->visible = false;
  586. }
  587. }
  588. void StackQueue::StackBox::setUnit(const battle::Unit * unit, size_t turn)
  589. {
  590. if(unit)
  591. {
  592. boundUnitID = unit->unitId();
  593. background->colorize(unit->unitOwner());
  594. icon->visible = true;
  595. // temporary code for mod compatibility:
  596. // first, set icon that should definitely exist (arrow tower icon in base vcmi mod)
  597. // second, try to switch to icon that should be provided by mod
  598. // if mod is not up to date and does have arrow tower icon yet - second setFrame call will fail and retain previously set image
  599. // for 1.2 release & later next line should be moved into 'else' block
  600. icon->setFrame(unit->creatureIconIndex(), 0);
  601. if (unit->unitType()->getId() == CreatureID::ARROW_TOWERS)
  602. icon->setFrame(owner->getSiegeShooterIconID(), 1);
  603. amount->setText(TextOperations::formatMetric(unit->getCount(), 4));
  604. if(stateIcon)
  605. {
  606. if(unit->defended((int)turn) || (turn > 0 && unit->defended((int)turn - 1)))
  607. {
  608. stateIcon->setFrame(0, 0);
  609. stateIcon->visible = true;
  610. }
  611. else if(unit->waited((int)turn))
  612. {
  613. stateIcon->setFrame(1, 0);
  614. stateIcon->visible = true;
  615. }
  616. else
  617. {
  618. stateIcon->visible = false;
  619. }
  620. }
  621. }
  622. else
  623. {
  624. boundUnitID = boost::none;
  625. background->colorize(PlayerColor::NEUTRAL);
  626. icon->visible = false;
  627. icon->setFrame(0);
  628. amount->setText("");
  629. if(stateIcon)
  630. stateIcon->visible = false;
  631. }
  632. }
  633. boost::optional<uint32_t> StackQueue::StackBox::getBoundUnitID() const
  634. {
  635. return boundUnitID;
  636. }
  637. void StackQueue::StackBox::toggleHighlight(bool value)
  638. {
  639. highlighted = value;
  640. }
  641. void StackQueue::StackBox::show(SDL_Surface *to)
  642. {
  643. if(highlighted)
  644. CSDL_Ext::drawBorder(to, background->pos.x, background->pos.y, background->pos.w, background->pos.h, { 0, 255, 255, 255 }, 2);
  645. CIntObject::show(to);
  646. }