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