AdventureMapButton.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. #include "AdventureMapButton.h"
  2. #include "CAdvmapInterface.h"
  3. #include "SDL_Extensions.h"
  4. #include "../hch/CDefHandler.h"
  5. #include "CGameInfo.h"
  6. #include "../hch/CLodHandler.h"
  7. #include "../hch/CGeneralTextHandler.h"
  8. #include "../hch/CTownHandler.h"
  9. #include "../CCallback.h"
  10. #include "CConfigHandler.h"
  11. #include "Graphics.h"
  12. #include "CBattleInterface.h"
  13. #include "CPlayerInterface.h"
  14. #include "CMessage.h"
  15. /*
  16. * AdevntureMapButton.cpp, part of VCMI engine
  17. *
  18. * Authors: listed in file AUTHORS in main folder
  19. *
  20. * License: GNU General Public License v2.0 or later
  21. * Full text of license available in license.txt file, in main folder
  22. *
  23. */
  24. CButtonBase::CButtonBase()
  25. {
  26. bitmapOffset = 0;
  27. curimg=0;
  28. type=-1;
  29. abs=false;
  30. //active=false;
  31. notFreeButton = false;
  32. ourObj=NULL;
  33. state=0;
  34. text = NULL;
  35. }
  36. CButtonBase::~CButtonBase()
  37. {
  38. delete text;
  39. if(notFreeButton)
  40. return;
  41. for(int i =0; i<imgs.size();i++)
  42. for(int j=0;j<imgs[i].size();j++)
  43. SDL_FreeSurface(imgs[i][j]);
  44. }
  45. void CButtonBase::show(SDL_Surface * to)
  46. {
  47. int img = std::min(state+bitmapOffset,int(imgs[curimg].size()-1));
  48. img = std::max(0, img);
  49. if (abs)
  50. {
  51. blitAt(imgs[curimg][img],pos.x,pos.y,to);
  52. if(text)
  53. {
  54. CSDL_Ext::printAt(text->text, text->x + pos.x + state, text->y + pos.y + state, text->font, zwykly, to);
  55. }
  56. }
  57. else
  58. {
  59. blitAt(imgs[curimg][img],pos.x+ourObj->pos.x,pos.y+ourObj->pos.y,to);
  60. }
  61. }
  62. void CButtonBase::showAll( SDL_Surface * to )
  63. {
  64. show(to);
  65. }
  66. void CButtonBase::addTextOverlay( const std::string Text, EFonts font )
  67. {
  68. text = new TextOverlay;
  69. text->text = Text;
  70. const Font *f = graphics->fonts[font];
  71. text->x = pos.w/2 - f->getWidth(Text.c_str())/2;
  72. text->y = pos.h/2 - f->height/2;
  73. text->font = font;
  74. }
  75. AdventureMapButton::AdventureMapButton ()
  76. {
  77. type=2;
  78. abs=true;
  79. hoverable = false;
  80. //active=false;
  81. ourObj=NULL;
  82. state=0;
  83. blocked = actOnDown = false;
  84. }
  85. //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)
  86. //{
  87. // init(Callback, Name, HelpBox, playerColoredButton, defName, add, x, y, activ);
  88. //}
  89. 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 )
  90. {
  91. std::map<int,std::string> pom;
  92. pom[0] = Name;
  93. init(Callback, pom, HelpBox, playerColoredButton, defName, add, x, y, key);
  94. }
  95. 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 */ )
  96. {
  97. init(Callback, Name, HelpBox, playerColoredButton, defName, add, x, y, key);
  98. }
  99. AdventureMapButton::AdventureMapButton( const std::string &Name, const std::string &HelpBox, const CFunctionList<void()> &Callback, config::ButtonInfo *info, int key/*=0*/ )
  100. {
  101. std::map<int,std::string> pom;
  102. pom[0] = Name;
  103. init(Callback, pom, HelpBox, info->playerColoured, info->defName, &info->additionalDefs, info->x, info->y, key);
  104. }
  105. 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 */ )
  106. {
  107. std::map<int,std::string> pom;
  108. pom[0] = help.first;
  109. init(Callback, pom, help.second, playerColoredButton, defName, add, x, y, key);
  110. }
  111. void AdventureMapButton::clickLeft(tribool down, bool previousState)
  112. {
  113. if(blocked)
  114. return;
  115. if (down)
  116. state=1;
  117. else
  118. state=0;
  119. show(screenBuf);
  120. if (actOnDown && down)
  121. {
  122. callback();
  123. }
  124. else if (!actOnDown && previousState && (down==false))
  125. {
  126. callback();
  127. }
  128. }
  129. void AdventureMapButton::clickRight(tribool down, bool previousState)
  130. {
  131. if(down && helpBox.size()) //there is no point to show window with nothing inside...
  132. if(LOCPLINT)
  133. LOCPLINT->adventureInt->handleRightClick(helpBox,down,this);
  134. else
  135. GH.pushInt(new CRClickPopupInt(CMessage::genWindow(helpBox, 0),true));
  136. }
  137. void AdventureMapButton::hover (bool on)
  138. {
  139. if(hoverable)
  140. {
  141. if(on)
  142. state = 2;
  143. else
  144. state = 0;
  145. }
  146. if(pressedL)
  147. {
  148. if(on)
  149. state = 1;
  150. else
  151. state = hoverable ? 2 : 0;
  152. }
  153. ////Hoverable::hover(on);
  154. std::string *name = (vstd::contains(hoverTexts,state))
  155. ? (&hoverTexts[state])
  156. : (vstd::contains(hoverTexts,0) ? (&hoverTexts[0]) : NULL);
  157. if(LOCPLINT && name && blocked!=1) //if there is no name, there is nohing to display also
  158. {
  159. if (LOCPLINT->battleInt) //for battle buttons
  160. {
  161. if(on && LOCPLINT->battleInt->console->alterTxt == "")
  162. {
  163. LOCPLINT->battleInt->console->alterTxt = *name;
  164. LOCPLINT->battleInt->console->whoSetAlter = 1;
  165. }
  166. else if (LOCPLINT->battleInt->console->alterTxt == *name)
  167. {
  168. LOCPLINT->battleInt->console->alterTxt = "";
  169. LOCPLINT->battleInt->console->whoSetAlter = 0;
  170. }
  171. }
  172. else //for other buttons
  173. {
  174. if (on)
  175. LOCPLINT->statusbar->print(*name);
  176. else if ( LOCPLINT->statusbar->getCurrent()==(*name) )
  177. LOCPLINT->statusbar->clear();
  178. }
  179. }
  180. }
  181. void AdventureMapButton::activate()
  182. {
  183. // if (active) return;
  184. // active=true;
  185. activateLClick();
  186. activateRClick();
  187. activateHover();
  188. activateKeys();
  189. }
  190. void AdventureMapButton::deactivate()
  191. {
  192. // if (!active) return;
  193. // active=false;
  194. deactivateLClick();
  195. deactivateRClick();
  196. deactivateHover();
  197. deactivateKeys();
  198. }
  199. 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)
  200. {
  201. callback = Callback;
  202. blocked = actOnDown = false;
  203. type=2;
  204. abs=true;
  205. hoverable = false;
  206. //active=false;
  207. ourObj=NULL;
  208. assignedKeys.insert(key);
  209. state=0;
  210. hoverTexts = Name;
  211. helpBox=HelpBox;
  212. setDef(defName, playerColoredButton);
  213. if (add && add->size())
  214. {
  215. imgs.resize(imgs.size()+add->size());
  216. for (size_t i=0; i<add->size();i++)
  217. {
  218. CDefHandler *temp = CDefHandler::giveDef((*add)[i]);
  219. temp->notFreeImgs = true;
  220. for (size_t j=0;j<temp->ourImages.size();j++)
  221. {
  222. imgs[i+1].push_back(temp->ourImages[j].bitmap);
  223. if(playerColoredButton)
  224. {
  225. graphics->blueToPlayersAdv(imgs[1+i][j],LOCPLINT->playerID);
  226. }
  227. }
  228. delete temp;
  229. }
  230. //delete add;
  231. }
  232. pos.x += x;
  233. pos.y += y;
  234. pos.w = imgs[curimg][0]->w;
  235. pos.h = imgs[curimg][0]->h -1;
  236. }
  237. void AdventureMapButton::block( ui8 on )
  238. {
  239. blocked = on;
  240. state = 0;
  241. bitmapOffset = on ? 2 : 0;
  242. show(screenBuf);
  243. }
  244. void AdventureMapButton::setDef(const std::string & defName, bool playerColoredButton)
  245. {
  246. CDefHandler * temp = CDefHandler::giveDef(defName);
  247. temp->notFreeImgs = true;
  248. for (size_t i=0;i<temp->ourImages.size();i++)
  249. {
  250. imgs.resize(1);
  251. imgs[0].push_back(temp->ourImages[i].bitmap);
  252. if(playerColoredButton)
  253. {
  254. graphics->blueToPlayersAdv(imgs[curimg][i],LOCPLINT->playerID);
  255. }
  256. }
  257. delete temp;
  258. }
  259. void CHighlightableButton::select(bool on)
  260. {
  261. selected = on;
  262. state = selected ? 3 : 0;
  263. if(selected)
  264. callback();
  265. else
  266. callback2();
  267. if(hoverTexts.size()>1)
  268. {
  269. hover(false);
  270. hover(true);
  271. }
  272. }
  273. void CHighlightableButton::clickLeft(tribool down, bool previousState)
  274. {
  275. if(blocked)
  276. return;
  277. if (down)
  278. state=1;
  279. else
  280. state = selected ? 3 : 0;
  281. show(screenBuf);
  282. if(previousState && down == false)
  283. {
  284. if(!onlyOn || !selected)
  285. select(!selected);
  286. }
  287. }
  288. 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)
  289. : onlyOn(false), selected(false), callback2(onDeselect)
  290. {
  291. init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,key);
  292. }
  293. CHighlightableButton::CHighlightableButton( const std::pair<std::string, std::string> help, const CFunctionList<void()> &onSelect, int x, int y, const std::string &defName, int myid, int key/*=0*/, std::vector<std::string> * add /*= NULL*/, bool playerColoredButton /*= false */ )
  294. {
  295. ID = myid;
  296. std::map<int,std::string> pom;
  297. pom[0] = help.first;
  298. init(onSelect, pom, help.second, playerColoredButton, defName, add, x, y, key);
  299. }
  300. void CHighlightableButtonsGroup::addButton(CHighlightableButton* bt)
  301. {
  302. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  303. buttons.push_back(bt);
  304. }
  305. 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)
  306. {
  307. CHighlightableButton *bt = new CHighlightableButton(OnSelect, 0, tooltip, HelpBox, false, defName, 0, x, y, key);
  308. if(musicLike)
  309. {
  310. bt->bitmapOffset = buttons.size() - 3;
  311. }
  312. bt->ID = uid;
  313. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  314. bt->onlyOn = true;
  315. buttons.push_back(bt);
  316. }
  317. CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons)
  318. : musicLike(musicLikeButtons), onChange(OnChange)
  319. {}
  320. CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
  321. {
  322. for(size_t i=0;i<buttons.size();i++)
  323. {
  324. delete buttons[i];
  325. }
  326. }
  327. void CHighlightableButtonsGroup::activate()
  328. {
  329. for(size_t i=0;i<buttons.size();i++)
  330. {
  331. buttons[i]->activate();
  332. }
  333. }
  334. void CHighlightableButtonsGroup::deactivate()
  335. {
  336. for(size_t i=0;i<buttons.size();i++)
  337. {
  338. buttons[i]->deactivate();
  339. }
  340. }
  341. void CHighlightableButtonsGroup::select(int id, bool mode)
  342. {
  343. CHighlightableButton *bt = NULL;
  344. if(mode)
  345. {
  346. for(size_t i=0;i<buttons.size() && !bt; ++i)
  347. if (buttons[i]->ID == id)
  348. bt = buttons[i];
  349. }
  350. else
  351. {
  352. bt = buttons[id];
  353. }
  354. bt->select(true);
  355. selectionChanged(bt->ID);
  356. }
  357. void CHighlightableButtonsGroup::selectionChanged(int to)
  358. {
  359. for(size_t i=0;i<buttons.size(); ++i)
  360. if(buttons[i]->ID!=to && buttons[i]->selected)
  361. buttons[i]->select(false);
  362. onChange(to);
  363. }
  364. void CHighlightableButtonsGroup::show(SDL_Surface * to )
  365. {
  366. for(size_t i=0;i<buttons.size(); ++i)
  367. {
  368. if(!musicLike || (musicLike && buttons[i]->selected)) //if musicLike, print only selected button
  369. buttons[i]->show(to);
  370. }
  371. }
  372. void CHighlightableButtonsGroup::showAll( SDL_Surface * to )
  373. {
  374. show(to);
  375. }
  376. void CSlider::sliderClicked()
  377. {
  378. if(!moving)
  379. {
  380. activateMouseMove();
  381. moving = true;
  382. }
  383. }
  384. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  385. {
  386. float v = 0;
  387. if(horizontal)
  388. {
  389. 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 )
  390. return;
  391. v = sEvent.x - pos.x - 24;
  392. v *= positions;
  393. v /= (pos.w - 48);
  394. }
  395. else
  396. {
  397. if(std::abs(sEvent.x-(pos.x+pos.w/2)) > pos.w/2+40 || std::abs(sEvent.y-(pos.y+pos.h/2)) > pos.h/2 )
  398. return;
  399. v = sEvent.y - pos.y - 24;
  400. v *= positions;
  401. v /= (pos.h - 48);
  402. }
  403. v += 0.5f;
  404. if(v!=value)
  405. {
  406. moveTo(v);
  407. redrawSlider();
  408. }
  409. }
  410. void CSlider::redrawSlider()
  411. {
  412. slider->show(screenBuf);
  413. }
  414. void CSlider::moveLeft()
  415. {
  416. moveTo(value-1);
  417. }
  418. void CSlider::moveRight()
  419. {
  420. moveTo(value+1);
  421. }
  422. void CSlider::moveTo(int to)
  423. {
  424. amax(to, 0);
  425. amin(to, positions);
  426. //same, old position?
  427. if(value == to)
  428. return;
  429. value = to;
  430. if(horizontal)
  431. {
  432. if(positions)
  433. {
  434. float part = (float)to/positions;
  435. part*=(pos.w-48);
  436. slider->pos.x = part + pos.x + 16;
  437. }
  438. else
  439. slider->pos.x = pos.x+16;
  440. }
  441. else
  442. {
  443. if(positions)
  444. {
  445. float part = (float)to/positions;
  446. part*=(pos.h-48);
  447. slider->pos.y = part + pos.y + 16;
  448. }
  449. else
  450. slider->pos.y = pos.y+16;
  451. }
  452. moved(to);
  453. }
  454. void CSlider::clickLeft(tribool down, bool previousState)
  455. {
  456. if(down)
  457. {
  458. float pw = 0;
  459. float rw = 0;
  460. if(horizontal)
  461. {
  462. pw = GH.current->motion.x-pos.x-24;
  463. rw = pw / ((float)(pos.w-48));
  464. }
  465. else
  466. {
  467. pw = GH.current->motion.y-pos.y-24;
  468. rw = pw / ((float)(pos.h-48));
  469. }
  470. if(pw < -8 || pw > (horizontal ? pos.w : pos.h) + 8)
  471. return;
  472. // if (rw>1) return;
  473. // if (rw<0) return;
  474. moveTo(rw * positions + 0.5f);
  475. return;
  476. }
  477. if(moving)
  478. {
  479. deactivateMouseMove();
  480. moving = false;
  481. }
  482. }
  483. CSlider::~CSlider()
  484. {
  485. delete imgs;
  486. }
  487. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, int style)
  488. :capacity(Capacity),amount(Amount),horizontal(Horizontal), moved(Moved)
  489. {
  490. OBJ_CONSTRUCTION;
  491. setAmount(amount);
  492. used = LCLICK;
  493. moving = false;
  494. strongInterest = true;
  495. left = new AdventureMapButton;
  496. right = new AdventureMapButton;
  497. slider = new AdventureMapButton;
  498. if(horizontal)
  499. {
  500. left->pos.y = slider->pos.y = right->pos.y = pos.y = y;
  501. left->pos.x = pos.x = x;
  502. right->pos.x = x + totalw - 16;
  503. }
  504. else
  505. {
  506. left->pos.x = slider->pos.x = right->pos.x = pos.x = x;
  507. left->pos.y = pos.y = y;
  508. right->pos.y = y + totalw - 16;
  509. }
  510. left->callback = boost::bind(&CSlider::moveLeft,this);
  511. right->callback = boost::bind(&CSlider::moveRight,this);
  512. slider->callback = boost::bind(&CSlider::sliderClicked,this);
  513. left->pos.w = left->pos.h = right->pos.w = right->pos.h = slider->pos.w = slider->pos.h = 16;
  514. if(horizontal)
  515. {
  516. pos.h = 16;
  517. pos.w = totalw;
  518. }
  519. else
  520. {
  521. pos.w = 16;
  522. pos.h = totalw;
  523. }
  524. if(style == 0)
  525. {
  526. imgs = CDefHandler::giveDefEss("IGPCRDIV.DEF");
  527. left->imgs.resize(1); right->imgs.resize(1); slider->imgs.resize(1);
  528. left->imgs[0].push_back(imgs->ourImages[0].bitmap); left->imgs[0].push_back(imgs->ourImages[1].bitmap);
  529. right->imgs[0].push_back(imgs->ourImages[2].bitmap); right->imgs[0].push_back(imgs->ourImages[3].bitmap);
  530. slider->imgs[0].push_back(imgs->ourImages[4].bitmap);
  531. left->notFreeButton = right->notFreeButton = slider->notFreeButton = true;
  532. }
  533. else
  534. {
  535. imgs = NULL;
  536. left->setDef(horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF", false);
  537. right->setDef(horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF", false);
  538. slider->setDef("SCNRBSL.DEF", false);
  539. }
  540. slider->actOnDown = true;
  541. value = -1;
  542. moveTo(Value);
  543. }
  544. void CSlider::block( bool on )
  545. {
  546. left->block(on);
  547. right->block(on);
  548. slider->block(on);
  549. }
  550. void CSlider::setAmount( int to )
  551. {
  552. positions = to - capacity;
  553. amax(positions, 1);
  554. }