CSpellWindow.cpp 21 KB

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