AdventureMapButton.cpp 6.3 KB

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