CSpellWindow.cpp 22 KB

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