CSpellWindow.cpp 22 KB

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