AdventureMapButton.cpp 10 KB

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