AdventureMapButton.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "CAdvmapInterface.h"
  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. #include "CCallback.h"
  10. AdventureMapButton::AdventureMapButton ()
  11. {
  12. type=2;
  13. abs=true;
  14. active=false;
  15. ourObj=NULL;
  16. state=0;
  17. actOnDown = false;
  18. }
  19. AdventureMapButton::AdventureMapButton
  20. ( std::string Name, std::string HelpBox, boost::function<void()> Callback, int x, int y, std::string defName, bool activ, std::vector<std::string> * add, bool playerColoredButton )
  21. {
  22. callback = Callback;
  23. actOnDown = false;
  24. type=2;
  25. abs=true;
  26. active=false;
  27. ourObj=NULL;
  28. state=0;
  29. name=Name;
  30. helpBox=HelpBox;
  31. colorChange = playerColoredButton;
  32. int est = LOCPLINT->playerID;
  33. CDefHandler * temp = CGI->spriteh->giveDef(defName);
  34. temp->notFreeImgs = true;
  35. for (int i=0;i<temp->ourImages.size();i++)
  36. {
  37. imgs.resize(1);
  38. imgs[0].push_back(temp->ourImages[i].bitmap);
  39. if(playerColoredButton)
  40. CSDL_Ext::blueToPlayersAdv(imgs[curimg][i],LOCPLINT->playerID);
  41. }
  42. delete temp;
  43. if (add)
  44. {
  45. imgs.resize(imgs.size()+add->size());
  46. for (int i=0; i<add->size();i++)
  47. {
  48. temp = CGI->spriteh->giveDef((*add)[i]);
  49. temp->notFreeImgs = true;
  50. for (int j=0;j<temp->ourImages.size();j++)
  51. {
  52. imgs[i+1].push_back(temp->ourImages[j].bitmap);
  53. if(playerColoredButton)
  54. CSDL_Ext::blueToPlayersAdv(imgs[1+i][j],LOCPLINT->playerID);
  55. }
  56. delete temp;
  57. }
  58. delete add;
  59. }
  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. void AdventureMapButton::clickLeft (tribool down)
  68. {
  69. if (down)
  70. {
  71. state=1;
  72. }
  73. else
  74. {
  75. state=0;
  76. }
  77. show();
  78. if (actOnDown && down)
  79. {
  80. pressedL=state;
  81. callback();
  82. }
  83. else if (pressedL && (down==false))
  84. {
  85. pressedL=state;
  86. callback();
  87. }
  88. else
  89. {
  90. pressedL=state;
  91. }
  92. }
  93. void AdventureMapButton::clickRight (tribool down)
  94. {
  95. if(helpBox.size()) //there is no point to show window with nothing inside...
  96. LOCPLINT->adventureInt->handleRightClick(helpBox,down,this);
  97. }
  98. void AdventureMapButton::hover (bool on)
  99. {
  100. Hoverable::hover(on);
  101. if(name.size()) //if there is no name, there is nohing to display also
  102. {
  103. if (on)
  104. LOCPLINT->statusbar->print(name);
  105. else if (LOCPLINT->statusbar->getCurrent()==name)
  106. LOCPLINT->statusbar->clear();
  107. }
  108. }
  109. void AdventureMapButton::activate()
  110. {
  111. if (active) return;
  112. active=true;
  113. ClickableL::activate();
  114. ClickableR::activate();
  115. Hoverable::activate();
  116. KeyInterested::activate();
  117. }
  118. void AdventureMapButton::keyPressed (SDL_KeyboardEvent & key)
  119. {
  120. //TODO: check if it's shortcut
  121. }
  122. void AdventureMapButton::deactivate()
  123. {
  124. if (!active) return;
  125. active=false;
  126. ClickableL::deactivate();
  127. ClickableR::deactivate();
  128. Hoverable::deactivate();
  129. KeyInterested::deactivate();
  130. }
  131. void CSlider::sliderClicked()
  132. {
  133. if(!moving)
  134. {
  135. MotionInterested::activate();
  136. moving = true;
  137. }
  138. }
  139. void CSlider::mouseMoved (SDL_MouseMotionEvent & sEvent)
  140. {
  141. float v = sEvent.x - pos.x - 24;
  142. v/= (pos.w - 48);
  143. v*=amount;
  144. if(v!=value)
  145. {
  146. moveTo(v);
  147. redrawSlider();
  148. }
  149. }
  150. void CSlider::redrawSlider()
  151. {
  152. slider.show();
  153. }
  154. void CSlider::moveLeft()
  155. {
  156. moveTo(value-1);
  157. }
  158. void CSlider::moveRight()
  159. {
  160. moveTo(value+1);
  161. }
  162. void CSlider::moveTo(int to)
  163. {
  164. if(to<0)
  165. to=0;
  166. else if(to>amount)
  167. to=amount;
  168. value = to;
  169. float part = (float)to/amount;
  170. part*=(pos.w-48);
  171. slider.pos.x = part + pos.x + 16;
  172. moved(to);
  173. }
  174. void CSlider::activate() // makes button active
  175. {
  176. left.activate();
  177. right.activate();
  178. slider.activate();
  179. ClickableL::activate();
  180. }
  181. void CSlider::deactivate() // makes button inactive (but doesn't delete)
  182. {
  183. left.deactivate();
  184. right.deactivate();
  185. slider.deactivate();
  186. ClickableL::deactivate();
  187. }
  188. void CSlider::clickLeft (tribool down)
  189. {
  190. if(down)
  191. {
  192. float pw = LOCPLINT->current->motion.x-pos.x-16;
  193. float rw = pw / ((float)(pos.w-32));
  194. if (rw>1) return;
  195. if (rw<0) return;
  196. moveTo(rw*amount);
  197. return;
  198. }
  199. if(moving)
  200. {
  201. MotionInterested::deactivate();
  202. moving = false;
  203. }
  204. }
  205. void CSlider::show(SDL_Surface * to)
  206. {
  207. left.show();
  208. right.show();
  209. slider.show();
  210. }
  211. CSlider::~CSlider()
  212. {
  213. delete imgs;
  214. }
  215. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal)
  216. :capacity(Capacity),amount(Amount),value(Value),horizontal(Horizontal), moved(Moved)
  217. {
  218. moving = false;
  219. strongInterest = true;
  220. imgs = CGI->spriteh->giveDefEss("IGPCRDIV.DEF");
  221. left.pos.y = slider.pos.y = right.pos.y = pos.y = y;
  222. left.pos.x = pos.x = x;
  223. right.pos.x = x + totalw - 16;
  224. //left.owner = right.owner = slider.owner = this;
  225. //left.function = &CSlider::moveLeft;
  226. //right.function = &CSlider::moveRight;
  227. //slider.function = &CSlider::sliderClicked;
  228. left.callback = boost::bind(&CSlider::moveLeft,this);
  229. left.pos.w = left.pos.h = right.pos.w = right.pos.h = slider.pos.w = slider.pos.h = pos.h = 16;
  230. pos.w = totalw;
  231. left.imgs.resize(1); right.imgs.resize(1); slider.imgs.resize(1);
  232. left.imgs[0].push_back(imgs->ourImages[0].bitmap); left.imgs[0].push_back(imgs->ourImages[1].bitmap);
  233. right.imgs[0].push_back(imgs->ourImages[2].bitmap); right.imgs[0].push_back(imgs->ourImages[3].bitmap);
  234. slider.imgs[0].push_back(imgs->ourImages[4].bitmap);
  235. left.notFreeButton = right.notFreeButton = slider.notFreeButton = true;
  236. slider.actOnDown = true;
  237. moveTo(value);
  238. }