AdventureMapButton.cpp 16 KB

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