CSpellWindow.cpp 23 KB

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