2
0

AdventureMapButton.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 (const 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. if(!onlyOn || !selected)
  180. select(!selected);
  181. }
  182. else
  183. {
  184. pressedL=state;
  185. }
  186. }
  187. 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 )
  188. {
  189. onlyOn = false;
  190. init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,activ);
  191. callback2 = onDeselect;
  192. }
  193. void CHighlightableButtonsGroup::addButton(CHighlightableButton* bt)
  194. {
  195. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  196. buttons.push_back(bt);
  197. }
  198. 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)
  199. {
  200. CHighlightableButton *bt = new CHighlightableButton(OnSelect,0,tooltip,HelpBox,false,defName,0,x,y,0);
  201. bt->ID = uid;
  202. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  203. bt->onlyOn = true;
  204. buttons.push_back(bt);
  205. }
  206. CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange)
  207. {
  208. onChange = OnChange;
  209. }
  210. CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
  211. {
  212. for(int i=0;i<buttons.size();i++)
  213. delete buttons[i];
  214. }
  215. void CHighlightableButtonsGroup::activate()
  216. {
  217. for(int i=0;i<buttons.size();i++)
  218. buttons[i]->activate();
  219. }
  220. void CHighlightableButtonsGroup::deactivate()
  221. {
  222. for(int i=0;i<buttons.size();i++)
  223. buttons[i]->deactivate();
  224. }
  225. void CHighlightableButtonsGroup::select(int id, bool mode)
  226. {
  227. CHighlightableButton *bt = NULL;
  228. if(mode)
  229. {
  230. for(int i=0;i<buttons.size() && !bt;i++)
  231. if (buttons[i]->ID == id)
  232. bt = buttons[i];
  233. }
  234. else
  235. {
  236. bt = buttons[id];
  237. }
  238. bt->select(true);
  239. selectionChanged(bt->ID);
  240. }
  241. void CHighlightableButtonsGroup::selectionChanged(int to)
  242. {
  243. for(int i=0;i<buttons.size();i++)
  244. if(buttons[i]->ID!=to && buttons[i]->selected)
  245. buttons[i]->select(false);
  246. onChange(to);
  247. }
  248. void CHighlightableButtonsGroup::show(SDL_Surface * to )
  249. {
  250. for(int i=0;i<buttons.size();i++)
  251. buttons[i]->show();
  252. }
  253. void CSlider::sliderClicked()
  254. {
  255. if(!moving)
  256. {
  257. MotionInterested::activate();
  258. moving = true;
  259. }
  260. }
  261. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  262. {
  263. float v = sEvent.x - pos.x - 24;
  264. v/= (pos.w - 48);
  265. v*=amount;
  266. if(v!=value)
  267. {
  268. moveTo(v);
  269. redrawSlider();
  270. }
  271. }
  272. void CSlider::redrawSlider()
  273. {
  274. slider.show();
  275. }
  276. void CSlider::moveLeft()
  277. {
  278. moveTo(value-1);
  279. }
  280. void CSlider::moveRight()
  281. {
  282. moveTo(value+1);
  283. }
  284. void CSlider::moveTo(int to)
  285. {
  286. if(to<0)
  287. to=0;
  288. else if(to>amount)
  289. to=amount;
  290. value = to;
  291. if(amount)
  292. {
  293. float part = (float)to/amount;
  294. part*=(pos.w-48);
  295. slider.pos.x = part + pos.x + 16;
  296. }
  297. else
  298. slider.pos.x = pos.x+16;
  299. moved(to);
  300. }
  301. void CSlider::activate() // makes button active
  302. {
  303. left.activate();
  304. right.activate();
  305. slider.activate();
  306. ClickableL::activate();
  307. }
  308. void CSlider::deactivate() // makes button inactive (but doesn't delete)
  309. {
  310. left.deactivate();
  311. right.deactivate();
  312. slider.deactivate();
  313. ClickableL::deactivate();
  314. }
  315. void CSlider::clickLeft (tribool down)
  316. {
  317. if(down)
  318. {
  319. float pw = LOCPLINT->current->motion.x-pos.x-16;
  320. float rw = pw / ((float)(pos.w-32));
  321. if (rw>1) return;
  322. if (rw<0) return;
  323. moveTo(rw*amount);
  324. return;
  325. }
  326. if(moving)
  327. {
  328. MotionInterested::deactivate();
  329. moving = false;
  330. }
  331. }
  332. void CSlider::show(SDL_Surface * to)
  333. {
  334. left.show();
  335. right.show();
  336. slider.show();
  337. }
  338. CSlider::~CSlider()
  339. {
  340. delete imgs;
  341. }
  342. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal)
  343. :capacity(Capacity),amount(Amount),value(Value),horizontal(Horizontal), moved(Moved)
  344. {
  345. moving = false;
  346. strongInterest = true;
  347. imgs = CDefHandler::giveDefEss("IGPCRDIV.DEF");
  348. left.pos.y = slider.pos.y = right.pos.y = pos.y = y;
  349. left.pos.x = pos.x = x;
  350. right.pos.x = x + totalw - 16;
  351. left.callback = boost::bind(&CSlider::moveLeft,this);
  352. right.callback = boost::bind(&CSlider::moveRight,this);
  353. slider.callback = boost::bind(&CSlider::sliderClicked,this);
  354. left.pos.w = left.pos.h = right.pos.w = right.pos.h = slider.pos.w = slider.pos.h = pos.h = 16;
  355. pos.w = totalw;
  356. left.imgs.resize(1); right.imgs.resize(1); slider.imgs.resize(1);
  357. left.imgs[0].push_back(imgs->ourImages[0].bitmap); left.imgs[0].push_back(imgs->ourImages[1].bitmap);
  358. right.imgs[0].push_back(imgs->ourImages[2].bitmap); right.imgs[0].push_back(imgs->ourImages[3].bitmap);
  359. slider.imgs[0].push_back(imgs->ourImages[4].bitmap);
  360. left.notFreeButton = right.notFreeButton = slider.notFreeButton = true;
  361. slider.actOnDown = true;
  362. moveTo(value);
  363. }
  364. void CSlider::block( bool on )
  365. {
  366. left.block(on);
  367. right.block(on);
  368. slider.block(on);
  369. }