AdventureMapButton.cpp 12 KB

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