CSpellWindow.cpp 19 KB

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