AdventureMapButton.cpp 7.8 KB

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