AdventureMapButton.cpp 15 KB

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