AdventureMapButton.cpp 5.6 KB

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