CSpellWindow.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. #include "CSpellWindow.h"
  2. #include "Graphics.h"
  3. #include "../hch/CDefHandler.h"
  4. #include "../hch/CObjectHandler.h"
  5. #include "../hch/CSpellHandler.h"
  6. #include "../hch/CGeneralTextHandler.h"
  7. #include "../hch/CVideoHandler.h"
  8. #include "CAdvmapInterface.h"
  9. #include "CBattleInterface.h"
  10. #include "CGameInfo.h"
  11. #include "SDL_Extensions.h"
  12. #include "CMessage.h"
  13. #include "CPlayerInterface.h"
  14. #include "../CCallback.h"
  15. #include <boost/bind.hpp>
  16. #include <sstream>
  17. /*
  18. * CSpellWindow.cpp, part of VCMI engine
  19. *
  20. * Authors: listed in file AUTHORS in main folder
  21. *
  22. * License: GNU General Public License v2.0 or later
  23. * Full text of license available in license.txt file, in main folder
  24. *
  25. */
  26. extern SDL_Surface * screen;
  27. extern SDL_Color tytulowy, zwykly, darkTitle;
  28. extern TTF_Font *GEOR16;
  29. SpellbookInteractiveArea::SpellbookInteractiveArea(const SDL_Rect & myRect, boost::function<void()> funcL, const std::string & textR, boost::function<void()> funcHon, boost::function<void()> funcHoff)
  30. {
  31. pos = myRect;
  32. onLeft = funcL;
  33. textOnRclick = textR;
  34. onHoverOn = funcHon;
  35. onHoverOff = funcHoff;
  36. }
  37. void SpellbookInteractiveArea::clickLeft(tribool down, bool previousState)
  38. {
  39. if(!down)
  40. {
  41. onLeft();
  42. }
  43. }
  44. void SpellbookInteractiveArea::clickRight(tribool down, bool previousState)
  45. {
  46. LOCPLINT->adventureInt->handleRightClick(textOnRclick, down, this);
  47. }
  48. void SpellbookInteractiveArea::hover(bool on)
  49. {
  50. //Hoverable::hover(on);
  51. if(on)
  52. {
  53. onHoverOn();
  54. }
  55. else
  56. {
  57. onHoverOff();
  58. }
  59. }
  60. void SpellbookInteractiveArea::activate()
  61. {
  62. activateLClick();
  63. activateRClick();
  64. activateHover();
  65. }
  66. void SpellbookInteractiveArea::deactivate()
  67. {
  68. deactivateLClick();
  69. deactivateRClick();
  70. deactivateHover();
  71. }
  72. CSpellWindow::CSpellWindow(const SDL_Rect & myRect, const CGHeroInstance * _myHero, bool openOnBattleSpells):
  73. battleSpellsOnly(openOnBattleSpells),
  74. selectedTab(4),
  75. spellSite(0),
  76. myHero(_myHero)
  77. {
  78. //initializing castable spells
  79. for(ui32 v=0; v<CGI->spellh->spells.size(); ++v)
  80. {
  81. if( !CGI->spellh->spells[v].creatureAbility && myHero->canCastThisSpell(&CGI->spellh->spells[v]) )
  82. mySpells.insert(v);
  83. }
  84. //initializing schools' levels
  85. for(int b=0; b<4; ++b) schoolLvls[b] = 0;
  86. for(size_t b=0; b<myHero->secSkills.size(); ++b)
  87. {
  88. switch(myHero->secSkills[b].first)
  89. {
  90. case 14: //fire magic
  91. schoolLvls[1] = myHero->secSkills[b].second;
  92. break;
  93. case 15: //air magic
  94. schoolLvls[0] = myHero->secSkills[b].second;
  95. break;
  96. case 16: //water magic
  97. schoolLvls[2] = myHero->secSkills[b].second;
  98. break;
  99. case 17: //earth magic
  100. schoolLvls[3] = myHero->secSkills[b].second;
  101. break;
  102. }
  103. }
  104. //initializing sizes of spellbook's parts
  105. for(int b=0; b<5; ++b)
  106. sitesPerTabAdv[b] = 0;
  107. for(int b=0; b<5; ++b)
  108. sitesPerTabBattle[b] = 0;
  109. for(std::set<ui32>::const_iterator g = mySpells.begin(); g!=mySpells.end(); ++g)
  110. {
  111. if(CGI->spellh->spells[*g].combatSpell)
  112. {
  113. ++(sitesPerTabBattle[4]);
  114. }
  115. else
  116. {
  117. ++(sitesPerTabAdv[4]);
  118. }
  119. if(CGI->spellh->spells[*g].air)
  120. {
  121. if(CGI->spellh->spells[*g].combatSpell)
  122. {
  123. ++(sitesPerTabBattle[0]);
  124. }
  125. else
  126. {
  127. ++(sitesPerTabAdv[0]);
  128. }
  129. }
  130. if(CGI->spellh->spells[*g].fire)
  131. {
  132. if(CGI->spellh->spells[*g].combatSpell)
  133. {
  134. ++(sitesPerTabBattle[1]);
  135. }
  136. else
  137. {
  138. ++(sitesPerTabAdv[1]);
  139. }
  140. }
  141. if(CGI->spellh->spells[*g].water)
  142. {
  143. if(CGI->spellh->spells[*g].combatSpell)
  144. {
  145. ++(sitesPerTabBattle[2]);
  146. }
  147. else
  148. {
  149. ++(sitesPerTabAdv[2]);
  150. }
  151. }
  152. if(CGI->spellh->spells[*g].earth)
  153. {
  154. if(CGI->spellh->spells[*g].combatSpell)
  155. {
  156. ++(sitesPerTabBattle[3]);
  157. }
  158. else
  159. {
  160. ++(sitesPerTabAdv[3]);
  161. }
  162. }
  163. }
  164. if(sitesPerTabAdv[4] % 12 == 0)
  165. sitesPerTabAdv[4]/=12;
  166. else
  167. sitesPerTabAdv[4] = sitesPerTabAdv[4]/12 + 1;
  168. for(int v=0; v<4; ++v)
  169. {
  170. if(sitesPerTabAdv[v] <= 10)
  171. sitesPerTabAdv[v] = 1;
  172. else
  173. {
  174. if((sitesPerTabAdv[v] - 10) % 12 == 0)
  175. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 1;
  176. else
  177. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 2;
  178. }
  179. }
  180. if(sitesPerTabBattle[4] % 12 == 0)
  181. sitesPerTabBattle[4]/=12;
  182. else
  183. sitesPerTabBattle[4] = sitesPerTabBattle[4]/12 + 1;
  184. for(int v=0; v<4; ++v)
  185. {
  186. if(sitesPerTabBattle[v] <= 10)
  187. sitesPerTabBattle[v] = 1;
  188. else
  189. {
  190. if((sitesPerTabBattle[v] - 10) % 12 == 0)
  191. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 1;
  192. else
  193. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 2;
  194. }
  195. }
  196. //numbers of spell pages computed
  197. pos = myRect;
  198. background = BitmapHandler::loadBitmap("SpelBack.bmp");
  199. graphics->blueToPlayersAdv(background, myHero->tempOwner);
  200. std::ostringstream mana;
  201. mana<<myHero->mana;
  202. CSDL_Ext::printAtMiddle(mana.str(), 434, 425, GEOR16, tytulowy, background);
  203. leftCorner = BitmapHandler::loadBitmap("SpelTrnL.bmp", true);
  204. rightCorner = BitmapHandler::loadBitmap("SpelTrnR.bmp", true);
  205. spells = CDefHandler::giveDef("Spells.def");
  206. spellTab = CDefHandler::giveDef("SpelTab.def");
  207. schools = CDefHandler::giveDef("Schools.def");
  208. schoolBorders[0] = CDefHandler::giveDef("SplevA.def");
  209. schoolBorders[1] = CDefHandler::giveDef("SplevF.def");
  210. schoolBorders[2] = CDefHandler::giveDef("SplevW.def");
  211. schoolBorders[3] = CDefHandler::giveDef("SplevE.def");
  212. statusBar = new CStatusBar(7 + pos.x, 569 + pos.y, "Spelroll.bmp");
  213. SDL_Rect temp_rect = genRect(45, 35, 479 + pos.x, 405 + pos.y);
  214. exitBtn = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fexitb, this), CGI->generaltexth->zelp[460].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[460].first)), boost::bind(&CStatusBar::clear, statusBar));
  215. temp_rect = genRect(45, 35, 221 + pos.x, 405 + pos.y);
  216. battleSpells = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fbattleSpellsb, this), CGI->generaltexth->zelp[453].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[453].first)), boost::bind(&CStatusBar::clear, statusBar));
  217. temp_rect = genRect(45, 35, 355 + pos.x, 405 + pos.y);
  218. adventureSpells = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fadvSpellsb, this), CGI->generaltexth->zelp[452].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[452].first)), boost::bind(&CStatusBar::clear, statusBar));
  219. temp_rect = genRect(45, 35, 418 + pos.x, 405 + pos.y);
  220. manaPoints = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fmanaPtsb, this), CGI->generaltexth->zelp[459].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[459].first)), boost::bind(&CStatusBar::clear, statusBar));
  221. temp_rect = genRect(36, 56, 549 + pos.x, 94 + pos.y);
  222. selectSpellsA = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsAb, this), CGI->generaltexth->zelp[454].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[454].first)), boost::bind(&CStatusBar::clear, statusBar));
  223. temp_rect = genRect(36, 56, 549 + pos.x, 151 + pos.y);
  224. selectSpellsE = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsEb, this), CGI->generaltexth->zelp[457].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[457].first)), boost::bind(&CStatusBar::clear, statusBar));
  225. temp_rect = genRect(36, 56, 549 + pos.x, 210 + pos.y);
  226. selectSpellsF = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsFb, this), CGI->generaltexth->zelp[455].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[455].first)), boost::bind(&CStatusBar::clear, statusBar));
  227. temp_rect = genRect(36, 56, 549 + pos.x, 270 + pos.y);
  228. selectSpellsW = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsWb, this), CGI->generaltexth->zelp[456].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[456].first)), boost::bind(&CStatusBar::clear, statusBar));
  229. temp_rect = genRect(36, 56, 549 + pos.x, 330 + pos.y);
  230. selectSpellsAll = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsAllb, this), CGI->generaltexth->zelp[458].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[458].first)), boost::bind(&CStatusBar::clear, statusBar));
  231. temp_rect = genRect(leftCorner->h, leftCorner->w, 97 + pos.x, 77 + pos.y);
  232. lCorner = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fLcornerb, this), CGI->generaltexth->zelp[450].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[450].first)), boost::bind(&CStatusBar::clear, statusBar));
  233. temp_rect = genRect(rightCorner->h, rightCorner->w, 487 + pos.x, 72 + pos.y);
  234. rCorner = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fRcornerb, this), CGI->generaltexth->zelp[451].second, boost::bind(&CStatusBar::print, statusBar, (CGI->generaltexth->zelp[451].first)), boost::bind(&CStatusBar::clear, statusBar));
  235. //areas for spells
  236. int xpos = 117 + pos.x, ypos = 90 + pos.y;
  237. for(int v=0; v<12; ++v)
  238. {
  239. temp_rect = genRect(65, 78, xpos, ypos);
  240. spellAreas[v] = new SpellArea(temp_rect, this);
  241. if(v == 5) //to right page
  242. {
  243. xpos = 336 + pos.x; ypos = 90 + pos.y;
  244. }
  245. else
  246. {
  247. if(v%2 == 0)
  248. {
  249. xpos+=85;
  250. }
  251. else
  252. {
  253. xpos -= 85; ypos+=97;
  254. }
  255. }
  256. }
  257. computeSpellsPerArea();
  258. }
  259. CSpellWindow::~CSpellWindow()
  260. {
  261. SDL_FreeSurface(background);
  262. SDL_FreeSurface(leftCorner);
  263. SDL_FreeSurface(rightCorner);
  264. delete spells;
  265. delete spellTab;
  266. delete schools;
  267. for(int b=0; b<4; ++b)
  268. delete schoolBorders[b];
  269. delete exitBtn;
  270. delete battleSpells;
  271. delete adventureSpells;
  272. delete manaPoints;
  273. delete statusBar;
  274. delete selectSpellsA;
  275. delete selectSpellsE;
  276. delete selectSpellsF;
  277. delete selectSpellsW;
  278. delete selectSpellsAll;
  279. delete lCorner;
  280. delete rCorner;
  281. for(int g=0; g<12; ++g)
  282. {
  283. delete spellAreas[g];
  284. }
  285. }
  286. void CSpellWindow::fexitb()
  287. {
  288. GH.popIntTotally(this);
  289. }
  290. void CSpellWindow::fadvSpellsb()
  291. {
  292. if (battleSpellsOnly == true) {
  293. turnPageRight();
  294. battleSpellsOnly = false;
  295. spellSite = 0;
  296. }
  297. computeSpellsPerArea();
  298. }
  299. void CSpellWindow::fbattleSpellsb()
  300. {
  301. if (battleSpellsOnly == false) {
  302. turnPageLeft();
  303. battleSpellsOnly = true;
  304. spellSite = 0;
  305. }
  306. computeSpellsPerArea();
  307. }
  308. void CSpellWindow::fmanaPtsb()
  309. {
  310. }
  311. void CSpellWindow::fspellsAb()
  312. {
  313. if (selectedTab != 0) {
  314. turnPageRight();
  315. selectedTab = 0;
  316. spellSite = 0;
  317. }
  318. computeSpellsPerArea();
  319. }
  320. void CSpellWindow::fspellsEb()
  321. {
  322. if (selectedTab != 3) {
  323. turnPageRight();
  324. selectedTab = 3;
  325. spellSite = 0;
  326. }
  327. computeSpellsPerArea();
  328. }
  329. void CSpellWindow::fspellsFb()
  330. {
  331. if (selectedTab != 1) {
  332. turnPageRight();
  333. selectedTab = 1;
  334. spellSite = 0;
  335. }
  336. computeSpellsPerArea();
  337. }
  338. void CSpellWindow::fspellsWb()
  339. {
  340. if (selectedTab != 2) {
  341. turnPageRight();
  342. selectedTab = 2;
  343. spellSite = 0;
  344. }
  345. computeSpellsPerArea();
  346. }
  347. void CSpellWindow::fspellsAllb()
  348. {
  349. if (selectedTab != 4) {
  350. turnPageRight();
  351. selectedTab = 4;
  352. spellSite = 0;
  353. }
  354. computeSpellsPerArea();
  355. }
  356. void CSpellWindow::fLcornerb()
  357. {
  358. if(spellSite>0) {
  359. turnPageLeft();
  360. --spellSite;
  361. }
  362. computeSpellsPerArea();
  363. }
  364. void CSpellWindow::fRcornerb()
  365. {
  366. if((spellSite + 1) < (battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab])) {
  367. turnPageRight();
  368. ++spellSite;
  369. }
  370. computeSpellsPerArea();
  371. }
  372. void CSpellWindow::show(SDL_Surface *to)
  373. {
  374. SDL_BlitSurface(background, NULL, to, &pos);
  375. blitAt(spellTab->ourImages[selectedTab].bitmap, 524 + pos.x, 88 + pos.y, to);
  376. statusBar->show(to);
  377. //printing school images
  378. if(selectedTab!=4 && spellSite == 0)
  379. {
  380. blitAt(schools->ourImages[selectedTab].bitmap, 117 + pos.x, 74 + pos.y, to);
  381. }
  382. //printing corners
  383. if(spellSite!=0)
  384. {
  385. blitAt(leftCorner, lCorner->pos.x, lCorner->pos.y, to);
  386. }
  387. if((spellSite+1) < (battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab]) )
  388. {
  389. blitAt(rightCorner, rCorner->pos.x, rCorner->pos.y, to);
  390. }
  391. //printing spell info
  392. for(int b=0; b<12; ++b)
  393. {
  394. if(spellAreas[b]->mySpell == -1)
  395. continue;
  396. const CSpell * spell = &CGI->spellh->spells[spellAreas[b]->mySpell];
  397. //int b2 = -1; //TODO use me
  398. blitAt(spells->ourImages[spellAreas[b]->mySpell].bitmap, spellAreas[b]->pos.x, spellAreas[b]->pos.y, to);
  399. Uint8 bestSchool = -1,
  400. bestslvl = myHero->getSpellSchoolLevel( spell );
  401. if(spell->air)
  402. bestSchool = 0;
  403. if(spell->fire)
  404. bestSchool = 1;
  405. if(spell->water)
  406. bestSchool = 2;
  407. if(spell->earth)
  408. bestSchool = 3;
  409. //printing border (indicates level of magic school)
  410. blitAt(schoolBorders[bestSchool]->ourImages[bestslvl].bitmap, spellAreas[b]->pos.x, spellAreas[b]->pos.y, to);
  411. SDL_Color firstLineColor, secondLineColor;
  412. if(LOCPLINT->cb->getSpellCost(spell, myHero) > myHero->mana) //hero cannot cast this spell
  413. {
  414. firstLineColor = zwykly;
  415. secondLineColor = darkTitle;
  416. }
  417. else
  418. {
  419. firstLineColor = tytulowy;
  420. secondLineColor = zwykly;
  421. }
  422. //printing spell's name
  423. CSDL_Ext::printAtMiddle(spell->name, spellAreas[b]->pos.x + 39, spellAreas[b]->pos.y + 70, FONT_TIMES, firstLineColor, to);
  424. //printing lvl
  425. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[171 + spell->level], spellAreas[b]->pos.x + 39, spellAreas[b]->pos.y + 82, FONT_TIMES, secondLineColor, to);
  426. //printing cost
  427. std::ostringstream ss;
  428. ss<<CGI->generaltexth->allTexts[387]<<": "<<LOCPLINT->cb->getSpellCost(spell, myHero);
  429. CSDL_Ext::printAtMiddle(ss.str(), spellAreas[b]->pos.x + 39, spellAreas[b]->pos.y + 94, FONT_TIMES, secondLineColor, to);
  430. }
  431. }
  432. class SpellbookSpellSorter
  433. {
  434. public:
  435. bool operator()(const int & a, const int & b)
  436. {
  437. CSpell A = CGI->spellh->spells[a];
  438. CSpell B = CGI->spellh->spells[b];
  439. if(A.level<B.level)
  440. return true;
  441. if(A.level>B.level)
  442. return false;
  443. if(A.air && !B.air)
  444. return true;
  445. if(!A.air && B.air)
  446. return false;
  447. if(A.fire && !B.fire)
  448. return true;
  449. if(!A.fire && B.fire)
  450. return false;
  451. if(A.water && !B.water)
  452. return true;
  453. if(!A.water && B.water)
  454. return false;
  455. if(A.earth && !B.earth)
  456. return true;
  457. if(!A.earth && B.earth)
  458. return false;
  459. return A.name < B.name;
  460. }
  461. } spellsorter;
  462. void CSpellWindow::computeSpellsPerArea()
  463. {
  464. std::vector<ui32> spellsCurSite;
  465. for(std::set<ui32>::const_iterator it = mySpells.begin(); it != mySpells.end(); ++it)
  466. {
  467. if(CGI->spellh->spells[*it].combatSpell ^ !battleSpellsOnly
  468. && ((CGI->spellh->spells[*it].air && selectedTab == 0) ||
  469. (CGI->spellh->spells[*it].fire && selectedTab == 1) ||
  470. (CGI->spellh->spells[*it].water && selectedTab == 2) ||
  471. (CGI->spellh->spells[*it].earth && selectedTab == 3) ||
  472. selectedTab == 4 )
  473. )
  474. {
  475. spellsCurSite.push_back(*it);
  476. }
  477. }
  478. std::sort(spellsCurSite.begin(), spellsCurSite.end(), spellsorter);
  479. if(selectedTab == 4)
  480. {
  481. if(spellsCurSite.size() > 12)
  482. {
  483. spellsCurSite = std::vector<ui32>(spellsCurSite.begin() + spellSite*12, spellsCurSite.end());
  484. if(spellsCurSite.size() > 12)
  485. {
  486. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  487. }
  488. }
  489. }
  490. else //selectedTab == 0, 1, 2 or 3
  491. {
  492. if(spellsCurSite.size() > 10)
  493. {
  494. if(spellSite == 0)
  495. {
  496. spellsCurSite.erase(spellsCurSite.begin()+10, spellsCurSite.end());
  497. }
  498. else
  499. {
  500. spellsCurSite = std::vector<ui32>(spellsCurSite.begin() + (spellSite-1)*12 + 10, spellsCurSite.end());
  501. if(spellsCurSite.size() > 12)
  502. {
  503. spellsCurSite.erase(spellsCurSite.begin()+12, spellsCurSite.end());
  504. }
  505. }
  506. }
  507. }
  508. //applying
  509. if(selectedTab == 4 || spellSite != 0)
  510. {
  511. for(size_t c=0; c<12; ++c)
  512. {
  513. if(c<spellsCurSite.size())
  514. {
  515. spellAreas[c]->mySpell = spellsCurSite[c];
  516. }
  517. else
  518. {
  519. spellAreas[c]->mySpell = -1;
  520. }
  521. }
  522. }
  523. else
  524. {
  525. spellAreas[0]->mySpell = -1;
  526. spellAreas[1]->mySpell = -1;
  527. for(size_t c=0; c<10; ++c)
  528. {
  529. if(c<spellsCurSite.size())
  530. spellAreas[c+2]->mySpell = spellsCurSite[c];
  531. else
  532. spellAreas[c+2]->mySpell = -1;
  533. }
  534. }
  535. }
  536. void CSpellWindow::activate()
  537. {
  538. activateKeys();
  539. exitBtn->activate();
  540. battleSpells->activate();
  541. adventureSpells->activate();
  542. manaPoints->activate();
  543. selectSpellsA->activate();
  544. selectSpellsE->activate();
  545. selectSpellsF->activate();
  546. selectSpellsW->activate();
  547. selectSpellsAll->activate();
  548. lCorner->activate();
  549. rCorner->activate();
  550. for(int g=0; g<12; ++g)
  551. {
  552. spellAreas[g]->activate();
  553. }
  554. }
  555. void CSpellWindow::deactivate()
  556. {
  557. deactivateKeys();
  558. exitBtn->deactivate();
  559. battleSpells->deactivate();
  560. adventureSpells->deactivate();
  561. manaPoints->deactivate();
  562. selectSpellsA->deactivate();
  563. selectSpellsE->deactivate();
  564. selectSpellsF->deactivate();
  565. selectSpellsW->deactivate();
  566. selectSpellsAll->deactivate();
  567. lCorner->deactivate();
  568. rCorner->deactivate();
  569. for(int g=0; g<12; ++g)
  570. {
  571. spellAreas[g]->deactivate();
  572. }
  573. }
  574. void CSpellWindow::turnPageLeft()
  575. {
  576. CGI->videoh->openAndPlayVideo("PGTRNLFT.SMK", pos.x+13, pos.y+15, screen);
  577. }
  578. void CSpellWindow::turnPageRight()
  579. {
  580. CGI->videoh->openAndPlayVideo("PGTRNRGH.SMK", pos.x+13, pos.y+15, screen);
  581. }
  582. void CSpellWindow::keyPressed(const SDL_KeyboardEvent & key)
  583. {
  584. if(key.keysym.sym == SDLK_ESCAPE || key.keysym.sym == SDLK_RETURN)
  585. fexitb();
  586. }
  587. CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
  588. {
  589. this->pos = pos;
  590. this->owner = owner;
  591. }
  592. void CSpellWindow::SpellArea::clickLeft(tribool down, bool previousState)
  593. {
  594. if(!down && mySpell!=-1)
  595. {
  596. //we will cast a spell
  597. if(LOCPLINT->battleInt && LOCPLINT->cb->battleCanCastSpell() && LOCPLINT->cb->getSpellCost(&CGI->spellh->spells[mySpell], owner->myHero) <= owner->myHero->mana) //if battle window is open
  598. {
  599. LOCPLINT->battleInt->castThisSpell(mySpell);
  600. owner->fexitb();
  601. }
  602. }
  603. }
  604. void CSpellWindow::SpellArea::clickRight(tribool down, bool previousState)
  605. {
  606. if(down && mySpell != -1)
  607. {
  608. SDL_Surface *spellBox = CMessage::drawBoxTextBitmapSub(
  609. LOCPLINT->playerID,
  610. CGI->spellh->spells[mySpell].descriptions[0], this->owner->spells->ourImages[mySpell].bitmap,
  611. CGI->spellh->spells[mySpell].name,30,30);
  612. CInfoPopup *vinya = new CInfoPopup(spellBox, true);
  613. GH.pushInt(vinya);
  614. }
  615. }
  616. void CSpellWindow::SpellArea::hover(bool on)
  617. {
  618. //Hoverable::hover(on);
  619. if(mySpell != -1)
  620. {
  621. if(on)
  622. {
  623. std::ostringstream ss;
  624. ss<<CGI->spellh->spells[mySpell].name<<" ("<<CGI->generaltexth->allTexts[171+CGI->spellh->spells[mySpell].level]<<")";
  625. owner->statusBar->print(ss.str());
  626. }
  627. else
  628. {
  629. owner->statusBar->clear();
  630. }
  631. }
  632. }
  633. void CSpellWindow::SpellArea::activate()
  634. {
  635. activateLClick();
  636. activateRClick();
  637. activateHover();
  638. }
  639. void CSpellWindow::SpellArea::deactivate()
  640. {
  641. deactivateLClick();
  642. deactivateRClick();
  643. deactivateHover();
  644. }