CBattleInterfaceClasses.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. #include "StdInc.h"
  2. #include "CBattleInterfaceClasses.h"
  3. #include "../gui/SDL_Extensions.h"
  4. #include "CBattleInterface.h"
  5. #include "../CGameInfo.h"
  6. #include "../CDefHandler.h"
  7. #include "../gui/CCursorHandler.h"
  8. #include "../CPlayerInterface.h"
  9. #include "../../CCallback.h"
  10. #include "../CSpellWindow.h"
  11. #include "../Graphics.h"
  12. #include "../../lib/CConfigHandler.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../gui/CIntObjectClasses.h"
  15. #include "../../lib/CGeneralTextHandler.h"
  16. #include "../../lib/NetPacks.h"
  17. #include "../../lib/CCreatureHandler.h"
  18. #include "../../lib/BattleState.h"
  19. #include "../../lib/StartInfo.h"
  20. #include "../CMusicHandler.h"
  21. #include "../CVideoHandler.h"
  22. #include "../../lib/CTownHandler.h"
  23. #include "../CBitmapHandler.h"
  24. #include "../CCreatureWindow.h"
  25. #include "../CMessage.h"
  26. /*
  27. * CBattleInterfaceClasses.cpp, part of VCMI engine
  28. *
  29. * Authors: listed in file AUTHORS in main folder
  30. *
  31. * License: GNU General Public License v2.0 or later
  32. * Full text of license available in license.txt file, in main folder
  33. *
  34. */
  35. void CBattleConsole::showAll(SDL_Surface * to)
  36. {
  37. Point textPos(pos.x + pos.w/2, pos.y + 17);
  38. if(ingcAlter.size())
  39. {
  40. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(ingcAlter, pos.w, FONT_SMALL), Colors::WHITE, textPos);
  41. }
  42. else if(alterTxt.size())
  43. {
  44. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(alterTxt, pos.w, FONT_SMALL), Colors::WHITE, textPos);
  45. }
  46. else if(texts.size())
  47. {
  48. if(texts.size()==1)
  49. {
  50. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(texts[0], pos.w, FONT_SMALL), Colors::WHITE, textPos);
  51. }
  52. else
  53. {
  54. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(texts[lastShown - 1], pos.w, FONT_SMALL), Colors::WHITE, textPos);
  55. textPos.y += 16;
  56. graphics->fonts[FONT_SMALL]->renderTextLinesCenter(to, CMessage::breakText(texts[lastShown], pos.w, FONT_SMALL), Colors::WHITE, textPos);
  57. }
  58. }
  59. }
  60. bool CBattleConsole::addText(const std::string & text)
  61. {
  62. if(text.size()>70)
  63. return false; //text too long!
  64. int firstInToken = 0;
  65. for(size_t i = 0; i < text.size(); ++i) //tokenize
  66. {
  67. if(text[i] == 10)
  68. {
  69. texts.push_back( text.substr(firstInToken, i-firstInToken) );
  70. firstInToken = i+1;
  71. }
  72. }
  73. texts.push_back( text.substr(firstInToken, text.size()) );
  74. lastShown = texts.size()-1;
  75. return true;
  76. }
  77. void CBattleConsole::alterText(const std::string &text)
  78. {
  79. //char buf[500];
  80. //sprintf(buf, text.c_str());
  81. //alterTxt = buf;
  82. alterTxt = text;
  83. }
  84. void CBattleConsole::eraseText(ui32 pos)
  85. {
  86. if(pos < texts.size())
  87. {
  88. texts.erase(texts.begin() + pos);
  89. if(lastShown == texts.size())
  90. --lastShown;
  91. }
  92. }
  93. void CBattleConsole::changeTextAt(const std::string & text, ui32 pos)
  94. {
  95. if(pos >= texts.size()) //no such pos
  96. return;
  97. texts[pos] = text;
  98. }
  99. void CBattleConsole::scrollUp(ui32 by)
  100. {
  101. if(lastShown > static_cast<int>(by))
  102. lastShown -= by;
  103. }
  104. void CBattleConsole::scrollDown(ui32 by)
  105. {
  106. if(lastShown + by < texts.size())
  107. lastShown += by;
  108. }
  109. CBattleConsole::CBattleConsole() : lastShown(-1), alterTxt(""), whoSetAlter(0)
  110. {}
  111. void CBattleHero::show(SDL_Surface * to)
  112. {
  113. //animation of flag
  114. SDL_Rect temp_rect;
  115. if(flip)
  116. {
  117. temp_rect = genRect(
  118. flag->ourImages[flagAnim].bitmap->h,
  119. flag->ourImages[flagAnim].bitmap->w,
  120. pos.x + 61,
  121. pos.y + 39);
  122. }
  123. else
  124. {
  125. temp_rect = genRect(
  126. flag->ourImages[flagAnim].bitmap->h,
  127. flag->ourImages[flagAnim].bitmap->w,
  128. pos.x + 72,
  129. pos.y + 39);
  130. }
  131. CSDL_Ext::blit8bppAlphaTo24bpp(
  132. flag->ourImages[flagAnim].bitmap,
  133. nullptr,
  134. screen,
  135. &temp_rect);
  136. //animation of hero
  137. SDL_Rect rect = pos;
  138. CSDL_Ext::blit8bppAlphaTo24bpp(dh->ourImages[currentFrame].bitmap, nullptr, to, &rect);
  139. if ( ++animCount == 4 )
  140. {
  141. animCount = 0;
  142. if ( ++flagAnim >= flag->ourImages.size())
  143. flagAnim = 0;
  144. if ( ++currentFrame >= lastFrame)
  145. switchToNextPhase();
  146. }
  147. }
  148. void CBattleHero::setPhase(int newPhase)
  149. {
  150. nextPhase = newPhase;
  151. switchToNextPhase(); //immediately switch to next phase and then restore idling phase
  152. nextPhase = 0;
  153. }
  154. void CBattleHero::clickLeft(tribool down, bool previousState)
  155. {
  156. if(myOwner->spellDestSelectMode) //we are casting a spell
  157. return;
  158. if(!down && myHero != nullptr && myOwner->myTurn && myOwner->getCurrentPlayerInterface()->cb->battleCanCastSpell()) //check conditions
  159. {
  160. for(int it=0; it<GameConstants::BFIELD_SIZE; ++it) //do nothing when any hex is hovered - hero's animation overlaps battlefield
  161. {
  162. if(myOwner->bfield[it]->hovered && myOwner->bfield[it]->strictHovered)
  163. return;
  164. }
  165. CCS->curh->changeGraphic(ECursor::ADVENTURE, 0);
  166. auto spellWindow = new CSpellWindow(genRect(595, 620, (screen->w - 620)/2, (screen->h - 595)/2), myHero, myOwner->getCurrentPlayerInterface());
  167. GH.pushInt(spellWindow);
  168. }
  169. }
  170. void CBattleHero::switchToNextPhase()
  171. {
  172. if (phase != nextPhase)
  173. {
  174. phase = nextPhase;
  175. //find first and last frames of our animation
  176. for (firstFrame = 0;
  177. firstFrame < dh->ourImages.size() && dh->ourImages[firstFrame].groupNumber != phase;
  178. firstFrame++);
  179. for (lastFrame = firstFrame;
  180. lastFrame < dh->ourImages.size() && dh->ourImages[lastFrame].groupNumber == phase;
  181. lastFrame++);
  182. }
  183. currentFrame = firstFrame;
  184. }
  185. CBattleHero::CBattleHero(const std::string & defName, bool flipG, PlayerColor player, const CGHeroInstance * hero, const CBattleInterface * owner):
  186. flip(flipG),
  187. myHero(hero),
  188. myOwner(owner),
  189. phase(1),
  190. nextPhase(0),
  191. flagAnim(0),
  192. animCount(0)
  193. {
  194. dh = CDefHandler::giveDef( defName );
  195. for(auto & elem : dh->ourImages) //transforming images
  196. {
  197. if(flip)
  198. {
  199. SDL_Surface * hlp = CSDL_Ext::verticalFlip(elem.bitmap);
  200. SDL_FreeSurface(elem.bitmap);
  201. elem.bitmap = hlp;
  202. }
  203. CSDL_Ext::alphaTransform(elem.bitmap);
  204. }
  205. if(flip)
  206. flag = CDefHandler::giveDef("CMFLAGR.DEF");
  207. else
  208. flag = CDefHandler::giveDef("CMFLAGL.DEF");
  209. //coloring flag and adding transparency
  210. for(auto & elem : flag->ourImages)
  211. {
  212. CSDL_Ext::alphaTransform(elem.bitmap);
  213. graphics->blueToPlayersAdv(elem.bitmap, player);
  214. }
  215. addUsedEvents(LCLICK);
  216. switchToNextPhase();
  217. }
  218. CBattleHero::~CBattleHero()
  219. {
  220. delete dh;
  221. delete flag;
  222. }
  223. CBattleOptionsWindow::CBattleOptionsWindow(const SDL_Rect & position, CBattleInterface *owner)
  224. {
  225. OBJ_CONSTRUCTION_CAPTURING_ALL;
  226. pos = position;
  227. background = new CPicture("comopbck.bmp");
  228. background->colorize(owner->getCurrentPlayerInterface()->playerID);
  229. viewGrid = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintCellBorders, owner, true), boost::bind(&CBattleInterface::setPrintCellBorders, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[427].first)(3,CGI->generaltexth->zelp[427].first), CGI->generaltexth->zelp[427].second, false, "sysopchk.def", nullptr, 25, 56, false);
  230. viewGrid->select(settings["battle"]["cellBorders"].Bool());
  231. movementShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintStackRange, owner, true), boost::bind(&CBattleInterface::setPrintStackRange, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[428].first)(3,CGI->generaltexth->zelp[428].first), CGI->generaltexth->zelp[428].second, false, "sysopchk.def", nullptr, 25, 89, false);
  232. movementShadow->select(settings["battle"]["stackRange"].Bool());
  233. mouseShadow = new CHighlightableButton(boost::bind(&CBattleInterface::setPrintMouseShadow, owner, true), boost::bind(&CBattleInterface::setPrintMouseShadow, owner, false), boost::assign::map_list_of(0,CGI->generaltexth->zelp[429].first)(3,CGI->generaltexth->zelp[429].first), CGI->generaltexth->zelp[429].second, false, "sysopchk.def", nullptr, 25, 122, false);
  234. mouseShadow->select(settings["battle"]["mouseShadow"].Bool());
  235. animSpeeds = new CHighlightableButtonsGroup(0);
  236. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[422].first),CGI->generaltexth->zelp[422].second, "sysopb9.def", 28, 225, 40);
  237. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[423].first),CGI->generaltexth->zelp[423].second, "sysob10.def", 92, 225, 63);
  238. animSpeeds->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[424].first),CGI->generaltexth->zelp[424].second, "sysob11.def",156, 225, 100);
  239. animSpeeds->select(owner->getAnimSpeed(), 1);
  240. animSpeeds->onChange = boost::bind(&CBattleInterface::setAnimSpeed, owner, _1);
  241. setToDefault = new CAdventureMapButton (CGI->generaltexth->zelp[393], boost::bind(&CBattleOptionsWindow::bDefaultf,this), 246, 359, "codefaul.def");
  242. setToDefault->swappedImages = true;
  243. setToDefault->update();
  244. exit = new CAdventureMapButton (CGI->generaltexth->zelp[392], boost::bind(&CBattleOptionsWindow::bExitf,this), 357, 359, "soretrn.def",SDLK_RETURN);
  245. exit->swappedImages = true;
  246. exit->update();
  247. //creating labels
  248. labels.push_back(new CLabel(242, 32, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[392]));//window title
  249. labels.push_back(new CLabel(122, 214, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[393]));//animation speed
  250. labels.push_back(new CLabel(122, 293, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[394]));//music volume
  251. labels.push_back(new CLabel(122, 359, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[395]));//effects' volume
  252. labels.push_back(new CLabel(353, 66, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[396]));//auto - combat options
  253. labels.push_back(new CLabel(353, 265, FONT_MEDIUM, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[397]));//creature info
  254. //auto - combat options
  255. labels.push_back(new CLabel(283, 86, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[398]));//creatures
  256. labels.push_back(new CLabel(283, 116, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[399]));//spells
  257. labels.push_back(new CLabel(283, 146, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[400]));//catapult
  258. labels.push_back(new CLabel(283, 176, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[151]));//ballista
  259. labels.push_back(new CLabel(283, 206, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[401]));//first aid tent
  260. //creature info
  261. labels.push_back(new CLabel(283, 285, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[402]));//all stats
  262. labels.push_back(new CLabel(283, 315, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[403]));//spells only
  263. //general options
  264. labels.push_back(new CLabel(61, 57, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[404]));
  265. labels.push_back(new CLabel(61, 90, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[405]));
  266. labels.push_back(new CLabel(61, 123, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[406]));
  267. labels.push_back(new CLabel(61, 156, FONT_MEDIUM, TOPLEFT, Colors::WHITE, CGI->generaltexth->allTexts[407]));
  268. }
  269. void CBattleOptionsWindow::bDefaultf()
  270. {
  271. }
  272. void CBattleOptionsWindow::bExitf()
  273. {
  274. GH.popIntTotally(this);
  275. }
  276. CBattleResultWindow::CBattleResultWindow(const BattleResult &br, const SDL_Rect & pos, CPlayerInterface &_owner)
  277. : owner(_owner)
  278. {
  279. OBJ_CONSTRUCTION_CAPTURING_ALL;
  280. this->pos = pos;
  281. CPicture * bg = new CPicture("CPRESULT");
  282. bg->colorize(owner.playerID);
  283. exit = new CAdventureMapButton ("", "", boost::bind(&CBattleResultWindow::bExitf,this), 384, 505, "iok6432.def", SDLK_RETURN);
  284. exit->borderColor = Colors::METALLIC_GOLD;
  285. exit->borderEnabled = true;
  286. if(br.winner==0) //attacker won
  287. {
  288. new CLabel( 59, 124, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]);
  289. new CLabel(408, 124, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]);
  290. }
  291. else //if(br.winner==1)
  292. {
  293. new CLabel( 59, 124, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[411]);
  294. new CLabel(412, 124, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[410]);
  295. }
  296. new CLabel(232, 302, FONT_BIG, CENTER, Colors::YELLOW, CGI->generaltexth->allTexts[407]);
  297. new CLabel(232, 332, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[408]);
  298. new CLabel(232, 428, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[409]);
  299. std::string sideNames[2] = {"N/A", "N/A"};
  300. for(int i = 0; i < 2; i++)
  301. {
  302. auto heroInfo = owner.cb->battleGetHeroInfo(i);
  303. const int xs[] = {21, 392};
  304. if(heroInfo.portrait >= 0) //attacking hero
  305. {
  306. new CAnimImage("PortraitsLarge", heroInfo.portrait, 0, xs[i], 38);
  307. sideNames[i] = heroInfo.name;
  308. }
  309. else
  310. {
  311. auto stacks = owner.cb->battleGetAllStacks();
  312. vstd::erase_if(stacks, [i](const CStack *stack) //erase stack of other side and not coming from garrison
  313. { return stack->attackerOwned == i || !stack->base; });
  314. auto best = vstd::maxElementByFun(stacks, [](const CStack *stack){ return stack->type->AIValue; });
  315. if(best != stacks.end()) //should be always but to be safe...
  316. {
  317. new CAnimImage("TWCRPORT", (*best)->type->idNumber+2, 0, xs[i], 38);
  318. sideNames[i] = CGI->creh->creatures[(*best)->type->idNumber]->namePl;
  319. }
  320. }
  321. }
  322. //printing attacker and defender's names
  323. new CLabel( 89, 37, FONT_SMALL, TOPLEFT, Colors::WHITE, sideNames[0]);
  324. new CLabel( 381, 53, FONT_SMALL, BOTTOMRIGHT, Colors::WHITE, sideNames[1]);
  325. //printing casualties
  326. for(int step = 0; step < 2; ++step)
  327. {
  328. if(br.casualties[step].size()==0)
  329. {
  330. new CLabel( 235, 360 + 97*step, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[523]);
  331. }
  332. else
  333. {
  334. int xPos = 235 - (br.casualties[step].size()*32 + (br.casualties[step].size() - 1)*10)/2; //increment by 42 with each picture
  335. int yPos = 344 + step*97;
  336. for(auto & elem : br.casualties[step])
  337. {
  338. new CAnimImage("CPRSMALL", CGI->creh->creatures[elem.first]->iconIndex, 0, xPos, yPos);
  339. std::ostringstream amount;
  340. amount<<elem.second;
  341. new CLabel( xPos+16, yPos + 42, FONT_SMALL, CENTER, Colors::WHITE, amount.str());
  342. xPos += 42;
  343. }
  344. }
  345. }
  346. //printing result description
  347. bool weAreAttacker = !(owner.cb->battleGetMySide());
  348. if((br.winner == 0 && weAreAttacker) || (br.winner == 1 && !weAreAttacker)) //we've won
  349. {
  350. int text=-1;
  351. switch(br.result)
  352. {
  353. case BattleResult::NORMAL: text = 304; break;
  354. case BattleResult::ESCAPE: text = 303; break;
  355. case BattleResult::SURRENDER: text = 302; break;
  356. }
  357. CCS->musich->playMusic("Music/Win Battle", false);
  358. CCS->videoh->open("WIN3.BIK");
  359. std::string str = CGI->generaltexth->allTexts[text];
  360. const CGHeroInstance * ourHero = owner.cb->battleGetMyHero();
  361. if (ourHero)
  362. {
  363. str += CGI->generaltexth->allTexts[305];
  364. boost::algorithm::replace_first(str,"%s",ourHero->name);
  365. boost::algorithm::replace_first(str,"%d",boost::lexical_cast<std::string>(br.exp[weAreAttacker?0:1]));
  366. }
  367. new CTextBox(str, Rect(69, 203, 330, 68), 0, FONT_SMALL, CENTER, Colors::WHITE);
  368. }
  369. else // we lose
  370. {
  371. switch(br.result)
  372. {
  373. case BattleResult::NORMAL:
  374. {
  375. CCS->musich->playMusic("Music/LoseCombat", false);
  376. CCS->videoh->open("LBSTART.BIK");
  377. new CLabel(235, 235, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[311]);
  378. break;
  379. }
  380. case BattleResult::ESCAPE: //flee
  381. {
  382. CCS->musich->playMusic("Music/Retreat Battle", false);
  383. CCS->videoh->open("RTSTART.BIK");
  384. new CLabel(235, 235, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[310]);
  385. break;
  386. }
  387. case BattleResult::SURRENDER:
  388. {
  389. CCS->musich->playMusic("Music/Surrender Battle", false);
  390. CCS->videoh->open("SURRENDER.BIK");
  391. new CLabel(235, 235, FONT_SMALL, CENTER, Colors::WHITE, CGI->generaltexth->allTexts[309]);
  392. break;
  393. }
  394. }
  395. }
  396. }
  397. CBattleResultWindow::~CBattleResultWindow()
  398. {
  399. }
  400. void CBattleResultWindow::activate()
  401. {
  402. owner.showingDialog->set(true);
  403. CIntObject::activate();
  404. }
  405. void CBattleResultWindow::show(SDL_Surface * to)
  406. {
  407. CIntObject::show(to);
  408. CCS->videoh->update(pos.x + 107, pos.y + 70, screen, true, false);
  409. }
  410. void CBattleResultWindow::bExitf()
  411. {
  412. if(LOCPLINT->cb->getStartInfo()->mode == StartInfo::DUEL)
  413. {
  414. CGuiHandler::pushSDLEvent(SDL_QUIT);
  415. return;
  416. }
  417. CPlayerInterface &intTmp = owner; //copy reference because "this" will be destructed soon
  418. GH.popIntTotally(this);
  419. if(dynamic_cast<CBattleInterface*>(GH.topInt()))
  420. GH.popInts(1); //pop battle interface if present
  421. //Result window and battle interface are gone. We requested all dialogs to be closed before opening the battle,
  422. //so we can be sure that there is no dialogs left on GUI stack.
  423. intTmp.showingDialog->setn(false);
  424. CCS->videoh->close();
  425. }
  426. Point CClickableHex::getXYUnitAnim(BattleHex hexNum, const CStack * stack, CBattleInterface * cbi)
  427. {
  428. assert(cbi);
  429. Point ret(-500, -500); //returned value
  430. if(stack && stack->position < 0) //creatures in turrets
  431. {
  432. switch(stack->position)
  433. {
  434. case -2: //keep
  435. ret = cbi->siegeH->town->town->clientInfo.siegePositions[18];
  436. break;
  437. case -3: //lower turret
  438. ret = cbi->siegeH->town->town->clientInfo.siegePositions[19];
  439. break;
  440. case -4: //upper turret
  441. ret = cbi->siegeH->town->town->clientInfo.siegePositions[20];
  442. break;
  443. }
  444. }
  445. else
  446. {
  447. static const Point basePos(-190, -139); // position of creature in topleft corner
  448. static const int imageShiftX = 30; // X offset to base pos for facing right stacks, negative for facing left
  449. ret.x = basePos.x + 22 * ( (hexNum.getY() + 1)%2 ) + 44 * hexNum.getX();
  450. ret.y = basePos.y + 42 * hexNum.getY();
  451. if (stack)
  452. {
  453. if(cbi->creDir[stack->ID])
  454. ret.x += imageShiftX;
  455. else
  456. ret.x -= imageShiftX;
  457. //shifting position for double - hex creatures
  458. if(stack->doubleWide())
  459. {
  460. if(stack->attackerOwned)
  461. {
  462. if(cbi->creDir[stack->ID])
  463. ret.x -= 44;
  464. }
  465. else
  466. {
  467. if(!cbi->creDir[stack->ID])
  468. ret.x += 44;
  469. }
  470. }
  471. }
  472. }
  473. //returning
  474. return ret +CPlayerInterface::battleInt->pos;
  475. }
  476. void CClickableHex::hover(bool on)
  477. {
  478. hovered = on;
  479. //Hoverable::hover(on);
  480. if(!on && setAlterText)
  481. {
  482. myInterface->console->alterTxt = std::string();
  483. setAlterText = false;
  484. }
  485. }
  486. CClickableHex::CClickableHex() : setAlterText(false), myNumber(-1), accessible(true), hovered(false), strictHovered(false), myInterface(nullptr)
  487. {
  488. addUsedEvents(LCLICK | RCLICK | HOVER | MOVE);
  489. }
  490. void CClickableHex::mouseMoved(const SDL_MouseMotionEvent &sEvent)
  491. {
  492. if(myInterface->cellShade)
  493. {
  494. if(CSDL_Ext::SDL_GetPixel(myInterface->cellShade, sEvent.x-pos.x, sEvent.y-pos.y) == 0) //hovered pixel is outside hex
  495. {
  496. strictHovered = false;
  497. }
  498. else //hovered pixel is inside hex
  499. {
  500. strictHovered = true;
  501. }
  502. }
  503. if(hovered && strictHovered) //print attacked creature to console
  504. {
  505. const CStack * attackedStack = myInterface->getCurrentPlayerInterface()->cb->battleGetStackByPos(myNumber);
  506. if(myInterface->console->alterTxt.size() == 0 &&attackedStack != nullptr &&
  507. attackedStack->owner != myInterface->getCurrentPlayerInterface()->playerID &&
  508. attackedStack->alive())
  509. {
  510. char tabh[160];
  511. const std::string & attackedName = attackedStack->count == 1 ? attackedStack->getCreature()->nameSing : attackedStack->getCreature()->namePl;
  512. sprintf(tabh, CGI->generaltexth->allTexts[220].c_str(), attackedName.c_str());
  513. myInterface->console->alterTxt = std::string(tabh);
  514. setAlterText = true;
  515. }
  516. }
  517. else if(setAlterText)
  518. {
  519. myInterface->console->alterTxt = std::string();
  520. setAlterText = false;
  521. }
  522. }
  523. void CClickableHex::clickLeft(tribool down, bool previousState)
  524. {
  525. if(!down && hovered && strictHovered) //we've been really clicked!
  526. {
  527. myInterface->hexLclicked(myNumber);
  528. }
  529. }
  530. void CClickableHex::clickRight(tribool down, bool previousState)
  531. {
  532. const CStack * myst = myInterface->getCurrentPlayerInterface()->cb->battleGetStackByPos(myNumber); //stack info
  533. if(hovered && strictHovered && myst!=nullptr)
  534. {
  535. if(!myst->alive()) return;
  536. if(down)
  537. {
  538. GH.pushInt(createCreWindow(myst));
  539. }
  540. }
  541. }
  542. void CStackQueue::update()
  543. {
  544. stacksSorted.clear();
  545. owner->getCurrentPlayerInterface()->cb->battleGetStackQueue(stacksSorted, stackBoxes.size());
  546. if(stacksSorted.size())
  547. {
  548. for (int i = 0; i < stackBoxes.size() ; i++)
  549. {
  550. stackBoxes[i]->setStack(stacksSorted[i]);
  551. }
  552. }
  553. else
  554. {
  555. //no stacks on battlefield... what to do with queue?
  556. }
  557. }
  558. CStackQueue::CStackQueue(bool Embedded, CBattleInterface * _owner)
  559. :embedded(Embedded), owner(_owner)
  560. {
  561. OBJ_CONSTRUCTION_CAPTURING_ALL;
  562. if(embedded)
  563. {
  564. bg = nullptr;
  565. pos.w = QUEUE_SIZE * 37;
  566. pos.h = 46;
  567. pos.x = screen->w/2 - pos.w/2;
  568. pos.y = (screen->h - 600)/2 + 10;
  569. }
  570. else
  571. {
  572. bg = BitmapHandler::loadBitmap("DIBOXBCK");
  573. pos.w = 800;
  574. pos.h = 85;
  575. }
  576. stackBoxes.resize(QUEUE_SIZE);
  577. for (int i = 0; i < stackBoxes.size(); i++)
  578. {
  579. stackBoxes[i] = new StackBox(embedded);
  580. stackBoxes[i]->moveBy(Point(1 + (embedded ? 36 : 80)*i, 0));
  581. }
  582. }
  583. CStackQueue::~CStackQueue()
  584. {
  585. SDL_FreeSurface(bg);
  586. }
  587. void CStackQueue::showAll(SDL_Surface * to)
  588. {
  589. blitBg(to);
  590. CIntObject::showAll(to);
  591. }
  592. void CStackQueue::blitBg( SDL_Surface * to )
  593. {
  594. if(bg)
  595. {
  596. SDL_SetClipRect(to, &pos);
  597. CSDL_Ext::fillTexture(to, bg);
  598. SDL_SetClipRect(to, nullptr);
  599. }
  600. }
  601. void CStackQueue::StackBox::showAll(SDL_Surface * to)
  602. {
  603. assert(stack);
  604. bg->colorize(stack->owner);
  605. CIntObject::showAll(to);
  606. if(small)
  607. printAtMiddleLoc(makeNumberShort(stack->count), pos.w/2, pos.h - 7, FONT_SMALL, Colors::WHITE, to);
  608. else
  609. printAtMiddleLoc(makeNumberShort(stack->count), pos.w/2, pos.h - 8, FONT_MEDIUM, Colors::WHITE, to);
  610. }
  611. void CStackQueue::StackBox::setStack( const CStack *stack )
  612. {
  613. this->stack = stack;
  614. assert(stack);
  615. icon->setFrame(stack->getCreature()->iconIndex);
  616. }
  617. CStackQueue::StackBox::StackBox(bool small):
  618. stack(nullptr),
  619. small(small)
  620. {
  621. OBJ_CONSTRUCTION_CAPTURING_ALL;
  622. bg = new CPicture(small ? "StackQueueBgSmall" : "StackQueueBgBig" );
  623. if (small)
  624. {
  625. icon = new CAnimImage("CPRSMALL", 0, 0, 5, 2);
  626. }
  627. else
  628. icon = new CAnimImage("TWCRPORT", 0, 0, 9, 1);
  629. pos.w = bg->pos.w;
  630. pos.h = bg->pos.h;
  631. }