CSpellWindow.cpp 20 KB

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