AdventureMapButton.cpp 7.9 KB

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