CSpellWindow.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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 "../../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. }
  254. void CSpellWindow::fRcornerb()
  255. {
  256. if((currentPage + 1) < (pagesWithinCurrentTab()))
  257. {
  258. turnPageRight();
  259. setCurrentPage(currentPage + 1);
  260. }
  261. computeSpellsPerArea();
  262. }
  263. void CSpellWindow::show(Canvas & to)
  264. {
  265. statusBar->show(to);
  266. }
  267. void CSpellWindow::computeSpellsPerArea()
  268. {
  269. std::vector<const CSpell *> spellsCurSite;
  270. spellsCurSite.reserve(mySpells.size());
  271. for(const CSpell * spell : mySpells)
  272. {
  273. if(spell->isCombat() ^ !battleSpellsOnly
  274. && ((selectedTab == 4) || spell->school.at(SpellSchool(selectedTab)))
  275. )
  276. {
  277. spellsCurSite.push_back(spell);
  278. }
  279. }
  280. if(selectedTab == 4)
  281. {
  282. if(spellsCurSite.size() > 12)
  283. {
  284. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + currentPage*12, spellsCurSite.end());
  285. if(spellsCurSite.size() > 12)
  286. {
  287. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  288. }
  289. }
  290. }
  291. else //selectedTab == 0, 1, 2 or 3
  292. {
  293. if(spellsCurSite.size() > 10)
  294. {
  295. if(currentPage == 0)
  296. {
  297. spellsCurSite.erase(spellsCurSite.begin()+10, spellsCurSite.end());
  298. }
  299. else
  300. {
  301. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + (currentPage-1)*12 + 10, spellsCurSite.end());
  302. if(spellsCurSite.size() > 12)
  303. {
  304. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  305. }
  306. }
  307. }
  308. }
  309. //applying
  310. if(selectedTab == 4 || currentPage != 0)
  311. {
  312. for(size_t c=0; c<12; ++c)
  313. {
  314. if(c < spellsCurSite.size())
  315. {
  316. spellAreas[c]->setSpell(spellsCurSite[c]);
  317. }
  318. else
  319. {
  320. spellAreas[c]->setSpell(nullptr);
  321. }
  322. }
  323. }
  324. else
  325. {
  326. spellAreas[0]->setSpell(nullptr);
  327. spellAreas[1]->setSpell(nullptr);
  328. for(size_t c=0; c<10; ++c)
  329. {
  330. if(c < spellsCurSite.size())
  331. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  332. else
  333. spellAreas[c+2]->setSpell(nullptr);
  334. }
  335. }
  336. redraw();
  337. }
  338. void CSpellWindow::setCurrentPage(int value)
  339. {
  340. currentPage = value;
  341. schoolPicture->visible = selectedTab!=4 && currentPage == 0;
  342. if(selectedTab != 4)
  343. schoolPicture->setFrame(selectedTab, 0);
  344. leftCorner->visible = currentPage != 0;
  345. rightCorner->visible = (currentPage+1) < pagesWithinCurrentTab();
  346. mana->setText(std::to_string(myHero->mana));//just in case, it will be possible to cast spell without closing book
  347. }
  348. void CSpellWindow::turnPageLeft()
  349. {
  350. if(settings["video"]["spellbookAnimation"].Bool())
  351. CCS->videoh->openAndPlayVideo("PGTRNLFT.SMK", pos.x+13, pos.y+15);
  352. }
  353. void CSpellWindow::turnPageRight()
  354. {
  355. if(settings["video"]["spellbookAnimation"].Bool())
  356. CCS->videoh->openAndPlayVideo("PGTRNRGH.SMK", pos.x+13, pos.y+15);
  357. }
  358. void CSpellWindow::keyPressed(EShortcut key)
  359. {
  360. switch(key)
  361. {
  362. case EShortcut::GLOBAL_RETURN:
  363. fexitb();
  364. break;
  365. case EShortcut::MOVE_LEFT:
  366. fLcornerb();
  367. break;
  368. case EShortcut::MOVE_RIGHT:
  369. fRcornerb();
  370. break;
  371. case EShortcut::MOVE_UP:
  372. case EShortcut::MOVE_DOWN:
  373. {
  374. bool down = key == EShortcut::MOVE_DOWN;
  375. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  376. int index = -1;
  377. while(schoolsOrder[++index] != selectedTab);
  378. index += (down ? 1 : -1);
  379. vstd::abetween<int>(index, 0, std::size(schoolsOrder) - 1);
  380. if(selectedTab != schoolsOrder[index])
  381. selectSchool(schoolsOrder[index]);
  382. break;
  383. }
  384. case EShortcut::SPELLBOOK_TAB_COMBAT:
  385. fbattleSpellsb();
  386. break;
  387. case EShortcut::SPELLBOOK_TAB_ADVENTURE:
  388. fadvSpellsb();
  389. break;
  390. }
  391. }
  392. int CSpellWindow::pagesWithinCurrentTab()
  393. {
  394. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  395. }
  396. CSpellWindow::SpellArea::SpellArea(Rect pos, CSpellWindow * owner)
  397. {
  398. this->pos = pos;
  399. this->owner = owner;
  400. addUsedEvents(LCLICK | RCLICK | HOVER);
  401. schoolLevel = -1;
  402. mySpell = nullptr;
  403. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  404. image = std::make_shared<CAnimImage>(owner->spellIcons, 0, 0);
  405. image->visible = false;
  406. name = std::make_shared<CLabel>(39, 70, FONT_TINY, ETextAlignment::CENTER);
  407. level = std::make_shared<CLabel>(39, 82, FONT_TINY, ETextAlignment::CENTER);
  408. cost = std::make_shared<CLabel>(39, 94, FONT_TINY, ETextAlignment::CENTER);
  409. for(auto l : {name, level, cost})
  410. l->setAutoRedraw(false);
  411. }
  412. CSpellWindow::SpellArea::~SpellArea() = default;
  413. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  414. {
  415. if(mySpell && !down)
  416. {
  417. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  418. if(spellCost > owner->myHero->mana) //insufficient mana
  419. {
  420. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[206]) % spellCost % owner->myHero->mana));
  421. return;
  422. }
  423. //anything that is not combat spell is adventure spell
  424. //this not an error in general to cast even creature ability with hero
  425. const bool combatSpell = mySpell->isCombat();
  426. if(combatSpell == mySpell->isAdventure())
  427. {
  428. logGlobal->error("Spell have invalid flags");
  429. return;
  430. }
  431. const bool inCombat = owner->myInt->battleInt != nullptr;
  432. const bool inCastle = owner->myInt->castleInt != nullptr;
  433. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  434. if((combatSpell ^ inCombat) || inCastle)
  435. {
  436. std::vector<std::shared_ptr<CComponent>> hlp(1, std::make_shared<CComponent>(CComponent::spell, mySpell->id, 0));
  437. LOCPLINT->showInfoDialog(mySpell->getDescriptionTranslated(schoolLevel), hlp);
  438. }
  439. else if(combatSpell)
  440. {
  441. spells::detail::ProblemImpl problem;
  442. if(mySpell->canBeCast(problem, owner->myInt->cb.get(), spells::Mode::HERO, owner->myHero))
  443. {
  444. owner->myInt->battleInt->castThisSpell(mySpell->id);
  445. owner->fexitb();
  446. }
  447. else
  448. {
  449. std::vector<std::string> texts;
  450. problem.getAll(texts);
  451. if(!texts.empty())
  452. LOCPLINT->showInfoDialog(texts.front());
  453. else
  454. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.spellUnknownProblem"));
  455. }
  456. }
  457. else //adventure spell
  458. {
  459. const CGHeroInstance * h = owner->myHero;
  460. GH.windows().popWindows(1);
  461. auto guard = vstd::makeScopeGuard([this]()
  462. {
  463. owner->myInt->localState->spellbookSettings.spellbookLastTabAdvmap = owner->selectedTab;
  464. owner->myInt->localState->spellbookSettings.spellbokLastPageAdvmap = owner->currentPage;
  465. });
  466. if(mySpell->getTargetType() == spells::AimType::LOCATION)
  467. adventureInt->enterCastingMode(mySpell);
  468. else if(mySpell->getTargetType() == spells::AimType::NO_TARGET)
  469. owner->myInt->cb->castSpell(h, mySpell->id);
  470. else
  471. logGlobal->error("Invalid spell target type");
  472. }
  473. }
  474. }
  475. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  476. {
  477. if(mySpell && down)
  478. {
  479. std::string dmgInfo;
  480. auto causedDmg = owner->myInt->cb->estimateSpellDamage(mySpell, owner->myHero);
  481. if(causedDmg == 0 || mySpell->id == SpellID::TITANS_LIGHTNING_BOLT) //Titan's Lightning Bolt already has damage info included
  482. dmgInfo.clear();
  483. else
  484. {
  485. dmgInfo = CGI->generaltexth->allTexts[343];
  486. boost::algorithm::replace_first(dmgInfo, "%d", std::to_string(causedDmg));
  487. }
  488. CRClickPopup::createAndPush(mySpell->getDescriptionTranslated(schoolLevel) + dmgInfo, std::make_shared<CComponent>(CComponent::spell, mySpell->id));
  489. }
  490. }
  491. void CSpellWindow::SpellArea::hover(bool on)
  492. {
  493. if(mySpell)
  494. {
  495. if(on)
  496. owner->statusBar->write(boost::to_string(boost::format("%s (%s)") % mySpell->getNameTranslated() % CGI->generaltexth->allTexts[171+mySpell->level]));
  497. else
  498. owner->statusBar->clear();
  499. }
  500. }
  501. void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
  502. {
  503. schoolBorder.reset();
  504. image->visible = false;
  505. name->setText("");
  506. level->setText("");
  507. cost->setText("");
  508. mySpell = spell;
  509. if(mySpell)
  510. {
  511. int32_t whichSchool = 0; //0 - air magic, 1 - fire magic, 2 - water magic, 3 - earth magic,
  512. schoolLevel = owner->myHero->getSpellSchoolLevel(mySpell, &whichSchool);
  513. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  514. image->setFrame(mySpell->id);
  515. image->visible = true;
  516. {
  517. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  518. schoolBorder = std::make_shared<CAnimImage>(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab], schoolLevel);
  519. }
  520. SDL_Color firstLineColor, secondLineColor;
  521. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  522. {
  523. firstLineColor = Colors::WHITE;
  524. secondLineColor = Colors::ORANGE;
  525. }
  526. else
  527. {
  528. firstLineColor = Colors::YELLOW;
  529. secondLineColor = Colors::WHITE;
  530. }
  531. name->color = firstLineColor;
  532. name->setText(mySpell->getNameTranslated());
  533. level->color = secondLineColor;
  534. if(schoolLevel > 0)
  535. {
  536. boost::format fmt("%s/%s");
  537. fmt % CGI->generaltexth->allTexts[171 + mySpell->level];
  538. fmt % CGI->generaltexth->levels[3+(schoolLevel-1)];//lines 4-6
  539. level->setText(fmt.str());
  540. }
  541. else
  542. level->setText(CGI->generaltexth->allTexts[171 + mySpell->level]);
  543. cost->color = secondLineColor;
  544. boost::format costfmt("%s: %d");
  545. costfmt % CGI->generaltexth->allTexts[387] % spellCost;
  546. cost->setText(costfmt.str());
  547. }
  548. }