CSpellWindow.cpp 18 KB

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