AdventureMapButton.cpp 11 KB

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