CSpellWindow.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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 "CAdvmapInterface.h"
  14. #include "GUIClasses.h"
  15. #include "InfoWindows.h"
  16. #include "CCastleInterface.h"
  17. #include "../CGameInfo.h"
  18. #include "../CMessage.h"
  19. #include "../CMT.h"
  20. #include "../CPlayerInterface.h"
  21. #include "../CVideoHandler.h"
  22. #include "../Graphics.h"
  23. #include "../battle/CBattleInterface.h"
  24. #include "../gui/CAnimation.h"
  25. #include "../gui/CGuiHandler.h"
  26. #include "../gui/SDL_Extensions.h"
  27. #include "../widgets/MiscWidgets.h"
  28. #include "../widgets/CComponent.h"
  29. #include "../widgets/TextControls.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 SDL_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. adventureInt->handleRightClick(helpText, down);
  54. }
  55. void CSpellWindow::InteractiveArea::hover(bool on)
  56. {
  57. if(on)
  58. owner->statusBar->setText(hoverText);
  59. else
  60. owner->statusBar->clear();
  61. }
  62. class SpellbookSpellSorter
  63. {
  64. public:
  65. bool operator()(const CSpell * A, const CSpell * B)
  66. {
  67. if(A->level < B->level)
  68. return true;
  69. if(A->level > B->level)
  70. return false;
  71. for(ui8 schoolId = 0; schoolId < 4; schoolId++)
  72. {
  73. if(A->school.at((ESpellSchool)schoolId) && !B->school.at((ESpellSchool)schoolId))
  74. return true;
  75. if(!A->school.at((ESpellSchool)schoolId) && B->school.at((ESpellSchool)schoolId))
  76. return false;
  77. }
  78. return A->name < B->name;
  79. }
  80. } spellsorter;
  81. CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  82. CWindowObject(PLAYER_COLORED, "SpelBack"),
  83. battleSpellsOnly(openOnBattleSpells),
  84. selectedTab(4),
  85. currentPage(0),
  86. myHero(_myHero),
  87. myInt(_myInt)
  88. {
  89. OBJ_CONSTRUCTION_CAPTURING_ALL;
  90. //initializing castable spells
  91. mySpells.reserve(CGI->spellh->objects.size());
  92. for(const CSpell * spell : CGI->spellh->objects)
  93. {
  94. if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell))
  95. mySpells.push_back(spell);
  96. }
  97. std::sort(mySpells.begin(), mySpells.end(), spellsorter);
  98. //initializing sizes of spellbook's parts
  99. for(auto & elem : sitesPerTabAdv)
  100. elem = 0;
  101. for(auto & elem : sitesPerTabBattle)
  102. elem = 0;
  103. for(const auto spell : mySpells)
  104. {
  105. int * sitesPerOurTab = spell->isCombatSpell() ? sitesPerTabBattle : sitesPerTabAdv;
  106. ++sitesPerOurTab[4];
  107. spell->forEachSchool([&sitesPerOurTab](const spells::SchoolInfo & school, bool & stop)
  108. {
  109. ++sitesPerOurTab[(ui8)school.id];
  110. });
  111. }
  112. if(sitesPerTabAdv[4] % 12 == 0)
  113. sitesPerTabAdv[4]/=12;
  114. else
  115. sitesPerTabAdv[4] = sitesPerTabAdv[4]/12 + 1;
  116. for(int v=0; v<4; ++v)
  117. {
  118. if(sitesPerTabAdv[v] <= 10)
  119. sitesPerTabAdv[v] = 1;
  120. else
  121. {
  122. if((sitesPerTabAdv[v] - 10) % 12 == 0)
  123. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 1;
  124. else
  125. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 2;
  126. }
  127. }
  128. if(sitesPerTabBattle[4] % 12 == 0)
  129. sitesPerTabBattle[4]/=12;
  130. else
  131. sitesPerTabBattle[4] = sitesPerTabBattle[4]/12 + 1;
  132. for(int v=0; v<4; ++v)
  133. {
  134. if(sitesPerTabBattle[v] <= 10)
  135. sitesPerTabBattle[v] = 1;
  136. else
  137. {
  138. if((sitesPerTabBattle[v] - 10) % 12 == 0)
  139. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 1;
  140. else
  141. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 2;
  142. }
  143. }
  144. //numbers of spell pages computed
  145. leftCorner = new CPicture("SpelTrnL.bmp", 97, 77);
  146. rightCorner = new CPicture("SpelTrnR.bmp", 487, 72);
  147. spells = std::make_shared<CAnimation>("Spells");
  148. spellTab = new CAnimImage("SpelTab", selectedTab, 0, 524, 88);
  149. schools = new CAnimImage("Schools",0,0,117,74);
  150. schoolBorders[0] = std::make_shared<CAnimation>("SplevA.def");
  151. schoolBorders[1] = std::make_shared<CAnimation>("SplevF.def");
  152. schoolBorders[2] = std::make_shared<CAnimation>("SplevW.def");
  153. schoolBorders[3] = std::make_shared<CAnimation>("SplevE.def");
  154. for(auto item : schoolBorders)
  155. item->load();
  156. mana = new CLabel(435, 426, FONT_SMALL, CENTER, Colors::YELLOW, boost::lexical_cast<std::string>(myHero->mana));
  157. statusBar = new CGStatusBar(7, 569, "Spelroll.bmp");
  158. SDL_Rect temp_rect = genRect(45, 35, 479 + pos.x, 405 + pos.y);
  159. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fexitb, this), 460, this);
  160. temp_rect = genRect(45, 35, 221 + pos.x, 405 + pos.y);
  161. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fbattleSpellsb, this), 453, this);
  162. temp_rect = genRect(45, 35, 355 + pos.x, 405 + pos.y);
  163. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fadvSpellsb, this), 452, this);
  164. temp_rect = genRect(45, 35, 418 + pos.x, 405 + pos.y);
  165. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fmanaPtsb, this), 459, this);
  166. temp_rect = genRect(36, 56, 549 + pos.x, 94 + pos.y);
  167. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 0), 454, this);
  168. temp_rect = genRect(36, 56, 549 + pos.x, 151 + pos.y);
  169. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 3), 457, this);
  170. temp_rect = genRect(36, 56, 549 + pos.x, 210 + pos.y);
  171. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 1), 455, this);
  172. temp_rect = genRect(36, 56, 549 + pos.x, 270 + pos.y);
  173. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 2), 456, this);
  174. temp_rect = genRect(36, 56, 549 + pos.x, 330 + pos.y);
  175. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::selectSchool, this, 4), 458, this);
  176. temp_rect = genRect(leftCorner->bg->h, leftCorner->bg->w, 97 + pos.x, 77 + pos.y);
  177. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fLcornerb, this), 450, this);
  178. temp_rect = genRect(rightCorner->bg->h, rightCorner->bg->w, 487 + pos.x, 72 + pos.y);
  179. new InteractiveArea(temp_rect, std::bind(&CSpellWindow::fRcornerb, this), 451, this);
  180. //areas for spells
  181. int xpos = 117 + pos.x, ypos = 90 + pos.y;
  182. for(int v=0; v<12; ++v)
  183. {
  184. temp_rect = genRect(65, 78, xpos, ypos);
  185. spellAreas[v] = new SpellArea(temp_rect, this);
  186. if(v == 5) //to right page
  187. {
  188. xpos = 336 + pos.x; ypos = 90 + pos.y;
  189. }
  190. else
  191. {
  192. if(v%2 == 0)
  193. {
  194. xpos+=85;
  195. }
  196. else
  197. {
  198. xpos -= 85; ypos+=97;
  199. }
  200. }
  201. }
  202. selectedTab = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap;
  203. spellTab->setFrame(selectedTab, 0);
  204. int cp = battleSpellsOnly ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap;
  205. // spellbook last page battle index is not reset after battle, so this needs to stay here
  206. vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1));
  207. setCurrentPage(cp);
  208. computeSpellsPerArea();
  209. addUsedEvents(KEYBOARD);
  210. }
  211. CSpellWindow::~CSpellWindow()
  212. {
  213. for(auto item : schoolBorders)
  214. item->unload();
  215. spells->unload();
  216. }
  217. void CSpellWindow::fexitb()
  218. {
  219. (myInt->battleInt ? myInt->spellbookSettings.spellbookLastTabBattle : myInt->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  220. (myInt->battleInt ? myInt->spellbookSettings.spellbookLastPageBattle : myInt->spellbookSettings.spellbokLastPageAdvmap) = currentPage;
  221. GH.popIntTotally(this);
  222. }
  223. void CSpellWindow::fadvSpellsb()
  224. {
  225. if (battleSpellsOnly == true)
  226. {
  227. turnPageRight();
  228. battleSpellsOnly = false;
  229. setCurrentPage(0);
  230. }
  231. computeSpellsPerArea();
  232. }
  233. void CSpellWindow::fbattleSpellsb()
  234. {
  235. if (battleSpellsOnly == false)
  236. {
  237. turnPageLeft();
  238. battleSpellsOnly = true;
  239. setCurrentPage(0);
  240. }
  241. computeSpellsPerArea();
  242. }
  243. void CSpellWindow::fmanaPtsb()
  244. {
  245. }
  246. void CSpellWindow::selectSchool(int school)
  247. {
  248. if (selectedTab != school)
  249. {
  250. if (selectedTab < school)
  251. turnPageLeft();
  252. else
  253. turnPageRight();
  254. selectedTab = school;
  255. spellTab->setFrame(selectedTab, 0);
  256. setCurrentPage(0);
  257. }
  258. computeSpellsPerArea();
  259. }
  260. void CSpellWindow::fLcornerb()
  261. {
  262. if(currentPage>0)
  263. {
  264. turnPageLeft();
  265. setCurrentPage(currentPage - 1);
  266. }
  267. computeSpellsPerArea();
  268. GH.breakEventHandling();
  269. }
  270. void CSpellWindow::fRcornerb()
  271. {
  272. if((currentPage + 1) < (pagesWithinCurrentTab()))
  273. {
  274. turnPageRight();
  275. setCurrentPage(currentPage + 1);
  276. }
  277. computeSpellsPerArea();
  278. GH.breakEventHandling();
  279. }
  280. void CSpellWindow::show(SDL_Surface * to)
  281. {
  282. statusBar->show(to);
  283. }
  284. void CSpellWindow::computeSpellsPerArea()
  285. {
  286. std::vector<const CSpell *> spellsCurSite;
  287. spellsCurSite.reserve(mySpells.size());
  288. for(const CSpell * spell : mySpells)
  289. {
  290. if(spell->isCombatSpell() ^ !battleSpellsOnly
  291. && ((selectedTab == 4) || spell->school.at((ESpellSchool)selectedTab))
  292. )
  293. {
  294. spellsCurSite.push_back(spell);
  295. }
  296. }
  297. if(selectedTab == 4)
  298. {
  299. if(spellsCurSite.size() > 12)
  300. {
  301. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + currentPage*12, spellsCurSite.end());
  302. if(spellsCurSite.size() > 12)
  303. {
  304. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  305. }
  306. }
  307. }
  308. else //selectedTab == 0, 1, 2 or 3
  309. {
  310. if(spellsCurSite.size() > 10)
  311. {
  312. if(currentPage == 0)
  313. {
  314. spellsCurSite.erase(spellsCurSite.begin()+10, spellsCurSite.end());
  315. }
  316. else
  317. {
  318. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + (currentPage-1)*12 + 10, spellsCurSite.end());
  319. if(spellsCurSite.size() > 12)
  320. {
  321. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  322. }
  323. }
  324. }
  325. }
  326. //applying
  327. if(selectedTab == 4 || currentPage != 0)
  328. {
  329. for(size_t c=0; c<12; ++c)
  330. {
  331. if(c < spellsCurSite.size())
  332. {
  333. spellAreas[c]->setSpell(spellsCurSite[c]);
  334. }
  335. else
  336. {
  337. spellAreas[c]->setSpell(nullptr);
  338. }
  339. }
  340. }
  341. else
  342. {
  343. spellAreas[0]->setSpell(nullptr);
  344. spellAreas[1]->setSpell(nullptr);
  345. for(size_t c=0; c<10; ++c)
  346. {
  347. if(c < spellsCurSite.size())
  348. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  349. else
  350. spellAreas[c+2]->setSpell(nullptr);
  351. }
  352. }
  353. redraw();
  354. }
  355. void CSpellWindow::setCurrentPage(int value)
  356. {
  357. currentPage = value;
  358. schools->visible = selectedTab!=4 && currentPage == 0;
  359. if(selectedTab != 4)
  360. schools->setFrame(selectedTab, 0);
  361. leftCorner->visible = currentPage != 0;
  362. rightCorner->visible = (currentPage+1) < pagesWithinCurrentTab();
  363. mana->setText(boost::lexical_cast<std::string>(myHero->mana));//just in case, it will be possible to cast spell without closing book
  364. }
  365. void CSpellWindow::turnPageLeft()
  366. {
  367. if (settings["video"]["spellbookAnimation"].Bool())
  368. CCS->videoh->openAndPlayVideo("PGTRNLFT.SMK", pos.x+13, pos.y+15, screen);
  369. }
  370. void CSpellWindow::turnPageRight()
  371. {
  372. if (settings["video"]["spellbookAnimation"].Bool())
  373. CCS->videoh->openAndPlayVideo("PGTRNRGH.SMK", pos.x+13, pos.y+15, screen);
  374. }
  375. void CSpellWindow::keyPressed(const SDL_KeyboardEvent & key)
  376. {
  377. if(key.keysym.sym == SDLK_ESCAPE || key.keysym.sym == SDLK_RETURN)
  378. {
  379. fexitb();
  380. return;
  381. }
  382. if(key.state == SDL_PRESSED)
  383. {
  384. switch(key.keysym.sym)
  385. {
  386. case SDLK_LEFT:
  387. fLcornerb();
  388. break;
  389. case SDLK_RIGHT:
  390. fRcornerb();
  391. break;
  392. case SDLK_UP:
  393. case SDLK_DOWN:
  394. {
  395. bool down = key.keysym.sym == SDLK_DOWN;
  396. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  397. int index = -1;
  398. while(schoolsOrder[++index] != selectedTab);
  399. index += (down ? 1 : -1);
  400. vstd::abetween(index, 0, ARRAY_COUNT(schoolsOrder) - 1);
  401. if(selectedTab != schoolsOrder[index])
  402. selectSchool(schoolsOrder[index]);
  403. break;
  404. }
  405. case SDLK_c:
  406. fbattleSpellsb();
  407. break;
  408. case SDLK_a:
  409. fadvSpellsb();
  410. break;
  411. default://to get rid of warnings
  412. break;
  413. }
  414. //alt + 1234567890-= casts spell from 1 - 12 slot
  415. if(myInt->altPressed())
  416. {
  417. SDL_Keycode hlpKey = key.keysym.sym;
  418. if(CGuiHandler::isNumKey(hlpKey, false))
  419. {
  420. if(hlpKey == SDLK_KP_PLUS)
  421. hlpKey = SDLK_EQUALS;
  422. else
  423. hlpKey = CGuiHandler::numToDigit(hlpKey);
  424. }
  425. 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};
  426. int index = -1;
  427. while(++index < ARRAY_COUNT(spellSelectors) && spellSelectors[index] != hlpKey);
  428. if(index >= ARRAY_COUNT(spellSelectors))
  429. return;
  430. //try casting spell
  431. spellAreas[index]->clickLeft(false, true);
  432. }
  433. }
  434. }
  435. int CSpellWindow::pagesWithinCurrentTab()
  436. {
  437. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  438. }
  439. CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
  440. {
  441. this->pos = pos;
  442. this->owner = owner;
  443. addUsedEvents(LCLICK | RCLICK | HOVER);
  444. spellCost = whichSchool = schoolLevel = -1;
  445. mySpell = nullptr;
  446. schoolBorder = nullptr;
  447. OBJ_CONSTRUCTION_CAPTURING_ALL;
  448. image = new CAnimImage(owner->spells, 0, 0);
  449. image->visible = false;
  450. name = new CLabel(39, 70, FONT_TINY, CENTER);
  451. level = new CLabel(39, 82, FONT_TINY, CENTER);
  452. cost = new CLabel(39, 94, FONT_TINY, CENTER);
  453. for(auto l : {name, level, cost})
  454. l->autoRedraw = false;
  455. }
  456. CSpellWindow::SpellArea::~SpellArea()
  457. {
  458. }
  459. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  460. {
  461. if(mySpell && !down)
  462. {
  463. int spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  464. if(spellCost > owner->myHero->mana) //insufficient mana
  465. {
  466. owner->myInt->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[206]) % spellCost % owner->myHero->mana));
  467. return;
  468. }
  469. //anything that is not combat spell is adventure spell
  470. //this not an error in general to cast even creature ability with hero
  471. const bool combatSpell = mySpell->isCombatSpell();
  472. if(mySpell->isCombatSpell() != !mySpell->isAdventureSpell())
  473. {
  474. logGlobal->error("Spell have invalid flags");
  475. return;
  476. }
  477. const bool inCombat = owner->myInt->battleInt != nullptr;
  478. const bool inCastle = owner->myInt->castleInt != nullptr;
  479. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  480. if((combatSpell ^ inCombat) || inCastle)
  481. {
  482. std::vector<CComponent*> hlp(1, new CComponent(CComponent::spell, mySpell->id, 0));
  483. owner->myInt->showInfoDialog(mySpell->getLevelInfo(schoolLevel).description, hlp);
  484. }
  485. else if(combatSpell)
  486. {
  487. spells::detail::ProblemImpl problem;
  488. if(mySpell->canBeCast(problem, owner->myInt->cb.get(), spells::Mode::HERO, owner->myHero))
  489. {
  490. owner->myInt->battleInt->castThisSpell(mySpell->id);
  491. owner->fexitb();
  492. }
  493. else
  494. {
  495. std::vector<std::string> texts;
  496. problem.getAll(texts);
  497. if(!texts.empty())
  498. owner->myInt->showInfoDialog(texts.front());
  499. else
  500. owner->myInt->showInfoDialog("Unknown problem with this spell, no more information available.");
  501. }
  502. }
  503. else //adventure spell
  504. {
  505. const CGHeroInstance * h = owner->myHero;
  506. GH.popInt(owner);
  507. auto guard = vstd::makeScopeGuard([this]()
  508. {
  509. owner->myInt->spellbookSettings.spellbookLastTabAdvmap = owner->selectedTab;
  510. owner->myInt->spellbookSettings.spellbokLastPageAdvmap = owner->currentPage;
  511. });
  512. if(mySpell->getTargetType() == spells::AimType::LOCATION)
  513. adventureInt->enterCastingMode(mySpell);
  514. else if(mySpell->getTargetType() == spells::AimType::NO_TARGET)
  515. owner->myInt->cb->castSpell(h, mySpell->id);
  516. else
  517. logGlobal->error("Invalid spell target type");
  518. }
  519. }
  520. }
  521. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  522. {
  523. if(mySpell && down)
  524. {
  525. std::string dmgInfo;
  526. auto causedDmg = owner->myInt->cb->estimateSpellDamage(mySpell, owner->myHero);
  527. if(causedDmg == 0 || mySpell->id == SpellID::TITANS_LIGHTNING_BOLT) //Titan's Lightning Bolt already has damage info included
  528. dmgInfo = "";
  529. else
  530. {
  531. dmgInfo = CGI->generaltexth->allTexts[343];
  532. boost::algorithm::replace_first(dmgInfo, "%d", boost::lexical_cast<std::string>(causedDmg));
  533. }
  534. CRClickPopup::createAndPush(mySpell->getLevelInfo(schoolLevel).description + dmgInfo,
  535. new CComponent(CComponent::spell, mySpell->id));
  536. }
  537. }
  538. void CSpellWindow::SpellArea::hover(bool on)
  539. {
  540. if(mySpell)
  541. {
  542. if(on)
  543. owner->statusBar->setText(boost::to_string(boost::format("%s (%s)") % mySpell->name % CGI->generaltexth->allTexts[171+mySpell->level]));
  544. else
  545. owner->statusBar->clear();
  546. }
  547. }
  548. void CSpellWindow::SpellArea::showAll(SDL_Surface * to)
  549. {
  550. if(mySpell)
  551. {
  552. //printing border (indicates level of magic school)
  553. if(schoolBorder)
  554. schoolBorder->draw(to, pos.x, pos.y);
  555. CIntObject::showAll(to);
  556. }
  557. }
  558. void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
  559. {
  560. schoolBorder = nullptr;
  561. image->visible = false;
  562. name->setText("");
  563. level->setText("");
  564. cost->setText("");
  565. mySpell = spell;
  566. if(mySpell)
  567. {
  568. schoolLevel = owner->myHero->getSpellSchoolLevel(mySpell, &whichSchool);
  569. spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  570. image->setFrame(mySpell->id);
  571. image->visible = true;
  572. schoolBorder = owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab]->getImage(schoolLevel,0);
  573. SDL_Color firstLineColor, secondLineColor;
  574. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  575. {
  576. static const SDL_Color unavailableSpell = {239, 189, 33, 0};
  577. firstLineColor = Colors::WHITE;
  578. secondLineColor = unavailableSpell;
  579. }
  580. else
  581. {
  582. firstLineColor = Colors::YELLOW;
  583. secondLineColor = Colors::WHITE;
  584. }
  585. name->color = firstLineColor;
  586. name->setText(mySpell->name);
  587. level->color = secondLineColor;
  588. if(schoolLevel > 0)
  589. {
  590. boost::format fmt("%s/%s");
  591. fmt % CGI->generaltexth->allTexts[171 + mySpell->level];
  592. fmt % CGI->generaltexth->levels.at(3+(schoolLevel-1));//lines 4-6
  593. level->setText(fmt.str());
  594. }
  595. else
  596. level->setText(CGI->generaltexth->allTexts[171 + mySpell->level]);
  597. cost->color = secondLineColor;
  598. boost::format costfmt("%s: %d");
  599. costfmt % CGI->generaltexth->allTexts[387] % spellCost;
  600. cost->setText(costfmt.str());
  601. }
  602. }