CSpellWindow.cpp 18 KB

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