CSpellWindow.cpp 21 KB

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