CSpellWindow.cpp 24 KB

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