CSpellWindow.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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 "../widgets/MiscWidgets.h"
  23. #include "../widgets/CComponent.h"
  24. #include "../widgets/TextControls.h"
  25. #include "../adventureMap/CAdventureMapInterface.h"
  26. #include "../render/CAnimation.h"
  27. #include "../renderSDL/SDL_Extensions.h"
  28. #include "../../CCallback.h"
  29. #include "../../lib/CConfigHandler.h"
  30. #include "../../lib/CGeneralTextHandler.h"
  31. #include "../../lib/spells/CSpellHandler.h"
  32. #include "../../lib/spells/Problem.h"
  33. #include "../../lib/GameConstants.h"
  34. #include "../../lib/mapObjects/CGHeroInstance.h"
  35. CSpellWindow::InteractiveArea::InteractiveArea(const Rect & myRect, std::function<void()> funcL, int helpTextId, CSpellWindow * _owner)
  36. {
  37. addUsedEvents(LCLICK | RCLICK | HOVER);
  38. pos = myRect;
  39. onLeft = funcL;
  40. hoverText = CGI->generaltexth->zelp[helpTextId].first;
  41. helpText = CGI->generaltexth->zelp[helpTextId].second;
  42. owner = _owner;
  43. }
  44. void CSpellWindow::InteractiveArea::clickLeft(tribool down, bool previousState)
  45. {
  46. if(!down)
  47. onLeft();
  48. }
  49. void CSpellWindow::InteractiveArea::clickRight(tribool down, bool previousState)
  50. {
  51. if (down)
  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(ui8 schoolId = 0; schoolId < 4; schoolId++)
  71. {
  72. if(A->school.at((ESpellSchool)schoolId) && !B->school.at((ESpellSchool)schoolId))
  73. return true;
  74. if(!A->school.at((ESpellSchool)schoolId) && B->school.at((ESpellSchool)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. GH.breakEventHandling();
  253. }
  254. void CSpellWindow::fRcornerb()
  255. {
  256. if((currentPage + 1) < (pagesWithinCurrentTab()))
  257. {
  258. turnPageRight();
  259. setCurrentPage(currentPage + 1);
  260. }
  261. computeSpellsPerArea();
  262. GH.breakEventHandling();
  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((ESpellSchool)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(const SDL_Keycode & key)
  360. {
  361. if(key == SDLK_ESCAPE || key == SDLK_RETURN)
  362. {
  363. fexitb();
  364. return;
  365. }
  366. else
  367. {
  368. switch(key)
  369. {
  370. case SDLK_LEFT:
  371. fLcornerb();
  372. break;
  373. case SDLK_RIGHT:
  374. fRcornerb();
  375. break;
  376. case SDLK_UP:
  377. case SDLK_DOWN:
  378. {
  379. bool down = key == SDLK_DOWN;
  380. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  381. int index = -1;
  382. while(schoolsOrder[++index] != selectedTab);
  383. index += (down ? 1 : -1);
  384. vstd::abetween<int>(index, 0, std::size(schoolsOrder) - 1);
  385. if(selectedTab != schoolsOrder[index])
  386. selectSchool(schoolsOrder[index]);
  387. break;
  388. }
  389. case SDLK_c:
  390. fbattleSpellsb();
  391. break;
  392. case SDLK_a:
  393. fadvSpellsb();
  394. break;
  395. default://to get rid of warnings
  396. break;
  397. }
  398. //alt + 1234567890-= casts spell from 1 - 12 slot
  399. if(GH.isKeyboardAltDown())
  400. {
  401. SDL_Keycode hlpKey = key;
  402. if(CGuiHandler::isNumKey(hlpKey, false))
  403. {
  404. if(hlpKey == SDLK_KP_PLUS)
  405. hlpKey = SDLK_EQUALS;
  406. else
  407. hlpKey = CGuiHandler::numToDigit(hlpKey);
  408. }
  409. static const SDL_Keycode spellSelectors[] = {SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5, SDLK_6, SDLK_7, SDLK_8, SDLK_9, SDLK_0, SDLK_MINUS, SDLK_EQUALS};
  410. int index = -1;
  411. while(++index < std::size(spellSelectors) && spellSelectors[index] != hlpKey);
  412. if(index >= std::size(spellSelectors))
  413. return;
  414. //try casting spell
  415. spellAreas[index]->clickLeft(false, true);
  416. }
  417. }
  418. }
  419. int CSpellWindow::pagesWithinCurrentTab()
  420. {
  421. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  422. }
  423. CSpellWindow::SpellArea::SpellArea(Rect pos, CSpellWindow * owner)
  424. {
  425. this->pos = pos;
  426. this->owner = owner;
  427. addUsedEvents(LCLICK | RCLICK | HOVER);
  428. schoolLevel = -1;
  429. mySpell = nullptr;
  430. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  431. image = std::make_shared<CAnimImage>(owner->spellIcons, 0, 0);
  432. image->visible = false;
  433. name = std::make_shared<CLabel>(39, 70, FONT_TINY, ETextAlignment::CENTER);
  434. level = std::make_shared<CLabel>(39, 82, FONT_TINY, ETextAlignment::CENTER);
  435. cost = std::make_shared<CLabel>(39, 94, FONT_TINY, ETextAlignment::CENTER);
  436. for(auto l : {name, level, cost})
  437. l->setAutoRedraw(false);
  438. }
  439. CSpellWindow::SpellArea::~SpellArea() = default;
  440. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  441. {
  442. if(mySpell && !down)
  443. {
  444. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  445. if(spellCost > owner->myHero->mana) //insufficient mana
  446. {
  447. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[206]) % spellCost % owner->myHero->mana));
  448. return;
  449. }
  450. //anything that is not combat spell is adventure spell
  451. //this not an error in general to cast even creature ability with hero
  452. const bool combatSpell = mySpell->isCombat();
  453. if(combatSpell == mySpell->isAdventure())
  454. {
  455. logGlobal->error("Spell have invalid flags");
  456. return;
  457. }
  458. const bool inCombat = owner->myInt->battleInt != nullptr;
  459. const bool inCastle = owner->myInt->castleInt != nullptr;
  460. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  461. if((combatSpell ^ inCombat) || inCastle)
  462. {
  463. std::vector<std::shared_ptr<CComponent>> hlp(1, std::make_shared<CComponent>(CComponent::spell, mySpell->id, 0));
  464. LOCPLINT->showInfoDialog(mySpell->getDescriptionTranslated(schoolLevel), hlp);
  465. }
  466. else if(combatSpell)
  467. {
  468. spells::detail::ProblemImpl problem;
  469. if(mySpell->canBeCast(problem, owner->myInt->cb.get(), spells::Mode::HERO, owner->myHero))
  470. {
  471. owner->myInt->battleInt->castThisSpell(mySpell->id);
  472. owner->fexitb();
  473. }
  474. else
  475. {
  476. std::vector<std::string> texts;
  477. problem.getAll(texts);
  478. if(!texts.empty())
  479. LOCPLINT->showInfoDialog(texts.front());
  480. else
  481. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.spellUnknownProblem"));
  482. }
  483. }
  484. else //adventure spell
  485. {
  486. const CGHeroInstance * h = owner->myHero;
  487. GH.popInts(1);
  488. auto guard = vstd::makeScopeGuard([this]()
  489. {
  490. owner->myInt->localState->spellbookSettings.spellbookLastTabAdvmap = owner->selectedTab;
  491. owner->myInt->localState->spellbookSettings.spellbokLastPageAdvmap = owner->currentPage;
  492. });
  493. if(mySpell->getTargetType() == spells::AimType::LOCATION)
  494. adventureInt->enterCastingMode(mySpell);
  495. else if(mySpell->getTargetType() == spells::AimType::NO_TARGET)
  496. owner->myInt->cb->castSpell(h, mySpell->id);
  497. else
  498. logGlobal->error("Invalid spell target type");
  499. }
  500. }
  501. }
  502. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  503. {
  504. if(mySpell && down)
  505. {
  506. std::string dmgInfo;
  507. auto causedDmg = owner->myInt->cb->estimateSpellDamage(mySpell, owner->myHero);
  508. if(causedDmg == 0 || mySpell->id == SpellID::TITANS_LIGHTNING_BOLT) //Titan's Lightning Bolt already has damage info included
  509. dmgInfo.clear();
  510. else
  511. {
  512. dmgInfo = CGI->generaltexth->allTexts[343];
  513. boost::algorithm::replace_first(dmgInfo, "%d", std::to_string(causedDmg));
  514. }
  515. CRClickPopup::createAndPush(mySpell->getDescriptionTranslated(schoolLevel) + dmgInfo, std::make_shared<CComponent>(CComponent::spell, mySpell->id));
  516. }
  517. }
  518. void CSpellWindow::SpellArea::hover(bool on)
  519. {
  520. if(mySpell)
  521. {
  522. if(on)
  523. owner->statusBar->write(boost::to_string(boost::format("%s (%s)") % mySpell->getNameTranslated() % CGI->generaltexth->allTexts[171+mySpell->level]));
  524. else
  525. owner->statusBar->clear();
  526. }
  527. }
  528. void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
  529. {
  530. schoolBorder.reset();
  531. image->visible = false;
  532. name->setText("");
  533. level->setText("");
  534. cost->setText("");
  535. mySpell = spell;
  536. if(mySpell)
  537. {
  538. int32_t whichSchool = 0; //0 - air magic, 1 - fire magic, 2 - water magic, 3 - earth magic,
  539. schoolLevel = owner->myHero->getSpellSchoolLevel(mySpell, &whichSchool);
  540. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  541. image->setFrame(mySpell->id);
  542. image->visible = true;
  543. {
  544. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  545. schoolBorder = std::make_shared<CAnimImage>(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab], schoolLevel);
  546. }
  547. SDL_Color firstLineColor, secondLineColor;
  548. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  549. {
  550. firstLineColor = Colors::WHITE;
  551. secondLineColor = Colors::ORANGE;
  552. }
  553. else
  554. {
  555. firstLineColor = Colors::YELLOW;
  556. secondLineColor = Colors::WHITE;
  557. }
  558. name->color = firstLineColor;
  559. name->setText(mySpell->getNameTranslated());
  560. level->color = secondLineColor;
  561. if(schoolLevel > 0)
  562. {
  563. boost::format fmt("%s/%s");
  564. fmt % CGI->generaltexth->allTexts[171 + mySpell->level];
  565. fmt % CGI->generaltexth->levels[3+(schoolLevel-1)];//lines 4-6
  566. level->setText(fmt.str());
  567. }
  568. else
  569. level->setText(CGI->generaltexth->allTexts[171 + mySpell->level]);
  570. cost->color = secondLineColor;
  571. boost::format costfmt("%s: %d");
  572. costfmt % CGI->generaltexth->allTexts[387] % spellCost;
  573. cost->setText(costfmt.str());
  574. }
  575. }