CSpellWindow.cpp 19 KB

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