AdventureMapButton.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. {
  227. onlyOn = false;
  228. init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,key);
  229. callback2 = onDeselect;
  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. bt->ID = uid;
  240. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  241. bt->onlyOn = true;
  242. buttons.push_back(bt);
  243. }
  244. CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange)
  245. {
  246. onChange = OnChange;
  247. }
  248. CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
  249. {
  250. for(size_t i=0;i<buttons.size();i++) {
  251. delete buttons[i]; //TODO smartpointers
  252. }
  253. }
  254. void CHighlightableButtonsGroup::activate()
  255. {
  256. for(size_t i=0;i<buttons.size();i++) {
  257. buttons[i]->activate();
  258. }
  259. }
  260. void CHighlightableButtonsGroup::deactivate()
  261. {
  262. for(size_t i=0;i<buttons.size();i++) {
  263. buttons[i]->deactivate();
  264. }
  265. }
  266. void CHighlightableButtonsGroup::select(int id, bool mode)
  267. {
  268. CHighlightableButton *bt = NULL;
  269. if(mode)
  270. {
  271. for(size_t i=0;i<buttons.size() && !bt; ++i)
  272. if (buttons[i]->ID == id)
  273. bt = buttons[i];
  274. }
  275. else
  276. {
  277. bt = buttons[id];
  278. }
  279. bt->select(true);
  280. selectionChanged(bt->ID);
  281. }
  282. void CHighlightableButtonsGroup::selectionChanged(int to)
  283. {
  284. for(size_t i=0;i<buttons.size(); ++i)
  285. if(buttons[i]->ID!=to && buttons[i]->selected)
  286. buttons[i]->select(false);
  287. onChange(to);
  288. }
  289. void CHighlightableButtonsGroup::show(SDL_Surface * to )
  290. {
  291. for(size_t i=0;i<buttons.size(); ++i)
  292. {
  293. buttons[i]->show(to);
  294. }
  295. }
  296. void CSlider::sliderClicked()
  297. {
  298. if(!moving)
  299. {
  300. MotionInterested::activate();
  301. moving = true;
  302. }
  303. }
  304. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  305. {
  306. 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 )
  307. return;
  308. float v = sEvent.x - pos.x - 24;
  309. v*=amount;
  310. v/= (pos.w - 48);
  311. v += 0.5f;
  312. if(v!=value)
  313. {
  314. moveTo(v);
  315. redrawSlider();
  316. }
  317. }
  318. void CSlider::redrawSlider()
  319. {
  320. slider.show(screen2);
  321. }
  322. void CSlider::moveLeft()
  323. {
  324. moveTo(value-1);
  325. }
  326. void CSlider::moveRight()
  327. {
  328. moveTo(value+1);
  329. }
  330. void CSlider::moveTo(int to)
  331. {
  332. if(to<0)
  333. to=0;
  334. else if(to>amount)
  335. to=amount;
  336. value = to;
  337. if(amount)
  338. {
  339. float part = (float)to/amount;
  340. part*=(pos.w-48);
  341. slider.pos.x = part + pos.x + 16;
  342. }
  343. else
  344. slider.pos.x = pos.x+16;
  345. moved(to);
  346. }
  347. void CSlider::activate() // makes button active
  348. {
  349. left.activate();
  350. right.activate();
  351. slider.activate();
  352. ClickableL::activate();
  353. }
  354. void CSlider::deactivate() // makes button inactive (but doesn't delete)
  355. {
  356. left.deactivate();
  357. right.deactivate();
  358. slider.deactivate();
  359. ClickableL::deactivate();
  360. }
  361. void CSlider::clickLeft (tribool down)
  362. {
  363. if(down)
  364. {
  365. float pw = LOCPLINT->current->motion.x-pos.x-24;
  366. float rw = pw / ((float)(pos.w-48));
  367. if (rw>1) return;
  368. if (rw<0) return;
  369. moveTo(rw*amount+0.5f);
  370. return;
  371. }
  372. if(moving)
  373. {
  374. MotionInterested::deactivate();
  375. moving = false;
  376. }
  377. }
  378. void CSlider::show(SDL_Surface * to)
  379. {
  380. left.show(to);
  381. right.show(to);
  382. slider.show(to);
  383. }
  384. CSlider::~CSlider()
  385. {
  386. delete imgs;
  387. }
  388. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal)
  389. :capacity(Capacity),amount(Amount),value(Value),horizontal(Horizontal), moved(Moved)
  390. {
  391. moving = false;
  392. strongInterest = true;
  393. imgs = CDefHandler::giveDefEss("IGPCRDIV.DEF");
  394. left.pos.y = slider.pos.y = right.pos.y = pos.y = y;
  395. left.pos.x = pos.x = x;
  396. right.pos.x = x + totalw - 16;
  397. left.callback = boost::bind(&CSlider::moveLeft,this);
  398. right.callback = boost::bind(&CSlider::moveRight,this);
  399. slider.callback = boost::bind(&CSlider::sliderClicked,this);
  400. left.pos.w = left.pos.h = right.pos.w = right.pos.h = slider.pos.w = slider.pos.h = pos.h = 16;
  401. pos.w = totalw;
  402. left.imgs.resize(1); right.imgs.resize(1); slider.imgs.resize(1);
  403. left.imgs[0].push_back(imgs->ourImages[0].bitmap); left.imgs[0].push_back(imgs->ourImages[1].bitmap);
  404. right.imgs[0].push_back(imgs->ourImages[2].bitmap); right.imgs[0].push_back(imgs->ourImages[3].bitmap);
  405. slider.imgs[0].push_back(imgs->ourImages[4].bitmap);
  406. left.notFreeButton = right.notFreeButton = slider.notFreeButton = true;
  407. slider.actOnDown = true;
  408. moveTo(value);
  409. }
  410. void CSlider::block( bool on )
  411. {
  412. left.block(on);
  413. right.block(on);
  414. slider.block(on);
  415. }