AdventureMapButton.cpp 16 KB

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