CSpellWindow.cpp 23 KB

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