AdventureMapButton.cpp 16 KB

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