AdventureMapButton.cpp 16 KB

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