AdventureMapButton.cpp 12 KB

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