AdventureMapButton.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. #include "UIFramework/CGuiHandler.h"
  15. /*
  16. * AdventureMapButton.cpp, part of VCMI engine
  17. *
  18. * Authors: listed in file AUTHORS in main folder
  19. *
  20. * License: GNU General Public License v2.0 or later
  21. * Full text of license available in license.txt file, in main folder
  22. *
  23. */
  24. CButtonBase::CButtonBase()
  25. {
  26. swappedImages = keepFrame = false;
  27. bitmapOffset = 0;
  28. state=NORMAL;
  29. image = NULL;
  30. text = NULL;
  31. }
  32. CButtonBase::~CButtonBase()
  33. {
  34. }
  35. void CButtonBase::update()
  36. {
  37. if (text)
  38. {
  39. if (state == PRESSED)
  40. text->moveTo(SPoint(pos.x+pos.w/2+1, pos.y+pos.h/2+1));
  41. else
  42. text->moveTo(SPoint(pos.x+pos.w/2, pos.y+pos.h/2));
  43. }
  44. int newPos = (int)state + bitmapOffset;
  45. if (newPos < 0)
  46. newPos = 0;
  47. if (state == HIGHLIGHTED && image->size() < 4)
  48. newPos = image->size()-1;
  49. if (swappedImages)
  50. {
  51. if (newPos == 0) newPos = 1;
  52. else if (newPos == 1) newPos = 0;
  53. }
  54. if (!keepFrame)
  55. image->setFrame(newPos);
  56. if (active)
  57. redraw();
  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::showAll(SDL_Surface *to)
  232. {
  233. CIntObject::showAll(to);
  234. if (borderEnabled && borderColor.unused == 0)
  235. CSDL_Ext::drawBorder(to, pos.x - 1, pos.y - 1, pos.w + 2, pos.h + 2, int3(borderColor.r, borderColor.g, borderColor.b));
  236. }
  237. void CHighlightableButton::select(bool on)
  238. {
  239. selected = on;
  240. if (on)
  241. {
  242. setState(HIGHLIGHTED);
  243. callback();
  244. borderEnabled = true;
  245. }
  246. else
  247. {
  248. setState(NORMAL);
  249. callback2();
  250. borderEnabled = false;
  251. }
  252. if(hoverTexts.size()>1)
  253. {
  254. hover(false);
  255. hover(true);
  256. }
  257. }
  258. void CHighlightableButton::clickLeft(tribool down, bool previousState)
  259. {
  260. if(isBlocked())
  261. return;
  262. if (down && !(onlyOn && isHighlighted()))
  263. {
  264. CCS->soundh->playSound(soundBase::button);
  265. setState(PRESSED);
  266. }
  267. if(previousState && down == false && getState() == PRESSED)
  268. {
  269. //if(!onlyOn || !isHighlighted())
  270. select(!selected);
  271. }
  272. }
  273. 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)
  274. : onlyOn(false), selected(false), callback2(onDeselect)
  275. {
  276. init(onSelect,Name,HelpBox,playerColoredButton,defName,add,x,y,key);
  277. }
  278. 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 */ )
  279. : onlyOn(false), selected(false) // TODO: callback2(???)
  280. {
  281. ID = myid;
  282. std::map<int,std::string> pom;
  283. pom[0] = help.first;
  284. init(onSelect, pom, help.second, playerColoredButton, defName, add, x, y, key);
  285. }
  286. 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 */ )
  287. : onlyOn(false), selected(false) // TODO: callback2(???)
  288. {
  289. ID = myid;
  290. std::map<int,std::string> pom;
  291. pom[0] = Name;
  292. init(onSelect, pom,HelpBox, playerColoredButton, defName, add, x, y, key);
  293. }
  294. void CHighlightableButtonsGroup::addButton(CHighlightableButton* bt)
  295. {
  296. if (bt->parent)
  297. bt->parent->removeChild(bt);
  298. addChild(bt);
  299. bt->recActions = defActions;//FIXME: not needed?
  300. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  301. bt->onlyOn = true;
  302. buttons.push_back(bt);
  303. }
  304. 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)
  305. {
  306. OBJ_CONSTRUCTION_CAPTURING_ALL;
  307. CHighlightableButton *bt = new CHighlightableButton(OnSelect, 0, tooltip, HelpBox, false, defName, 0, x, y, key);
  308. if(musicLike)
  309. {
  310. if (buttons.size() > 3)
  311. bt->setOffset(buttons.size()-3);
  312. }
  313. bt->ID = uid;
  314. bt->callback += boost::bind(&CHighlightableButtonsGroup::selectionChanged,this,bt->ID);
  315. bt->onlyOn = true;
  316. buttons.push_back(bt);
  317. }
  318. CHighlightableButtonsGroup::CHighlightableButtonsGroup(const CFunctionList2<void(int)> &OnChange, bool musicLikeButtons)
  319. : onChange(OnChange), musicLike(musicLikeButtons)
  320. {}
  321. CHighlightableButtonsGroup::~CHighlightableButtonsGroup()
  322. {
  323. }
  324. void CHighlightableButtonsGroup::select(int id, bool mode)
  325. {
  326. CHighlightableButton *bt = NULL;
  327. if(mode)
  328. {
  329. for(size_t i=0;i<buttons.size() && !bt; ++i)
  330. if (buttons[i]->ID == id)
  331. bt = buttons[i];
  332. }
  333. else
  334. {
  335. bt = buttons[id];
  336. }
  337. bt->select(true);
  338. selectionChanged(bt->ID);
  339. }
  340. void CHighlightableButtonsGroup::selectionChanged(int to)
  341. {
  342. for(size_t i=0;i<buttons.size(); ++i)
  343. if(buttons[i]->ID!=to && buttons[i]->isHighlighted())
  344. buttons[i]->select(false);
  345. onChange(to);
  346. if (parent)
  347. parent->redraw();
  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. double 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.5;
  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. vstd::amax(to, 0);
  427. vstd::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. double part = static_cast<double>(to) / positions;
  437. part*=(pos.w-48);
  438. int newPos = part + pos.x + 16 - slider->pos.x;
  439. slider->moveBy(SPoint(newPos, 0));
  440. }
  441. else
  442. slider->moveTo(SPoint(pos.x+16, pos.y));
  443. }
  444. else
  445. {
  446. if(positions)
  447. {
  448. double part = static_cast<double>(to) / positions;
  449. part*=(pos.h-48);
  450. int newPos = part + pos.y + 16 - slider->pos.y;
  451. slider->moveBy(SPoint(0, newPos));
  452. }
  453. else
  454. slider->moveTo(SPoint(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. double pw = 0;
  464. double rw = 0;
  465. if(horizontal)
  466. {
  467. pw = GH.current->motion.x-pos.x-25;
  468. rw = pw / static_cast<double>(pos.w - 48);
  469. }
  470. else
  471. {
  472. pw = GH.current->motion.y-pos.y-24;
  473. rw = pw / (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.5);
  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. vstd::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. }