AdventureMapButton.cpp 6.2 KB

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