AdventureMapButton.cpp 16 KB

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