AdventureMapButton.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 "CCallback.h"
  9. #include "client/Graphics.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 = CDefHandler::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. graphics->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 = CDefHandler::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. graphics->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. if(!callback.empty())
  82. callback();
  83. }
  84. else if (pressedL && (down==false))
  85. {
  86. pressedL=state;
  87. if(!callback.empty())
  88. callback();
  89. }
  90. else
  91. {
  92. pressedL=state;
  93. }
  94. }
  95. void AdventureMapButton::clickRight (tribool down)
  96. {
  97. if(helpBox.size()) //there is no point to show window with nothing inside...
  98. LOCPLINT->adventureInt->handleRightClick(helpBox,down,this);
  99. }
  100. void AdventureMapButton::hover (bool on)
  101. {
  102. Hoverable::hover(on);
  103. if(name.size()) //if there is no name, there is nohing to display also
  104. {
  105. if (on)
  106. LOCPLINT->statusbar->print(name);
  107. else if (LOCPLINT->statusbar->getCurrent()==name)
  108. LOCPLINT->statusbar->clear();
  109. }
  110. }
  111. void AdventureMapButton::activate()
  112. {
  113. if (active) return;
  114. active=true;
  115. ClickableL::activate();
  116. ClickableR::activate();
  117. Hoverable::activate();
  118. KeyInterested::activate();
  119. }
  120. void AdventureMapButton::keyPressed (SDL_KeyboardEvent & key)
  121. {
  122. //TODO: check if it's shortcut
  123. }
  124. void AdventureMapButton::deactivate()
  125. {
  126. if (!active) return;
  127. active=false;
  128. ClickableL::deactivate();
  129. ClickableR::deactivate();
  130. Hoverable::deactivate();
  131. KeyInterested::deactivate();
  132. }
  133. void CSlider::sliderClicked()
  134. {
  135. if(!moving)
  136. {
  137. MotionInterested::activate();
  138. moving = true;
  139. }
  140. }
  141. void CSlider::mouseMoved (SDL_MouseMotionEvent & sEvent)
  142. {
  143. float v = sEvent.x - pos.x - 24;
  144. v/= (pos.w - 48);
  145. v*=amount;
  146. if(v!=value)
  147. {
  148. moveTo(v);
  149. redrawSlider();
  150. }
  151. }
  152. void CSlider::redrawSlider()
  153. {
  154. slider.show();
  155. }
  156. void CSlider::moveLeft()
  157. {
  158. moveTo(value-1);
  159. }
  160. void CSlider::moveRight()
  161. {
  162. moveTo(value+1);
  163. }
  164. void CSlider::moveTo(int to)
  165. {
  166. if(to<0)
  167. to=0;
  168. else if(to>amount)
  169. to=amount;
  170. value = to;
  171. if(amount)
  172. {
  173. float part = (float)to/amount;
  174. part*=(pos.w-48);
  175. slider.pos.x = part + pos.x + 16;
  176. }
  177. else
  178. slider.pos.x = pos.x+16;
  179. moved(to);
  180. }
  181. void CSlider::activate() // makes button active
  182. {
  183. left.activate();
  184. right.activate();
  185. slider.activate();
  186. ClickableL::activate();
  187. }
  188. void CSlider::deactivate() // makes button inactive (but doesn't delete)
  189. {
  190. left.deactivate();
  191. right.deactivate();
  192. slider.deactivate();
  193. ClickableL::deactivate();
  194. }
  195. void CSlider::clickLeft (tribool down)
  196. {
  197. if(down)
  198. {
  199. float pw = LOCPLINT->current->motion.x-pos.x-16;
  200. float rw = pw / ((float)(pos.w-32));
  201. if (rw>1) return;
  202. if (rw<0) return;
  203. moveTo(rw*amount);
  204. return;
  205. }
  206. if(moving)
  207. {
  208. MotionInterested::deactivate();
  209. moving = false;
  210. }
  211. }
  212. void CSlider::show(SDL_Surface * to)
  213. {
  214. left.show();
  215. right.show();
  216. slider.show();
  217. }
  218. CSlider::~CSlider()
  219. {
  220. delete imgs;
  221. }
  222. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal)
  223. :capacity(Capacity),amount(Amount),value(Value),horizontal(Horizontal), moved(Moved)
  224. {
  225. moving = false;
  226. strongInterest = true;
  227. imgs = CDefHandler::giveDefEss("IGPCRDIV.DEF");
  228. left.pos.y = slider.pos.y = right.pos.y = pos.y = y;
  229. left.pos.x = pos.x = x;
  230. right.pos.x = x + totalw - 16;
  231. left.callback = boost::bind(&CSlider::moveLeft,this);
  232. right.callback = boost::bind(&CSlider::moveRight,this);
  233. slider.callback = boost::bind(&CSlider::sliderClicked,this);
  234. left.pos.w = left.pos.h = right.pos.w = right.pos.h = slider.pos.w = slider.pos.h = pos.h = 16;
  235. pos.w = totalw;
  236. left.imgs.resize(1); right.imgs.resize(1); slider.imgs.resize(1);
  237. left.imgs[0].push_back(imgs->ourImages[0].bitmap); left.imgs[0].push_back(imgs->ourImages[1].bitmap);
  238. right.imgs[0].push_back(imgs->ourImages[2].bitmap); right.imgs[0].push_back(imgs->ourImages[3].bitmap);
  239. slider.imgs[0].push_back(imgs->ourImages[4].bitmap);
  240. left.notFreeButton = right.notFreeButton = slider.notFreeButton = true;
  241. slider.actOnDown = true;
  242. moveTo(value);
  243. }