AdventureMapButton.cpp 12 KB

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