AdventureMapButton.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. int est = LOCPLINT->playerID;
  112. CDefHandler * temp = CDefHandler::giveDef(defName);
  113. temp->notFreeImgs = true;
  114. for (int i=0;i<temp->ourImages.size();i++)
  115. {
  116. imgs.resize(1);
  117. imgs[0].push_back(temp->ourImages[i].bitmap);
  118. if(playerColoredButton)
  119. graphics->blueToPlayersAdv(imgs[curimg][i],LOCPLINT->playerID);
  120. }
  121. delete temp;
  122. if (add)
  123. {
  124. imgs.resize(imgs.size()+add->size());
  125. for (int i=0; i<add->size();i++)
  126. {
  127. temp = CDefHandler::giveDef((*add)[i]);
  128. temp->notFreeImgs = true;
  129. for (int j=0;j<temp->ourImages.size();j++)
  130. {
  131. imgs[i+1].push_back(temp->ourImages[j].bitmap);
  132. if(playerColoredButton)
  133. graphics->blueToPlayersAdv(imgs[1+i][j],LOCPLINT->playerID);
  134. }
  135. delete temp;
  136. }
  137. delete add;
  138. }
  139. pos.x=x;
  140. pos.y=y;
  141. pos.w = imgs[curimg][0]->w;
  142. pos.h = imgs[curimg][0]->h -1;
  143. if (activ)
  144. activate();
  145. }
  146. void AdventureMapButton::block( bool on )
  147. {
  148. blocked = on;
  149. state = 0;
  150. bitmapOffset = on ? 2 : 0;
  151. show();
  152. }
  153. void CHighlightableButton::clickLeft( tribool down )
  154. {
  155. if(blocked)
  156. return;
  157. if (down)
  158. state=1;
  159. else
  160. state = selected ? 3 : 0;
  161. show();
  162. if (pressedL && (down==false))
  163. {
  164. pressedL=state;
  165. selected = !selected;
  166. state = selected ? 3 : 0;
  167. if(selected)
  168. callback();
  169. else
  170. callback2();
  171. if(hoverTexts.size()>1)
  172. {
  173. hover(false);
  174. hover(true);
  175. }
  176. }
  177. else
  178. {
  179. pressedL=state;
  180. }
  181. }
  182. 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 )
  183. {
  184. init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,activ);
  185. callback2 = onDeselect;
  186. }
  187. void CSlider::sliderClicked()
  188. {
  189. if(!moving)
  190. {
  191. MotionInterested::activate();
  192. moving = true;
  193. }
  194. }
  195. void CSlider::mouseMoved (SDL_MouseMotionEvent & sEvent)
  196. {
  197. float v = sEvent.x - pos.x - 24;
  198. v/= (pos.w - 48);
  199. v*=amount;
  200. if(v!=value)
  201. {
  202. moveTo(v);
  203. redrawSlider();
  204. }
  205. }
  206. void CSlider::redrawSlider()
  207. {
  208. slider.show();
  209. }
  210. void CSlider::moveLeft()
  211. {
  212. moveTo(value-1);
  213. }
  214. void CSlider::moveRight()
  215. {
  216. moveTo(value+1);
  217. }
  218. void CSlider::moveTo(int to)
  219. {
  220. if(to<0)
  221. to=0;
  222. else if(to>amount)
  223. to=amount;
  224. value = to;
  225. if(amount)
  226. {
  227. float part = (float)to/amount;
  228. part*=(pos.w-48);
  229. slider.pos.x = part + pos.x + 16;
  230. }
  231. else
  232. slider.pos.x = pos.x+16;
  233. moved(to);
  234. }
  235. void CSlider::activate() // makes button active
  236. {
  237. left.activate();
  238. right.activate();
  239. slider.activate();
  240. ClickableL::activate();
  241. }
  242. void CSlider::deactivate() // makes button inactive (but doesn't delete)
  243. {
  244. left.deactivate();
  245. right.deactivate();
  246. slider.deactivate();
  247. ClickableL::deactivate();
  248. }
  249. void CSlider::clickLeft (tribool down)
  250. {
  251. if(down)
  252. {
  253. float pw = LOCPLINT->current->motion.x-pos.x-16;
  254. float rw = pw / ((float)(pos.w-32));
  255. if (rw>1) return;
  256. if (rw<0) return;
  257. moveTo(rw*amount);
  258. return;
  259. }
  260. if(moving)
  261. {
  262. MotionInterested::deactivate();
  263. moving = false;
  264. }
  265. }
  266. void CSlider::show(SDL_Surface * to)
  267. {
  268. left.show();
  269. right.show();
  270. slider.show();
  271. }
  272. CSlider::~CSlider()
  273. {
  274. delete imgs;
  275. }
  276. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal)
  277. :capacity(Capacity),amount(Amount),value(Value),horizontal(Horizontal), moved(Moved)
  278. {
  279. moving = false;
  280. strongInterest = true;
  281. imgs = CDefHandler::giveDefEss("IGPCRDIV.DEF");
  282. left.pos.y = slider.pos.y = right.pos.y = pos.y = y;
  283. left.pos.x = pos.x = x;
  284. right.pos.x = x + totalw - 16;
  285. left.callback = boost::bind(&CSlider::moveLeft,this);
  286. right.callback = boost::bind(&CSlider::moveRight,this);
  287. slider.callback = boost::bind(&CSlider::sliderClicked,this);
  288. left.pos.w = left.pos.h = right.pos.w = right.pos.h = slider.pos.w = slider.pos.h = pos.h = 16;
  289. pos.w = totalw;
  290. left.imgs.resize(1); right.imgs.resize(1); slider.imgs.resize(1);
  291. left.imgs[0].push_back(imgs->ourImages[0].bitmap); left.imgs[0].push_back(imgs->ourImages[1].bitmap);
  292. right.imgs[0].push_back(imgs->ourImages[2].bitmap); right.imgs[0].push_back(imgs->ourImages[3].bitmap);
  293. slider.imgs[0].push_back(imgs->ourImages[4].bitmap);
  294. left.notFreeButton = right.notFreeButton = slider.notFreeButton = true;
  295. slider.actOnDown = true;
  296. moveTo(value);
  297. }
  298. void CSlider::block( bool on )
  299. {
  300. left.block(on);
  301. right.block(on);
  302. slider.block(on);
  303. }