AdventureMapButton.h 8.6 KB

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