AdventureMapButton.cpp 16 KB

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