CSpellWindow.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. adventureInt->handleRightClick(helpText, down);
  51. }
  52. void CSpellWindow::InteractiveArea::hover(bool on)
  53. {
  54. if(on)
  55. owner->statusBar->write(hoverText);
  56. else
  57. owner->statusBar->clear();
  58. }
  59. class SpellbookSpellSorter
  60. {
  61. public:
  62. bool operator()(const CSpell * A, const CSpell * B)
  63. {
  64. if(A->level < B->level)
  65. return true;
  66. if(A->level > B->level)
  67. return false;
  68. for(ui8 schoolId = 0; schoolId < 4; schoolId++)
  69. {
  70. if(A->school.at((ESpellSchool)schoolId) && !B->school.at((ESpellSchool)schoolId))
  71. return true;
  72. if(!A->school.at((ESpellSchool)schoolId) && B->school.at((ESpellSchool)schoolId))
  73. return false;
  74. }
  75. return A->getNameTranslated() < B->getNameTranslated();
  76. }
  77. } spellsorter;
  78. CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  79. CWindowObject(PLAYER_COLORED, "SpelBack"),
  80. battleSpellsOnly(openOnBattleSpells),
  81. selectedTab(4),
  82. currentPage(0),
  83. myHero(_myHero),
  84. myInt(_myInt)
  85. {
  86. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  87. //initializing castable spells
  88. mySpells.reserve(CGI->spellh->objects.size());
  89. for(const CSpell * spell : CGI->spellh->objects)
  90. {
  91. if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell))
  92. mySpells.push_back(spell);
  93. }
  94. std::sort(mySpells.begin(), mySpells.end(), spellsorter);
  95. //initializing sizes of spellbook's parts
  96. for(auto & elem : sitesPerTabAdv)
  97. elem = 0;
  98. for(auto & elem : sitesPerTabBattle)
  99. elem = 0;
  100. for(const auto spell : mySpells)
  101. {
  102. int * sitesPerOurTab = spell->isCombat() ? sitesPerTabBattle : sitesPerTabAdv;
  103. ++sitesPerOurTab[4];
  104. spell->forEachSchool([&sitesPerOurTab](const spells::SchoolInfo & school, bool & stop)
  105. {
  106. ++sitesPerOurTab[(ui8)school.id];
  107. });
  108. }
  109. if(sitesPerTabAdv[4] % 12 == 0)
  110. sitesPerTabAdv[4]/=12;
  111. else
  112. sitesPerTabAdv[4] = sitesPerTabAdv[4]/12 + 1;
  113. for(int v=0; v<4; ++v)
  114. {
  115. if(sitesPerTabAdv[v] <= 10)
  116. sitesPerTabAdv[v] = 1;
  117. else
  118. {
  119. if((sitesPerTabAdv[v] - 10) % 12 == 0)
  120. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 1;
  121. else
  122. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 2;
  123. }
  124. }
  125. if(sitesPerTabBattle[4] % 12 == 0)
  126. sitesPerTabBattle[4]/=12;
  127. else
  128. sitesPerTabBattle[4] = sitesPerTabBattle[4]/12 + 1;
  129. for(int v=0; v<4; ++v)
  130. {
  131. if(sitesPerTabBattle[v] <= 10)
  132. sitesPerTabBattle[v] = 1;
  133. else
  134. {
  135. if((sitesPerTabBattle[v] - 10) % 12 == 0)
  136. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 1;
  137. else
  138. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 2;
  139. }
  140. }
  141. //numbers of spell pages computed
  142. leftCorner = std::make_shared<CPicture>("SpelTrnL.bmp", 97, 77);
  143. rightCorner = std::make_shared<CPicture>("SpelTrnR.bmp", 487, 72);
  144. spellIcons = std::make_shared<CAnimation>("Spells");
  145. schoolTab = std::make_shared<CAnimImage>("SpelTab", selectedTab, 0, 524, 88);
  146. schoolPicture = std::make_shared<CAnimImage>("Schools", 0, 0, 117, 74);
  147. schoolBorders[0] = std::make_shared<CAnimation>("SplevA.def");
  148. schoolBorders[1] = std::make_shared<CAnimation>("SplevF.def");
  149. schoolBorders[2] = std::make_shared<CAnimation>("SplevW.def");
  150. schoolBorders[3] = std::make_shared<CAnimation>("SplevE.def");
  151. for(auto item : schoolBorders)
  152. item->preload();
  153. mana = std::make_shared<CLabel>(435, 426, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, boost::lexical_cast<std::string>(myHero->mana));
  154. statusBar = CGStatusBar::create(7, 569, "Spelroll.bmp");
  155. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 479 + pos.x, 405 + pos.y, 36, 56), std::bind(&CSpellWindow::fexitb, this), 460, this));
  156. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 221 + pos.x, 405 + pos.y, 36, 56), std::bind(&CSpellWindow::fbattleSpellsb, this), 453, this));
  157. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 355 + pos.x, 405 + pos.y, 36, 56), std::bind(&CSpellWindow::fadvSpellsb, this), 452, this));
  158. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 418 + pos.x, 405 + pos.y, 36, 56), std::bind(&CSpellWindow::fmanaPtsb, this), 459, this));
  159. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x, 94 + pos.y, 36, 56), std::bind(&CSpellWindow::selectSchool, this, 0), 454, this));
  160. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x, 151 + pos.y, 45, 35), std::bind(&CSpellWindow::selectSchool, this, 3), 457, this));
  161. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x, 210 + pos.y, 45, 35), std::bind(&CSpellWindow::selectSchool, this, 1), 455, this));
  162. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x, 270 + pos.y, 45, 35), std::bind(&CSpellWindow::selectSchool, this, 2), 456, this));
  163. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x, 330 + pos.y, 45, 35), std::bind(&CSpellWindow::selectSchool, this, 4), 458, this));
  164. 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));
  165. 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));
  166. //areas for spells
  167. int xpos = 117 + pos.x, ypos = 90 + pos.y;
  168. for(int v=0; v<12; ++v)
  169. {
  170. spellAreas[v] = std::make_shared<SpellArea>( Rect(xpos, ypos, 65, 78), this);
  171. if(v == 5) //to right page
  172. {
  173. xpos = 336 + pos.x; ypos = 90 + pos.y;
  174. }
  175. else
  176. {
  177. if(v%2 == 0)
  178. {
  179. xpos+=85;
  180. }
  181. else
  182. {
  183. xpos -= 85; ypos+=97;
  184. }
  185. }
  186. }
  187. selectedTab = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap;
  188. schoolTab->setFrame(selectedTab, 0);
  189. int cp = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap;
  190. // spellbook last page battle index is not reset after battle, so this needs to stay here
  191. vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1));
  192. setCurrentPage(cp);
  193. computeSpellsPerArea();
  194. addUsedEvents(KEYBOARD);
  195. }
  196. CSpellWindow::~CSpellWindow()
  197. {
  198. }
  199. void CSpellWindow::fexitb()
  200. {
  201. (myInt->battleInt ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  202. (myInt->battleInt ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap) = currentPage;
  203. close();
  204. }
  205. void CSpellWindow::fadvSpellsb()
  206. {
  207. if(battleSpellsOnly == true)
  208. {
  209. turnPageRight();
  210. battleSpellsOnly = false;
  211. setCurrentPage(0);
  212. }
  213. computeSpellsPerArea();
  214. }
  215. void CSpellWindow::fbattleSpellsb()
  216. {
  217. if(battleSpellsOnly == false)
  218. {
  219. turnPageLeft();
  220. battleSpellsOnly = true;
  221. setCurrentPage(0);
  222. }
  223. computeSpellsPerArea();
  224. }
  225. void CSpellWindow::fmanaPtsb()
  226. {
  227. }
  228. void CSpellWindow::selectSchool(int school)
  229. {
  230. if(selectedTab != school)
  231. {
  232. if(selectedTab < school)
  233. turnPageLeft();
  234. else
  235. turnPageRight();
  236. selectedTab = school;
  237. schoolTab->setFrame(selectedTab, 0);
  238. setCurrentPage(0);
  239. }
  240. computeSpellsPerArea();
  241. }
  242. void CSpellWindow::fLcornerb()
  243. {
  244. if(currentPage>0)
  245. {
  246. turnPageLeft();
  247. setCurrentPage(currentPage - 1);
  248. }
  249. computeSpellsPerArea();
  250. GH.breakEventHandling();
  251. }
  252. void CSpellWindow::fRcornerb()
  253. {
  254. if((currentPage + 1) < (pagesWithinCurrentTab()))
  255. {
  256. turnPageRight();
  257. setCurrentPage(currentPage + 1);
  258. }
  259. computeSpellsPerArea();
  260. GH.breakEventHandling();
  261. }
  262. void CSpellWindow::show(SDL_Surface * 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((ESpellSchool)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(boost::lexical_cast<std::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(const SDL_Keycode & key)
  358. {
  359. if(key == SDLK_ESCAPE || key == SDLK_RETURN)
  360. {
  361. fexitb();
  362. return;
  363. }
  364. else
  365. {
  366. switch(key)
  367. {
  368. case SDLK_LEFT:
  369. fLcornerb();
  370. break;
  371. case SDLK_RIGHT:
  372. fRcornerb();
  373. break;
  374. case SDLK_UP:
  375. case SDLK_DOWN:
  376. {
  377. bool down = key == SDLK_DOWN;
  378. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  379. int index = -1;
  380. while(schoolsOrder[++index] != selectedTab);
  381. index += (down ? 1 : -1);
  382. vstd::abetween(index, 0, ARRAY_COUNT(schoolsOrder) - 1);
  383. if(selectedTab != schoolsOrder[index])
  384. selectSchool(schoolsOrder[index]);
  385. break;
  386. }
  387. case SDLK_c:
  388. fbattleSpellsb();
  389. break;
  390. case SDLK_a:
  391. fadvSpellsb();
  392. break;
  393. default://to get rid of warnings
  394. break;
  395. }
  396. //alt + 1234567890-= casts spell from 1 - 12 slot
  397. if(GH.isKeyboardAltDown())
  398. {
  399. SDL_Keycode hlpKey = key;
  400. if(CGuiHandler::isNumKey(hlpKey, false))
  401. {
  402. if(hlpKey == SDLK_KP_PLUS)
  403. hlpKey = SDLK_EQUALS;
  404. else
  405. hlpKey = CGuiHandler::numToDigit(hlpKey);
  406. }
  407. 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};
  408. int index = -1;
  409. while(++index < ARRAY_COUNT(spellSelectors) && spellSelectors[index] != hlpKey);
  410. if(index >= ARRAY_COUNT(spellSelectors))
  411. return;
  412. //try casting spell
  413. spellAreas[index]->clickLeft(false, true);
  414. }
  415. }
  416. }
  417. int CSpellWindow::pagesWithinCurrentTab()
  418. {
  419. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  420. }
  421. CSpellWindow::SpellArea::SpellArea(Rect pos, CSpellWindow * owner)
  422. {
  423. this->pos = pos;
  424. this->owner = owner;
  425. addUsedEvents(LCLICK | RCLICK | HOVER);
  426. schoolLevel = -1;
  427. mySpell = nullptr;
  428. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  429. image = std::make_shared<CAnimImage>(owner->spellIcons, 0, 0);
  430. image->visible = false;
  431. name = std::make_shared<CLabel>(39, 70, FONT_TINY, ETextAlignment::CENTER);
  432. level = std::make_shared<CLabel>(39, 82, FONT_TINY, ETextAlignment::CENTER);
  433. cost = std::make_shared<CLabel>(39, 94, FONT_TINY, ETextAlignment::CENTER);
  434. for(auto l : {name, level, cost})
  435. l->setAutoRedraw(false);
  436. }
  437. CSpellWindow::SpellArea::~SpellArea() = default;
  438. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  439. {
  440. if(mySpell && !down)
  441. {
  442. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  443. if(spellCost > owner->myHero->mana) //insufficient mana
  444. {
  445. owner->myInt->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[206]) % spellCost % owner->myHero->mana));
  446. return;
  447. }
  448. //anything that is not combat spell is adventure spell
  449. //this not an error in general to cast even creature ability with hero
  450. const bool combatSpell = mySpell->isCombat();
  451. if(combatSpell == mySpell->isAdventure())
  452. {
  453. logGlobal->error("Spell have invalid flags");
  454. return;
  455. }
  456. const bool inCombat = owner->myInt->battleInt != nullptr;
  457. const bool inCastle = owner->myInt->castleInt != nullptr;
  458. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  459. if((combatSpell ^ inCombat) || inCastle)
  460. {
  461. std::vector<std::shared_ptr<CComponent>> hlp(1, std::make_shared<CComponent>(CComponent::spell, mySpell->id, 0));
  462. owner->myInt->showInfoDialog(mySpell->getDescriptionTranslated(schoolLevel), hlp);
  463. }
  464. else if(combatSpell)
  465. {
  466. spells::detail::ProblemImpl problem;
  467. if(mySpell->canBeCast(problem, owner->myInt->cb.get(), spells::Mode::HERO, owner->myHero))
  468. {
  469. owner->myInt->battleInt->castThisSpell(mySpell->id);
  470. owner->fexitb();
  471. }
  472. else
  473. {
  474. std::vector<std::string> texts;
  475. problem.getAll(texts);
  476. if(!texts.empty())
  477. owner->myInt->showInfoDialog(texts.front());
  478. else
  479. owner->myInt->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.spellUnknownProblem"));
  480. }
  481. }
  482. else //adventure spell
  483. {
  484. const CGHeroInstance * h = owner->myHero;
  485. GH.popInts(1);
  486. auto guard = vstd::makeScopeGuard([this]()
  487. {
  488. owner->myInt->spellbookSettings.spellbookLastTabAdvmap = owner->selectedTab;
  489. owner->myInt->spellbookSettings.spellbokLastPageAdvmap = owner->currentPage;
  490. });
  491. if(mySpell->getTargetType() == spells::AimType::LOCATION)
  492. adventureInt->enterCastingMode(mySpell);
  493. else if(mySpell->getTargetType() == spells::AimType::NO_TARGET)
  494. owner->myInt->cb->castSpell(h, mySpell->id);
  495. else
  496. logGlobal->error("Invalid spell target type");
  497. }
  498. }
  499. }
  500. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  501. {
  502. if(mySpell && down)
  503. {
  504. std::string dmgInfo;
  505. auto causedDmg = owner->myInt->cb->estimateSpellDamage(mySpell, owner->myHero);
  506. if(causedDmg == 0 || mySpell->id == SpellID::TITANS_LIGHTNING_BOLT) //Titan's Lightning Bolt already has damage info included
  507. dmgInfo.clear();
  508. else
  509. {
  510. dmgInfo = CGI->generaltexth->allTexts[343];
  511. boost::algorithm::replace_first(dmgInfo, "%d", boost::lexical_cast<std::string>(causedDmg));
  512. }
  513. CRClickPopup::createAndPush(mySpell->getDescriptionTranslated(schoolLevel) + dmgInfo, std::make_shared<CComponent>(CComponent::spell, mySpell->id));
  514. }
  515. }
  516. void CSpellWindow::SpellArea::hover(bool on)
  517. {
  518. if(mySpell)
  519. {
  520. if(on)
  521. owner->statusBar->write(boost::to_string(boost::format("%s (%s)") % mySpell->getNameTranslated() % CGI->generaltexth->allTexts[171+mySpell->level]));
  522. else
  523. owner->statusBar->clear();
  524. }
  525. }
  526. void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
  527. {
  528. schoolBorder.reset();
  529. image->visible = false;
  530. name->setText("");
  531. level->setText("");
  532. cost->setText("");
  533. mySpell = spell;
  534. if(mySpell)
  535. {
  536. int32_t whichSchool = 0; //0 - air magic, 1 - fire magic, 2 - water magic, 3 - earth magic,
  537. schoolLevel = owner->myHero->getSpellSchoolLevel(mySpell, &whichSchool);
  538. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  539. image->setFrame(mySpell->id);
  540. image->visible = true;
  541. {
  542. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  543. schoolBorder = std::make_shared<CAnimImage>(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab], schoolLevel);
  544. }
  545. SDL_Color firstLineColor, secondLineColor;
  546. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  547. {
  548. firstLineColor = Colors::WHITE;
  549. secondLineColor = Colors::ORANGE;
  550. }
  551. else
  552. {
  553. firstLineColor = Colors::YELLOW;
  554. secondLineColor = Colors::WHITE;
  555. }
  556. name->color = firstLineColor;
  557. name->setText(mySpell->getNameTranslated());
  558. level->color = secondLineColor;
  559. if(schoolLevel > 0)
  560. {
  561. boost::format fmt("%s/%s");
  562. fmt % CGI->generaltexth->allTexts[171 + mySpell->level];
  563. fmt % CGI->generaltexth->levels[3+(schoolLevel-1)];//lines 4-6
  564. level->setText(fmt.str());
  565. }
  566. else
  567. level->setText(CGI->generaltexth->allTexts[171 + mySpell->level]);
  568. cost->color = secondLineColor;
  569. boost::format costfmt("%s: %d");
  570. costfmt % CGI->generaltexth->allTexts[387] % spellCost;
  571. cost->setText(costfmt.str());
  572. }
  573. }