CSpellWindow.cpp 19 KB

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