CSpellWindow.cpp 18 KB

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