CSpellWindow.cpp 27 KB

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