CSpellWindow.cpp 18 KB

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