AdventureMapButton.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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();
  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->curint == static_cast<CMainInterface*>(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();
  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();
  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. buttons[i]->show();
  284. }
  285. }
  286. void CSlider::sliderClicked()
  287. {
  288. if(!moving)
  289. {
  290. MotionInterested::activate();
  291. moving = true;
  292. }
  293. }
  294. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  295. {
  296. 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 )
  297. return;
  298. float v = sEvent.x - pos.x - 24;
  299. v/= (pos.w - 48);
  300. v*=amount;
  301. if(v!=value)
  302. {
  303. moveTo(v);
  304. redrawSlider();
  305. }
  306. }
  307. void CSlider::redrawSlider()
  308. {
  309. slider.show();
  310. }
  311. void CSlider::moveLeft()
  312. {
  313. moveTo(value-1);
  314. }
  315. void CSlider::moveRight()
  316. {
  317. moveTo(value+1);
  318. }
  319. void CSlider::moveTo(int to)
  320. {
  321. if(to<0)
  322. to=0;
  323. else if(to>amount)
  324. to=amount;
  325. value = to;
  326. if(amount)
  327. {
  328. float part = (float)to/amount;
  329. part*=(pos.w-48);
  330. slider.pos.x = part + pos.x + 16;
  331. }
  332. else
  333. slider.pos.x = pos.x+16;
  334. moved(to);
  335. }
  336. void CSlider::activate() // makes button active
  337. {
  338. left.activate();
  339. right.activate();
  340. slider.activate();
  341. ClickableL::activate();
  342. }
  343. void CSlider::deactivate() // makes button inactive (but doesn't delete)
  344. {
  345. left.deactivate();
  346. right.deactivate();
  347. slider.deactivate();
  348. ClickableL::deactivate();
  349. }
  350. void CSlider::clickLeft (tribool down)
  351. {
  352. if(down)
  353. {
  354. float pw = LOCPLINT->current->motion.x-pos.x-16;
  355. float rw = pw / ((float)(pos.w-32));
  356. if (rw>1) return;
  357. if (rw<0) return;
  358. moveTo(rw*amount);
  359. return;
  360. }
  361. if(moving)
  362. {
  363. MotionInterested::deactivate();
  364. moving = false;
  365. }
  366. }
  367. void CSlider::show(SDL_Surface * to)
  368. {
  369. left.show();
  370. right.show();
  371. slider.show();
  372. }
  373. CSlider::~CSlider()
  374. {
  375. delete imgs;
  376. }
  377. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal)
  378. :capacity(Capacity),amount(Amount),value(Value),horizontal(Horizontal), moved(Moved)
  379. {
  380. moving = false;
  381. strongInterest = true;
  382. imgs = CDefHandler::giveDefEss("IGPCRDIV.DEF");
  383. left.pos.y = slider.pos.y = right.pos.y = pos.y = y;
  384. left.pos.x = pos.x = x;
  385. right.pos.x = x + totalw - 16;
  386. left.callback = boost::bind(&CSlider::moveLeft,this);
  387. right.callback = boost::bind(&CSlider::moveRight,this);
  388. slider.callback = boost::bind(&CSlider::sliderClicked,this);
  389. left.pos.w = left.pos.h = right.pos.w = right.pos.h = slider.pos.w = slider.pos.h = pos.h = 16;
  390. pos.w = totalw;
  391. left.imgs.resize(1); right.imgs.resize(1); slider.imgs.resize(1);
  392. left.imgs[0].push_back(imgs->ourImages[0].bitmap); left.imgs[0].push_back(imgs->ourImages[1].bitmap);
  393. right.imgs[0].push_back(imgs->ourImages[2].bitmap); right.imgs[0].push_back(imgs->ourImages[3].bitmap);
  394. slider.imgs[0].push_back(imgs->ourImages[4].bitmap);
  395. left.notFreeButton = right.notFreeButton = slider.notFreeButton = true;
  396. slider.actOnDown = true;
  397. moveTo(value);
  398. }
  399. void CSlider::block( bool on )
  400. {
  401. left.block(on);
  402. right.block(on);
  403. slider.block(on);
  404. }