AdventureMapButton.cpp 12 KB

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