CSpellWindow.cpp 20 KB

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