CSpellWindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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/CPreGameTextHandler.h"
  7. #include "../CAdvmapInterface.h"
  8. #include "../CGameInfo.h"
  9. #include "../SDL_Extensions.h"
  10. #include <boost/bind.hpp>
  11. #include <sstream>
  12. extern SDL_Surface * screen;
  13. extern SDL_Color tytulowy, zwykly ;
  14. extern TTF_Font *GEOR16;
  15. SpellbookInteractiveArea::SpellbookInteractiveArea(SDL_Rect & myRect, boost::function<void()> funcL, std::string textR, boost::function<void()> funcHon, boost::function<void()> funcHoff)
  16. {
  17. pos = myRect;
  18. onLeft = funcL;
  19. textOnRclick = textR;
  20. onHoverOn = funcHon;
  21. onHoverOff = funcHoff;
  22. }
  23. void SpellbookInteractiveArea::clickLeft(boost::logic::tribool down)
  24. {
  25. if(!down)
  26. {
  27. onLeft();
  28. }
  29. }
  30. void SpellbookInteractiveArea::clickRight(boost::logic::tribool down)
  31. {
  32. LOCPLINT->adventureInt->handleRightClick(textOnRclick, down, this);
  33. }
  34. void SpellbookInteractiveArea::hover(bool on)
  35. {
  36. Hoverable::hover(on);
  37. if(on)
  38. {
  39. onHoverOn();
  40. }
  41. else
  42. {
  43. onHoverOff();
  44. }
  45. }
  46. void SpellbookInteractiveArea::activate()
  47. {
  48. ClickableL::activate();
  49. ClickableR::activate();
  50. Hoverable::activate();
  51. }
  52. void SpellbookInteractiveArea::deactivate()
  53. {
  54. ClickableL::deactivate();
  55. ClickableR::deactivate();
  56. Hoverable::deactivate();
  57. }
  58. CSpellWindow::CSpellWindow(const SDL_Rect & myRect, const CGHeroInstance * myHero): selectedTab(4), spellSite(0), battleSpellsOnly(true)
  59. {
  60. //for testing only
  61. for(ui32 v=0; v<CGI->spellh->spells.size(); ++v)
  62. {
  63. ((CGHeroInstance*)(myHero))->spells.insert(v);
  64. }
  65. //initializing sizes of spellbook's parts
  66. for(int b=0; b<5; ++b)
  67. sitesPerTabAdv[b] = 0;
  68. for(int b=0; b<5; ++b)
  69. sitesPerTabBattle[b] = 0;
  70. for(std::set<ui32>::const_iterator g = myHero->spells.begin(); g!=myHero->spells.end(); ++g)
  71. {
  72. if(CGI->spellh->spells[*g].creatureAbility)
  73. continue; //currently we don't want tu put here creature abilities
  74. if(CGI->spellh->spells[*g].air)
  75. {
  76. if(CGI->spellh->spells[*g].combatSpell)
  77. {
  78. ++(sitesPerTabBattle[0]);
  79. ++(sitesPerTabBattle[4]);
  80. }
  81. else
  82. {
  83. ++(sitesPerTabAdv[0]);
  84. ++(sitesPerTabAdv[4]);
  85. }
  86. }
  87. if(CGI->spellh->spells[*g].fire)
  88. {
  89. if(CGI->spellh->spells[*g].combatSpell)
  90. {
  91. ++(sitesPerTabBattle[1]);
  92. ++(sitesPerTabBattle[4]);
  93. }
  94. else
  95. {
  96. ++(sitesPerTabAdv[1]);
  97. ++(sitesPerTabAdv[4]);
  98. }
  99. }
  100. if(CGI->spellh->spells[*g].water)
  101. {
  102. if(CGI->spellh->spells[*g].combatSpell)
  103. {
  104. ++(sitesPerTabBattle[2]);
  105. ++(sitesPerTabBattle[4]);
  106. }
  107. else
  108. {
  109. ++(sitesPerTabAdv[2]);
  110. ++(sitesPerTabAdv[4]);
  111. }
  112. }
  113. if(CGI->spellh->spells[*g].earth)
  114. {
  115. if(CGI->spellh->spells[*g].combatSpell)
  116. {
  117. ++(sitesPerTabBattle[3]);
  118. ++(sitesPerTabBattle[4]);
  119. }
  120. else
  121. {
  122. ++(sitesPerTabAdv[3]);
  123. ++(sitesPerTabAdv[4]);
  124. }
  125. }
  126. }
  127. if(sitesPerTabAdv[4] % 12 == 0)
  128. sitesPerTabAdv[4]/=12;
  129. else
  130. sitesPerTabAdv[4] = sitesPerTabAdv[4]/12 + 1;
  131. for(int v=0; v<4; ++v)
  132. {
  133. if(sitesPerTabAdv[v] <= 10)
  134. sitesPerTabAdv[v] = 1;
  135. else
  136. {
  137. if((sitesPerTabAdv[v] - 10) % 12 == 0)
  138. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 1;
  139. else
  140. sitesPerTabAdv[v] = (sitesPerTabAdv[v] - 10) / 12 + 2;
  141. }
  142. }
  143. if(sitesPerTabBattle[4] % 12 == 0)
  144. sitesPerTabBattle[4]/=12;
  145. else
  146. sitesPerTabBattle[4] = sitesPerTabBattle[4]/12 + 1;
  147. for(int v=0; v<4; ++v)
  148. {
  149. if(sitesPerTabBattle[v] <= 10)
  150. sitesPerTabBattle[v] = 1;
  151. else
  152. {
  153. if((sitesPerTabBattle[v] - 10) % 12 == 0)
  154. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 1;
  155. else
  156. sitesPerTabBattle[v] = (sitesPerTabBattle[v] - 10) / 12 + 2;
  157. }
  158. }
  159. //numbers of spell pages computed
  160. pos = myRect;
  161. background = BitmapHandler::loadBitmap("SpelBack.bmp");
  162. graphics->blueToPlayersAdv(background, myHero->tempOwner);
  163. std::stringstream mana;
  164. mana<<myHero->mana;
  165. CSDL_Ext::printAtMiddle(mana.str(), 434, 425, GEOR16, tytulowy, background);
  166. leftCorner = BitmapHandler::loadBitmap("SpelTrnL.bmp", true);
  167. rightCorner = BitmapHandler::loadBitmap("SpelTrnR.bmp", true);
  168. spells = CDefHandler::giveDef("Spells.def");
  169. spellTab = CDefHandler::giveDef("SpelTab.def");
  170. schools = CDefHandler::giveDef("Schools.def");
  171. schoolW = CDefHandler::giveDef("SplevW.def");
  172. schoolE = CDefHandler::giveDef("SplevE.def");
  173. schoolF = CDefHandler::giveDef("SplevF.def");
  174. schoolA = CDefHandler::giveDef("SplevA.def");
  175. statusBar = new CStatusBar(97, 571, "Spelroll.bmp");
  176. SDL_Rect temp_rect = genRect(45, 35, 569, 407);
  177. exitBtn = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fexitb, this), CGI->preth->zelp[460].second, boost::bind(&CStatusBar::print, statusBar, (CGI->preth->zelp[460].first)), boost::bind(&CStatusBar::clear, statusBar));
  178. temp_rect = genRect(45, 35, 311, 407);
  179. battleSpells = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fbattleSpellsb, this), CGI->preth->zelp[453].second, boost::bind(&CStatusBar::print, statusBar, (CGI->preth->zelp[453].first)), boost::bind(&CStatusBar::clear, statusBar));
  180. temp_rect = genRect(45, 35, 445, 407);
  181. adventureSpells = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fadvSpellsb, this), CGI->preth->zelp[452].second, boost::bind(&CStatusBar::print, statusBar, (CGI->preth->zelp[452].first)), boost::bind(&CStatusBar::clear, statusBar));
  182. temp_rect = genRect(45, 35, 508, 407);
  183. manaPoints = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fmanaPtsb, this), CGI->preth->zelp[459].second, boost::bind(&CStatusBar::print, statusBar, (CGI->preth->zelp[459].first)), boost::bind(&CStatusBar::clear, statusBar));
  184. temp_rect = genRect(36, 56, 639, 96);
  185. selectSpellsA = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsAb, this), CGI->preth->zelp[454].second, boost::bind(&CStatusBar::print, statusBar, (CGI->preth->zelp[454].first)), boost::bind(&CStatusBar::clear, statusBar));
  186. temp_rect = genRect(36, 56, 639, 153);
  187. selectSpellsE = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsEb, this), CGI->preth->zelp[457].second, boost::bind(&CStatusBar::print, statusBar, (CGI->preth->zelp[457].first)), boost::bind(&CStatusBar::clear, statusBar));
  188. temp_rect = genRect(36, 56, 639, 212);
  189. selectSpellsF = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsFb, this), CGI->preth->zelp[455].second, boost::bind(&CStatusBar::print, statusBar, (CGI->preth->zelp[455].first)), boost::bind(&CStatusBar::clear, statusBar));
  190. temp_rect = genRect(36, 56, 639, 272);
  191. selectSpellsW = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsWb, this), CGI->preth->zelp[456].second, boost::bind(&CStatusBar::print, statusBar, (CGI->preth->zelp[456].first)), boost::bind(&CStatusBar::clear, statusBar));
  192. temp_rect = genRect(36, 56, 639, 332);
  193. selectSpellsAll = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fspellsAllb, this), CGI->preth->zelp[458].second, boost::bind(&CStatusBar::print, statusBar, (CGI->preth->zelp[458].first)), boost::bind(&CStatusBar::clear, statusBar));
  194. temp_rect = genRect(leftCorner->h, leftCorner->w, 187, 79);
  195. lCorner = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fLcornerb, this), CGI->preth->zelp[450].second, boost::bind(&CStatusBar::print, statusBar, (CGI->preth->zelp[450].first)), boost::bind(&CStatusBar::clear, statusBar));
  196. temp_rect = genRect(rightCorner->h, rightCorner->w, 577, 76);
  197. rCorner = new SpellbookInteractiveArea(temp_rect, boost::bind(&CSpellWindow::fRcornerb, this), CGI->preth->zelp[451].second, boost::bind(&CStatusBar::print, statusBar, (CGI->preth->zelp[451].first)), boost::bind(&CStatusBar::clear, statusBar));
  198. }
  199. CSpellWindow::~CSpellWindow()
  200. {
  201. SDL_FreeSurface(background);
  202. SDL_FreeSurface(leftCorner);
  203. SDL_FreeSurface(rightCorner);
  204. delete spells;
  205. delete spellTab;
  206. delete schools;
  207. delete schoolW;
  208. delete schoolE;
  209. delete schoolF;
  210. delete schoolA;
  211. delete exitBtn;
  212. delete battleSpells;
  213. delete adventureSpells;
  214. delete manaPoints;
  215. delete statusBar;
  216. delete selectSpellsA;
  217. delete selectSpellsE;
  218. delete selectSpellsF;
  219. delete selectSpellsW;
  220. delete selectSpellsAll;
  221. delete lCorner;
  222. delete rCorner;
  223. }
  224. void CSpellWindow::fexitb()
  225. {
  226. deactivate();
  227. for(int g=0; g<LOCPLINT->objsToBlit.size(); ++g)
  228. {
  229. if(dynamic_cast<CSpellWindow*>(LOCPLINT->objsToBlit[g]))
  230. {
  231. LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+g);
  232. break;
  233. }
  234. }
  235. delete this;
  236. LOCPLINT->curint->activate();
  237. }
  238. void CSpellWindow::fadvSpellsb()
  239. {
  240. battleSpellsOnly = false;
  241. }
  242. void CSpellWindow::fbattleSpellsb()
  243. {
  244. battleSpellsOnly = true;
  245. }
  246. void CSpellWindow::fmanaPtsb()
  247. {
  248. }
  249. void CSpellWindow::fspellsAb()
  250. {
  251. selectedTab = 0;
  252. spellSite = 0;
  253. }
  254. void CSpellWindow::fspellsEb()
  255. {
  256. selectedTab = 3;
  257. spellSite = 0;
  258. }
  259. void CSpellWindow::fspellsFb()
  260. {
  261. selectedTab = 1;
  262. spellSite = 0;
  263. }
  264. void CSpellWindow::fspellsWb()
  265. {
  266. selectedTab = 2;
  267. spellSite = 0;
  268. }
  269. void CSpellWindow::fspellsAllb()
  270. {
  271. selectedTab = 4;
  272. spellSite = 0;
  273. }
  274. void CSpellWindow::fLcornerb()
  275. {
  276. if(spellSite>0)
  277. --spellSite;
  278. }
  279. void CSpellWindow::fRcornerb()
  280. {
  281. if((spellSite + 1) < (battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab]))
  282. ++spellSite;
  283. }
  284. void CSpellWindow::show(SDL_Surface *to)
  285. {
  286. if(to == NULL) //evaluating to
  287. to = screen;
  288. SDL_BlitSurface(background, NULL, to, &pos);
  289. blitAt(spellTab->ourImages[selectedTab].bitmap, 614, 96, to);
  290. statusBar->show();
  291. if(selectedTab!=4 && spellSite == 0)
  292. {
  293. blitAt(schools->ourImages[selectedTab].bitmap, 207, 76, to);
  294. }
  295. if(spellSite!=0)
  296. {
  297. blitAt(leftCorner, lCorner->pos.x, lCorner->pos.y, to);
  298. }
  299. if((spellSite+1) != (battleSpellsOnly ? sitesPerTabBattle[selectedTab] : sitesPerTabAdv[selectedTab]) )
  300. {
  301. blitAt(rightCorner, rCorner->pos.x, rCorner->pos.y, to);
  302. }
  303. }
  304. void CSpellWindow::activate()
  305. {
  306. exitBtn->activate();
  307. battleSpells->activate();
  308. adventureSpells->activate();
  309. manaPoints->activate();
  310. selectSpellsA->activate();
  311. selectSpellsE->activate();
  312. selectSpellsF->activate();
  313. selectSpellsW->activate();
  314. selectSpellsAll->activate();
  315. lCorner->activate();
  316. rCorner->activate();
  317. }
  318. void CSpellWindow::deactivate()
  319. {
  320. exitBtn->deactivate();
  321. battleSpells->deactivate();
  322. adventureSpells->deactivate();
  323. manaPoints->deactivate();
  324. selectSpellsA->deactivate();
  325. selectSpellsE->deactivate();
  326. selectSpellsF->deactivate();
  327. selectSpellsW->deactivate();
  328. selectSpellsAll->deactivate();
  329. lCorner->deactivate();
  330. rCorner->deactivate();
  331. }