CSpellWindow.cpp 18 KB

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