AdventureMapButton.cpp 16 KB

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