AdventureMapButton.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/CGeneralTextHandler.h"
  7. #include "hch/CTownHandler.h"
  8. #include "CCallback.h"
  9. #include "client/CConfigHandler.h"
  10. #include "client/Graphics.h"
  11. AdventureMapButton::AdventureMapButton ()
  12. {
  13. type=2;
  14. abs=true;
  15. active=false;
  16. ourObj=NULL;
  17. state=0;
  18. blocked = actOnDown = false;
  19. }
  20. //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)
  21. //{
  22. // init(Callback, Name, HelpBox, playerColoredButton, defName, add, x, y, activ);
  23. //}
  24. 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 )
  25. {
  26. std::map<int,std::string> pom;
  27. pom[0] = Name;
  28. init(Callback, pom, HelpBox, playerColoredButton, defName, add, x, y, key);
  29. }
  30. 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 */ )
  31. {
  32. init(Callback, Name, HelpBox, playerColoredButton, defName, add, x, y, key);
  33. }
  34. AdventureMapButton::AdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, config::ButtonInfo *info, int key/*=0*/ )
  35. {
  36. std::map<int,std::string> pom;
  37. pom[0] = Name;
  38. init(Callback, pom, HelpBox, info->playerColoured, info->defName, &info->additionalDefs, info->x, info->y, key);
  39. }
  40. void AdventureMapButton::clickLeft (tribool down)
  41. {
  42. if(blocked)
  43. return;
  44. if (down)
  45. state=1;
  46. else
  47. state=0;
  48. show();
  49. if (actOnDown && down)
  50. {
  51. pressedL=state;
  52. //if(!callback.empty())
  53. callback();
  54. }
  55. else if (pressedL && (down==false))
  56. {
  57. pressedL=state;
  58. //if(!callback.empty())
  59. callback();
  60. }
  61. else
  62. {
  63. pressedL=state;
  64. }
  65. }
  66. void AdventureMapButton::clickRight (tribool down)
  67. {
  68. if(helpBox.size()) //there is no point to show window with nothing inside...
  69. LOCPLINT->adventureInt->handleRightClick(helpBox,down,this);
  70. }
  71. void AdventureMapButton::hover (bool on)
  72. {
  73. Hoverable::hover(on);
  74. std::string *name = (vstd::contains(hoverTexts,state))
  75. ? (&hoverTexts[state])
  76. : (vstd::contains(hoverTexts,0) ? (&hoverTexts[0]) : NULL);
  77. if(name) //if there is no name, there is nohing to display also
  78. {
  79. if (on)
  80. LOCPLINT->statusbar->print(*name);
  81. else if ( LOCPLINT->statusbar->getCurrent()==(*name) )
  82. LOCPLINT->statusbar->clear();
  83. }
  84. }
  85. void AdventureMapButton::activate()
  86. {
  87. if (active) return;
  88. active=true;
  89. ClickableL::activate();
  90. ClickableR::activate();
  91. Hoverable::activate();
  92. KeyInterested::activate();
  93. }
  94. void AdventureMapButton::deactivate()
  95. {
  96. if (!active) return;
  97. active=false;
  98. ClickableL::deactivate();
  99. ClickableR::deactivate();
  100. Hoverable::deactivate();
  101. KeyInterested::deactivate();
  102. }
  103. 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)
  104. {
  105. callback = Callback;
  106. blocked = actOnDown = false;
  107. type=2;
  108. abs=true;
  109. active=false;
  110. ourObj=NULL;
  111. assignedKeys.insert(key);
  112. state=0;
  113. hoverTexts = Name;
  114. helpBox=HelpBox;
  115. int est = LOCPLINT->playerID;
  116. CDefHandler * temp = CDefHandler::giveDef(defName);
  117. temp->notFreeImgs = true;
  118. for (int i=0;i<temp->ourImages.size();i++)
  119. {
  120. imgs.resize(1);
  121. imgs[0].push_back(temp->ourImages[i].bitmap);
  122. if(playerColoredButton)
  123. graphics->blueToPlayersAdv(imgs[curimg][i],LOCPLINT->playerID);
  124. }
  125. delete temp;
  126. if (add && add->size())
  127. {
  128. imgs.resize(imgs.size()+add->size());
  129. for (int i=0; i<add->size();i++)
  130. {
  131. temp = CDefHandler::giveDef((*add)[i]);
  132. temp->notFreeImgs = true;
  133. for (int j=0;j<temp->ourImages.size();j++)
  134. {
  135. imgs[i+1].push_back(temp->ourImages[j].bitmap);
  136. if(playerColoredButton)
  137. graphics->blueToPlayersAdv(imgs[1+i][j],LOCPLINT->playerID);
  138. }
  139. delete temp;
  140. }
  141. //delete add;
  142. }
  143. pos.x=x;
  144. pos.y=y;
  145. pos.w = imgs[curimg][0]->w;
  146. pos.h = imgs[curimg][0]->h -1;
  147. }
  148. void AdventureMapButton::block( bool on )
  149. {
  150. blocked = on;
  151. state = 0;
  152. bitmapOffset = on ? 2 : 0;
  153. show();
  154. }
  155. void CHighlightableButton::select(bool on)
  156. {
  157. selected = on;
  158. state = selected ? 3 : 0;
  159. if(selected)
  160. callback();
  161. else
  162. callback2();
  163. if(hoverTexts.size()>1)
  164. {
  165. hover(false);
  166. hover(true);
  167. }
  168. }
  169. void CHighlightableButton::clickLeft( tribool down )
  170. {
  171. if(blocked)
  172. return;
  173. if (down)
  174. state=1;
  175. else
  176. state = selected ? 3 : 0;
  177. show();
  178. if (pressedL && (down==false))
  179. {
  180. pressedL=state;
  181. if(!onlyOn || !selected)
  182. select(!selected);
  183. }
  184. else
  185. {
  186. pressedL=state;
  187. }
  188. }
  189. 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 )
  190. {
  191. onlyOn = false;
  192. init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,key);
  193. callback2 = onDeselect;
  194. }
  195. void CHighlightableButtonsGroup::addButton(CHighlightableButton* bt)
  196. {
  197. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  198. buttons.push_back(bt);
  199. }
  200. 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)
  201. {
  202. CHighlightableButton *bt = new CHighlightableButton(OnSelect,0,tooltip,HelpBox,false,defName,0,x,y,key);
  203. bt->ID = uid;
  204. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  205. bt->onlyOn = true;
  206. buttons.push_back(bt);
  207. }
  208. CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange)
  209. {
  210. onChange = OnChange;
  211. }
  212. CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
  213. {
  214. for(int i=0;i<buttons.size();i++)
  215. delete buttons[i];
  216. }
  217. void CHighlightableButtonsGroup::activate()
  218. {
  219. for(int i=0;i<buttons.size();i++)
  220. buttons[i]->activate();
  221. }
  222. void CHighlightableButtonsGroup::deactivate()
  223. {
  224. for(int i=0;i<buttons.size();i++)
  225. buttons[i]->deactivate();
  226. }
  227. void CHighlightableButtonsGroup::select(int id, bool mode)
  228. {
  229. CHighlightableButton *bt = NULL;
  230. if(mode)
  231. {
  232. for(int i=0;i<buttons.size() && !bt;i++)
  233. if (buttons[i]->ID == id)
  234. bt = buttons[i];
  235. }
  236. else
  237. {
  238. bt = buttons[id];
  239. }
  240. bt->select(true);
  241. selectionChanged(bt->ID);
  242. }
  243. void CHighlightableButtonsGroup::selectionChanged(int to)
  244. {
  245. for(int i=0;i<buttons.size();i++)
  246. if(buttons[i]->ID!=to && buttons[i]->selected)
  247. buttons[i]->select(false);
  248. onChange(to);
  249. }
  250. void CHighlightableButtonsGroup::show(SDL_Surface * to )
  251. {
  252. for(int i=0;i<buttons.size();i++)
  253. buttons[i]->show();
  254. }
  255. void CSlider::sliderClicked()
  256. {
  257. if(!moving)
  258. {
  259. MotionInterested::activate();
  260. moving = true;
  261. }
  262. }
  263. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  264. {
  265. if( std::abs(sEvent.y-(pos.y+pos.h/2)) > pos.h/2+40 || std::abs(sEvent.x-(pos.x+pos.w/2)) > pos.w/2 )
  266. return;
  267. float v = sEvent.x - pos.x - 24;
  268. v/= (pos.w - 48);
  269. v*=amount;
  270. if(v!=value)
  271. {
  272. moveTo(v);
  273. redrawSlider();
  274. }
  275. }
  276. void CSlider::redrawSlider()
  277. {
  278. slider.show();
  279. }
  280. void CSlider::moveLeft()
  281. {
  282. moveTo(value-1);
  283. }
  284. void CSlider::moveRight()
  285. {
  286. moveTo(value+1);
  287. }
  288. void CSlider::moveTo(int to)
  289. {
  290. if(to<0)
  291. to=0;
  292. else if(to>amount)
  293. to=amount;
  294. value = to;
  295. if(amount)
  296. {
  297. float part = (float)to/amount;
  298. part*=(pos.w-48);
  299. slider.pos.x = part + pos.x + 16;
  300. }
  301. else
  302. slider.pos.x = pos.x+16;
  303. moved(to);
  304. }
  305. void CSlider::activate() // makes button active
  306. {
  307. left.activate();
  308. right.activate();
  309. slider.activate();
  310. ClickableL::activate();
  311. }
  312. void CSlider::deactivate() // makes button inactive (but doesn't delete)
  313. {
  314. left.deactivate();
  315. right.deactivate();
  316. slider.deactivate();
  317. ClickableL::deactivate();
  318. }
  319. void CSlider::clickLeft (tribool down)
  320. {
  321. if(down)
  322. {
  323. float pw = LOCPLINT->current->motion.x-pos.x-16;
  324. float rw = pw / ((float)(pos.w-32));
  325. if (rw>1) return;
  326. if (rw<0) return;
  327. moveTo(rw*amount);
  328. return;
  329. }
  330. if(moving)
  331. {
  332. MotionInterested::deactivate();
  333. moving = false;
  334. }
  335. }
  336. void CSlider::show(SDL_Surface * to)
  337. {
  338. left.show();
  339. right.show();
  340. slider.show();
  341. }
  342. CSlider::~CSlider()
  343. {
  344. delete imgs;
  345. }
  346. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal)
  347. :capacity(Capacity),amount(Amount),value(Value),horizontal(Horizontal), moved(Moved)
  348. {
  349. moving = false;
  350. strongInterest = true;
  351. imgs = CDefHandler::giveDefEss("IGPCRDIV.DEF");
  352. left.pos.y = slider.pos.y = right.pos.y = pos.y = y;
  353. left.pos.x = pos.x = x;
  354. right.pos.x = x + totalw - 16;
  355. left.callback = boost::bind(&CSlider::moveLeft,this);
  356. right.callback = boost::bind(&CSlider::moveRight,this);
  357. slider.callback = boost::bind(&CSlider::sliderClicked,this);
  358. left.pos.w = left.pos.h = right.pos.w = right.pos.h = slider.pos.w = slider.pos.h = pos.h = 16;
  359. pos.w = totalw;
  360. left.imgs.resize(1); right.imgs.resize(1); slider.imgs.resize(1);
  361. left.imgs[0].push_back(imgs->ourImages[0].bitmap); left.imgs[0].push_back(imgs->ourImages[1].bitmap);
  362. right.imgs[0].push_back(imgs->ourImages[2].bitmap); right.imgs[0].push_back(imgs->ourImages[3].bitmap);
  363. slider.imgs[0].push_back(imgs->ourImages[4].bitmap);
  364. left.notFreeButton = right.notFreeButton = slider.notFreeButton = true;
  365. slider.actOnDown = true;
  366. moveTo(value);
  367. }
  368. void CSlider::block( bool on )
  369. {
  370. left.block(on);
  371. right.block(on);
  372. slider.block(on);
  373. }