AdventureMapButton.cpp 9.7 KB

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