AdventureMapButton.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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,int key, 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, key);
  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, int key, std::vector<std::string> * add /*= NULL*/, bool playerColoredButton /*= false */ )
  30. {
  31. init(Callback, Name, HelpBox, playerColoredButton, defName, add, x, y, key);
  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::deactivate()
  88. {
  89. if (!active) return;
  90. active=false;
  91. ClickableL::deactivate();
  92. ClickableR::deactivate();
  93. Hoverable::deactivate();
  94. KeyInterested::deactivate();
  95. }
  96. 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, int key)
  97. {
  98. callback = Callback;
  99. blocked = actOnDown = false;
  100. type=2;
  101. abs=true;
  102. active=false;
  103. ourObj=NULL;
  104. assignedKeys.insert(key);
  105. state=0;
  106. hoverTexts = Name;
  107. helpBox=HelpBox;
  108. int est = LOCPLINT->playerID;
  109. CDefHandler * temp = CDefHandler::giveDef(defName);
  110. temp->notFreeImgs = true;
  111. for (int i=0;i<temp->ourImages.size();i++)
  112. {
  113. imgs.resize(1);
  114. imgs[0].push_back(temp->ourImages[i].bitmap);
  115. if(playerColoredButton)
  116. graphics->blueToPlayersAdv(imgs[curimg][i],LOCPLINT->playerID);
  117. }
  118. delete temp;
  119. if (add)
  120. {
  121. imgs.resize(imgs.size()+add->size());
  122. for (int i=0; i<add->size();i++)
  123. {
  124. temp = CDefHandler::giveDef((*add)[i]);
  125. temp->notFreeImgs = true;
  126. for (int j=0;j<temp->ourImages.size();j++)
  127. {
  128. imgs[i+1].push_back(temp->ourImages[j].bitmap);
  129. if(playerColoredButton)
  130. graphics->blueToPlayersAdv(imgs[1+i][j],LOCPLINT->playerID);
  131. }
  132. delete temp;
  133. }
  134. delete add;
  135. }
  136. pos.x=x;
  137. pos.y=y;
  138. pos.w = imgs[curimg][0]->w;
  139. pos.h = imgs[curimg][0]->h -1;
  140. }
  141. void AdventureMapButton::block( bool on )
  142. {
  143. blocked = on;
  144. state = 0;
  145. bitmapOffset = on ? 2 : 0;
  146. show();
  147. }
  148. void CHighlightableButton::select(bool on)
  149. {
  150. selected = on;
  151. state = selected ? 3 : 0;
  152. if(selected)
  153. callback();
  154. else
  155. callback2();
  156. if(hoverTexts.size()>1)
  157. {
  158. hover(false);
  159. hover(true);
  160. }
  161. }
  162. void CHighlightableButton::clickLeft( tribool down )
  163. {
  164. if(blocked)
  165. return;
  166. if (down)
  167. state=1;
  168. else
  169. state = selected ? 3 : 0;
  170. show();
  171. if (pressedL && (down==false))
  172. {
  173. pressedL=state;
  174. if(!onlyOn || !selected)
  175. select(!selected);
  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, int key )
  183. {
  184. onlyOn = false;
  185. init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,key);
  186. callback2 = onDeselect;
  187. }
  188. void CHighlightableButtonsGroup::addButton(CHighlightableButton* bt)
  189. {
  190. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  191. buttons.push_back(bt);
  192. }
  193. void CHighlightableButtonsGroup::addButton(const std::map<int,std::string> &tooltip, const std::string &HelpBox, const std::string &defName, int x, int y, int uid, const CFunctionList<void()> &OnSelect, int key)
  194. {
  195. CHighlightableButton *bt = new CHighlightableButton(OnSelect,0,tooltip,HelpBox,false,defName,0,x,y,key);
  196. bt->ID = uid;
  197. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  198. bt->onlyOn = true;
  199. buttons.push_back(bt);
  200. }
  201. CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange)
  202. {
  203. onChange = OnChange;
  204. }
  205. CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
  206. {
  207. for(int i=0;i<buttons.size();i++)
  208. delete buttons[i];
  209. }
  210. void CHighlightableButtonsGroup::activate()
  211. {
  212. for(int i=0;i<buttons.size();i++)
  213. buttons[i]->activate();
  214. }
  215. void CHighlightableButtonsGroup::deactivate()
  216. {
  217. for(int i=0;i<buttons.size();i++)
  218. buttons[i]->deactivate();
  219. }
  220. void CHighlightableButtonsGroup::select(int id, bool mode)
  221. {
  222. CHighlightableButton *bt = NULL;
  223. if(mode)
  224. {
  225. for(int i=0;i<buttons.size() && !bt;i++)
  226. if (buttons[i]->ID == id)
  227. bt = buttons[i];
  228. }
  229. else
  230. {
  231. bt = buttons[id];
  232. }
  233. bt->select(true);
  234. selectionChanged(bt->ID);
  235. }
  236. void CHighlightableButtonsGroup::selectionChanged(int to)
  237. {
  238. for(int i=0;i<buttons.size();i++)
  239. if(buttons[i]->ID!=to && buttons[i]->selected)
  240. buttons[i]->select(false);
  241. onChange(to);
  242. }
  243. void CHighlightableButtonsGroup::show(SDL_Surface * to )
  244. {
  245. for(int i=0;i<buttons.size();i++)
  246. buttons[i]->show();
  247. }
  248. void CSlider::sliderClicked()
  249. {
  250. if(!moving)
  251. {
  252. MotionInterested::activate();
  253. moving = true;
  254. }
  255. }
  256. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  257. {
  258. float v = sEvent.x - pos.x - 24;
  259. v/= (pos.w - 48);
  260. v*=amount;
  261. if(v!=value)
  262. {
  263. moveTo(v);
  264. redrawSlider();
  265. }
  266. }
  267. void CSlider::redrawSlider()
  268. {
  269. slider.show();
  270. }
  271. void CSlider::moveLeft()
  272. {
  273. moveTo(value-1);
  274. }
  275. void CSlider::moveRight()
  276. {
  277. moveTo(value+1);
  278. }
  279. void CSlider::moveTo(int to)
  280. {
  281. if(to<0)
  282. to=0;
  283. else if(to>amount)
  284. to=amount;
  285. value = to;
  286. if(amount)
  287. {
  288. float part = (float)to/amount;
  289. part*=(pos.w-48);
  290. slider.pos.x = part + pos.x + 16;
  291. }
  292. else
  293. slider.pos.x = pos.x+16;
  294. moved(to);
  295. }
  296. void CSlider::activate() // makes button active
  297. {
  298. left.activate();
  299. right.activate();
  300. slider.activate();
  301. ClickableL::activate();
  302. }
  303. void CSlider::deactivate() // makes button inactive (but doesn't delete)
  304. {
  305. left.deactivate();
  306. right.deactivate();
  307. slider.deactivate();
  308. ClickableL::deactivate();
  309. }
  310. void CSlider::clickLeft (tribool down)
  311. {
  312. if(down)
  313. {
  314. float pw = LOCPLINT->current->motion.x-pos.x-16;
  315. float rw = pw / ((float)(pos.w-32));
  316. if (rw>1) return;
  317. if (rw<0) return;
  318. moveTo(rw*amount);
  319. return;
  320. }
  321. if(moving)
  322. {
  323. MotionInterested::deactivate();
  324. moving = false;
  325. }
  326. }
  327. void CSlider::show(SDL_Surface * to)
  328. {
  329. left.show();
  330. right.show();
  331. slider.show();
  332. }
  333. CSlider::~CSlider()
  334. {
  335. delete imgs;
  336. }
  337. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal)
  338. :capacity(Capacity),amount(Amount),value(Value),horizontal(Horizontal), moved(Moved)
  339. {
  340. moving = false;
  341. strongInterest = true;
  342. imgs = CDefHandler::giveDefEss("IGPCRDIV.DEF");
  343. left.pos.y = slider.pos.y = right.pos.y = pos.y = y;
  344. left.pos.x = pos.x = x;
  345. right.pos.x = x + totalw - 16;
  346. left.callback = boost::bind(&CSlider::moveLeft,this);
  347. right.callback = boost::bind(&CSlider::moveRight,this);
  348. slider.callback = boost::bind(&CSlider::sliderClicked,this);
  349. left.pos.w = left.pos.h = right.pos.w = right.pos.h = slider.pos.w = slider.pos.h = pos.h = 16;
  350. pos.w = totalw;
  351. left.imgs.resize(1); right.imgs.resize(1); slider.imgs.resize(1);
  352. left.imgs[0].push_back(imgs->ourImages[0].bitmap); left.imgs[0].push_back(imgs->ourImages[1].bitmap);
  353. right.imgs[0].push_back(imgs->ourImages[2].bitmap); right.imgs[0].push_back(imgs->ourImages[3].bitmap);
  354. slider.imgs[0].push_back(imgs->ourImages[4].bitmap);
  355. left.notFreeButton = right.notFreeButton = slider.notFreeButton = true;
  356. slider.actOnDown = true;
  357. moveTo(value);
  358. }
  359. void CSlider::block( bool on )
  360. {
  361. left.block(on);
  362. right.block(on);
  363. slider.block(on);
  364. }