CSpellWindow.cpp 25 KB

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