CSpellWindow.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. /*
  2. * CSpellWindow.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CSpellWindow.h"
  12. #include "../../lib/ScopeGuard.h"
  13. #include "GUIClasses.h"
  14. #include "InfoWindows.h"
  15. #include "CCastleInterface.h"
  16. #include "../CGameInfo.h"
  17. #include "../CPlayerInterface.h"
  18. #include "../PlayerLocalState.h"
  19. #include "../CVideoHandler.h"
  20. #include "../battle/BattleInterface.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../gui/Shortcut.h"
  23. #include "../gui/WindowHandler.h"
  24. #include "../widgets/MiscWidgets.h"
  25. #include "../widgets/CComponent.h"
  26. #include "../widgets/TextControls.h"
  27. #include "../adventureMap/AdventureMapInterface.h"
  28. #include "../render/CAnimation.h"
  29. #include "../render/IRenderHandler.h"
  30. #include "../render/IImage.h"
  31. #include "../render/IImageLoader.h"
  32. #include "../render/Canvas.h"
  33. #include "../../CCallback.h"
  34. #include "../../lib/CConfigHandler.h"
  35. #include "../../lib/CGeneralTextHandler.h"
  36. #include "../../lib/spells/CSpellHandler.h"
  37. #include "../../lib/spells/Problem.h"
  38. #include "../../lib/GameConstants.h"
  39. #include "../../lib/mapObjects/CGHeroInstance.h"
  40. CSpellWindow::InteractiveArea::InteractiveArea(const Rect & myRect, std::function<void()> funcL, int helpTextId, CSpellWindow * _owner)
  41. {
  42. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  43. pos = myRect;
  44. onLeft = funcL;
  45. hoverText = CGI->generaltexth->zelp[helpTextId].first;
  46. helpText = CGI->generaltexth->zelp[helpTextId].second;
  47. owner = _owner;
  48. }
  49. void CSpellWindow::InteractiveArea::clickPressed(const Point & cursorPosition)
  50. {
  51. onLeft();
  52. }
  53. void CSpellWindow::InteractiveArea::showPopupWindow(const Point & cursorPosition)
  54. {
  55. CRClickPopup::createAndPush(helpText);
  56. }
  57. void CSpellWindow::InteractiveArea::hover(bool on)
  58. {
  59. if(on)
  60. owner->statusBar->write(hoverText);
  61. else
  62. owner->statusBar->clear();
  63. }
  64. class SpellbookSpellSorter
  65. {
  66. public:
  67. bool operator()(const CSpell * A, const CSpell * B)
  68. {
  69. if(A->getLevel() < B->getLevel())
  70. return true;
  71. if(A->getLevel() > B->getLevel())
  72. return false;
  73. for(auto schoolId = 0; schoolId < GameConstants::DEFAULT_SCHOOLS; schoolId++)
  74. {
  75. if(A->school.at(SpellSchool(schoolId)) && !B->school.at(SpellSchool(schoolId)))
  76. return true;
  77. if(!A->school.at(SpellSchool(schoolId)) && B->school.at(SpellSchool(schoolId)))
  78. return false;
  79. }
  80. return A->getNameTranslated() < B->getNameTranslated();
  81. }
  82. } spellsorter;
  83. CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  84. CWindowObject(PLAYER_COLORED),
  85. battleSpellsOnly(openOnBattleSpells),
  86. selectedTab(4),
  87. currentPage(0),
  88. myHero(_myHero),
  89. myInt(_myInt),
  90. isBigSpellbook(true)
  91. {
  92. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  93. if(isBigSpellbook)
  94. background = std::make_shared<CPicture>(createBigSpellBook(), Point(0, 0));
  95. else
  96. {
  97. background = std::make_shared<CPicture>(ImagePath::builtin("SpelBack"), 0, 0);
  98. offL = offR = offT = offB = offRM = 0;
  99. spellsPerPage = 12;
  100. }
  101. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  102. //initializing castable spells
  103. mySpells.reserve(CGI->spellh->objects.size());
  104. for(const CSpell * spell : CGI->spellh->objects)
  105. {
  106. if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell))
  107. mySpells.push_back(spell);
  108. }
  109. std::sort(mySpells.begin(), mySpells.end(), spellsorter);
  110. //initializing sizes of spellbook's parts
  111. for(auto & elem : sitesPerTabAdv)
  112. elem = 0;
  113. for(auto & elem : sitesPerTabBattle)
  114. elem = 0;
  115. for(const auto spell : mySpells)
  116. {
  117. int * sitesPerOurTab = spell->isCombat() ? sitesPerTabBattle : sitesPerTabAdv;
  118. ++sitesPerOurTab[4];
  119. spell->forEachSchool([&sitesPerOurTab](const SpellSchool & school, bool & stop)
  120. {
  121. ++sitesPerOurTab[school];
  122. });
  123. }
  124. if(sitesPerTabAdv[4] % spellsPerPage == 0)
  125. sitesPerTabAdv[4]/=spellsPerPage;
  126. else
  127. sitesPerTabAdv[4] = sitesPerTabAdv[4]/spellsPerPage + 1;
  128. for(int v=0; v<4; ++v)
  129. {
  130. if(sitesPerTabAdv[v] <= spellsPerPage - 2)
  131. sitesPerTabAdv[v] = 1;
  132. else
  133. {
  134. if((sitesPerTabAdv[v] - spellsPerPage - 2) % spellsPerPage == 0)
  135. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - spellsPerPage - 2) / spellsPerPage + 1;
  136. else
  137. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - spellsPerPage - 2) / spellsPerPage + 2;
  138. }
  139. }
  140. if(sitesPerTabBattle[4] % spellsPerPage == 0)
  141. sitesPerTabBattle[4]/=spellsPerPage;
  142. else
  143. sitesPerTabBattle[4] = sitesPerTabBattle[4]/spellsPerPage + 1;
  144. for(int v=0; v<4; ++v)
  145. {
  146. if(sitesPerTabBattle[v] <= spellsPerPage - 2)
  147. sitesPerTabBattle[v] = 1;
  148. else
  149. {
  150. if((sitesPerTabBattle[v] - spellsPerPage - 2) % spellsPerPage == 0)
  151. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - spellsPerPage - 2) / spellsPerPage + 1;
  152. else
  153. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - spellsPerPage - 2) / spellsPerPage + 2;
  154. }
  155. }
  156. //numbers of spell pages computed
  157. leftCorner = std::make_shared<CPicture>(ImagePath::builtin("SpelTrnL.bmp"), 97 + offL, 77 + offT);
  158. rightCorner = std::make_shared<CPicture>(ImagePath::builtin("SpelTrnR.bmp"), 487 + offR, 72 + offT);
  159. spellIcons = GH.renderHandler().loadAnimation(AnimationPath::builtin("Spells"));
  160. schoolTab = std::make_shared<CAnimImage>(AnimationPath::builtin("SpelTab"), selectedTab, 0, 524 + offR, 88);
  161. schoolPicture = std::make_shared<CAnimImage>(AnimationPath::builtin("Schools"), 0, 0, 117 + offL, 74 + offT);
  162. schoolBorders[0] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevA.def"));
  163. schoolBorders[1] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevF.def"));
  164. schoolBorders[2] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevW.def"));
  165. schoolBorders[3] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevE.def"));
  166. for(auto item : schoolBorders)
  167. item->preload();
  168. mana = std::make_shared<CLabel>(435 + offL, 426 + offR, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, std::to_string(myHero->mana));
  169. statusBar = CGStatusBar::create(7, 569, ImagePath::builtin("Spelroll.bmp"));
  170. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 479 + pos.x, 405 + pos.y + offB, 36, 56), std::bind(&CSpellWindow::fexitb, this), 460, this));
  171. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 221 + pos.x, 405 + pos.y + offB, 36, 56), std::bind(&CSpellWindow::fbattleSpellsb, this), 453, this));
  172. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 355 + pos.x, 405 + pos.y + offB, 36, 56), std::bind(&CSpellWindow::fadvSpellsb, this), 452, this));
  173. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 418 + pos.x, 405 + pos.y + offB, 36, 56), std::bind(&CSpellWindow::fmanaPtsb, this), 459, this));
  174. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x + offR, 94 + pos.y, 36, 56), std::bind(&CSpellWindow::selectSchool, this, 0), 454, this));
  175. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x + offR, 151 + pos.y, 45, 35), std::bind(&CSpellWindow::selectSchool, this, 3), 457, this));
  176. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x + offR, 210 + pos.y, 45, 35), std::bind(&CSpellWindow::selectSchool, this, 1), 455, this));
  177. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x + offR, 270 + pos.y, 45, 35), std::bind(&CSpellWindow::selectSchool, this, 2), 456, this));
  178. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 549 + pos.x + offR, 330 + pos.y, 45, 35), std::bind(&CSpellWindow::selectSchool, this, 4), 458, this));
  179. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 97 + offL + pos.x, 77 + offT + pos.y, leftCorner->pos.h, leftCorner->pos.w ), std::bind(&CSpellWindow::fLcornerb, this), 450, this));
  180. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 487 + offR + pos.x, 72 + offT + pos.y, rightCorner->pos.h, rightCorner->pos.w ), std::bind(&CSpellWindow::fRcornerb, this), 451, this));
  181. //areas for spells
  182. int xpos = 117 + offL + pos.x, ypos = 90 + offT + pos.y;
  183. for(int v=0; v<spellsPerPage; ++v)
  184. {
  185. spellAreas[v] = std::make_shared<SpellArea>( Rect(xpos, ypos, 65, 78), this);
  186. if(v == (spellsPerPage / 2) - 1) //to right page
  187. {
  188. xpos = offRM + 336 + pos.x; ypos = 90 + offT + pos.y;
  189. }
  190. else
  191. {
  192. if(v%3 == 0 || v%3 == 1)
  193. {
  194. xpos+=85;
  195. }
  196. else
  197. {
  198. xpos -= 2*85; ypos+=97;
  199. }
  200. }
  201. }
  202. selectedTab = battleSpellsOnly ? myInt->localState->spellbookSettings.spellbookLastTabBattle : myInt->localState->spellbookSettings.spellbookLastTabAdvmap;
  203. schoolTab->setFrame(selectedTab, 0);
  204. int cp = battleSpellsOnly ? myInt->localState->spellbookSettings.spellbookLastPageBattle : myInt->localState->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. }
  214. std::shared_ptr<IImage> CSpellWindow::createBigSpellBook()
  215. {
  216. std::shared_ptr<IImage> img = GH.renderHandler().loadImage(ImagePath::builtin("SpelBack"));
  217. Canvas canvas = Canvas(Point(800, 600));
  218. // edges
  219. canvas.draw(img, Point(0, 0), Rect(10, 38, 90, 45));
  220. canvas.draw(img, Point(0, 460), Rect(10, 400, 90, 141));
  221. canvas.draw(img, Point(705, 0), Rect(509, 38, 95, 45));
  222. canvas.draw(img, Point(705, 460), Rect(509, 400, 95, 141));
  223. // left / right
  224. Canvas tmp1 = Canvas(Point(90, 355 - 45));
  225. tmp1.draw(img, Point(0, 0), Rect(10, 38 + 45, 90, 355 - 45));
  226. canvas.drawScaled(tmp1, Point(0, 45), Point(90, 415));
  227. Canvas tmp2 = Canvas(Point(95, 355 - 45));
  228. tmp2.draw(img, Point(0, 0), Rect(509, 38 + 45, 95, 355 - 45));
  229. canvas.drawScaled(tmp2, Point(705, 45), Point(95, 415));
  230. // top / bottom
  231. Canvas tmp3 = Canvas(Point(409, 45));
  232. tmp3.draw(img, Point(0, 0), Rect(100, 38, 409, 45));
  233. canvas.drawScaled(tmp3, Point(90, 0), Point(615, 45));
  234. Canvas tmp4 = Canvas(Point(409, 141));
  235. tmp4.draw(img, Point(0, 0), Rect(100, 400, 409, 141));
  236. canvas.drawScaled(tmp4, Point(90, 460), Point(615, 141));
  237. // middle
  238. Canvas tmp5 = Canvas(Point(409, 141));
  239. tmp5.draw(img, Point(0, 0), Rect(100, 38 + 45, 509 - 10, 400 - 38));
  240. canvas.drawScaled(tmp5, Point(90, 45), Point(615, 415));
  241. return GH.renderHandler().createImage(canvas.getInternalSurface());
  242. }
  243. void CSpellWindow::fexitb()
  244. {
  245. (myInt->battleInt ? myInt->localState->spellbookSettings.spellbookLastTabBattle : myInt->localState->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  246. (myInt->battleInt ? myInt->localState->spellbookSettings.spellbookLastPageBattle : myInt->localState->spellbookSettings.spellbokLastPageAdvmap) = currentPage;
  247. close();
  248. }
  249. void CSpellWindow::fadvSpellsb()
  250. {
  251. if(battleSpellsOnly == true)
  252. {
  253. turnPageRight();
  254. battleSpellsOnly = false;
  255. setCurrentPage(0);
  256. }
  257. computeSpellsPerArea();
  258. }
  259. void CSpellWindow::fbattleSpellsb()
  260. {
  261. if(battleSpellsOnly == false)
  262. {
  263. turnPageLeft();
  264. battleSpellsOnly = true;
  265. setCurrentPage(0);
  266. }
  267. computeSpellsPerArea();
  268. }
  269. void CSpellWindow::fmanaPtsb()
  270. {
  271. }
  272. void CSpellWindow::selectSchool(int school)
  273. {
  274. if(selectedTab != school)
  275. {
  276. if(selectedTab < school)
  277. turnPageLeft();
  278. else
  279. turnPageRight();
  280. selectedTab = school;
  281. schoolTab->setFrame(selectedTab, 0);
  282. setCurrentPage(0);
  283. }
  284. computeSpellsPerArea();
  285. }
  286. void CSpellWindow::fLcornerb()
  287. {
  288. if(currentPage>0)
  289. {
  290. turnPageLeft();
  291. setCurrentPage(currentPage - 1);
  292. }
  293. computeSpellsPerArea();
  294. }
  295. void CSpellWindow::fRcornerb()
  296. {
  297. if((currentPage + 1) < (pagesWithinCurrentTab()))
  298. {
  299. turnPageRight();
  300. setCurrentPage(currentPage + 1);
  301. }
  302. computeSpellsPerArea();
  303. }
  304. void CSpellWindow::show(Canvas & to)
  305. {
  306. statusBar->show(to);
  307. }
  308. void CSpellWindow::computeSpellsPerArea()
  309. {
  310. std::vector<const CSpell *> spellsCurSite;
  311. spellsCurSite.reserve(mySpells.size());
  312. for(const CSpell * spell : mySpells)
  313. {
  314. if(spell->isCombat() ^ !battleSpellsOnly
  315. && ((selectedTab == 4) || spell->school.at(SpellSchool(selectedTab)))
  316. )
  317. {
  318. spellsCurSite.push_back(spell);
  319. }
  320. }
  321. if(selectedTab == 4)
  322. {
  323. if(spellsCurSite.size() > spellsPerPage)
  324. {
  325. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + currentPage*spellsPerPage, spellsCurSite.end());
  326. if(spellsCurSite.size() > spellsPerPage)
  327. {
  328. spellsCurSite.erase(spellsCurSite.begin()+spellsPerPage, spellsCurSite.end());
  329. }
  330. }
  331. }
  332. else //selectedTab == 0, 1, 2 or 3
  333. {
  334. if(spellsCurSite.size() > spellsPerPage - 2)
  335. {
  336. if(currentPage == 0)
  337. {
  338. spellsCurSite.erase(spellsCurSite.begin()+spellsPerPage-2, spellsCurSite.end());
  339. }
  340. else
  341. {
  342. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + (currentPage-1)*spellsPerPage + spellsPerPage-2, spellsCurSite.end());
  343. if(spellsCurSite.size() > spellsPerPage)
  344. {
  345. spellsCurSite.erase(spellsCurSite.begin()+spellsPerPage, spellsCurSite.end());
  346. }
  347. }
  348. }
  349. }
  350. //applying
  351. if(selectedTab == 4 || currentPage != 0)
  352. {
  353. for(size_t c=0; c<spellsPerPage; ++c)
  354. {
  355. if(c < spellsCurSite.size())
  356. {
  357. spellAreas[c]->setSpell(spellsCurSite[c]);
  358. }
  359. else
  360. {
  361. spellAreas[c]->setSpell(nullptr);
  362. }
  363. }
  364. }
  365. else
  366. {
  367. spellAreas[0]->setSpell(nullptr);
  368. spellAreas[1]->setSpell(nullptr);
  369. for(size_t c=0; c<spellsPerPage-2; ++c)
  370. {
  371. if(c < spellsCurSite.size())
  372. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  373. else
  374. spellAreas[c+2]->setSpell(nullptr);
  375. }
  376. }
  377. redraw();
  378. }
  379. void CSpellWindow::setCurrentPage(int value)
  380. {
  381. currentPage = value;
  382. schoolPicture->visible = selectedTab!=4 && currentPage == 0;
  383. if(selectedTab != 4)
  384. schoolPicture->setFrame(selectedTab, 0);
  385. leftCorner->visible = currentPage != 0;
  386. rightCorner->visible = (currentPage+1) < pagesWithinCurrentTab();
  387. mana->setText(std::to_string(myHero->mana));//just in case, it will be possible to cast spell without closing book
  388. }
  389. void CSpellWindow::turnPageLeft()
  390. {
  391. if(settings["video"]["spellbookAnimation"].Bool() && !isBigSpellbook)
  392. CCS->videoh->openAndPlayVideo(VideoPath::builtin("PGTRNLFT.SMK"), pos.x+13, pos.y+15);
  393. }
  394. void CSpellWindow::turnPageRight()
  395. {
  396. if(settings["video"]["spellbookAnimation"].Bool() && !isBigSpellbook)
  397. CCS->videoh->openAndPlayVideo(VideoPath::builtin("PGTRNRGH.SMK"), pos.x+13, pos.y+15);
  398. }
  399. void CSpellWindow::keyPressed(EShortcut key)
  400. {
  401. switch(key)
  402. {
  403. case EShortcut::GLOBAL_RETURN:
  404. fexitb();
  405. break;
  406. case EShortcut::MOVE_LEFT:
  407. fLcornerb();
  408. break;
  409. case EShortcut::MOVE_RIGHT:
  410. fRcornerb();
  411. break;
  412. case EShortcut::MOVE_UP:
  413. case EShortcut::MOVE_DOWN:
  414. {
  415. bool down = key == EShortcut::MOVE_DOWN;
  416. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  417. int index = -1;
  418. while(schoolsOrder[++index] != selectedTab);
  419. index += (down ? 1 : -1);
  420. vstd::abetween<int>(index, 0, std::size(schoolsOrder) - 1);
  421. if(selectedTab != schoolsOrder[index])
  422. selectSchool(schoolsOrder[index]);
  423. break;
  424. }
  425. case EShortcut::SPELLBOOK_TAB_COMBAT:
  426. fbattleSpellsb();
  427. break;
  428. case EShortcut::SPELLBOOK_TAB_ADVENTURE:
  429. fadvSpellsb();
  430. break;
  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 | SHOW_POPUP | 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::clickPressed(const Point & cursorPosition)
  455. {
  456. if(mySpell)
  457. {
  458. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  459. if(spellCost > owner->myHero->mana) //insufficient mana
  460. {
  461. LOCPLINT->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. LOCPLINT->showInfoDialog(mySpell->getDescriptionTranslated(schoolLevel), hlp);
  479. }
  480. else if(combatSpell)
  481. {
  482. spells::detail::ProblemImpl problem;
  483. if(mySpell->canBeCast(problem, owner->myInt->battleInt->getBattle().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. LOCPLINT->showInfoDialog(texts.front());
  494. else
  495. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.spellUnknownProblem"));
  496. }
  497. }
  498. else //adventure spell
  499. {
  500. const CGHeroInstance * h = owner->myHero;
  501. GH.windows().popWindows(1);
  502. auto guard = vstd::makeScopeGuard([this]()
  503. {
  504. owner->myInt->localState->spellbookSettings.spellbookLastTabAdvmap = owner->selectedTab;
  505. owner->myInt->localState->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::showPopupWindow(const Point & cursorPosition)
  517. {
  518. if(mySpell)
  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", std::to_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::str(boost::format("%s (%s)") % mySpell->getNameTranslated() % CGI->generaltexth->allTexts[171+mySpell->getLevel()]));
  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. ColorRGBA 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->getLevel()];
  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->getLevel()]);
  584. cost->color = secondLineColor;
  585. boost::format costfmt("%s: %d");
  586. costfmt % CGI->generaltexth->allTexts[387] % spellCost;
  587. cost->setText(costfmt.str());
  588. }
  589. }