AdventureMapButton.cpp 16 KB

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