AdventureMapButton.cpp 11 KB

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