AdventureMapButton.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #pragma once
  2. #include "SDL_Extensions.h"
  3. #include "hch\CDefHandler.h"
  4. #include "CGameInfo.h"
  5. #include "hch\CLodHandler.h"
  6. #include "hch\CPreGameTextHandler.h"
  7. #include "hch/CTownHandler.h"
  8. template <typename T>
  9. AdventureMapButton<T>::AdventureMapButton ()
  10. {
  11. type=2;
  12. abs=true;
  13. active=false;
  14. ourObj=NULL;
  15. state=0;
  16. }
  17. template <typename T>
  18. AdventureMapButton<T>::AdventureMapButton
  19. ( std::string Name, std::string HelpBox, void(T::*Function)(), int x, int y, std::string defName, T* Owner, bool activ, std::vector<std::string> * add )
  20. {
  21. owner = Owner;
  22. type=2;
  23. abs=true;
  24. active=false;
  25. ourObj=NULL;
  26. state=0;
  27. name=Name;
  28. helpBox=HelpBox;
  29. int est = LOCPLINT->playerID;
  30. CDefHandler * temp = CGI->spriteh->giveDef(defName);
  31. temp->notFreeImgs = true;
  32. for (int i=0;i<temp->ourImages.size();i++)
  33. {
  34. imgs.resize(1);
  35. imgs[0].push_back(temp->ourImages[i].bitmap);
  36. CSDL_Ext::blueToPlayersAdv(imgs[curimg][i],LOCPLINT->playerID);
  37. }
  38. delete temp;
  39. if (add)
  40. {
  41. imgs.resize(imgs.size()+add->size());
  42. for (int i=0; i<add->size();i++)
  43. {
  44. temp = CGI->spriteh->giveDef((*add)[i]);
  45. temp->notFreeImgs = true;
  46. for (int j=0;j<temp->ourImages.size();j++)
  47. {
  48. imgs[i+1].push_back(temp->ourImages[j].bitmap);
  49. CSDL_Ext::blueToPlayersAdv(imgs[1+i][j],LOCPLINT->playerID);
  50. }
  51. delete temp;
  52. }
  53. delete add;
  54. }
  55. function = Function;
  56. pos.x=x;
  57. pos.y=y;
  58. pos.w = imgs[curimg][0]->w;
  59. pos.h = imgs[curimg][0]->h -1;
  60. if (activ)
  61. activate();
  62. }
  63. template <typename T>
  64. void AdventureMapButton<T>::clickLeft (tribool down)
  65. {
  66. if (down)
  67. {
  68. state=1;
  69. }
  70. else
  71. {
  72. state=0;
  73. }
  74. show();
  75. if (pressedL && (down==false))
  76. {
  77. pressedL=state;
  78. (owner->*function)();
  79. }
  80. else
  81. {
  82. pressedL=state;
  83. }
  84. }
  85. template <typename T>
  86. void AdventureMapButton<T>::clickRight (tribool down)
  87. {
  88. if(helpBox.size()) //there is no point to show window with nothing inside...
  89. LOCPLINT->adventureInt->handleRightClick(helpBox,down,this);
  90. }
  91. template <typename T>
  92. void AdventureMapButton<T>::hover (bool on)
  93. {
  94. Hoverable::hover(on);
  95. if (on)
  96. LOCPLINT->statusbar->print(name);
  97. else if (LOCPLINT->statusbar->getCurrent()==name)
  98. LOCPLINT->statusbar->clear();
  99. }
  100. template <typename T>
  101. void AdventureMapButton<T>::activate()
  102. {
  103. if (active) return;
  104. active=true;
  105. ClickableL::activate();
  106. ClickableR::activate();
  107. Hoverable::activate();
  108. KeyInterested::activate();
  109. }
  110. template <typename T>
  111. void AdventureMapButton<T>::keyPressed (SDL_KeyboardEvent & key)
  112. {
  113. //TODO: check if it's shortcut
  114. }
  115. template <typename T>
  116. void AdventureMapButton<T>::deactivate()
  117. {
  118. if (!active) return;
  119. active=false;
  120. ClickableL::deactivate();
  121. ClickableR::deactivate();
  122. Hoverable::deactivate();
  123. KeyInterested::deactivate();
  124. }
  125. template <typename T>
  126. CTownList<T>::~CTownList()
  127. {
  128. delete arrup;
  129. delete arrdo;
  130. }
  131. template <typename T>
  132. CTownList<T>::CTownList(int Size, SDL_Rect * Pos, int arupx, int arupy, int ardox, int ardoy)
  133. :CList(Size)
  134. {
  135. pos = *Pos;
  136. arrup = CGI->spriteh->giveDef("IAM014.DEF");
  137. arrdo = CGI->spriteh->giveDef("IAM015.DEF");
  138. arrupp.x=arupx;
  139. arrupp.y=arupy;
  140. arrupp.w=arrup->ourImages[0].bitmap->w;
  141. arrupp.h=arrup->ourImages[0].bitmap->h;
  142. arrdop.x=ardox;
  143. arrdop.y=ardoy;
  144. arrdop.w=arrdo->ourImages[0].bitmap->w;
  145. arrdop.h=arrdo->ourImages[0].bitmap->h;
  146. posporx = arrdop.x;
  147. pospory = arrupp.y + arrupp.h;
  148. pressed = indeterminate;
  149. from = 0;
  150. }
  151. template<typename T>
  152. void CTownList<T>::genList()
  153. {
  154. int howMany = LOCPLINT->cb->howManyTowns();
  155. for (int i=0;i<howMany;i++)
  156. {
  157. items.push_back(LOCPLINT->cb->getTownInfo(i,0));
  158. }
  159. }
  160. template<typename T>
  161. void CTownList<T>::select(int which)
  162. {
  163. if (which>=items.size())
  164. return;
  165. selected = which;
  166. if(owner)
  167. (owner->*fun)();
  168. }
  169. template<typename T>
  170. void CTownList<T>::mouseMoved (SDL_MouseMotionEvent & sEvent)
  171. {
  172. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  173. {
  174. if (from>0)
  175. LOCPLINT->adventureInt->statusbar.print(CGI->preth->advTListUp.first);
  176. else
  177. LOCPLINT->adventureInt->statusbar.clear();
  178. return;
  179. }
  180. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  181. {
  182. if ((items.size()-from) > SIZE)
  183. LOCPLINT->adventureInt->statusbar.print(CGI->preth->advTListDown.first);
  184. else
  185. LOCPLINT->adventureInt->statusbar.clear();
  186. return;
  187. }
  188. //if not buttons then heroes
  189. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  190. hx-=pos.x;
  191. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  192. float ny = (float)hy/(float)32;
  193. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  194. {
  195. LOCPLINT->adventureInt->statusbar.clear();
  196. return;
  197. };
  198. //LOCPLINT->adventureInt->statusbar.print( items[from+ny]->name + ", " + items[from+ny]->town->name ); //TODO - uncomment when pointer to the town type is initialized
  199. }
  200. template<typename T>
  201. void CTownList<T>::clickLeft(tribool down)
  202. {
  203. if (down)
  204. {
  205. /***************************ARROWS*****************************************/
  206. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  207. {
  208. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  209. pressed = true;
  210. return;
  211. }
  212. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>SIZE))
  213. {
  214. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  215. pressed = false;
  216. return;
  217. }
  218. /***************************TOWNS*****************************************/
  219. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  220. hx-=pos.x;
  221. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  222. float ny = (float)hy/(float)32;
  223. if (ny>SIZE || ny<0)
  224. return;
  225. if (SIZE==5 && ((int)(ny+from))==selected && (LOCPLINT->adventureInt->selection.type == TOWNI_TYPE))
  226. LOCPLINT->openTownWindow(items[selected]);//print town screen
  227. else
  228. select(ny+from);
  229. }
  230. else
  231. {
  232. if (indeterminate(pressed))
  233. return;
  234. if (pressed) //up
  235. {
  236. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  237. pressed = indeterminate;
  238. if (!down)
  239. {
  240. from--;
  241. if (from<0)
  242. from=0;
  243. draw();
  244. }
  245. }
  246. else if (!pressed) //down
  247. {
  248. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  249. pressed = indeterminate;
  250. if (!down)
  251. {
  252. from++;
  253. //if (from<items.size()-5)
  254. // from=items.size()-5;
  255. draw();
  256. }
  257. }
  258. else
  259. throw 0;
  260. }
  261. }
  262. template<typename T>
  263. void CTownList<T>::clickRight(tribool down)
  264. {
  265. if (down)
  266. {
  267. /***************************ARROWS*****************************************/
  268. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  269. {
  270. LOCPLINT->adventureInt->handleRightClick(CGI->preth->advTListUp.second,down,this);
  271. }
  272. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  273. {
  274. LOCPLINT->adventureInt->handleRightClick(CGI->preth->advTListDown.second,down,this);
  275. }
  276. }
  277. else
  278. {
  279. LOCPLINT->adventureInt->handleRightClick(CGI->preth->advTListUp.second,down,this);
  280. LOCPLINT->adventureInt->handleRightClick(CGI->preth->advTListDown.second,down,this);
  281. }
  282. }
  283. template<typename T>
  284. void CTownList<T>::hover (bool on)
  285. {
  286. }
  287. template<typename T>
  288. void CTownList<T>::keyPressed (SDL_KeyboardEvent & key)
  289. {
  290. }
  291. template<typename T>
  292. void CTownList<T>::draw()
  293. {
  294. for (int iT=0+from;iT<SIZE+from;iT++)
  295. {
  296. int i = iT-from;
  297. if (iT>=items.size())
  298. {
  299. blitAt(CGI->townh->getPic(-1),posporx,pospory+i*32);
  300. continue;
  301. }
  302. blitAt(CGI->townh->getPic(items[iT]->subID,items[iT]->hasFort(),items[iT]->builded),posporx,pospory+i*32);
  303. if ((selected == iT) && (LOCPLINT->adventureInt->selection.type == TOWNI_TYPE))
  304. {
  305. blitAt(CGI->townh->getPic(-2),posporx,pospory+i*32);
  306. }
  307. }
  308. if (from>0)
  309. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  310. else
  311. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y);
  312. if (items.size()-from>SIZE)
  313. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  314. else
  315. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y);
  316. }