AdventureMapButton.cpp 15 KB

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