AdventureMapButton.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. #include "StdInc.h"
  2. #include "AdventureMapButton.h"
  3. #include "CAnimation.h"
  4. #include "SDL_Extensions.h"
  5. #include "CGameInfo.h"
  6. #include "../CCallback.h"
  7. #include "CConfigHandler.h"
  8. #include "BattleInterface/CBattleInterface.h"
  9. #include "BattleInterface/CBattleConsole.h"
  10. #include "CPlayerInterface.h"
  11. #include "CMessage.h"
  12. #include "CMusicHandler.h"
  13. #include "GUIClasses.h"
  14. /*
  15. * AdventureMapButton.cpp, part of VCMI engine
  16. *
  17. * Authors: listed in file AUTHORS in main folder
  18. *
  19. * License: GNU General Public License v2.0 or later
  20. * Full text of license available in license.txt file, in main folder
  21. *
  22. */
  23. CButtonBase::CButtonBase()
  24. {
  25. swappedImages = keepFrame = false;
  26. bitmapOffset = 0;
  27. state=NORMAL;
  28. image = NULL;
  29. text = NULL;
  30. }
  31. CButtonBase::~CButtonBase()
  32. {
  33. }
  34. void CButtonBase::update()
  35. {
  36. if (text)
  37. {
  38. if (state == PRESSED)
  39. text->moveTo(Point(pos.x+pos.w/2+1, pos.y+pos.h/2+1));
  40. else
  41. text->moveTo(Point(pos.x+pos.w/2, pos.y+pos.h/2));
  42. }
  43. int newPos = (int)state + bitmapOffset;
  44. if (newPos < 0)
  45. newPos = 0;
  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. if (buttons.size() > 3)
  310. bt->setOffset(buttons.size()-3);
  311. }
  312. bt->ID = uid;
  313. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  314. bt->onlyOn = true;
  315. buttons.push_back(bt);
  316. }
  317. CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons)
  318. : onChange(OnChange), musicLike(musicLikeButtons)
  319. {}
  320. CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
  321. {
  322. }
  323. void CHighlightableButtonsGroup::select(int id, bool mode)
  324. {
  325. CHighlightableButton *bt = NULL;
  326. if(mode)
  327. {
  328. for(size_t i=0;i<buttons.size() && !bt; ++i)
  329. if (buttons[i]->ID == id)
  330. bt = buttons[i];
  331. }
  332. else
  333. {
  334. bt = buttons[id];
  335. }
  336. bt->select(true);
  337. selectionChanged(bt->ID);
  338. }
  339. void CHighlightableButtonsGroup::selectionChanged(int to)
  340. {
  341. for(size_t i=0;i<buttons.size(); ++i)
  342. if(buttons[i]->ID!=to && buttons[i]->isHighlighted())
  343. buttons[i]->select(false);
  344. onChange(to);
  345. if (parent)
  346. parent->redraw();
  347. }
  348. void CHighlightableButtonsGroup::show(SDL_Surface * to )
  349. {
  350. if (musicLike)
  351. {
  352. for(size_t i=0;i<buttons.size(); ++i)
  353. if(buttons[i]->isHighlighted())
  354. buttons[i]->show(to);
  355. }
  356. else
  357. CIntObject::show(to);
  358. }
  359. void CHighlightableButtonsGroup::showAll( SDL_Surface * to )
  360. {
  361. if (musicLike)
  362. {
  363. for(size_t i=0;i<buttons.size(); ++i)
  364. if(buttons[i]->isHighlighted())
  365. buttons[i]->showAll(to);
  366. }
  367. else
  368. CIntObject::showAll(to);
  369. }
  370. void CHighlightableButtonsGroup::block( ui8 on )
  371. {
  372. for(size_t i=0;i<buttons.size(); ++i)
  373. {
  374. buttons[i]->block(on);
  375. }
  376. }
  377. void CSlider::sliderClicked()
  378. {
  379. if(!(active & MOVE))
  380. {
  381. activateMouseMove();
  382. used |= MOVE;
  383. }
  384. }
  385. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  386. {
  387. double v = 0;
  388. if(horizontal)
  389. {
  390. 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 )
  391. return;
  392. v = sEvent.x - pos.x - 24;
  393. v *= positions;
  394. v /= (pos.w - 48);
  395. }
  396. else
  397. {
  398. 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 )
  399. return;
  400. v = sEvent.y - pos.y - 24;
  401. v *= positions;
  402. v /= (pos.h - 48);
  403. }
  404. v += 0.5;
  405. if(v!=value)
  406. {
  407. moveTo(v);
  408. redrawSlider();
  409. }
  410. }
  411. void CSlider::redrawSlider()
  412. {
  413. //slider->show(screenBuf);
  414. }
  415. void CSlider::moveLeft()
  416. {
  417. moveTo(value-1);
  418. }
  419. void CSlider::moveRight()
  420. {
  421. moveTo(value+1);
  422. }
  423. void CSlider::moveTo(int to)
  424. {
  425. vstd::amax(to, 0);
  426. vstd::amin(to, positions);
  427. //same, old position?
  428. if(value == to)
  429. return;
  430. value = to;
  431. if(horizontal)
  432. {
  433. if(positions)
  434. {
  435. double part = static_cast<double>(to) / positions;
  436. part*=(pos.w-48);
  437. int newPos = part + pos.x + 16 - slider->pos.x;
  438. slider->moveBy(Point(newPos, 0));
  439. }
  440. else
  441. slider->moveTo(Point(pos.x+16, pos.y));
  442. }
  443. else
  444. {
  445. if(positions)
  446. {
  447. double part = static_cast<double>(to) / positions;
  448. part*=(pos.h-48);
  449. int newPos = part + pos.y + 16 - slider->pos.y;
  450. slider->moveBy(Point(0, newPos));
  451. }
  452. else
  453. slider->moveTo(Point(pos.x, pos.y+16));
  454. }
  455. if(moved)
  456. moved(to);
  457. }
  458. void CSlider::clickLeft(tribool down, bool previousState)
  459. {
  460. if(down && !slider->isBlocked())
  461. {
  462. double pw = 0;
  463. double rw = 0;
  464. if(horizontal)
  465. {
  466. pw = GH.current->motion.x-pos.x-25;
  467. rw = pw / static_cast<double>(pos.w - 48);
  468. }
  469. else
  470. {
  471. pw = GH.current->motion.y-pos.y-24;
  472. rw = pw / (pos.h-48);
  473. }
  474. if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
  475. return;
  476. // if (rw>1) return;
  477. // if (rw<0) return;
  478. slider->clickLeft(true, slider->pressedL);
  479. moveTo(rw * positions + 0.5);
  480. return;
  481. }
  482. if(active & MOVE)
  483. {
  484. deactivateMouseMove();
  485. used &= ~MOVE;
  486. }
  487. }
  488. CSlider::~CSlider()
  489. {
  490. }
  491. CSlider::CSlider(int x, int y, int totalw, boost::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, int style)
  492. :capacity(Capacity),amount(Amount),horizontal(Horizontal), moved(Moved)
  493. {
  494. OBJ_CONSTRUCTION_CAPTURING_ALL;
  495. setAmount(amount);
  496. used = LCLICK;
  497. strongInterest = true;
  498. left = new AdventureMapButton();
  499. right = new AdventureMapButton();
  500. slider = new AdventureMapButton();
  501. pos.x += x;
  502. pos.y += y;
  503. if(horizontal)
  504. {
  505. left->pos.y = slider->pos.y = right->pos.y = pos.y;
  506. left->pos.x = pos.x;
  507. right->pos.x = pos.x + totalw - 16;
  508. }
  509. else
  510. {
  511. left->pos.x = slider->pos.x = right->pos.x = pos.x;
  512. left->pos.y = pos.y;
  513. right->pos.y = pos.y + totalw - 16;
  514. }
  515. left->callback = boost::bind(&CSlider::moveLeft,this);
  516. right->callback = boost::bind(&CSlider::moveRight,this);
  517. slider->callback = boost::bind(&CSlider::sliderClicked,this);
  518. left->pos.w = left->pos.h = right->pos.w = right->pos.h = slider->pos.w = slider->pos.h = 16;
  519. if(horizontal)
  520. {
  521. pos.h = 16;
  522. pos.w = totalw;
  523. }
  524. else
  525. {
  526. pos.w = 16;
  527. pos.h = totalw;
  528. }
  529. if(style == 0)
  530. {
  531. std::string name = horizontal?"IGPCRDIV.DEF":"OVBUTN2.DEF";
  532. CAnimation *animLeft = new CAnimation(name);
  533. left->setImage(animLeft);
  534. left->setOffset(0);
  535. CAnimation *animRight = new CAnimation(name);
  536. right->setImage(animRight);
  537. right->setOffset(2);
  538. CAnimation *animSlider = new CAnimation(name);
  539. slider->setImage(animSlider);
  540. slider->setOffset(4);
  541. }
  542. else
  543. {
  544. left->setImage(new CAnimation(horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF"));
  545. right->setImage(new CAnimation(horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF"));
  546. slider->setImage(new CAnimation("SCNRBSL.DEF"));
  547. }
  548. slider->actOnDown = true;
  549. slider->soundDisabled = true;
  550. left->soundDisabled = true;
  551. right->soundDisabled = true;
  552. value = -1;
  553. moveTo(Value);
  554. }
  555. void CSlider::block( bool on )
  556. {
  557. left->block(on);
  558. right->block(on);
  559. slider->block(on);
  560. }
  561. void CSlider::setAmount( int to )
  562. {
  563. amount = to;
  564. positions = to - capacity;
  565. vstd::amax(positions, 0);
  566. }
  567. void CSlider::showAll(SDL_Surface * to)
  568. {
  569. CSDL_Ext::fillRect(to, &pos, 0);
  570. CIntObject::showAll(to);
  571. }
  572. void CSlider::wheelScrolled(bool down, bool in)
  573. {
  574. moveTo(value + 3 * (down ? +1 : -1));
  575. }
  576. void CSlider::keyPressed(const SDL_KeyboardEvent & key)
  577. {
  578. if(key.state != SDL_PRESSED) return;
  579. int moveDest = 0;
  580. switch(key.keysym.sym)
  581. {
  582. case SDLK_UP:
  583. moveDest = value - 1;
  584. break;
  585. case SDLK_DOWN:
  586. moveDest = value + 1;
  587. break;
  588. case SDLK_PAGEUP:
  589. moveDest = value - capacity + 1;
  590. break;
  591. case SDLK_PAGEDOWN:
  592. moveDest = value + capacity - 1;
  593. break;
  594. case SDLK_HOME:
  595. moveDest = 0;
  596. break;
  597. case SDLK_END:
  598. moveDest = amount - capacity;
  599. break;
  600. default:
  601. return;
  602. }
  603. moveTo(moveDest);
  604. }
  605. void CSlider::moveToMax()
  606. {
  607. moveTo(amount);
  608. }