AdventureMapButton.cpp 16 KB

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