CSpellWindow.cpp 23 KB

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