CSpellWindow.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * CSpellWindow.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CSpellWindow.h"
  12. #include "../../lib/ScopeGuard.h"
  13. #include "GUIClasses.h"
  14. #include "InfoWindows.h"
  15. #include "CCastleInterface.h"
  16. #include "../CGameInfo.h"
  17. #include "../CPlayerInterface.h"
  18. #include "../PlayerLocalState.h"
  19. #include "../CVideoHandler.h"
  20. #include "../battle/BattleInterface.h"
  21. #include "../gui/CGuiHandler.h"
  22. #include "../gui/Shortcut.h"
  23. #include "../gui/WindowHandler.h"
  24. #include "../widgets/MiscWidgets.h"
  25. #include "../widgets/CComponent.h"
  26. #include "../widgets/TextControls.h"
  27. #include "../adventureMap/AdventureMapInterface.h"
  28. #include "../render/CAnimation.h"
  29. #include "../render/IRenderHandler.h"
  30. #include "../render/IImage.h"
  31. #include "../render/IImageLoader.h"
  32. #include "../render/Canvas.h"
  33. #include "../../CCallback.h"
  34. #include "../../lib/CConfigHandler.h"
  35. #include "../../lib/CGeneralTextHandler.h"
  36. #include "../../lib/spells/CSpellHandler.h"
  37. #include "../../lib/spells/Problem.h"
  38. #include "../../lib/GameConstants.h"
  39. #include "../../lib/mapObjects/CGHeroInstance.h"
  40. CSpellWindow::InteractiveArea::InteractiveArea(const Rect & myRect, std::function<void()> funcL, int helpTextId, CSpellWindow * _owner)
  41. {
  42. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  43. pos = myRect;
  44. onLeft = funcL;
  45. hoverText = CGI->generaltexth->zelp[helpTextId].first;
  46. helpText = CGI->generaltexth->zelp[helpTextId].second;
  47. owner = _owner;
  48. }
  49. void CSpellWindow::InteractiveArea::clickPressed(const Point & cursorPosition)
  50. {
  51. onLeft();
  52. }
  53. void CSpellWindow::InteractiveArea::showPopupWindow(const Point & cursorPosition)
  54. {
  55. CRClickPopup::createAndPush(helpText);
  56. }
  57. void CSpellWindow::InteractiveArea::hover(bool on)
  58. {
  59. if(on)
  60. owner->statusBar->write(hoverText);
  61. else
  62. owner->statusBar->clear();
  63. }
  64. class SpellbookSpellSorter
  65. {
  66. public:
  67. bool operator()(const CSpell * A, const CSpell * B)
  68. {
  69. if(A->getLevel() < B->getLevel())
  70. return true;
  71. if(A->getLevel() > B->getLevel())
  72. return false;
  73. for(auto schoolId = 0; schoolId < GameConstants::DEFAULT_SCHOOLS; schoolId++)
  74. {
  75. if(A->school.at(SpellSchool(schoolId)) && !B->school.at(SpellSchool(schoolId)))
  76. return true;
  77. if(!A->school.at(SpellSchool(schoolId)) && B->school.at(SpellSchool(schoolId)))
  78. return false;
  79. }
  80. return A->getNameTranslated() < B->getNameTranslated();
  81. }
  82. } spellsorter;
  83. CSpellWindow::CSpellWindow(const CGHeroInstance * _myHero, CPlayerInterface * _myInt, bool openOnBattleSpells):
  84. CWindowObject(PLAYER_COLORED | (settings["general"]["enableUiEnhancements"].Bool() ? BORDERED : 0)),
  85. battleSpellsOnly(openOnBattleSpells),
  86. selectedTab(4),
  87. currentPage(0),
  88. myHero(_myHero),
  89. myInt(_myInt),
  90. isBigSpellbook(settings["general"]["enableUiEnhancements"].Bool()),
  91. spellsPerPage(24),
  92. offL(-11),
  93. offR(195),
  94. offRM(110),
  95. offT(-37),
  96. offB(56)
  97. {
  98. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  99. if(isBigSpellbook)
  100. {
  101. background = std::make_shared<CPicture>(createBigSpellBook(), Point(0, 0));
  102. updateShadow();
  103. }
  104. else
  105. {
  106. background = std::make_shared<CPicture>(ImagePath::builtin("SpelBack"), 0, 0);
  107. offL = offR = offT = offB = offRM = 0;
  108. spellsPerPage = 12;
  109. }
  110. pos = background->center(Point(pos.w/2 + pos.x, pos.h/2 + pos.y));
  111. //initializing castable spells
  112. mySpells.reserve(CGI->spellh->objects.size());
  113. for(const CSpell * spell : CGI->spellh->objects)
  114. {
  115. if(!spell->isCreatureAbility() && myHero->canCastThisSpell(spell))
  116. mySpells.push_back(spell);
  117. }
  118. std::sort(mySpells.begin(), mySpells.end(), spellsorter);
  119. //initializing sizes of spellbook's parts
  120. for(auto & elem : sitesPerTabAdv)
  121. elem = 0;
  122. for(auto & elem : sitesPerTabBattle)
  123. elem = 0;
  124. for(const auto spell : mySpells)
  125. {
  126. int * sitesPerOurTab = spell->isCombat() ? sitesPerTabBattle : sitesPerTabAdv;
  127. ++sitesPerOurTab[4];
  128. spell->forEachSchool([&sitesPerOurTab](const SpellSchool & school, bool & stop)
  129. {
  130. ++sitesPerOurTab[school];
  131. });
  132. }
  133. if(sitesPerTabAdv[4] % spellsPerPage == 0)
  134. sitesPerTabAdv[4]/=spellsPerPage;
  135. else
  136. sitesPerTabAdv[4] = sitesPerTabAdv[4]/spellsPerPage + 1;
  137. for(int v=0; v<4; ++v)
  138. {
  139. if(sitesPerTabAdv[v] <= spellsPerPage - 2)
  140. sitesPerTabAdv[v] = 1;
  141. else
  142. {
  143. if((sitesPerTabAdv[v] - spellsPerPage - 2) % spellsPerPage == 0)
  144. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - spellsPerPage - 2) / spellsPerPage + 1;
  145. else
  146. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - spellsPerPage - 2) / spellsPerPage + 2;
  147. }
  148. }
  149. if(sitesPerTabBattle[4] % spellsPerPage == 0)
  150. sitesPerTabBattle[4]/=spellsPerPage;
  151. else
  152. sitesPerTabBattle[4] = sitesPerTabBattle[4]/spellsPerPage + 1;
  153. for(int v=0; v<4; ++v)
  154. {
  155. if(sitesPerTabBattle[v] <= spellsPerPage - 2)
  156. sitesPerTabBattle[v] = 1;
  157. else
  158. {
  159. if((sitesPerTabBattle[v] - spellsPerPage - 2) % spellsPerPage == 0)
  160. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - spellsPerPage - 2) / spellsPerPage + 1;
  161. else
  162. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - spellsPerPage - 2) / spellsPerPage + 2;
  163. }
  164. }
  165. //numbers of spell pages computed
  166. leftCorner = std::make_shared<CPicture>(ImagePath::builtin("SpelTrnL.bmp"), 97 + offL, 77 + offT);
  167. rightCorner = std::make_shared<CPicture>(ImagePath::builtin("SpelTrnR.bmp"), 487 + offR, 72 + offT);
  168. spellIcons = GH.renderHandler().loadAnimation(AnimationPath::builtin("Spells"));
  169. schoolTab = std::make_shared<CAnimImage>(AnimationPath::builtin("SpelTab"), selectedTab, 0, 524 + offR, 88);
  170. schoolPicture = std::make_shared<CAnimImage>(AnimationPath::builtin("Schools"), 0, 0, 117 + offL, 74 + offT);
  171. schoolBorders[0] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevA.def"));
  172. schoolBorders[1] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevF.def"));
  173. schoolBorders[2] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevW.def"));
  174. schoolBorders[3] = GH.renderHandler().loadAnimation(AnimationPath::builtin("SplevE.def"));
  175. for(auto item : schoolBorders)
  176. item->preload();
  177. mana = std::make_shared<CLabel>(435 + (isBigSpellbook ? 159 : 0), 426 + offB, FONT_SMALL, ETextAlignment::CENTER, Colors::YELLOW, std::to_string(myHero->mana));
  178. if(isBigSpellbook)
  179. statusBar = CGStatusBar::create(400, 587);
  180. else
  181. statusBar = CGStatusBar::create(7, 569, ImagePath::builtin("Spelroll.bmp"));
  182. Rect schoolRect( 549 + pos.x + offR, 94 + pos.y, 45, 35);
  183. 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));
  184. 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));
  185. 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));
  186. 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));
  187. interactiveAreas.push_back(std::make_shared<InteractiveArea>( schoolRect + Point(0, 0), std::bind(&CSpellWindow::selectSchool, this, 0), 454, this));
  188. interactiveAreas.push_back(std::make_shared<InteractiveArea>( schoolRect + Point(0, 57), std::bind(&CSpellWindow::selectSchool, this, 3), 457, this));
  189. interactiveAreas.push_back(std::make_shared<InteractiveArea>( schoolRect + Point(0, 116), std::bind(&CSpellWindow::selectSchool, this, 1), 455, this));
  190. interactiveAreas.push_back(std::make_shared<InteractiveArea>( schoolRect + Point(0, 176), std::bind(&CSpellWindow::selectSchool, this, 2), 456, this));
  191. interactiveAreas.push_back(std::make_shared<InteractiveArea>( schoolRect + Point(0, 236), std::bind(&CSpellWindow::selectSchool, this, 4), 458, this));
  192. 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));
  193. 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));
  194. //areas for spells
  195. int xpos = 117 + offL + pos.x, ypos = 90 + offT + pos.y;
  196. for(int v=0; v<spellsPerPage; ++v)
  197. {
  198. spellAreas[v] = std::make_shared<SpellArea>( Rect(xpos, ypos, 65, 78), this);
  199. if(v == (spellsPerPage / 2) - 1) //to right page
  200. {
  201. xpos = offRM + 336 + pos.x; ypos = 90 + offT + pos.y;
  202. }
  203. else
  204. {
  205. if(v%(isBigSpellbook ? 3 : 2) == 0 || (v%3 == 1 && isBigSpellbook))
  206. {
  207. xpos+=85;
  208. }
  209. else
  210. {
  211. xpos -= (isBigSpellbook ? 2 : 1)*85; ypos+=97;
  212. }
  213. }
  214. }
  215. selectedTab = battleSpellsOnly ? myInt->localState->spellbookSettings.spellbookLastTabBattle : myInt->localState->spellbookSettings.spellbookLastTabAdvmap;
  216. schoolTab->setFrame(selectedTab, 0);
  217. int cp = battleSpellsOnly ? myInt->localState->spellbookSettings.spellbookLastPageBattle : myInt->localState->spellbookSettings.spellbokLastPageAdvmap;
  218. // spellbook last page battle index is not reset after battle, so this needs to stay here
  219. vstd::abetween(cp, 0, std::max(0, pagesWithinCurrentTab() - 1));
  220. setCurrentPage(cp);
  221. computeSpellsPerArea();
  222. addUsedEvents(KEYBOARD);
  223. }
  224. CSpellWindow::~CSpellWindow()
  225. {
  226. }
  227. std::shared_ptr<IImage> CSpellWindow::createBigSpellBook()
  228. {
  229. std::shared_ptr<IImage> img = GH.renderHandler().loadImage(ImagePath::builtin("SpelBack"));
  230. Canvas canvas = Canvas(Point(800, 600));
  231. // edges
  232. canvas.draw(img, Point(0, 0), Rect(15, 38, 90, 45));
  233. canvas.draw(img, Point(0, 460), Rect(15, 400, 90, 141));
  234. canvas.draw(img, Point(705, 0), Rect(509, 38, 95, 45));
  235. canvas.draw(img, Point(705, 460), Rect(509, 400, 95, 141));
  236. // left / right
  237. Canvas tmp1 = Canvas(Point(90, 355 - 45));
  238. tmp1.draw(img, Point(0, 0), Rect(15, 38 + 45, 90, 355 - 45));
  239. canvas.drawScaled(tmp1, Point(0, 45), Point(90, 415));
  240. Canvas tmp2 = Canvas(Point(95, 355 - 45));
  241. tmp2.draw(img, Point(0, 0), Rect(509, 38 + 45, 95, 355 - 45));
  242. canvas.drawScaled(tmp2, Point(705, 45), Point(95, 415));
  243. // top / bottom
  244. Canvas tmp3 = Canvas(Point(409, 45));
  245. tmp3.draw(img, Point(0, 0), Rect(100, 38, 409, 45));
  246. canvas.drawScaled(tmp3, Point(90, 0), Point(615, 45));
  247. Canvas tmp4 = Canvas(Point(409, 141));
  248. tmp4.draw(img, Point(0, 0), Rect(100, 400, 409, 141));
  249. canvas.drawScaled(tmp4, Point(90, 460), Point(615, 141));
  250. // middle
  251. Canvas tmp5 = Canvas(Point(409, 141));
  252. tmp5.draw(img, Point(0, 0), Rect(100, 38 + 45, 509 - 15, 400 - 38));
  253. canvas.drawScaled(tmp5, Point(90, 45), Point(615, 415));
  254. return GH.renderHandler().createImage(canvas.getInternalSurface());
  255. }
  256. void CSpellWindow::fexitb()
  257. {
  258. (myInt->battleInt ? myInt->localState->spellbookSettings.spellbookLastTabBattle : myInt->localState->spellbookSettings.spellbookLastTabAdvmap) = selectedTab;
  259. (myInt->battleInt ? myInt->localState->spellbookSettings.spellbookLastPageBattle : myInt->localState->spellbookSettings.spellbokLastPageAdvmap) = currentPage;
  260. close();
  261. }
  262. void CSpellWindow::fadvSpellsb()
  263. {
  264. if(battleSpellsOnly == true)
  265. {
  266. turnPageRight();
  267. battleSpellsOnly = false;
  268. setCurrentPage(0);
  269. }
  270. computeSpellsPerArea();
  271. }
  272. void CSpellWindow::fbattleSpellsb()
  273. {
  274. if(battleSpellsOnly == false)
  275. {
  276. turnPageLeft();
  277. battleSpellsOnly = true;
  278. setCurrentPage(0);
  279. }
  280. computeSpellsPerArea();
  281. }
  282. void CSpellWindow::fmanaPtsb()
  283. {
  284. }
  285. void CSpellWindow::selectSchool(int school)
  286. {
  287. if(selectedTab != school)
  288. {
  289. if(selectedTab < school)
  290. turnPageLeft();
  291. else
  292. turnPageRight();
  293. selectedTab = school;
  294. schoolTab->setFrame(selectedTab, 0);
  295. setCurrentPage(0);
  296. }
  297. computeSpellsPerArea();
  298. }
  299. void CSpellWindow::fLcornerb()
  300. {
  301. if(currentPage>0)
  302. {
  303. turnPageLeft();
  304. setCurrentPage(currentPage - 1);
  305. }
  306. computeSpellsPerArea();
  307. }
  308. void CSpellWindow::fRcornerb()
  309. {
  310. if((currentPage + 1) < (pagesWithinCurrentTab()))
  311. {
  312. turnPageRight();
  313. setCurrentPage(currentPage + 1);
  314. }
  315. computeSpellsPerArea();
  316. }
  317. void CSpellWindow::show(Canvas & to)
  318. {
  319. statusBar->show(to);
  320. }
  321. void CSpellWindow::computeSpellsPerArea()
  322. {
  323. std::vector<const CSpell *> spellsCurSite;
  324. spellsCurSite.reserve(mySpells.size());
  325. for(const CSpell * spell : mySpells)
  326. {
  327. if(spell->isCombat() ^ !battleSpellsOnly
  328. && ((selectedTab == 4) || spell->school.at(SpellSchool(selectedTab)))
  329. )
  330. {
  331. spellsCurSite.push_back(spell);
  332. }
  333. }
  334. if(selectedTab == 4)
  335. {
  336. if(spellsCurSite.size() > spellsPerPage)
  337. {
  338. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + currentPage*spellsPerPage, spellsCurSite.end());
  339. if(spellsCurSite.size() > spellsPerPage)
  340. {
  341. spellsCurSite.erase(spellsCurSite.begin()+spellsPerPage, spellsCurSite.end());
  342. }
  343. }
  344. }
  345. else //selectedTab == 0, 1, 2 or 3
  346. {
  347. if(spellsCurSite.size() > spellsPerPage - 2)
  348. {
  349. if(currentPage == 0)
  350. {
  351. spellsCurSite.erase(spellsCurSite.begin()+spellsPerPage-2, spellsCurSite.end());
  352. }
  353. else
  354. {
  355. spellsCurSite = std::vector<const CSpell *>(spellsCurSite.begin() + (currentPage-1)*spellsPerPage + spellsPerPage-2, spellsCurSite.end());
  356. if(spellsCurSite.size() > spellsPerPage)
  357. {
  358. spellsCurSite.erase(spellsCurSite.begin()+spellsPerPage, spellsCurSite.end());
  359. }
  360. }
  361. }
  362. }
  363. //applying
  364. if(selectedTab == 4 || currentPage != 0)
  365. {
  366. for(size_t c=0; c<spellsPerPage; ++c)
  367. {
  368. if(c < spellsCurSite.size())
  369. {
  370. spellAreas[c]->setSpell(spellsCurSite[c]);
  371. }
  372. else
  373. {
  374. spellAreas[c]->setSpell(nullptr);
  375. }
  376. }
  377. }
  378. else
  379. {
  380. spellAreas[0]->setSpell(nullptr);
  381. spellAreas[1]->setSpell(nullptr);
  382. for(size_t c=0; c<spellsPerPage-2; ++c)
  383. {
  384. if(c < spellsCurSite.size())
  385. spellAreas[c+2]->setSpell(spellsCurSite[c]);
  386. else
  387. spellAreas[c+2]->setSpell(nullptr);
  388. }
  389. }
  390. redraw();
  391. }
  392. void CSpellWindow::setCurrentPage(int value)
  393. {
  394. currentPage = value;
  395. schoolPicture->visible = selectedTab!=4 && currentPage == 0;
  396. if(selectedTab != 4)
  397. schoolPicture->setFrame(selectedTab, 0);
  398. leftCorner->visible = currentPage != 0;
  399. rightCorner->visible = (currentPage+1) < pagesWithinCurrentTab();
  400. mana->setText(std::to_string(myHero->mana));//just in case, it will be possible to cast spell without closing book
  401. }
  402. void CSpellWindow::turnPageLeft()
  403. {
  404. if(settings["video"]["spellbookAnimation"].Bool() && !isBigSpellbook)
  405. CCS->videoh->openAndPlayVideo(VideoPath::builtin("PGTRNLFT.SMK"), pos.x+13, pos.y+15);
  406. }
  407. void CSpellWindow::turnPageRight()
  408. {
  409. if(settings["video"]["spellbookAnimation"].Bool() && !isBigSpellbook)
  410. CCS->videoh->openAndPlayVideo(VideoPath::builtin("PGTRNRGH.SMK"), pos.x+13, pos.y+15);
  411. }
  412. void CSpellWindow::keyPressed(EShortcut key)
  413. {
  414. switch(key)
  415. {
  416. case EShortcut::GLOBAL_RETURN:
  417. fexitb();
  418. break;
  419. case EShortcut::MOVE_LEFT:
  420. fLcornerb();
  421. break;
  422. case EShortcut::MOVE_RIGHT:
  423. fRcornerb();
  424. break;
  425. case EShortcut::MOVE_UP:
  426. case EShortcut::MOVE_DOWN:
  427. {
  428. bool down = key == EShortcut::MOVE_DOWN;
  429. static const int schoolsOrder[] = { 0, 3, 1, 2, 4 };
  430. int index = -1;
  431. while(schoolsOrder[++index] != selectedTab);
  432. index += (down ? 1 : -1);
  433. vstd::abetween<int>(index, 0, std::size(schoolsOrder) - 1);
  434. if(selectedTab != schoolsOrder[index])
  435. selectSchool(schoolsOrder[index]);
  436. break;
  437. }
  438. case EShortcut::SPELLBOOK_TAB_COMBAT:
  439. fbattleSpellsb();
  440. break;
  441. case EShortcut::SPELLBOOK_TAB_ADVENTURE:
  442. fadvSpellsb();
  443. break;
  444. }
  445. }
  446. int CSpellWindow::pagesWithinCurrentTab()
  447. {
  448. return battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab];
  449. }
  450. CSpellWindow::SpellArea::SpellArea(Rect pos, CSpellWindow * owner)
  451. {
  452. this->pos = pos;
  453. this->owner = owner;
  454. addUsedEvents(LCLICK | SHOW_POPUP | HOVER);
  455. schoolLevel = -1;
  456. mySpell = nullptr;
  457. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  458. image = std::make_shared<CAnimImage>(owner->spellIcons, 0, 0);
  459. image->visible = false;
  460. name = std::make_shared<CLabel>(39, 70, FONT_TINY, ETextAlignment::CENTER);
  461. level = std::make_shared<CLabel>(39, 82, FONT_TINY, ETextAlignment::CENTER);
  462. cost = std::make_shared<CLabel>(39, 94, FONT_TINY, ETextAlignment::CENTER);
  463. for(auto l : {name, level, cost})
  464. l->setAutoRedraw(false);
  465. }
  466. CSpellWindow::SpellArea::~SpellArea() = default;
  467. void CSpellWindow::SpellArea::clickPressed(const Point & cursorPosition)
  468. {
  469. if(mySpell)
  470. {
  471. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  472. if(spellCost > owner->myHero->mana) //insufficient mana
  473. {
  474. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[206]) % spellCost % owner->myHero->mana));
  475. return;
  476. }
  477. //anything that is not combat spell is adventure spell
  478. //this not an error in general to cast even creature ability with hero
  479. const bool combatSpell = mySpell->isCombat();
  480. if(combatSpell == mySpell->isAdventure())
  481. {
  482. logGlobal->error("Spell have invalid flags");
  483. return;
  484. }
  485. const bool inCombat = owner->myInt->battleInt != nullptr;
  486. const bool inCastle = owner->myInt->castleInt != nullptr;
  487. //battle spell on adv map or adventure map spell during combat => display infowindow, not cast
  488. if((combatSpell ^ inCombat) || inCastle)
  489. {
  490. std::vector<std::shared_ptr<CComponent>> hlp(1, std::make_shared<CComponent>(ComponentType::SPELL, mySpell->id));
  491. LOCPLINT->showInfoDialog(mySpell->getDescriptionTranslated(schoolLevel), hlp);
  492. }
  493. else if(combatSpell)
  494. {
  495. spells::detail::ProblemImpl problem;
  496. if(mySpell->canBeCast(problem, owner->myInt->battleInt->getBattle().get(), spells::Mode::HERO, owner->myHero))
  497. {
  498. owner->myInt->battleInt->castThisSpell(mySpell->id);
  499. owner->fexitb();
  500. }
  501. else
  502. {
  503. std::vector<std::string> texts;
  504. problem.getAll(texts);
  505. if(!texts.empty())
  506. LOCPLINT->showInfoDialog(texts.front());
  507. else
  508. LOCPLINT->showInfoDialog(CGI->generaltexth->translate("vcmi.adventureMap.spellUnknownProblem"));
  509. }
  510. }
  511. else //adventure spell
  512. {
  513. const CGHeroInstance * h = owner->myHero;
  514. GH.windows().popWindows(1);
  515. auto guard = vstd::makeScopeGuard([this]()
  516. {
  517. owner->myInt->localState->spellbookSettings.spellbookLastTabAdvmap = owner->selectedTab;
  518. owner->myInt->localState->spellbookSettings.spellbokLastPageAdvmap = owner->currentPage;
  519. });
  520. if(mySpell->getTargetType() == spells::AimType::LOCATION)
  521. adventureInt->enterCastingMode(mySpell);
  522. else if(mySpell->getTargetType() == spells::AimType::NO_TARGET)
  523. owner->myInt->cb->castSpell(h, mySpell->id);
  524. else
  525. logGlobal->error("Invalid spell target type");
  526. }
  527. }
  528. }
  529. void CSpellWindow::SpellArea::showPopupWindow(const Point & cursorPosition)
  530. {
  531. if(mySpell)
  532. {
  533. std::string dmgInfo;
  534. auto causedDmg = owner->myInt->cb->estimateSpellDamage(mySpell, owner->myHero);
  535. if(causedDmg == 0 || mySpell->id == SpellID::TITANS_LIGHTNING_BOLT) //Titan's Lightning Bolt already has damage info included
  536. dmgInfo.clear();
  537. else
  538. {
  539. dmgInfo = CGI->generaltexth->allTexts[343];
  540. boost::algorithm::replace_first(dmgInfo, "%d", std::to_string(causedDmg));
  541. }
  542. CRClickPopup::createAndPush(mySpell->getDescriptionTranslated(schoolLevel) + dmgInfo, std::make_shared<CComponent>(ComponentType::SPELL, mySpell->id));
  543. }
  544. }
  545. void CSpellWindow::SpellArea::hover(bool on)
  546. {
  547. if(mySpell)
  548. {
  549. if(on)
  550. owner->statusBar->write(boost::str(boost::format("%s (%s)") % mySpell->getNameTranslated() % CGI->generaltexth->allTexts[171+mySpell->getLevel()]));
  551. else
  552. owner->statusBar->clear();
  553. }
  554. }
  555. void CSpellWindow::SpellArea::setSpell(const CSpell * spell)
  556. {
  557. schoolBorder.reset();
  558. image->visible = false;
  559. name->setText("");
  560. level->setText("");
  561. cost->setText("");
  562. mySpell = spell;
  563. if(mySpell)
  564. {
  565. int32_t whichSchool = 0; //0 - air magic, 1 - fire magic, 2 - water magic, 3 - earth magic,
  566. schoolLevel = owner->myHero->getSpellSchoolLevel(mySpell, &whichSchool);
  567. auto spellCost = owner->myInt->cb->getSpellCost(mySpell, owner->myHero);
  568. image->setFrame(mySpell->id);
  569. image->visible = true;
  570. {
  571. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  572. schoolBorder = std::make_shared<CAnimImage>(owner->schoolBorders[owner->selectedTab >= 4 ? whichSchool : owner->selectedTab], schoolLevel);
  573. }
  574. ColorRGBA firstLineColor, secondLineColor;
  575. if(spellCost > owner->myHero->mana) //hero cannot cast this spell
  576. {
  577. firstLineColor = Colors::WHITE;
  578. secondLineColor = Colors::ORANGE;
  579. }
  580. else
  581. {
  582. firstLineColor = Colors::YELLOW;
  583. secondLineColor = Colors::WHITE;
  584. }
  585. name->color = firstLineColor;
  586. name->setText(mySpell->getNameTranslated());
  587. level->color = secondLineColor;
  588. if(schoolLevel > 0)
  589. {
  590. boost::format fmt("%s/%s");
  591. fmt % CGI->generaltexth->allTexts[171 + mySpell->getLevel()];
  592. fmt % CGI->generaltexth->levels[3+(schoolLevel-1)];//lines 4-6
  593. level->setText(fmt.str());
  594. }
  595. else
  596. level->setText(CGI->generaltexth->allTexts[171 + mySpell->getLevel()]);
  597. cost->color = secondLineColor;
  598. boost::format costfmt("%s: %d");
  599. costfmt % CGI->generaltexth->allTexts[387] % spellCost;
  600. cost->setText(costfmt.str());
  601. }
  602. }