CSpellWindow.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * CSpellWindow.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 "CSpellWindow.h"
  12. #include "../../lib/ScopeGuard.h"
  13. #include "GUIClasses.h"
  14. #include "InfoWindows.h"
  15. #include "CCastleInterface.h"
  16. #include "../CGameInfo.h"
  17. #include "../CPlayerInterface.h"
  18. #include "../PlayerLocalState.h"
  19. #include "../CVideoHandler.h"
  20. #include "../battle/BattleInterface.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../gui/Shortcut.h"
  23. #include "../gui/WindowHandler.h"
  24. #include "../widgets/MiscWidgets.h"
  25. #include "../widgets/CComponent.h"
  26. #include "../widgets/TextControls.h"
  27. #include "../adventureMap/AdventureMapInterface.h"
  28. #include "../render/CAnimation.h"
  29. #include "../render/IRenderHandler.h"
  30. #include "../render/IImage.h"
  31. #include "../render/IImageLoader.h"
  32. #include "../render/Canvas.h"
  33. #include "../../CCallback.h"
  34. #include "../../lib/CConfigHandler.h"
  35. #include "../../lib/CGeneralTextHandler.h"
  36. #include "../../lib/spells/CSpellHandler.h"
  37. #include "../../lib/spells/Problem.h"
  38. #include "../../lib/GameConstants.h"
  39. #include "../../lib/mapObjects/CGHeroInstance.h"
  40. CSpellWindow::InteractiveArea::InteractiveArea(const Rect & myRect, std::function<void()> funcL, int helpTextId, CSpellWindow * _owner)
  41. {
  42. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  43. pos = myRect;
  44. onLeft = funcL;
  45. hoverText = CGI->generaltexth->zelp[helpTextId].first;
  46. helpText = CGI->generaltexth->zelp[helpTextId].second;
  47. owner = _owner;
  48. }
  49. void CSpellWindow::InteractiveArea::clickPressed(const Point & cursorPosition)
  50. {
  51. onLeft();
  52. }
  53. void CSpellWindow::InteractiveArea::showPopupWindow(const Point & cursorPosition)
  54. {
  55. CRClickPopup::createAndPush(helpText);
  56. }
  57. void CSpellWindow::InteractiveArea::hover(bool on)
  58. {
  59. if(on)
  60. owner->statusBar->write(hoverText);
  61. else
  62. owner->statusBar->clear();
  63. }
  64. class SpellbookSpellSorter
  65. {
  66. public:
  67. bool operator()(const CSpell * A, const CSpell * B)
  68. {
  69. if(A->getLevel() < B->getLevel())
  70. return true;
  71. if(A->getLevel() > B->getLevel())
  72. return false;
  73. for(auto schoolId = 0; schoolId < GameConstants::DEFAULT_SCHOOLS; schoolId++)
  74. {
  75. if(A->school.at(SpellSchool(schoolId)) && !B->school.at(SpellSchool(schoolId)))
  76. return true;
  77. if(!A->school.at(SpellSchool(schoolId)) && B->school.at(SpellSchool(schoolId)))
  78. return false;
  79. }
  80. return A->getNameTranslated() < B->getNameTranslated();
  81. }
  82. } spellsorter;
  83. CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  84. CWindowObject(PLAYER_COLORED),
  85. battleSpellsOnly(openOnBattleSpells),
  86. selectedTab(4),
  87. currentPage(0),
  88. myHero(_myHero),
  89. myInt(_myInt),
  90. isBigSpellbook(true)
  91. {
  92. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  93. if(isBigSpellbook)
  94. background = std::make_shared<CPicture>(createBigSpellBook(), Point(0, 0));
  95. else
  96. background = std::make_shared<CPicture>(ImagePath::builtin("SpelBack"), 0, 0);
  97. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  98. //initializing castable spells
  99. mySpells.reserve(CGI->spellh->objects.size());
  100. for(const CSpell * spell : CGI->spellh->objects)
  101. {
  102. if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell))
  103. mySpells.push_back(spell);
  104. }
  105. std::sort(mySpells.begin(), mySpells.end(), spellsorter);
  106. //initializing sizes of spellbook's parts
  107. for(auto & elem : sitesPerTabAdv)
  108. elem = 0;
  109. for(auto & elem : sitesPerTabBattle)
  110. elem = 0;
  111. for(const auto spell : mySpells)
  112. {
  113. int * sitesPerOurTab = spell->isCombat() ? sitesPerTabBattle : sitesPerTabAdv;
  114. ++sitesPerOurTab[4];
  115. spell->forEachSchool([&sitesPerOurTab](const SpellSchool & school, bool & stop)
  116. {
  117. ++sitesPerOurTab[school];
  118. });
  119. }
  120. if(sitesPerTabAdv[4] % 12 == 0)
  121. sitesPerTabAdv[4]/=12;
  122. else
  123. sitesPerTabAdv[4] = sitesPerTabAdv[4]/12 + 1;
  124. for(int v=0; v<4; ++v)
  125. {
  126. if(sitesPerTabAdv[v] <= 10)
  127. sitesPerTabAdv[v] = 1;
  128. else
  129. {
  130. if((sitesPerTabAdv[v] - 10) % 12 == 0)
  131. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 1;
  132. else
  133. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 2;
  134. }
  135. }
  136. if(sitesPerTabBattle[4] % 12 == 0)
  137. sitesPerTabBattle[4]/=12;
  138. else
  139. sitesPerTabBattle[4] = sitesPerTabBattle[4]/12 + 1;
  140. for(int v=0; v<4; ++v)
  141. {
  142. if(sitesPerTabBattle[v] <= 10)
  143. sitesPerTabBattle[v] = 1;
  144. else
  145. {
  146. if((sitesPerTabBattle[v] - 10) % 12 == 0)
  147. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 1;
  148. else
  149. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 2;
  150. }
  151. }
  152. //numbers of spell pages computed
  153. leftCorner = std::make_shared<CPicture>(ImagePath::builtin("SpelTrnL.bmp"), 97, 77);
  154. rightCorner = std::make_shared<CPicture>(ImagePath::builtin("SpelTrnR.bmp"), 487, 72);
  155. spellIcons = GH.renderHandler().loadAnimation(AnimationPath::builtin("Spells"));
  156. schoolTab = std::make_shared<CAnimImage>(AnimationPath::builtin("SpelTab"), selectedTab, 0, 524, 88);
  157. schoolPicture = std::make_shared<CAnimImage>(AnimationPath::builtin("Schools"), 0, 0, 117, 74);
  158. schoolBorders[0] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevA.def"));
  159. schoolBorders[1] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevF.def"));
  160. schoolBorders[2] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevW.def"));
  161. schoolBorders[3] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevE.def"));
  162. for(auto item : schoolBorders)
  163. item->preload();
  164. mana = std::make_shared<CLabel>(435, 426, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, std::to_string(myHero->mana));
  165. statusBar = CGStatusBar::create(7, 569, ImagePath::builtin("Spelroll.bmp"));
  166. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 479 + pos.x, 405 + pos.y, 36, 56), std::bind(&CSpellWindow::fexitb, this), 460, this));
  167. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 221 + pos.x, 405 + pos.y, 36, 56), std::bind(&CSpellWindow::fbattleSpellsb, this), 453, this));
  168. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 355 + pos.x, 405 + pos.y, 36, 56), std::bind(&CSpellWindow::fadvSpellsb, this), 452, this));
  169. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 418 + pos.x, 405 + pos.y, 36, 56), std::bind(&CSpellWindow::fmanaPtsb, this), 459, this));
  170. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x, 94 + pos.y, 36, 56), std::bind(&CSpellWindow::selectSchool, this, 0), 454, this));
  171. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x, 151 + pos.y, 45, 35), std::bind(&CSpellWindow::selectSchool, this, 3), 457, this));
  172. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x, 210 + pos.y, 45, 35), std::bind(&CSpellWindow::selectSchool, this, 1), 455, this));
  173. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x, 270 + pos.y, 45, 35), std::bind(&CSpellWindow::selectSchool, this, 2), 456, this));
  174. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x, 330 + pos.y, 45, 35), std::bind(&CSpellWindow::selectSchool, this, 4), 458, this));
  175. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 97 + pos.x, 77 + pos.y, leftCorner->pos.h, leftCorner->pos.w ), std::bind(&CSpellWindow::fLcornerb, this), 450, this));
  176. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 487 + pos.x, 72 + pos.y, rightCorner->pos.h, rightCorner->pos.w ), std::bind(&CSpellWindow::fRcornerb, this), 451, this));
  177. //areas for spells
  178. int xpos = 117 + pos.x, ypos = 90 + pos.y;
  179. for(int v=0; v<12; ++v)
  180. {
  181. spellAreas[v] = std::make_shared<SpellArea>( Rect(xpos, ypos, 65, 78), this);
  182. if(v == 5) //to right page
  183. {
  184. xpos = 336 + pos.x; ypos = 90 + pos.y;
  185. }
  186. else
  187. {
  188. if(v%2 == 0)
  189. {
  190. xpos+=85;
  191. }
  192. else
  193. {
  194. xpos -= 85; ypos+=97;
  195. }
  196. }
  197. }
  198. selectedTab = battleSpellsOnly ? myInt->localState->spellbookSettings.spellbookLastTabBattle : myInt->localState->spellbookSettings.spellbookLastTabAdvmap;
  199. schoolTab->setFrame(selectedTab, 0);
  200. int cp = battleSpellsOnly ? myInt->localState->spellbookSettings.spellbookLastPageBattle : myInt->localState->spellbookSettings.spellbokLastPageAdvmap;
  201. // spellbook last page battle index is not reset after battle, so this needs to stay here
  202. vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1));
  203. setCurrentPage(cp);
  204. computeSpellsPerArea();
  205. addUsedEvents(KEYBOARD);
  206. }
  207. CSpellWindow::~CSpellWindow()
  208. {
  209. }
  210. std::shared_ptr<IImage> CSpellWindow::createBigSpellBook()
  211. {
  212. std::shared_ptr<IImage> img = GH.renderHandler().loadImage(ImagePath::builtin("SpelBack"));
  213. Canvas canvas = Canvas(Point(800, 600));
  214. // edges
  215. canvas.draw(img, Point(0, 0), Rect(10, 38, 90, 45));
  216. canvas.draw(img, Point(0, 460), Rect(10, 400, 90, 141));
  217. canvas.draw(img, Point(705, 0), Rect(509, 38, 95, 45));
  218. canvas.draw(img, Point(705, 460), Rect(509, 400, 95, 141));
  219. // left / right
  220. Canvas tmp1 = Canvas(Point(90, 355 - 45));
  221. tmp1.draw(img, Point(0, 0), Rect(10, 38 + 45, 90, 355 - 45));
  222. canvas.drawScaled(tmp1, Point(0, 45), Point(90, 415));
  223. Canvas tmp2 = Canvas(Point(95, 355 - 45));
  224. tmp2.draw(img, Point(0, 0), Rect(509, 38 + 45, 95, 355 - 45));
  225. canvas.drawScaled(tmp2, Point(705, 45), Point(95, 415));
  226. // top / bottom
  227. Canvas tmp3 = Canvas(Point(409, 45));
  228. tmp3.draw(img, Point(0, 0), Rect(100, 38, 409, 45));
  229. canvas.drawScaled(tmp3, Point(90, 0), Point(615, 45));
  230. Canvas tmp4 = Canvas(Point(409, 141));
  231. tmp4.draw(img, Point(0, 0), Rect(100, 400, 409, 141));
  232. canvas.drawScaled(tmp4, Point(90, 460), Point(615, 141));
  233. // middle
  234. Canvas tmp5 = Canvas(Point(409, 141));
  235. tmp5.draw(img, Point(0, 0), Rect(100, 38 + 45, 509 - 10, 400 - 38));
  236. canvas.drawScaled(tmp5, Point(90, 45), Point(615, 415));
  237. return GH.renderHandler().createImage(canvas.getInternalSurface());
  238. }
  239. void CSpellWindow::fexitb()
  240. {
  241. (myInt->battleInt ? myInt->localState->spellbookSettings.spellbookLastTabBattle : myInt->localState->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  242. (myInt->battleInt ? myInt->localState->spellbookSettings.spellbookLastPageBattle : myInt->localState->spellbookSettings.spellbokLastPageAdvmap) = currentPage;
  243. close();
  244. }
  245. void CSpellWindow::fadvSpellsb()
  246. {
  247. if(battleSpellsOnly == true)
  248. {
  249. turnPageRight();
  250. battleSpellsOnly = false;
  251. setCurrentPage(0);
  252. }
  253. computeSpellsPerArea();
  254. }
  255. void CSpellWindow::fbattleSpellsb()
  256. {
  257. if(battleSpellsOnly == false)
  258. {
  259. turnPageLeft();
  260. battleSpellsOnly = true;
  261. setCurrentPage(0);
  262. }
  263. computeSpellsPerArea();
  264. }
  265. void CSpellWindow::fmanaPtsb()
  266. {
  267. }
  268. void CSpellWindow::selectSchool(int school)
  269. {
  270. if(selectedTab != school)
  271. {
  272. if(selectedTab < school)
  273. turnPageLeft();
  274. else
  275. turnPageRight();
  276. selectedTab = school;
  277. schoolTab->setFrame(selectedTab, 0);
  278. setCurrentPage(0);
  279. }
  280. computeSpellsPerArea();
  281. }
  282. void CSpellWindow::fLcornerb()
  283. {
  284. if(currentPage>0)
  285. {
  286. turnPageLeft();
  287. setCurrentPage(currentPage - 1);
  288. }
  289. computeSpellsPerArea();
  290. }
  291. void CSpellWindow::fRcornerb()
  292. {
  293. if((currentPage + 1) < (pagesWithinCurrentTab()))
  294. {
  295. turnPageRight();
  296. setCurrentPage(currentPage + 1);
  297. }
  298. computeSpellsPerArea();
  299. }
  300. void CSpellWindow::show(Canvas & to)
  301. {
  302. statusBar->show(to);
  303. }
  304. void CSpellWindow::computeSpellsPerArea()
  305. {
  306. std::vector<const CSpell *> spellsCurSite;
  307. spellsCurSite.reserve(mySpells.size());
  308. for(const CSpell * spell : mySpells)
  309. {
  310. if(spell->isCombat() ^ !battleSpellsOnly
  311. && ((selectedTab == 4) || spell->school.at(SpellSchool(selectedTab)))
  312. )
  313. {
  314. spellsCurSite.push_back(spell);
  315. }
  316. }
  317. if(selectedTab == 4)
  318. {
  319. if(spellsCurSite.size() > 12)
  320. {
  321. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + currentPage*12, spellsCurSite.end());
  322. if(spellsCurSite.size() > 12)
  323. {
  324. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  325. }
  326. }
  327. }
  328. else //selectedTab == 0, 1, 2 or 3
  329. {
  330. if(spellsCurSite.size() > 10)
  331. {
  332. if(currentPage == 0)
  333. {
  334. spellsCurSite.erase(spellsCurSite.begin()+10, spellsCurSite.end());
  335. }
  336. else
  337. {
  338. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + (currentPage-1)*12 + 10, spellsCurSite.end());
  339. if(spellsCurSite.size() > 12)
  340. {
  341. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  342. }
  343. }
  344. }
  345. }
  346. //applying
  347. if(selectedTab == 4 || currentPage != 0)
  348. {
  349. for(size_t c=0; c<12; ++c)
  350. {
  351. if(c < spellsCurSite.size())
  352. {
  353. spellAreas[c]->setSpell(spellsCurSite[c]);
  354. }
  355. else
  356. {
  357. spellAreas[c]->setSpell(nullptr);
  358. }
  359. }
  360. }
  361. else
  362. {
  363. spellAreas[0]->setSpell(nullptr);
  364. spellAreas[1]->setSpell(nullptr);
  365. for(size_t c=0; c<10; ++c)
  366. {
  367. if(c < spellsCurSite.size())
  368. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  369. else
  370. spellAreas[c+2]->setSpell(nullptr);
  371. }
  372. }
  373. redraw();
  374. }
  375. void CSpellWindow::setCurrentPage(int value)
  376. {
  377. currentPage = value;
  378. schoolPicture->visible = selectedTab!=4 && currentPage == 0;
  379. if(selectedTab != 4)
  380. schoolPicture->setFrame(selectedTab, 0);
  381. leftCorner->visible = currentPage != 0;
  382. rightCorner->visible = (currentPage+1) < pagesWithinCurrentTab();
  383. mana->setText(std::to_string(myHero->mana));//just in case, it will be possible to cast spell without closing book
  384. }
  385. void CSpellWindow::turnPageLeft()
  386. {
  387. if(settings["video"]["spellbookAnimation"].Bool())
  388. CCS->videoh->openAndPlayVideo(VideoPath::builtin("PGTRNLFT.SMK"), pos.x+13, pos.y+15);
  389. }
  390. void CSpellWindow::turnPageRight()
  391. {
  392. if(settings["video"]["spellbookAnimation"].Bool())
  393. CCS->videoh->openAndPlayVideo(VideoPath::builtin("PGTRNRGH.SMK"), pos.x+13, pos.y+15);
  394. }
  395. void CSpellWindow::keyPressed(EShortcut key)
  396. {
  397. switch(key)
  398. {
  399. case EShortcut::GLOBAL_RETURN:
  400. fexitb();
  401. break;
  402. case EShortcut::MOVE_LEFT:
  403. fLcornerb();
  404. break;
  405. case EShortcut::MOVE_RIGHT:
  406. fRcornerb();
  407. break;
  408. case EShortcut::MOVE_UP:
  409. case EShortcut::MOVE_DOWN:
  410. {
  411. bool down = key == EShortcut::MOVE_DOWN;
  412. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  413. int index = -1;
  414. while(schoolsOrder[++index] != selectedTab);
  415. index += (down ? 1 : -1);
  416. vstd::abetween<int>(index, 0, std::size(schoolsOrder) - 1);
  417. if(selectedTab != schoolsOrder[index])
  418. selectSchool(schoolsOrder[index]);
  419. break;
  420. }
  421. case EShortcut::SPELLBOOK_TAB_COMBAT:
  422. fbattleSpellsb();
  423. break;
  424. case EShortcut::SPELLBOOK_TAB_ADVENTURE:
  425. fadvSpellsb();
  426. break;
  427. }
  428. }
  429. int CSpellWindow::pagesWithinCurrentTab()
  430. {
  431. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  432. }
  433. CSpellWindow::SpellArea::SpellArea(Rect pos, CSpellWindow * owner)
  434. {
  435. this->pos = pos;
  436. this->owner = owner;
  437. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  438. schoolLevel = -1;
  439. mySpell = nullptr;
  440. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  441. image = std::make_shared<CAnimImage>(owner->spellIcons, 0, 0);
  442. image->visible = false;
  443. name = std::make_shared<CLabel>(39, 70, FONT_TINY, ETextAlignment::CENTER);
  444. level = std::make_shared<CLabel>(39, 82, FONT_TINY, ETextAlignment::CENTER);
  445. cost = std::make_shared<CLabel>(39, 94, FONT_TINY, ETextAlignment::CENTER);
  446. for(auto l : {name, level, cost})
  447. l->setAutoRedraw(false);
  448. }
  449. CSpellWindow::SpellArea::~SpellArea() = default;
  450. void CSpellWindow::SpellArea::clickPressed(const Point & cursorPosition)
  451. {
  452. if(mySpell)
  453. {
  454. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  455. if(spellCost > owner->myHero->mana) //insufficient mana
  456. {
  457. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[206]) % spellCost % owner->myHero->mana));
  458. return;
  459. }
  460. //anything that is not combat spell is adventure spell
  461. //this not an error in general to cast even creature ability with hero
  462. const bool combatSpell = mySpell->isCombat();
  463. if(combatSpell == mySpell->isAdventure())
  464. {
  465. logGlobal->error("Spell have invalid flags");
  466. return;
  467. }
  468. const bool inCombat = owner->myInt->battleInt != nullptr;
  469. const bool inCastle = owner->myInt->castleInt != nullptr;
  470. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  471. if((combatSpell ^ inCombat) || inCastle)
  472. {
  473. std::vector<std::shared_ptr<CComponent>> hlp(1, std::make_shared<CComponent>(CComponent::spell, mySpell->id, 0));
  474. LOCPLINT->showInfoDialog(mySpell->getDescriptionTranslated(schoolLevel), hlp);
  475. }
  476. else if(combatSpell)
  477. {
  478. spells::detail::ProblemImpl problem;
  479. if(mySpell->canBeCast(problem, owner->myInt->battleInt->getBattle().get(), spells::Mode::HERO, owner->myHero))
  480. {
  481. owner->myInt->battleInt->castThisSpell(mySpell->id);
  482. owner->fexitb();
  483. }
  484. else
  485. {
  486. std::vector<std::string> texts;
  487. problem.getAll(texts);
  488. if(!texts.empty())
  489. LOCPLINT->showInfoDialog(texts.front());
  490. else
  491. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.spellUnknownProblem"));
  492. }
  493. }
  494. else //adventure spell
  495. {
  496. const CGHeroInstance * h = owner->myHero;
  497. GH.windows().popWindows(1);
  498. auto guard = vstd::makeScopeGuard([this]()
  499. {
  500. owner->myInt->localState->spellbookSettings.spellbookLastTabAdvmap = owner->selectedTab;
  501. owner->myInt->localState->spellbookSettings.spellbokLastPageAdvmap = owner->currentPage;
  502. });
  503. if(mySpell->getTargetType() == spells::AimType::LOCATION)
  504. adventureInt->enterCastingMode(mySpell);
  505. else if(mySpell->getTargetType() == spells::AimType::NO_TARGET)
  506. owner->myInt->cb->castSpell(h, mySpell->id);
  507. else
  508. logGlobal->error("Invalid spell target type");
  509. }
  510. }
  511. }
  512. void CSpellWindow::SpellArea::showPopupWindow(const Point & cursorPosition)
  513. {
  514. if(mySpell)
  515. {
  516. std::string dmgInfo;
  517. auto causedDmg = owner->myInt->cb->estimateSpellDamage(mySpell, owner->myHero);
  518. if(causedDmg == 0 || mySpell->id == SpellID::TITANS_LIGHTNING_BOLT) //Titan's Lightning Bolt already has damage info included
  519. dmgInfo.clear();
  520. else
  521. {
  522. dmgInfo = CGI->generaltexth->allTexts[343];
  523. boost::algorithm::replace_first(dmgInfo, "%d", std::to_string(causedDmg));
  524. }
  525. CRClickPopup::createAndPush(mySpell->getDescriptionTranslated(schoolLevel) + dmgInfo, std::make_shared<CComponent>(CComponent::spell, mySpell->id));
  526. }
  527. }
  528. void CSpellWindow::SpellArea::hover(bool on)
  529. {
  530. if(mySpell)
  531. {
  532. if(on)
  533. owner->statusBar->write(boost::str(boost::format("%s (%s)") % mySpell->getNameTranslated() % CGI->generaltexth->allTexts[171+mySpell->getLevel()]));
  534. else
  535. owner->statusBar->clear();
  536. }
  537. }
  538. void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
  539. {
  540. schoolBorder.reset();
  541. image->visible = false;
  542. name->setText("");
  543. level->setText("");
  544. cost->setText("");
  545. mySpell = spell;
  546. if(mySpell)
  547. {
  548. int32_t whichSchool = 0; //0 - air magic, 1 - fire magic, 2 - water magic, 3 - earth magic,
  549. schoolLevel = owner->myHero->getSpellSchoolLevel(mySpell, &whichSchool);
  550. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  551. image->setFrame(mySpell->id);
  552. image->visible = true;
  553. {
  554. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  555. schoolBorder = std::make_shared<CAnimImage>(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab], schoolLevel);
  556. }
  557. ColorRGBA firstLineColor, secondLineColor;
  558. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  559. {
  560. firstLineColor = Colors::WHITE;
  561. secondLineColor = Colors::ORANGE;
  562. }
  563. else
  564. {
  565. firstLineColor = Colors::YELLOW;
  566. secondLineColor = Colors::WHITE;
  567. }
  568. name->color = firstLineColor;
  569. name->setText(mySpell->getNameTranslated());
  570. level->color = secondLineColor;
  571. if(schoolLevel > 0)
  572. {
  573. boost::format fmt("%s/%s");
  574. fmt % CGI->generaltexth->allTexts[171 + mySpell->getLevel()];
  575. fmt % CGI->generaltexth->levels[3+(schoolLevel-1)];//lines 4-6
  576. level->setText(fmt.str());
  577. }
  578. else
  579. level->setText(CGI->generaltexth->allTexts[171 + mySpell->getLevel()]);
  580. cost->color = secondLineColor;
  581. boost::format costfmt("%s: %d");
  582. costfmt % CGI->generaltexth->allTexts[387] % spellCost;
  583. cost->setText(costfmt.str());
  584. }
  585. }