CSpellWindow.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 | (settings["gameTweaks"]["enableLargeSpellbook"].Bool() ? BORDERED : 0)),
  85. battleSpellsOnly(openOnBattleSpells),
  86. selectedTab(4),
  87. currentPage(0),
  88. myHero(_myHero),
  89. myInt(_myInt),
  90. isBigSpellbook(settings["gameTweaks"]["enableLargeSpellbook"].Bool()),
  91. spellsPerPage(24),
  92. offL(-11),
  93. offR(195),
  94. offRM(110),
  95. offT(-37),
  96. offB(56)
  97. {
  98. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  99. if(isBigSpellbook)
  100. {
  101. background = std::make_shared<CPicture>(createBigSpellBook(), Point(0, 0));
  102. updateShadow();
  103. }
  104. else
  105. {
  106. background = std::make_shared<CPicture>(ImagePath::builtin("SpelBack"), 0, 0);
  107. offL = offR = offT = offB = offRM = 0;
  108. spellsPerPage = 12;
  109. }
  110. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  111. if(settings["general"]["enableUiEnhancements"].Bool())
  112. {
  113. Rect r(90, isBigSpellbook ? 480 : 420, isBigSpellbook ? 160 : 110, 16);
  114. const ColorRGBA rectangleColor = ColorRGBA(0, 0, 0, 75);
  115. const ColorRGBA borderColor = ColorRGBA(128, 100, 75);
  116. const ColorRGBA grayedColor = ColorRGBA(158, 130, 105);
  117. searchBoxRectangle = std::make_shared<TransparentFilledRectangle>(r.resize(1), rectangleColor, borderColor);
  118. searchBoxDescription = std::make_shared<CLabel>(r.center().x, r.center().y, FONT_SMALL, ETextAlignment::CENTER, grayedColor, CGI->generaltexth->translate("vcmi.spellBook.search"));
  119. searchBox = std::make_shared<CTextInput>(r, FONT_SMALL, std::bind(&CSpellWindow::searchInput, this));
  120. }
  121. processSpells();
  122. //numbers of spell pages computed
  123. leftCorner = std::make_shared<CPicture>(ImagePath::builtin("SpelTrnL.bmp"), 97 + offL, 77 + offT);
  124. rightCorner = std::make_shared<CPicture>(ImagePath::builtin("SpelTrnR.bmp"), 487 + offR, 72 + offT);
  125. spellIcons = GH.renderHandler().loadAnimation(AnimationPath::builtin("Spells"));
  126. schoolTab = std::make_shared<CAnimImage>(AnimationPath::builtin("SpelTab"), selectedTab, 0, 524 + offR, 88);
  127. schoolPicture = std::make_shared<CAnimImage>(AnimationPath::builtin("Schools"), 0, 0, 117 + offL, 74 + offT);
  128. schoolBorders[0] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevA.def"));
  129. schoolBorders[1] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevF.def"));
  130. schoolBorders[2] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevW.def"));
  131. schoolBorders[3] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevE.def"));
  132. for(auto item : schoolBorders)
  133. item->preload();
  134. mana = std::make_shared<CLabel>(435 + (isBigSpellbook ? 159 : 0), 426 + offB, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, std::to_string(myHero->mana));
  135. if(isBigSpellbook)
  136. statusBar = CGStatusBar::create(400, 587);
  137. else
  138. statusBar = CGStatusBar::create(7, 569, ImagePath::builtin("Spelroll.bmp"));
  139. Rect schoolRect( 549 + pos.x + offR, 94 + pos.y, 45, 35);
  140. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 479 + pos.x + (isBigSpellbook ? 175 : 0), 405 + pos.y + offB, isBigSpellbook ? 60 : 36, 56), std::bind(&CSpellWindow::fexitb, this), 460, this));
  141. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 221 + pos.x + (isBigSpellbook ? 43 : 0), 405 + pos.y + offB, isBigSpellbook ? 60 : 36, 56), std::bind(&CSpellWindow::fbattleSpellsb, this), 453, this));
  142. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 355 + pos.x + (isBigSpellbook ? 110 : 0), 405 + pos.y + offB, isBigSpellbook ? 60 : 36, 56), std::bind(&CSpellWindow::fadvSpellsb, this), 452, this));
  143. interactiveAreas.push_back(std::make_shared<InteractiveArea>( Rect( 418 + pos.x + (isBigSpellbook ? 142 : 0), 405 + pos.y + offB, isBigSpellbook ? 60 : 36, 56), std::bind(&CSpellWindow::fmanaPtsb, this), 459, this));
  144. interactiveAreas.push_back(std::make_shared<InteractiveArea>( schoolRect + Point(0, 0), std::bind(&CSpellWindow::selectSchool, this, 0), 454, this));
  145. interactiveAreas.push_back(std::make_shared<InteractiveArea>( schoolRect + Point(0, 57), std::bind(&CSpellWindow::selectSchool, this, 3), 457, this));
  146. interactiveAreas.push_back(std::make_shared<InteractiveArea>( schoolRect + Point(0, 116), std::bind(&CSpellWindow::selectSchool, this, 1), 455, this));
  147. interactiveAreas.push_back(std::make_shared<InteractiveArea>( schoolRect + Point(0, 176), std::bind(&CSpellWindow::selectSchool, this, 2), 456, this));
  148. interactiveAreas.push_back(std::make_shared<InteractiveArea>( schoolRect + Point(0, 236), std::bind(&CSpellWindow::selectSchool, this, 4), 458, this));
  149. 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));
  150. 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));
  151. //areas for spells
  152. int xpos = 117 + offL + pos.x, ypos = 90 + offT + pos.y;
  153. for(int v=0; v<spellsPerPage; ++v)
  154. {
  155. spellAreas[v] = std::make_shared<SpellArea>( Rect(xpos, ypos, 65, 78), this);
  156. if(v == (spellsPerPage / 2) - 1) //to right page
  157. {
  158. xpos = offRM + 336 + pos.x; ypos = 90 + offT + pos.y;
  159. }
  160. else
  161. {
  162. if(v%(isBigSpellbook ? 3 : 2) == 0 || (v%3 == 1 && isBigSpellbook))
  163. {
  164. xpos+=85;
  165. }
  166. else
  167. {
  168. xpos -= (isBigSpellbook ? 2 : 1)*85; ypos+=97;
  169. }
  170. }
  171. }
  172. selectedTab = battleSpellsOnly ? myInt->localState->spellbookSettings.spellbookLastTabBattle : myInt->localState->spellbookSettings.spellbookLastTabAdvmap;
  173. schoolTab->setFrame(selectedTab, 0);
  174. int cp = battleSpellsOnly ? myInt->localState->spellbookSettings.spellbookLastPageBattle : myInt->localState->spellbookSettings.spellbookLastPageAdvmap;
  175. // spellbook last page battle index is not reset after battle, so this needs to stay here
  176. vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1));
  177. setCurrentPage(cp);
  178. computeSpellsPerArea();
  179. addUsedEvents(KEYBOARD);
  180. }
  181. CSpellWindow::~CSpellWindow()
  182. {
  183. }
  184. std::shared_ptr<IImage> CSpellWindow::createBigSpellBook()
  185. {
  186. std::shared_ptr<IImage> img = GH.renderHandler().loadImage(ImagePath::builtin("SpelBack"));
  187. Canvas canvas = Canvas(Point(800, 600));
  188. // edges
  189. canvas.draw(img, Point(0, 0), Rect(15, 38, 90, 45));
  190. canvas.draw(img, Point(0, 460), Rect(15, 400, 90, 141));
  191. canvas.draw(img, Point(705, 0), Rect(509, 38, 95, 45));
  192. canvas.draw(img, Point(705, 460), Rect(509, 400, 95, 141));
  193. // left / right
  194. Canvas tmp1 = Canvas(Point(90, 355 - 45));
  195. tmp1.draw(img, Point(0, 0), Rect(15, 38 + 45, 90, 355 - 45));
  196. canvas.drawScaled(tmp1, Point(0, 45), Point(90, 415));
  197. Canvas tmp2 = Canvas(Point(95, 355 - 45));
  198. tmp2.draw(img, Point(0, 0), Rect(509, 38 + 45, 95, 355 - 45));
  199. canvas.drawScaled(tmp2, Point(705, 45), Point(95, 415));
  200. // top / bottom
  201. Canvas tmp3 = Canvas(Point(409, 45));
  202. tmp3.draw(img, Point(0, 0), Rect(100, 38, 409, 45));
  203. canvas.drawScaled(tmp3, Point(90, 0), Point(615, 45));
  204. Canvas tmp4 = Canvas(Point(409, 141));
  205. tmp4.draw(img, Point(0, 0), Rect(100, 400, 409, 141));
  206. canvas.drawScaled(tmp4, Point(90, 460), Point(615, 141));
  207. // middle
  208. Canvas tmp5 = Canvas(Point(409, 141));
  209. tmp5.draw(img, Point(0, 0), Rect(100, 38 + 45, 509 - 15, 400 - 38));
  210. canvas.drawScaled(tmp5, Point(90, 45), Point(615, 415));
  211. return GH.renderHandler().createImage(canvas.getInternalSurface());
  212. }
  213. void CSpellWindow::searchInput()
  214. {
  215. if(searchBox)
  216. searchBoxDescription->setEnabled(searchBox->getText().empty());
  217. processSpells();
  218. int cp = 0;
  219. // spellbook last page battle index is not reset after battle, so this needs to stay here
  220. vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1));
  221. setCurrentPage(cp);
  222. computeSpellsPerArea();
  223. }
  224. void CSpellWindow::processSpells()
  225. {
  226. mySpells.clear();
  227. //initializing castable spells
  228. mySpells.reserve(CGI->spellh->objects.size());
  229. for(const CSpell * spell : CGI->spellh->objects)
  230. {
  231. bool searchTextFound = !searchBox || boost::algorithm::contains(boost::algorithm::to_lower_copy(spell->getNameTranslated()), boost::algorithm::to_lower_copy(searchBox->getText()));
  232. if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell) && searchTextFound)
  233. mySpells.push_back(spell);
  234. }
  235. std::sort(mySpells.begin(), mySpells.end(), spellsorter);
  236. //initializing sizes of spellbook's parts
  237. for(auto & elem : sitesPerTabAdv)
  238. elem = 0;
  239. for(auto & elem : sitesPerTabBattle)
  240. elem = 0;
  241. for(const auto spell : mySpells)
  242. {
  243. int * sitesPerOurTab = spell->isCombat() ? sitesPerTabBattle : sitesPerTabAdv;
  244. ++sitesPerOurTab[4];
  245. spell->forEachSchool([&sitesPerOurTab](const SpellSchool & school, bool & stop)
  246. {
  247. ++sitesPerOurTab[school];
  248. });
  249. }
  250. if(sitesPerTabAdv[4] % spellsPerPage == 0)
  251. sitesPerTabAdv[4]/=spellsPerPage;
  252. else
  253. sitesPerTabAdv[4] = sitesPerTabAdv[4]/spellsPerPage + 1;
  254. for(int v=0; v<4; ++v)
  255. {
  256. if(sitesPerTabAdv[v] <= spellsPerPage - 2)
  257. sitesPerTabAdv[v] = 1;
  258. else
  259. {
  260. if((sitesPerTabAdv[v] - spellsPerPage - 2) % spellsPerPage == 0)
  261. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - spellsPerPage - 2) / spellsPerPage + 1;
  262. else
  263. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - spellsPerPage - 2) / spellsPerPage + 2;
  264. }
  265. }
  266. if(sitesPerTabBattle[4] % spellsPerPage == 0)
  267. sitesPerTabBattle[4]/=spellsPerPage;
  268. else
  269. sitesPerTabBattle[4] = sitesPerTabBattle[4]/spellsPerPage + 1;
  270. for(int v=0; v<4; ++v)
  271. {
  272. if(sitesPerTabBattle[v] <= spellsPerPage - 2)
  273. sitesPerTabBattle[v] = 1;
  274. else
  275. {
  276. if((sitesPerTabBattle[v] - spellsPerPage - 2) % spellsPerPage == 0)
  277. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - spellsPerPage - 2) / spellsPerPage + 1;
  278. else
  279. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - spellsPerPage - 2) / spellsPerPage + 2;
  280. }
  281. }
  282. }
  283. void CSpellWindow::fexitb()
  284. {
  285. (myInt->battleInt ? myInt->localState->spellbookSettings.spellbookLastTabBattle : myInt->localState->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  286. (myInt->battleInt ? myInt->localState->spellbookSettings.spellbookLastPageBattle : myInt->localState->spellbookSettings.spellbookLastPageAdvmap) = currentPage;
  287. close();
  288. }
  289. void CSpellWindow::fadvSpellsb()
  290. {
  291. if(battleSpellsOnly == true)
  292. {
  293. turnPageRight();
  294. battleSpellsOnly = false;
  295. setCurrentPage(0);
  296. }
  297. computeSpellsPerArea();
  298. }
  299. void CSpellWindow::fbattleSpellsb()
  300. {
  301. if(battleSpellsOnly == false)
  302. {
  303. turnPageLeft();
  304. battleSpellsOnly = true;
  305. setCurrentPage(0);
  306. }
  307. computeSpellsPerArea();
  308. }
  309. void CSpellWindow::fmanaPtsb()
  310. {
  311. }
  312. void CSpellWindow::selectSchool(int school)
  313. {
  314. if(selectedTab != school)
  315. {
  316. if(selectedTab < school)
  317. turnPageLeft();
  318. else
  319. turnPageRight();
  320. selectedTab = school;
  321. schoolTab->setFrame(selectedTab, 0);
  322. setCurrentPage(0);
  323. }
  324. computeSpellsPerArea();
  325. }
  326. void CSpellWindow::fLcornerb()
  327. {
  328. if(currentPage>0)
  329. {
  330. turnPageLeft();
  331. setCurrentPage(currentPage - 1);
  332. }
  333. computeSpellsPerArea();
  334. }
  335. void CSpellWindow::fRcornerb()
  336. {
  337. if((currentPage + 1) < (pagesWithinCurrentTab()))
  338. {
  339. turnPageRight();
  340. setCurrentPage(currentPage + 1);
  341. }
  342. computeSpellsPerArea();
  343. }
  344. void CSpellWindow::show(Canvas & to)
  345. {
  346. statusBar->show(to);
  347. }
  348. void CSpellWindow::computeSpellsPerArea()
  349. {
  350. std::vector<const CSpell *> spellsCurSite;
  351. spellsCurSite.reserve(mySpells.size());
  352. for(const CSpell * spell : mySpells)
  353. {
  354. if(spell->isCombat() ^ !battleSpellsOnly
  355. && ((selectedTab == 4) || spell->school.at(SpellSchool(selectedTab)))
  356. )
  357. {
  358. spellsCurSite.push_back(spell);
  359. }
  360. }
  361. if(selectedTab == 4)
  362. {
  363. if(spellsCurSite.size() > spellsPerPage)
  364. {
  365. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + currentPage*spellsPerPage, spellsCurSite.end());
  366. if(spellsCurSite.size() > spellsPerPage)
  367. {
  368. spellsCurSite.erase(spellsCurSite.begin()+spellsPerPage, spellsCurSite.end());
  369. }
  370. }
  371. }
  372. else //selectedTab == 0, 1, 2 or 3
  373. {
  374. if(spellsCurSite.size() > spellsPerPage - 2)
  375. {
  376. if(currentPage == 0)
  377. {
  378. spellsCurSite.erase(spellsCurSite.begin()+spellsPerPage-2, spellsCurSite.end());
  379. }
  380. else
  381. {
  382. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + (currentPage-1)*spellsPerPage + spellsPerPage-2, spellsCurSite.end());
  383. if(spellsCurSite.size() > spellsPerPage)
  384. {
  385. spellsCurSite.erase(spellsCurSite.begin()+spellsPerPage, spellsCurSite.end());
  386. }
  387. }
  388. }
  389. }
  390. //applying
  391. if(selectedTab == 4 || currentPage != 0)
  392. {
  393. for(size_t c=0; c<spellsPerPage; ++c)
  394. {
  395. if(c < spellsCurSite.size())
  396. {
  397. spellAreas[c]->setSpell(spellsCurSite[c]);
  398. }
  399. else
  400. {
  401. spellAreas[c]->setSpell(nullptr);
  402. }
  403. }
  404. }
  405. else
  406. {
  407. spellAreas[0]->setSpell(nullptr);
  408. spellAreas[1]->setSpell(nullptr);
  409. for(size_t c=0; c<spellsPerPage-2; ++c)
  410. {
  411. if(c < spellsCurSite.size())
  412. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  413. else
  414. spellAreas[c+2]->setSpell(nullptr);
  415. }
  416. }
  417. redraw();
  418. }
  419. void CSpellWindow::setCurrentPage(int value)
  420. {
  421. currentPage = value;
  422. schoolPicture->visible = selectedTab!=4 && currentPage == 0;
  423. if(selectedTab != 4)
  424. schoolPicture->setFrame(selectedTab, 0);
  425. if (currentPage != 0)
  426. leftCorner->enable();
  427. else
  428. leftCorner->disable();
  429. if (currentPage + 1 < pagesWithinCurrentTab())
  430. rightCorner->enable();
  431. else
  432. rightCorner->disable();
  433. mana->setText(std::to_string(myHero->mana));//just in case, it will be possible to cast spell without closing book
  434. }
  435. void CSpellWindow::turnPageLeft()
  436. {
  437. if(settings["video"]["spellbookAnimation"].Bool() && !isBigSpellbook)
  438. CCS->videoh->openAndPlayVideo(VideoPath::builtin("PGTRNLFT.SMK"), pos.x+13, pos.y+15);
  439. }
  440. void CSpellWindow::turnPageRight()
  441. {
  442. if(settings["video"]["spellbookAnimation"].Bool() && !isBigSpellbook)
  443. CCS->videoh->openAndPlayVideo(VideoPath::builtin("PGTRNRGH.SMK"), pos.x+13, pos.y+15);
  444. }
  445. void CSpellWindow::keyPressed(EShortcut key)
  446. {
  447. switch(key)
  448. {
  449. case EShortcut::GLOBAL_RETURN:
  450. fexitb();
  451. break;
  452. case EShortcut::MOVE_LEFT:
  453. fLcornerb();
  454. break;
  455. case EShortcut::MOVE_RIGHT:
  456. fRcornerb();
  457. break;
  458. case EShortcut::MOVE_UP:
  459. case EShortcut::MOVE_DOWN:
  460. {
  461. bool down = key == EShortcut::MOVE_DOWN;
  462. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  463. int index = -1;
  464. while(schoolsOrder[++index] != selectedTab);
  465. index += (down ? 1 : -1);
  466. vstd::abetween<int>(index, 0, std::size(schoolsOrder) - 1);
  467. if(selectedTab != schoolsOrder[index])
  468. selectSchool(schoolsOrder[index]);
  469. break;
  470. }
  471. case EShortcut::SPELLBOOK_TAB_COMBAT:
  472. fbattleSpellsb();
  473. break;
  474. case EShortcut::SPELLBOOK_TAB_ADVENTURE:
  475. fadvSpellsb();
  476. break;
  477. }
  478. }
  479. int CSpellWindow::pagesWithinCurrentTab()
  480. {
  481. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  482. }
  483. CSpellWindow::SpellArea::SpellArea(Rect pos, CSpellWindow * owner)
  484. {
  485. this->pos = pos;
  486. this->owner = owner;
  487. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  488. schoolLevel = -1;
  489. mySpell = nullptr;
  490. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  491. image = std::make_shared<CAnimImage>(owner->spellIcons, 0, 0);
  492. image->visible = false;
  493. name = std::make_shared<CLabel>(39, 70, FONT_TINY, ETextAlignment::CENTER);
  494. level = std::make_shared<CLabel>(39, 82, FONT_TINY, ETextAlignment::CENTER);
  495. cost = std::make_shared<CLabel>(39, 94, FONT_TINY, ETextAlignment::CENTER);
  496. for(auto l : {name, level, cost})
  497. l->setAutoRedraw(false);
  498. }
  499. CSpellWindow::SpellArea::~SpellArea() = default;
  500. void CSpellWindow::SpellArea::clickPressed(const Point & cursorPosition)
  501. {
  502. if(mySpell)
  503. {
  504. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  505. if(spellCost > owner->myHero->mana) //insufficient mana
  506. {
  507. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[206]) % spellCost % owner->myHero->mana));
  508. return;
  509. }
  510. //anything that is not combat spell is adventure spell
  511. //this not an error in general to cast even creature ability with hero
  512. const bool combatSpell = mySpell->isCombat();
  513. if(combatSpell == mySpell->isAdventure())
  514. {
  515. logGlobal->error("Spell have invalid flags");
  516. return;
  517. }
  518. const bool inCombat = owner->myInt->battleInt != nullptr;
  519. const bool inCastle = owner->myInt->castleInt != nullptr;
  520. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  521. if((combatSpell ^ inCombat) || inCastle)
  522. {
  523. std::vector<std::shared_ptr<CComponent>> hlp(1, std::make_shared<CComponent>(ComponentType::SPELL, mySpell->id));
  524. LOCPLINT->showInfoDialog(mySpell->getDescriptionTranslated(schoolLevel), hlp);
  525. }
  526. else if(combatSpell)
  527. {
  528. spells::detail::ProblemImpl problem;
  529. if(mySpell->canBeCast(problem, owner->myInt->battleInt->getBattle().get(), spells::Mode::HERO, owner->myHero))
  530. {
  531. owner->myInt->battleInt->castThisSpell(mySpell->id);
  532. owner->fexitb();
  533. }
  534. else
  535. {
  536. std::vector<std::string> texts;
  537. problem.getAll(texts);
  538. if(!texts.empty())
  539. LOCPLINT->showInfoDialog(texts.front());
  540. else
  541. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.spellUnknownProblem"));
  542. }
  543. }
  544. else //adventure spell
  545. {
  546. const CGHeroInstance * h = owner->myHero;
  547. GH.windows().popWindows(1);
  548. auto guard = vstd::makeScopeGuard([this]()
  549. {
  550. owner->myInt->localState->spellbookSettings.spellbookLastTabAdvmap = owner->selectedTab;
  551. owner->myInt->localState->spellbookSettings.spellbookLastPageAdvmap = owner->currentPage;
  552. });
  553. if(mySpell->getTargetType() == spells::AimType::LOCATION)
  554. adventureInt->enterCastingMode(mySpell);
  555. else if(mySpell->getTargetType() == spells::AimType::NO_TARGET)
  556. owner->myInt->cb->castSpell(h, mySpell->id);
  557. else
  558. logGlobal->error("Invalid spell target type");
  559. }
  560. }
  561. }
  562. void CSpellWindow::SpellArea::showPopupWindow(const Point & cursorPosition)
  563. {
  564. if(mySpell)
  565. {
  566. std::string dmgInfo;
  567. auto causedDmg = owner->myInt->cb->estimateSpellDamage(mySpell, owner->myHero);
  568. if(causedDmg == 0 || mySpell->id == SpellID::TITANS_LIGHTNING_BOLT) //Titan's Lightning Bolt already has damage info included
  569. dmgInfo.clear();
  570. else
  571. {
  572. dmgInfo = CGI->generaltexth->allTexts[343];
  573. boost::algorithm::replace_first(dmgInfo, "%d", std::to_string(causedDmg));
  574. }
  575. CRClickPopup::createAndPush(mySpell->getDescriptionTranslated(schoolLevel) + dmgInfo, std::make_shared<CComponent>(ComponentType::SPELL, mySpell->id));
  576. }
  577. }
  578. void CSpellWindow::SpellArea::hover(bool on)
  579. {
  580. if(mySpell)
  581. {
  582. if(on)
  583. owner->statusBar->write(boost::str(boost::format("%s (%s)") % mySpell->getNameTranslated() % CGI->generaltexth->allTexts[171+mySpell->getLevel()]));
  584. else
  585. owner->statusBar->clear();
  586. }
  587. }
  588. void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
  589. {
  590. schoolBorder.reset();
  591. image->visible = false;
  592. name->setText("");
  593. level->setText("");
  594. cost->setText("");
  595. mySpell = spell;
  596. if(mySpell)
  597. {
  598. SpellSchool whichSchool; //0 - air magic, 1 - fire magic, 2 - water magic, 3 - earth magic,
  599. schoolLevel = owner->myHero->getSpellSchoolLevel(mySpell, &whichSchool);
  600. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  601. image->setFrame(mySpell->id);
  602. image->visible = true;
  603. {
  604. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  605. schoolBorder.reset();
  606. if (owner->selectedTab >= 4)
  607. {
  608. if (whichSchool.getNum() != SpellSchool())
  609. schoolBorder = std::make_shared<CAnimImage>(owner->schoolBorders.at(whichSchool.getNum()), schoolLevel);
  610. }
  611. else
  612. schoolBorder = std::make_shared<CAnimImage>(owner->schoolBorders.at(owner->selectedTab), schoolLevel);
  613. }
  614. ColorRGBA firstLineColor, secondLineColor;
  615. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  616. {
  617. firstLineColor = Colors::WHITE;
  618. secondLineColor = Colors::ORANGE;
  619. }
  620. else
  621. {
  622. firstLineColor = Colors::YELLOW;
  623. secondLineColor = Colors::WHITE;
  624. }
  625. name->color = firstLineColor;
  626. name->setText(mySpell->getNameTranslated());
  627. level->color = secondLineColor;
  628. if(schoolLevel > 0)
  629. {
  630. boost::format fmt("%s/%s");
  631. fmt % CGI->generaltexth->allTexts[171 + mySpell->getLevel()];
  632. fmt % CGI->generaltexth->levels[3+(schoolLevel-1)];//lines 4-6
  633. level->setText(fmt.str());
  634. }
  635. else
  636. level->setText(CGI->generaltexth->allTexts[171 + mySpell->getLevel()]);
  637. cost->color = secondLineColor;
  638. boost::format costfmt("%s: %d");
  639. costfmt % CGI->generaltexth->allTexts[387] % spellCost;
  640. cost->setText(costfmt.str());
  641. }
  642. }