CSpellWindow.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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 "../widgets/VideoWidget.h"
  30. #include "../adventureMap/AdventureMapInterface.h"
  31. #include "../render/AssetGenerator.h"
  32. #include "../../CCallback.h"
  33. #include "../../lib/CConfigHandler.h"
  34. #include "../../lib/texts/CGeneralTextHandler.h"
  35. #include "../../lib/spells/CSpellHandler.h"
  36. #include "../../lib/spells/ISpellMechanics.h"
  37. #include "../../lib/spells/Problem.h"
  38. #include "../../lib/texts/TextOperations.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, const 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;
  102. if(isBigSpellbook)
  103. {
  104. AssetGenerator::createBigSpellBook();
  105. background = std::make_shared<CPicture>(ImagePath::builtin("SpellBookLarge"), 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->getSpellbookSettings().spellbookLastTabBattle : myInt->localState->getSpellbookSettings().spellbookLastTabAdvmap;
  178. schoolTab->setFrame(selectedTab, 0);
  179. int cp = battleSpellsOnly ? myInt->localState->getSpellbookSettings().spellbookLastPageBattle : myInt->localState->getSpellbookSettings().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. void CSpellWindow::searchInput()
  190. {
  191. if(searchBox)
  192. searchBoxDescription->setEnabled(searchBox->getText().empty());
  193. processSpells();
  194. int cp = 0;
  195. // spellbook last page battle index is not reset after battle, so this needs to stay here
  196. vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1));
  197. setCurrentPage(cp);
  198. computeSpellsPerArea();
  199. }
  200. void CSpellWindow::processSpells()
  201. {
  202. mySpells.clear();
  203. //initializing castable spells
  204. mySpells.reserve(CGI->spellh->objects.size());
  205. for(auto const & spell : CGI->spellh->objects)
  206. {
  207. bool searchTextFound = !searchBox || TextOperations::textSearchSimilar(searchBox->getText(), spell->getNameTranslated());
  208. if(onSpellSelect)
  209. {
  210. if(spell->isCombat() == openOnBattleSpells && !spell->isSpecial() && !spell->isCreatureAbility() && searchTextFound && (showAllSpells->isSelected() || myHero->canCastThisSpell(spell.get())))
  211. mySpells.push_back(spell.get());
  212. continue;
  213. }
  214. if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell.get()) && searchTextFound)
  215. mySpells.push_back(spell.get());
  216. }
  217. SpellbookSpellSorter spellsorter;
  218. std::sort(mySpells.begin(), mySpells.end(), spellsorter);
  219. //initializing sizes of spellbook's parts
  220. for(auto & elem : sitesPerTabAdv)
  221. elem = 0;
  222. for(auto & elem : sitesPerTabBattle)
  223. elem = 0;
  224. for(const auto spell : mySpells)
  225. {
  226. int * sitesPerOurTab = spell->isCombat() ? sitesPerTabBattle : sitesPerTabAdv;
  227. ++sitesPerOurTab[4];
  228. spell->forEachSchool([&sitesPerOurTab](const SpellSchool & school, bool & stop)
  229. {
  230. ++sitesPerOurTab[school];
  231. });
  232. }
  233. if(sitesPerTabAdv[4] % spellsPerPage == 0)
  234. sitesPerTabAdv[4]/=spellsPerPage;
  235. else
  236. sitesPerTabAdv[4] = sitesPerTabAdv[4]/spellsPerPage + 1;
  237. for(int v=0; v<4; ++v)
  238. {
  239. if(sitesPerTabAdv[v] <= spellsPerPage - 2)
  240. sitesPerTabAdv[v] = 1;
  241. else
  242. {
  243. if((sitesPerTabAdv[v] - (spellsPerPage - 2)) % spellsPerPage == 0)
  244. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - (spellsPerPage - 2)) / spellsPerPage + 1;
  245. else
  246. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - (spellsPerPage - 2)) / spellsPerPage + 2;
  247. }
  248. }
  249. if(sitesPerTabBattle[4] % spellsPerPage == 0)
  250. sitesPerTabBattle[4]/=spellsPerPage;
  251. else
  252. sitesPerTabBattle[4] = sitesPerTabBattle[4]/spellsPerPage + 1;
  253. for(int v=0; v<4; ++v)
  254. {
  255. if(sitesPerTabBattle[v] <= spellsPerPage - 2)
  256. sitesPerTabBattle[v] = 1;
  257. else
  258. {
  259. if((sitesPerTabBattle[v] - (spellsPerPage - 2)) % spellsPerPage == 0)
  260. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - (spellsPerPage - 2)) / spellsPerPage + 1;
  261. else
  262. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - (spellsPerPage - 2)) / spellsPerPage + 2;
  263. }
  264. }
  265. }
  266. void CSpellWindow::fexitb()
  267. {
  268. auto spellBookState = myInt->localState->getSpellbookSettings();
  269. if(myInt->battleInt)
  270. {
  271. spellBookState.spellbookLastTabBattle = selectedTab;
  272. spellBookState.spellbookLastPageBattle = currentPage;
  273. }
  274. else
  275. {
  276. spellBookState.spellbookLastTabAdvmap = selectedTab;
  277. spellBookState.spellbookLastPageAdvmap = currentPage;
  278. }
  279. myInt->localState->setSpellbookSettings(spellBookState);
  280. if(onSpellSelect)
  281. onSpellSelect(SpellID::NONE);
  282. close();
  283. }
  284. void CSpellWindow::fadvSpellsb()
  285. {
  286. if(battleSpellsOnly == true)
  287. {
  288. turnPageRight();
  289. battleSpellsOnly = false;
  290. setCurrentPage(0);
  291. }
  292. computeSpellsPerArea();
  293. }
  294. void CSpellWindow::fbattleSpellsb()
  295. {
  296. if(battleSpellsOnly == false)
  297. {
  298. turnPageLeft();
  299. battleSpellsOnly = true;
  300. setCurrentPage(0);
  301. }
  302. computeSpellsPerArea();
  303. }
  304. void CSpellWindow::fmanaPtsb()
  305. {
  306. }
  307. void CSpellWindow::selectSchool(int school)
  308. {
  309. if(selectedTab != school)
  310. {
  311. if(selectedTab < school)
  312. turnPageLeft();
  313. else
  314. turnPageRight();
  315. selectedTab = school;
  316. schoolTab->setFrame(selectedTab, 0);
  317. setCurrentPage(0);
  318. }
  319. computeSpellsPerArea();
  320. }
  321. void CSpellWindow::fLcornerb()
  322. {
  323. if(currentPage>0)
  324. {
  325. turnPageLeft();
  326. setCurrentPage(currentPage - 1);
  327. }
  328. computeSpellsPerArea();
  329. }
  330. void CSpellWindow::fRcornerb()
  331. {
  332. if((currentPage + 1) < (pagesWithinCurrentTab()))
  333. {
  334. turnPageRight();
  335. setCurrentPage(currentPage + 1);
  336. }
  337. computeSpellsPerArea();
  338. }
  339. void CSpellWindow::show(Canvas & to)
  340. {
  341. if(video)
  342. video->show(to);
  343. statusBar->show(to);
  344. }
  345. void CSpellWindow::computeSpellsPerArea()
  346. {
  347. std::vector<const CSpell *> spellsCurSite;
  348. spellsCurSite.reserve(mySpells.size());
  349. for(const CSpell * spell : mySpells)
  350. {
  351. if(spell->isCombat() ^ !battleSpellsOnly
  352. && ((selectedTab == 4) || spell->school.at(SpellSchool(selectedTab)))
  353. )
  354. {
  355. spellsCurSite.push_back(spell);
  356. }
  357. }
  358. if(selectedTab == 4)
  359. {
  360. if(spellsCurSite.size() > spellsPerPage)
  361. {
  362. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + currentPage*spellsPerPage, spellsCurSite.end());
  363. if(spellsCurSite.size() > spellsPerPage)
  364. {
  365. spellsCurSite.erase(spellsCurSite.begin()+spellsPerPage, spellsCurSite.end());
  366. }
  367. }
  368. }
  369. else //selectedTab == 0, 1, 2 or 3
  370. {
  371. if(spellsCurSite.size() > spellsPerPage - 2)
  372. {
  373. if(currentPage == 0)
  374. {
  375. spellsCurSite.erase(spellsCurSite.begin()+spellsPerPage-2, spellsCurSite.end());
  376. }
  377. else
  378. {
  379. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + (currentPage-1)*spellsPerPage + spellsPerPage-2, spellsCurSite.end());
  380. if(spellsCurSite.size() > spellsPerPage)
  381. {
  382. spellsCurSite.erase(spellsCurSite.begin()+spellsPerPage, spellsCurSite.end());
  383. }
  384. }
  385. }
  386. }
  387. //applying
  388. if(selectedTab == 4 || currentPage != 0)
  389. {
  390. for(size_t c=0; c<spellsPerPage; ++c)
  391. {
  392. if(c < spellsCurSite.size())
  393. {
  394. spellAreas[c]->setSpell(spellsCurSite[c]);
  395. }
  396. else
  397. {
  398. spellAreas[c]->setSpell(nullptr);
  399. }
  400. }
  401. }
  402. else
  403. {
  404. spellAreas[0]->setSpell(nullptr);
  405. spellAreas[1]->setSpell(nullptr);
  406. for(size_t c=0; c<spellsPerPage-2; ++c)
  407. {
  408. if(c < spellsCurSite.size())
  409. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  410. else
  411. spellAreas[c+2]->setSpell(nullptr);
  412. }
  413. }
  414. redraw();
  415. }
  416. void CSpellWindow::setCurrentPage(int value)
  417. {
  418. currentPage = value;
  419. schoolPicture->visible = selectedTab!=4 && currentPage == 0;
  420. if(selectedTab != 4)
  421. schoolPicture->setFrame(selectedTab, 0);
  422. if (currentPage != 0)
  423. leftCorner->enable();
  424. else
  425. leftCorner->disable();
  426. if (currentPage + 1 < pagesWithinCurrentTab())
  427. rightCorner->enable();
  428. else
  429. rightCorner->disable();
  430. mana->setText(std::to_string(myHero->mana));//just in case, it will be possible to cast spell without closing book
  431. }
  432. void CSpellWindow::turnPageLeft()
  433. {
  434. OBJECT_CONSTRUCTION;
  435. if(settings["video"]["spellbookAnimation"].Bool() && !isBigSpellbook)
  436. video = std::make_shared<VideoWidgetOnce>(Point(13, 14), VideoPath::builtin("PGTRNLFT.SMK"), false, this);
  437. }
  438. void CSpellWindow::turnPageRight()
  439. {
  440. OBJECT_CONSTRUCTION;
  441. if(settings["video"]["spellbookAnimation"].Bool() && !isBigSpellbook)
  442. video = std::make_shared<VideoWidgetOnce>(Point(13, 14), VideoPath::builtin("PGTRNRGH.SMK"), false, this);
  443. }
  444. void CSpellWindow::onVideoPlaybackFinished()
  445. {
  446. video.reset();
  447. redraw();
  448. }
  449. void CSpellWindow::keyPressed(EShortcut key)
  450. {
  451. switch(key)
  452. {
  453. case EShortcut::GLOBAL_RETURN:
  454. fexitb();
  455. break;
  456. case EShortcut::MOVE_LEFT:
  457. fLcornerb();
  458. break;
  459. case EShortcut::MOVE_RIGHT:
  460. fRcornerb();
  461. break;
  462. case EShortcut::MOVE_UP:
  463. case EShortcut::MOVE_DOWN:
  464. {
  465. bool down = key == EShortcut::MOVE_DOWN;
  466. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  467. int index = -1;
  468. while(schoolsOrder[++index] != selectedTab);
  469. index += (down ? 1 : -1);
  470. vstd::abetween<int>(index, 0, std::size(schoolsOrder) - 1);
  471. if(selectedTab != schoolsOrder[index])
  472. selectSchool(schoolsOrder[index]);
  473. break;
  474. }
  475. case EShortcut::SPELLBOOK_TAB_COMBAT:
  476. fbattleSpellsb();
  477. break;
  478. case EShortcut::SPELLBOOK_TAB_ADVENTURE:
  479. fadvSpellsb();
  480. break;
  481. }
  482. }
  483. int CSpellWindow::pagesWithinCurrentTab()
  484. {
  485. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  486. }
  487. CSpellWindow::SpellArea::SpellArea(Rect pos, CSpellWindow * owner)
  488. {
  489. this->pos = pos;
  490. this->owner = owner;
  491. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  492. schoolLevel = -1;
  493. mySpell = nullptr;
  494. OBJECT_CONSTRUCTION;
  495. image = std::make_shared<CAnimImage>(AnimationPath::builtin("Spells"), 0, 0);
  496. image->visible = false;
  497. name = std::make_shared<CLabel>(39, 70, FONT_TINY, ETextAlignment::CENTER);
  498. level = std::make_shared<CLabel>(39, 82, FONT_TINY, ETextAlignment::CENTER);
  499. cost = std::make_shared<CLabel>(39, 94, FONT_TINY, ETextAlignment::CENTER);
  500. for(auto l : {name, level, cost})
  501. l->setAutoRedraw(false);
  502. }
  503. CSpellWindow::SpellArea::~SpellArea() = default;
  504. void CSpellWindow::SpellArea::clickPressed(const Point & cursorPosition)
  505. {
  506. if(mySpell)
  507. {
  508. if(owner->onSpellSelect)
  509. {
  510. owner->onSpellSelect(mySpell->id);
  511. owner->close();
  512. return;
  513. }
  514. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  515. if(spellCost > owner->myHero->mana) //insufficient mana
  516. {
  517. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[206]) % spellCost % owner->myHero->mana));
  518. return;
  519. }
  520. //anything that is not combat spell is adventure spell
  521. //this not an error in general to cast even creature ability with hero
  522. const bool combatSpell = mySpell->isCombat();
  523. if(combatSpell == mySpell->isAdventure())
  524. {
  525. logGlobal->error("Spell have invalid flags");
  526. return;
  527. }
  528. const bool inCombat = owner->myInt->battleInt != nullptr;
  529. const bool inCastle = owner->myInt->castleInt != nullptr;
  530. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  531. if((combatSpell != inCombat) || inCastle || (!combatSpell && !LOCPLINT->makingTurn))
  532. {
  533. std::vector<std::shared_ptr<CComponent>> hlp(1, std::make_shared<CComponent>(ComponentType::SPELL, mySpell->id));
  534. LOCPLINT->showInfoDialog(mySpell->getDescriptionTranslated(schoolLevel), hlp);
  535. }
  536. else if(combatSpell)
  537. {
  538. spells::detail::ProblemImpl problem;
  539. if(mySpell->canBeCast(problem, owner->myInt->battleInt->getBattle().get(), spells::Mode::HERO, owner->myHero))
  540. {
  541. owner->myInt->battleInt->castThisSpell(mySpell->id);
  542. owner->fexitb();
  543. }
  544. else
  545. {
  546. std::vector<std::string> texts;
  547. problem.getAll(texts);
  548. if(!texts.empty())
  549. LOCPLINT->showInfoDialog(texts.front());
  550. else
  551. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.spellUnknownProblem"));
  552. }
  553. }
  554. else //adventure spell
  555. {
  556. const CGHeroInstance * h = owner->myHero;
  557. GH.windows().popWindows(1);
  558. auto guard = vstd::makeScopeGuard([this]()
  559. {
  560. auto spellBookState = owner->myInt->localState->getSpellbookSettings();
  561. spellBookState.spellbookLastTabAdvmap = owner->selectedTab;
  562. spellBookState.spellbookLastPageAdvmap = owner->currentPage;
  563. owner->myInt->localState->setSpellbookSettings(spellBookState);
  564. });
  565. spells::detail::ProblemImpl problem;
  566. if (mySpell->getAdventureMechanics().canBeCast(problem, LOCPLINT->cb.get(), owner->myHero))
  567. {
  568. if(mySpell->getTargetType() == spells::AimType::LOCATION)
  569. adventureInt->enterCastingMode(mySpell);
  570. else if(mySpell->getTargetType() == spells::AimType::NO_TARGET)
  571. owner->myInt->cb->castSpell(h, mySpell->id);
  572. else
  573. logGlobal->error("Invalid spell target type");
  574. }
  575. else
  576. {
  577. std::vector<std::string> texts;
  578. problem.getAll(texts);
  579. if(!texts.empty())
  580. LOCPLINT->showInfoDialog(texts.front());
  581. else
  582. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.spellUnknownProblem"));
  583. }
  584. }
  585. }
  586. }
  587. void CSpellWindow::SpellArea::showPopupWindow(const Point & cursorPosition)
  588. {
  589. if(mySpell)
  590. {
  591. std::string dmgInfo;
  592. auto causedDmg = owner->myInt->cb->estimateSpellDamage(mySpell, owner->myHero);
  593. if(causedDmg == 0 || mySpell->id == SpellID::TITANS_LIGHTNING_BOLT) //Titan's Lightning Bolt already has damage info included
  594. dmgInfo.clear();
  595. else
  596. {
  597. dmgInfo = CGI->generaltexth->allTexts[343];
  598. boost::algorithm::replace_first(dmgInfo, "%d", std::to_string(causedDmg));
  599. }
  600. CRClickPopup::createAndPush(mySpell->getDescriptionTranslated(schoolLevel) + dmgInfo, std::make_shared<CComponent>(ComponentType::SPELL, mySpell->id));
  601. }
  602. }
  603. void CSpellWindow::SpellArea::hover(bool on)
  604. {
  605. if(mySpell)
  606. {
  607. if(on)
  608. owner->statusBar->write(boost::str(boost::format("%s (%s)") % mySpell->getNameTranslated() % CGI->generaltexth->allTexts[171+mySpell->getLevel()]));
  609. else
  610. owner->statusBar->clear();
  611. }
  612. }
  613. void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
  614. {
  615. schoolBorder.reset();
  616. image->visible = false;
  617. name->setText("");
  618. level->setText("");
  619. cost->setText("");
  620. mySpell = spell;
  621. if(mySpell)
  622. {
  623. SpellSchool whichSchool; //0 - air magic, 1 - fire magic, 2 - water magic, 3 - earth magic,
  624. schoolLevel = owner->myHero->getSpellSchoolLevel(mySpell, &whichSchool);
  625. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  626. image->setFrame(mySpell->id);
  627. image->visible = true;
  628. {
  629. OBJECT_CONSTRUCTION;
  630. static const std::array schoolBorders = {
  631. AnimationPath::builtin("SplevA.def"),
  632. AnimationPath::builtin("SplevF.def"),
  633. AnimationPath::builtin("SplevW.def"),
  634. AnimationPath::builtin("SplevE.def")
  635. };
  636. schoolBorder.reset();
  637. if (owner->selectedTab >= 4)
  638. {
  639. if (whichSchool.getNum() != SpellSchool())
  640. schoolBorder = std::make_shared<CAnimImage>(schoolBorders.at(whichSchool.getNum()), schoolLevel);
  641. }
  642. else
  643. schoolBorder = std::make_shared<CAnimImage>(schoolBorders.at(owner->selectedTab), schoolLevel);
  644. }
  645. ColorRGBA firstLineColor, secondLineColor;
  646. if(spellCost > owner->myHero->mana && !owner->onSpellSelect) //hero cannot cast this spell
  647. {
  648. firstLineColor = Colors::WHITE;
  649. secondLineColor = Colors::ORANGE;
  650. }
  651. else
  652. {
  653. firstLineColor = Colors::YELLOW;
  654. secondLineColor = Colors::WHITE;
  655. }
  656. name->color = firstLineColor;
  657. name->setText(mySpell->getNameTranslated());
  658. level->color = secondLineColor;
  659. if(schoolLevel > 0)
  660. {
  661. boost::format fmt("%s/%s");
  662. fmt % CGI->generaltexth->allTexts[171 + mySpell->getLevel()];
  663. fmt % CGI->generaltexth->levels[3+(schoolLevel-1)];//lines 4-6
  664. level->setText(fmt.str());
  665. }
  666. else
  667. level->setText(CGI->generaltexth->allTexts[171 + mySpell->getLevel()]);
  668. cost->color = secondLineColor;
  669. boost::format costfmt("%s: %d");
  670. costfmt % CGI->generaltexth->allTexts[387] % spellCost;
  671. cost->setText(costfmt.str());
  672. }
  673. }