CSpellWindow.cpp 19 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 "../CMT.h"
  18. #include "../CPlayerInterface.h"
  19. #include "../CVideoHandler.h"
  20. #include "../battle/BattleInterface.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../widgets/MiscWidgets.h"
  23. #include "../widgets/CComponent.h"
  24. #include "../widgets/TextControls.h"
  25. #include "../adventureMap/CAdvMapInt.h"
  26. #include "../render/CAnimation.h"
  27. #include "../../CCallback.h"
  28. #include "../../lib/CConfigHandler.h"
  29. #include "../../lib/CGeneralTextHandler.h"
  30. #include "../../lib/spells/CSpellHandler.h"
  31. #include "../../lib/spells/Problem.h"
  32. #include "../../lib/GameConstants.h"
  33. #include "../../lib/mapObjects/CGHeroInstance.h"
  34. CSpellWindow::InteractiveArea::InteractiveArea(const Rect & myRect, std::function<void()> funcL, int helpTextId, CSpellWindow * _owner)
  35. {
  36. addUsedEvents(LCLICK | RCLICK | HOVER);
  37. pos = myRect;
  38. onLeft = funcL;
  39. hoverText = CGI->generaltexth->zelp[helpTextId].first;
  40. helpText = CGI->generaltexth->zelp[helpTextId].second;
  41. owner = _owner;
  42. }
  43. void CSpellWindow::InteractiveArea::clickLeft(tribool down, bool previousState)
  44. {
  45. if(!down)
  46. onLeft();
  47. }
  48. void CSpellWindow::InteractiveArea::clickRight(tribool down, bool previousState)
  49. {
  50. adventureInt->handleRightClick(helpText, down);
  51. }
  52. void CSpellWindow::InteractiveArea::hover(bool on)
  53. {
  54. if(on)
  55. owner->statusBar->write(hoverText);
  56. else
  57. owner->statusBar->clear();
  58. }
  59. class SpellbookSpellSorter
  60. {
  61. public:
  62. bool operator()(const CSpell * A, const CSpell * B)
  63. {
  64. if(A->level < B->level)
  65. return true;
  66. if(A->level > B->level)
  67. return false;
  68. for(ui8 schoolId = 0; schoolId < 4; schoolId++)
  69. {
  70. if(A->school.at((ESpellSchool)schoolId) && !B->school.at((ESpellSchool)schoolId))
  71. return true;
  72. if(!A->school.at((ESpellSchool)schoolId) && B->school.at((ESpellSchool)schoolId))
  73. return false;
  74. }
  75. return A->getNameTranslated() < B->getNameTranslated();
  76. }
  77. } spellsorter;
  78. CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  79. CWindowObject(PLAYER_COLORED, "SpelBack"),
  80. battleSpellsOnly(openOnBattleSpells),
  81. selectedTab(4),
  82. currentPage(0),
  83. myHero(_myHero),
  84. myInt(_myInt)
  85. {
  86. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  87. //initializing castable spells
  88. mySpells.reserve(CGI->spellh->objects.size());
  89. for(const CSpell * spell : CGI->spellh->objects)
  90. {
  91. if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell))
  92. mySpells.push_back(spell);
  93. }
  94. std::sort(mySpells.begin(), mySpells.end(), spellsorter);
  95. //initializing sizes of spellbook's parts
  96. for(auto & elem : sitesPerTabAdv)
  97. elem = 0;
  98. for(auto & elem : sitesPerTabBattle)
  99. elem = 0;
  100. for(const auto spell : mySpells)
  101. {
  102. int * sitesPerOurTab = spell->isCombat() ? sitesPerTabBattle : sitesPerTabAdv;
  103. ++sitesPerOurTab[4];
  104. spell->forEachSchool([&sitesPerOurTab](const spells::SchoolInfo & school, bool & stop)
  105. {
  106. ++sitesPerOurTab[(ui8)school.id];
  107. });
  108. }
  109. if(sitesPerTabAdv[4] % 12 == 0)
  110. sitesPerTabAdv[4]/=12;
  111. else
  112. sitesPerTabAdv[4] = sitesPerTabAdv[4]/12 + 1;
  113. for(int v=0; v<4; ++v)
  114. {
  115. if(sitesPerTabAdv[v] <= 10)
  116. sitesPerTabAdv[v] = 1;
  117. else
  118. {
  119. if((sitesPerTabAdv[v] - 10) % 12 == 0)
  120. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 1;
  121. else
  122. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 2;
  123. }
  124. }
  125. if(sitesPerTabBattle[4] % 12 == 0)
  126. sitesPerTabBattle[4]/=12;
  127. else
  128. sitesPerTabBattle[4] = sitesPerTabBattle[4]/12 + 1;
  129. for(int v=0; v<4; ++v)
  130. {
  131. if(sitesPerTabBattle[v] <= 10)
  132. sitesPerTabBattle[v] = 1;
  133. else
  134. {
  135. if((sitesPerTabBattle[v] - 10) % 12 == 0)
  136. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 1;
  137. else
  138. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 2;
  139. }
  140. }
  141. //numbers of spell pages computed
  142. leftCorner = std::make_shared<CPicture>("SpelTrnL.bmp", 97, 77);
  143. rightCorner = std::make_shared<CPicture>("SpelTrnR.bmp", 487, 72);
  144. spellIcons = std::make_shared<CAnimation>("Spells");
  145. schoolTab = std::make_shared<CAnimImage>("SpelTab", selectedTab, 0, 524, 88);
  146. schoolPicture = std::make_shared<CAnimImage>("Schools", 0, 0, 117, 74);
  147. schoolBorders[0] = std::make_shared<CAnimation>("SplevA.def");
  148. schoolBorders[1] = std::make_shared<CAnimation>("SplevF.def");
  149. schoolBorders[2] = std::make_shared<CAnimation>("SplevW.def");
  150. schoolBorders[3] = std::make_shared<CAnimation>("SplevE.def");
  151. for(auto item : schoolBorders)
  152. item->preload();
  153. mana = std::make_shared<CLabel>(435, 426, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, boost::lexical_cast<std::string>(myHero->mana));
  154. statusBar = CGStatusBar::create(7, 569, "Spelroll.bmp");
  155. Rect temp_rect = CSDL_Ext::genRect(45, 35, 479 + pos.x, 405 + pos.y);
  156. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::fexitb, this), 460, this));
  157. temp_rect = CSDL_Ext::genRect(45, 35, 221 + pos.x, 405 + pos.y);
  158. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::fbattleSpellsb, this), 453, this));
  159. temp_rect = CSDL_Ext::genRect(45, 35, 355 + pos.x, 405 + pos.y);
  160. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::fadvSpellsb, this), 452, this));
  161. temp_rect = CSDL_Ext::genRect(45, 35, 418 + pos.x, 405 + pos.y);
  162. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::fmanaPtsb, this), 459, this));
  163. temp_rect = CSDL_Ext::genRect(36, 56, 549 + pos.x, 94 + pos.y);
  164. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 0), 454, this));
  165. temp_rect = CSDL_Ext::genRect(36, 56, 549 + pos.x, 151 + pos.y);
  166. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 3), 457, this));
  167. temp_rect = CSDL_Ext::genRect(36, 56, 549 + pos.x, 210 + pos.y);
  168. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 1), 455, this));
  169. temp_rect = CSDL_Ext::genRect(36, 56, 549 + pos.x, 270 + pos.y);
  170. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 2), 456, this));
  171. temp_rect = CSDL_Ext::genRect(36, 56, 549 + pos.x, 330 + pos.y);
  172. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 4), 458, this));
  173. temp_rect = CSDL_Ext::genRect(leftCorner->pos.h, leftCorner->pos.w, 97 + pos.x, 77 + pos.y);
  174. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, std::bind(&CSpellWindow::fLcornerb, this), 450, this));
  175. temp_rect = CSDL_Ext::genRect(rightCorner->pos.h, rightCorner->pos.w, 487 + pos.x, 72 + pos.y);
  176. interactiveAreas.push_back(std::make_shared<InteractiveArea>(temp_rect, 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. temp_rect = CSDL_Ext::genRect(65, 78, xpos, ypos);
  182. spellAreas[v] = std::make_shared<SpellArea>(temp_rect, this);
  183. if(v == 5) //to right page
  184. {
  185. xpos = 336 + pos.x; ypos = 90 + pos.y;
  186. }
  187. else
  188. {
  189. if(v%2 == 0)
  190. {
  191. xpos+=85;
  192. }
  193. else
  194. {
  195. xpos -= 85; ypos+=97;
  196. }
  197. }
  198. }
  199. selectedTab = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap;
  200. schoolTab->setFrame(selectedTab, 0);
  201. int cp = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap;
  202. // spellbook last page battle index is not reset after battle, so this needs to stay here
  203. vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1));
  204. setCurrentPage(cp);
  205. computeSpellsPerArea();
  206. addUsedEvents(KEYBOARD);
  207. }
  208. CSpellWindow::~CSpellWindow()
  209. {
  210. }
  211. void CSpellWindow::fexitb()
  212. {
  213. (myInt->battleInt ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  214. (myInt->battleInt ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap) = currentPage;
  215. close();
  216. }
  217. void CSpellWindow::fadvSpellsb()
  218. {
  219. if(battleSpellsOnly == true)
  220. {
  221. turnPageRight();
  222. battleSpellsOnly = false;
  223. setCurrentPage(0);
  224. }
  225. computeSpellsPerArea();
  226. }
  227. void CSpellWindow::fbattleSpellsb()
  228. {
  229. if(battleSpellsOnly == false)
  230. {
  231. turnPageLeft();
  232. battleSpellsOnly = true;
  233. setCurrentPage(0);
  234. }
  235. computeSpellsPerArea();
  236. }
  237. void CSpellWindow::fmanaPtsb()
  238. {
  239. }
  240. void CSpellWindow::selectSchool(int school)
  241. {
  242. if(selectedTab != school)
  243. {
  244. if(selectedTab < school)
  245. turnPageLeft();
  246. else
  247. turnPageRight();
  248. selectedTab = school;
  249. schoolTab->setFrame(selectedTab, 0);
  250. setCurrentPage(0);
  251. }
  252. computeSpellsPerArea();
  253. }
  254. void CSpellWindow::fLcornerb()
  255. {
  256. if(currentPage>0)
  257. {
  258. turnPageLeft();
  259. setCurrentPage(currentPage - 1);
  260. }
  261. computeSpellsPerArea();
  262. GH.breakEventHandling();
  263. }
  264. void CSpellWindow::fRcornerb()
  265. {
  266. if((currentPage + 1) < (pagesWithinCurrentTab()))
  267. {
  268. turnPageRight();
  269. setCurrentPage(currentPage + 1);
  270. }
  271. computeSpellsPerArea();
  272. GH.breakEventHandling();
  273. }
  274. void CSpellWindow::show(SDL_Surface * to)
  275. {
  276. statusBar->show(to);
  277. }
  278. void CSpellWindow::computeSpellsPerArea()
  279. {
  280. std::vector<const CSpell *> spellsCurSite;
  281. spellsCurSite.reserve(mySpells.size());
  282. for(const CSpell * spell : mySpells)
  283. {
  284. if(spell->isCombat() ^ !battleSpellsOnly
  285. && ((selectedTab == 4) || spell->school.at((ESpellSchool)selectedTab))
  286. )
  287. {
  288. spellsCurSite.push_back(spell);
  289. }
  290. }
  291. if(selectedTab == 4)
  292. {
  293. if(spellsCurSite.size() > 12)
  294. {
  295. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + currentPage*12, spellsCurSite.end());
  296. if(spellsCurSite.size() > 12)
  297. {
  298. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  299. }
  300. }
  301. }
  302. else //selectedTab == 0, 1, 2 or 3
  303. {
  304. if(spellsCurSite.size() > 10)
  305. {
  306. if(currentPage == 0)
  307. {
  308. spellsCurSite.erase(spellsCurSite.begin()+10, spellsCurSite.end());
  309. }
  310. else
  311. {
  312. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + (currentPage-1)*12 + 10, spellsCurSite.end());
  313. if(spellsCurSite.size() > 12)
  314. {
  315. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  316. }
  317. }
  318. }
  319. }
  320. //applying
  321. if(selectedTab == 4 || currentPage != 0)
  322. {
  323. for(size_t c=0; c<12; ++c)
  324. {
  325. if(c < spellsCurSite.size())
  326. {
  327. spellAreas[c]->setSpell(spellsCurSite[c]);
  328. }
  329. else
  330. {
  331. spellAreas[c]->setSpell(nullptr);
  332. }
  333. }
  334. }
  335. else
  336. {
  337. spellAreas[0]->setSpell(nullptr);
  338. spellAreas[1]->setSpell(nullptr);
  339. for(size_t c=0; c<10; ++c)
  340. {
  341. if(c < spellsCurSite.size())
  342. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  343. else
  344. spellAreas[c+2]->setSpell(nullptr);
  345. }
  346. }
  347. redraw();
  348. }
  349. void CSpellWindow::setCurrentPage(int value)
  350. {
  351. currentPage = value;
  352. schoolPicture->visible = selectedTab!=4 && currentPage == 0;
  353. if(selectedTab != 4)
  354. schoolPicture->setFrame(selectedTab, 0);
  355. leftCorner->visible = currentPage != 0;
  356. rightCorner->visible = (currentPage+1) < pagesWithinCurrentTab();
  357. mana->setText(boost::lexical_cast<std::string>(myHero->mana));//just in case, it will be possible to cast spell without closing book
  358. }
  359. void CSpellWindow::turnPageLeft()
  360. {
  361. if(settings["video"]["spellbookAnimation"].Bool())
  362. CCS->videoh->openAndPlayVideo("PGTRNLFT.SMK", pos.x+13, pos.y+15);
  363. }
  364. void CSpellWindow::turnPageRight()
  365. {
  366. if(settings["video"]["spellbookAnimation"].Bool())
  367. CCS->videoh->openAndPlayVideo("PGTRNRGH.SMK", pos.x+13, pos.y+15);
  368. }
  369. void CSpellWindow::keyPressed(const SDL_Keycode & key)
  370. {
  371. if(key == SDLK_ESCAPE || key == SDLK_RETURN)
  372. {
  373. fexitb();
  374. return;
  375. }
  376. else
  377. {
  378. switch(key)
  379. {
  380. case SDLK_LEFT:
  381. fLcornerb();
  382. break;
  383. case SDLK_RIGHT:
  384. fRcornerb();
  385. break;
  386. case SDLK_UP:
  387. case SDLK_DOWN:
  388. {
  389. bool down = key == SDLK_DOWN;
  390. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  391. int index = -1;
  392. while(schoolsOrder[++index] != selectedTab);
  393. index += (down ? 1 : -1);
  394. vstd::abetween(index, 0, ARRAY_COUNT(schoolsOrder) - 1);
  395. if(selectedTab != schoolsOrder[index])
  396. selectSchool(schoolsOrder[index]);
  397. break;
  398. }
  399. case SDLK_c:
  400. fbattleSpellsb();
  401. break;
  402. case SDLK_a:
  403. fadvSpellsb();
  404. break;
  405. default://to get rid of warnings
  406. break;
  407. }
  408. //alt + 1234567890-= casts spell from 1 - 12 slot
  409. if(GH.isKeyboardAltDown())
  410. {
  411. SDL_Keycode hlpKey = key;
  412. if(CGuiHandler::isNumKey(hlpKey, false))
  413. {
  414. if(hlpKey == SDLK_KP_PLUS)
  415. hlpKey = SDLK_EQUALS;
  416. else
  417. hlpKey = CGuiHandler::numToDigit(hlpKey);
  418. }
  419. static const SDL_Keycode spellSelectors[] = {SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5, SDLK_6, SDLK_7, SDLK_8, SDLK_9, SDLK_0, SDLK_MINUS, SDLK_EQUALS};
  420. int index = -1;
  421. while(++index < ARRAY_COUNT(spellSelectors) && spellSelectors[index] != hlpKey);
  422. if(index >= ARRAY_COUNT(spellSelectors))
  423. return;
  424. //try casting spell
  425. spellAreas[index]->clickLeft(false, true);
  426. }
  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 | RCLICK | 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::clickLeft(tribool down, bool previousState)
  451. {
  452. if(mySpell && !down)
  453. {
  454. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  455. if(spellCost > owner->myHero->mana) //insufficient mana
  456. {
  457. owner->myInt->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. owner->myInt->showInfoDialog(mySpell->getDescriptionTranslated(schoolLevel), hlp);
  475. }
  476. else if(combatSpell)
  477. {
  478. spells::detail::ProblemImpl problem;
  479. if(mySpell->canBeCast(problem, owner->myInt->cb.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. owner->myInt->showInfoDialog(texts.front());
  490. else
  491. owner->myInt->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.spellUnknownProblem"));
  492. }
  493. }
  494. else //adventure spell
  495. {
  496. const CGHeroInstance * h = owner->myHero;
  497. GH.popInts(1);
  498. auto guard = vstd::makeScopeGuard([this]()
  499. {
  500. owner->myInt->spellbookSettings.spellbookLastTabAdvmap = owner->selectedTab;
  501. owner->myInt->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::clickRight(tribool down, bool previousState)
  513. {
  514. if(mySpell && down)
  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", boost::lexical_cast<std::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::to_string(boost::format("%s (%s)") % mySpell->getNameTranslated() % CGI->generaltexth->allTexts[171+mySpell->level]));
  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. SDL_Color 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->level];
  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->level]);
  580. cost->color = secondLineColor;
  581. boost::format costfmt("%s: %d");
  582. costfmt % CGI->generaltexth->allTexts[387] % spellCost;
  583. cost->setText(costfmt.str());
  584. }
  585. }