Buttons.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. #include "StdInc.h"
  2. #include "Buttons.h"
  3. #include "Images.h"
  4. #include "TextControls.h"
  5. #include "../CMusicHandler.h"
  6. #include "../CGameInfo.h"
  7. #include "../CPlayerInterface.h"
  8. #include "../battle/CBattleInterface.h"
  9. #include "../battle/CBattleInterfaceClasses.h"
  10. #include "../gui/CAnimation.h"
  11. #include "../gui/CGuiHandler.h"
  12. #include "../windows/InfoWindows.h"
  13. #include "../../lib/CConfigHandler.h"
  14. /*
  15. * Buttons.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. ClickableArea::ClickableArea(CIntObject * object, CFunctionList<void()> callback):
  24. callback(callback),
  25. area(nullptr)
  26. {
  27. if (object)
  28. pos = object->pos;
  29. setArea(object);
  30. }
  31. void ClickableArea::addCallback(std::function<void()> callback)
  32. {
  33. this->callback += callback;
  34. }
  35. void ClickableArea::setArea(CIntObject * object)
  36. {
  37. delete area;
  38. addChild(area);
  39. pos.w = object->pos.w;
  40. pos.h = object->pos.h;
  41. }
  42. void ClickableArea::onClick()
  43. {
  44. callback();
  45. }
  46. void ClickableArea::clickLeft(tribool down, bool previousState)
  47. {
  48. if (down)
  49. onClick();
  50. }
  51. void CButton::update()
  52. {
  53. if (overlay)
  54. {
  55. if (state == PRESSED)
  56. overlay->moveTo(overlay->pos.centerIn(pos).topLeft() + Point(1,1));
  57. else
  58. overlay->moveTo(overlay->pos.centerIn(pos).topLeft());
  59. }
  60. int newPos = stateToIndex[int(state)];
  61. if (newPos < 0)
  62. newPos = 0;
  63. if (state == HIGHLIGHTED && image->size() < 4)
  64. newPos = image->size()-1;
  65. image->setFrame(newPos);
  66. if (active)
  67. redraw();
  68. }
  69. void CButton::addCallback(std::function<void()> callback)
  70. {
  71. this->callback += callback;
  72. }
  73. void CButton::addTextOverlay( const std::string &Text, EFonts font, SDL_Color color)
  74. {
  75. OBJ_CONSTRUCTION_CAPTURING_ALL;
  76. addOverlay(new CLabel(pos.w/2, pos.h/2, font, CENTER, color, Text));
  77. update();
  78. }
  79. void CButton::addOverlay(CIntObject *newOverlay)
  80. {
  81. delete overlay;
  82. overlay = newOverlay;
  83. addChild(newOverlay);
  84. overlay->moveTo(overlay->pos.centerIn(pos).topLeft());
  85. update();
  86. }
  87. void CButton::addImage(std::string filename)
  88. {
  89. imageNames.push_back(filename);
  90. }
  91. void CButton::addHoverText(ButtonState state, std::string text)
  92. {
  93. hoverTexts[state] = text;
  94. }
  95. void CButton::setImageOrder(int state1, int state2, int state3, int state4)
  96. {
  97. stateToIndex[0] = state1;
  98. stateToIndex[1] = state2;
  99. stateToIndex[2] = state3;
  100. stateToIndex[3] = state4;
  101. update();
  102. }
  103. void CButton::setState(ButtonState newState)
  104. {
  105. if (state == newState)
  106. return;
  107. state = newState;
  108. update();
  109. }
  110. CButton::ButtonState CButton::getState()
  111. {
  112. return state;
  113. }
  114. bool CButton::isBlocked()
  115. {
  116. return state == BLOCKED;
  117. }
  118. bool CButton::isHighlighted()
  119. {
  120. return state == HIGHLIGHTED;
  121. }
  122. void CButton::block(bool on)
  123. {
  124. setState(on?BLOCKED:NORMAL);
  125. }
  126. void CButton::onButtonClicked()
  127. {
  128. // debug logging to figure out pressed button (and as result - player actions) in case of crash
  129. logAnim->traceStream() << "Button clicked at " << pos.x << "x" << pos.y << ", " << callback.funcs.size() << " functions";
  130. CIntObject * parent = this->parent;
  131. std::string prefix = "Parent is";
  132. while (parent)
  133. {
  134. logAnim->traceStream() << prefix << typeid(*parent).name() << " at " << parent->pos.x << "x" << parent->pos.y;
  135. parent = parent->parent;
  136. prefix = '\t' + prefix;
  137. }
  138. callback();
  139. }
  140. void CButton::clickLeft(tribool down, bool previousState)
  141. {
  142. if(isBlocked())
  143. return;
  144. if (down)
  145. {
  146. if (!soundDisabled)
  147. CCS->soundh->playSound(soundBase::button);
  148. setState(PRESSED);
  149. }
  150. else if(hoverable && hovered)
  151. setState(HIGHLIGHTED);
  152. else
  153. setState(NORMAL);
  154. if (actOnDown && down)
  155. {
  156. onButtonClicked();
  157. }
  158. else if (!actOnDown && previousState && (down==false))
  159. {
  160. onButtonClicked();
  161. }
  162. }
  163. void CButton::clickRight(tribool down, bool previousState)
  164. {
  165. if(down && helpBox.size()) //there is no point to show window with nothing inside...
  166. CRClickPopup::createAndPush(helpBox);
  167. }
  168. void CButton::hover (bool on)
  169. {
  170. if(hoverable)
  171. {
  172. if(on)
  173. setState(HIGHLIGHTED);
  174. else
  175. setState(NORMAL);
  176. }
  177. if(pressedL && on)
  178. setState(PRESSED);
  179. std::string name = hoverTexts[getState()].empty()
  180. ? hoverTexts[getState()]
  181. : hoverTexts[0];
  182. if(!name.empty() && !isBlocked()) //if there is no name, there is nohing to display also
  183. {
  184. if (LOCPLINT && LOCPLINT->battleInt) //for battle buttons
  185. {
  186. if(on && LOCPLINT->battleInt->console->alterTxt == "")
  187. {
  188. LOCPLINT->battleInt->console->alterTxt = name;
  189. LOCPLINT->battleInt->console->whoSetAlter = 1;
  190. }
  191. else if (LOCPLINT->battleInt->console->alterTxt == name)
  192. {
  193. LOCPLINT->battleInt->console->alterTxt = "";
  194. LOCPLINT->battleInt->console->whoSetAlter = 0;
  195. }
  196. }
  197. else if(GH.statusbar) //for other buttons
  198. {
  199. if (on)
  200. GH.statusbar->setText(name);
  201. else if ( GH.statusbar->getText()==(name) )
  202. GH.statusbar->clear();
  203. }
  204. }
  205. }
  206. CButton::CButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help, CFunctionList<void()> Callback, int key, bool playerColoredButton):
  207. CKeyShortcut(key),
  208. callback(Callback)
  209. {
  210. addUsedEvents(LCLICK | RCLICK | HOVER | KEYBOARD);
  211. stateToIndex[0] = 0;
  212. stateToIndex[1] = 1;
  213. stateToIndex[2] = 2;
  214. stateToIndex[3] = 3;
  215. state=NORMAL;
  216. image = nullptr;
  217. overlay = nullptr;
  218. currentImage = -1;
  219. hoverable = actOnDown = soundDisabled = false;
  220. hoverTexts[0] = help.first;
  221. helpBox=help.second;
  222. pos.x += position.x;
  223. pos.y += position.y;
  224. if (!defName.empty())
  225. {
  226. imageNames.push_back(defName);
  227. setIndex(0, playerColoredButton);
  228. }
  229. }
  230. void CButton::setIndex(size_t index, bool playerColoredButton)
  231. {
  232. if (index == currentImage || index>=imageNames.size())
  233. return;
  234. currentImage = index;
  235. setImage(new CAnimation(imageNames[index]), playerColoredButton);
  236. }
  237. void CButton::setImage(CAnimation* anim, bool playerColoredButton, int animFlags)
  238. {
  239. OBJ_CONSTRUCTION_CAPTURING_ALL;
  240. image = new CAnimImage(anim, getState(), 0, 0, 0, animFlags);
  241. if (playerColoredButton)
  242. image->playerColored(LOCPLINT->playerID);
  243. pos = image->pos;
  244. }
  245. void CButton::setPlayerColor(PlayerColor player)
  246. {
  247. if (image)
  248. image->playerColored(player);
  249. }
  250. void CButton::showAll(SDL_Surface * to)
  251. {
  252. CIntObject::showAll(to);
  253. #ifdef VCMI_SDL1
  254. if (borderColor && borderColor->unused == 0)
  255. CSDL_Ext::drawBorder(to, pos.x-1, pos.y-1, pos.w+2, pos.h+2, int3(borderColor->r, borderColor->g, borderColor->b));
  256. #else
  257. if (borderColor && borderColor->a == 0)
  258. CSDL_Ext::drawBorder(to, pos.x-1, pos.y-1, pos.w+2, pos.h+2, int3(borderColor->r, borderColor->g, borderColor->b));
  259. #endif // 0
  260. }
  261. std::pair<std::string, std::string> CButton::tooltip()
  262. {
  263. return std::pair<std::string, std::string>();
  264. }
  265. std::pair<std::string, std::string> CButton::tooltip(const JsonNode & localizedTexts)
  266. {
  267. return std::make_pair(localizedTexts["label"].String(), localizedTexts["help"].String());
  268. }
  269. std::pair<std::string, std::string> CButton::tooltip(const std::string & hover, const std::string & help)
  270. {
  271. return std::make_pair(hover, help);
  272. }
  273. CToggleBase::CToggleBase(CFunctionList<void (bool)> callback):
  274. callback(callback)
  275. {
  276. }
  277. CToggleBase::~CToggleBase()
  278. {
  279. }
  280. void CToggleBase::doSelect(bool on)
  281. {
  282. // for overrides
  283. }
  284. void CToggleBase::setSelected(bool on)
  285. {
  286. selected = on;
  287. doSelect(on);
  288. }
  289. void CToggleBase::activate()
  290. {
  291. if (canActivate())
  292. setSelected(!selected);
  293. }
  294. bool CToggleBase::canActivate()
  295. {
  296. if (selected && !allowDeselection)
  297. return false;
  298. return true;
  299. }
  300. void CToggleBase::addCallback(std::function<void(bool)> function)
  301. {
  302. callback.add(function);
  303. }
  304. CToggleButton::CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  305. CFunctionList<void(bool)> callback, int key, bool playerColoredButton):
  306. CButton(position, defName, help, 0, key, playerColoredButton),
  307. CToggleBase(callback)
  308. {
  309. }
  310. void CToggleButton::doSelect(bool on)
  311. {
  312. if (on)
  313. {
  314. setState(HIGHLIGHTED);
  315. }
  316. else
  317. {
  318. setState(NORMAL);
  319. }
  320. }
  321. void CToggleButton::clickLeft(tribool down, bool previousState)
  322. {
  323. // force refresh
  324. hover(false);
  325. hover(true);
  326. if(isBlocked())
  327. return;
  328. if (down && canActivate())
  329. {
  330. CCS->soundh->playSound(soundBase::button);
  331. setState(PRESSED);
  332. }
  333. if(previousState)//mouse up
  334. {
  335. if(down == false && getState() == PRESSED)
  336. setSelected(!selected);
  337. else
  338. setSelected(selected);
  339. }
  340. }
  341. void CToggleGroup::addCallback(std::function<void(int)> callback)
  342. {
  343. onChange += callback;
  344. }
  345. void CToggleGroup::addToggle(int identifier, CToggleBase* bt)
  346. {
  347. if (auto intObj = dynamic_cast<CIntObject*>(bt)) // hack-ish workagound to avoid diamond problem with inheritance
  348. {
  349. if (intObj->parent)
  350. intObj->parent->removeChild(intObj);
  351. addChild(intObj);
  352. }
  353. bt->addCallback(boost::bind(&CToggleGroup::selectionChanged, this, identifier));
  354. bt->allowDeselection = false;
  355. assert(!buttons.count(identifier));
  356. buttons[identifier] = bt;
  357. }
  358. CToggleGroup::CToggleGroup(const CFunctionList<void(int)> &OnChange, bool musicLikeButtons)
  359. : onChange(OnChange), musicLike(musicLikeButtons)
  360. {}
  361. void CToggleGroup::setSelected(int id)
  362. {
  363. assert(!buttons.empty());
  364. auto bt = buttons[id];
  365. if (bt)
  366. {
  367. bt->setSelected(true);
  368. selectionChanged(id);
  369. }
  370. }
  371. void CToggleGroup::selectionChanged(int to)
  372. {
  373. if (buttons.count(selectedID))
  374. buttons[selectedID]->setSelected(false);
  375. if (buttons.count(to))
  376. buttons[to]->setSelected(true);
  377. selectedID = to;
  378. onChange(to);
  379. if (parent)
  380. parent->redraw();
  381. }
  382. void CToggleGroup::show(SDL_Surface * to)
  383. {
  384. if (musicLike)
  385. {
  386. if (auto intObj = dynamic_cast<CIntObject*>(buttons[selectedID])) // hack-ish workagound to avoid diamond problem with inheritance
  387. intObj->show(to);
  388. }
  389. else
  390. CIntObject::show(to);
  391. }
  392. void CToggleGroup::showAll(SDL_Surface * to)
  393. {
  394. if (musicLike)
  395. {
  396. if (auto intObj = dynamic_cast<CIntObject*>(buttons[selectedID])) // hack-ish workagound to avoid diamond problem with inheritance
  397. intObj->showAll(to);
  398. }
  399. else
  400. CIntObject::showAll(to);
  401. }
  402. void CSlider::sliderClicked()
  403. {
  404. if(!(active & MOVE))
  405. addUsedEvents(MOVE);
  406. }
  407. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  408. {
  409. double v = 0;
  410. if(horizontal)
  411. {
  412. 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 )
  413. return;
  414. v = sEvent.x - pos.x - 24;
  415. v *= positions;
  416. v /= (pos.w - 48);
  417. }
  418. else
  419. {
  420. 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 )
  421. return;
  422. v = sEvent.y - pos.y - 24;
  423. v *= positions;
  424. v /= (pos.h - 48);
  425. }
  426. v += 0.5;
  427. if(v!=value)
  428. {
  429. moveTo(v);
  430. redrawSlider();
  431. }
  432. }
  433. void CSlider::redrawSlider()
  434. {
  435. //slider->show(screenBuf);
  436. }
  437. void CSlider::moveLeft()
  438. {
  439. moveTo(value-1);
  440. }
  441. void CSlider::moveRight()
  442. {
  443. moveTo(value+1);
  444. }
  445. void CSlider::moveTo(int to)
  446. {
  447. vstd::amax(to, 0);
  448. vstd::amin(to, positions);
  449. //same, old position?
  450. if(value == to)
  451. return;
  452. value = to;
  453. if(horizontal)
  454. {
  455. if(positions)
  456. {
  457. double part = static_cast<double>(to) / positions;
  458. part*=(pos.w-48);
  459. int newPos = part + pos.x + 16 - slider->pos.x;
  460. slider->moveBy(Point(newPos, 0));
  461. }
  462. else
  463. slider->moveTo(Point(pos.x+16, pos.y));
  464. }
  465. else
  466. {
  467. if(positions)
  468. {
  469. double part = static_cast<double>(to) / positions;
  470. part*=(pos.h-48);
  471. int newPos = part + pos.y + 16 - slider->pos.y;
  472. slider->moveBy(Point(0, newPos));
  473. }
  474. else
  475. slider->moveTo(Point(pos.x, pos.y+16));
  476. }
  477. if(moved)
  478. moved(to);
  479. }
  480. void CSlider::clickLeft(tribool down, bool previousState)
  481. {
  482. if(down && !slider->isBlocked())
  483. {
  484. double pw = 0;
  485. double rw = 0;
  486. if(horizontal)
  487. {
  488. pw = GH.current->motion.x-pos.x-25;
  489. rw = pw / static_cast<double>(pos.w - 48);
  490. }
  491. else
  492. {
  493. pw = GH.current->motion.y-pos.y-24;
  494. rw = pw / (pos.h-48);
  495. }
  496. if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
  497. return;
  498. // if (rw>1) return;
  499. // if (rw<0) return;
  500. slider->clickLeft(true, slider->pressedL);
  501. moveTo(rw * positions + 0.5);
  502. return;
  503. }
  504. if(active & MOVE)
  505. removeUsedEvents(MOVE);
  506. }
  507. CSlider::~CSlider()
  508. {
  509. }
  510. CSlider::CSlider(int x, int y, int totalw, std::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, int style):
  511. capacity(Capacity),
  512. amount(Amount),
  513. scrollStep(1),
  514. horizontal(Horizontal),
  515. moved(Moved)
  516. {
  517. OBJ_CONSTRUCTION_CAPTURING_ALL;
  518. setAmount(amount);
  519. addUsedEvents(LCLICK | KEYBOARD | WHEEL);
  520. strongInterest = true;
  521. pos.x += x;
  522. pos.y += y;
  523. if(style == 0)
  524. {
  525. std::string name = horizontal?"IGPCRDIV.DEF":"OVBUTN2.DEF";
  526. //NOTE: this images do not have "blocked" frames. They should be implemented somehow (e.g. palette transform or something...)
  527. left = new CButton(Point(), name, CButton::tooltip());
  528. right = new CButton(Point(), name, CButton::tooltip());
  529. slider = new CButton(Point(), name, CButton::tooltip());
  530. left->setImageOrder(0, 1, 1, 1);
  531. right->setImageOrder(2, 3, 3, 3);
  532. slider->setImageOrder(4, 4, 4, 4);
  533. }
  534. else
  535. {
  536. left = new CButton(Point(), horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF", CButton::tooltip());
  537. right = new CButton(Point(), horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF", CButton::tooltip());
  538. slider = new CButton(Point(), "SCNRBSL.DEF", CButton::tooltip());
  539. }
  540. slider->actOnDown = true;
  541. slider->soundDisabled = true;
  542. left->soundDisabled = true;
  543. right->soundDisabled = true;
  544. if (horizontal)
  545. right->moveBy(Point(totalw - right->pos.w, 0));
  546. else
  547. right->moveBy(Point(0, totalw - right->pos.h));
  548. left->addCallback(boost::bind(&CSlider::moveLeft,this));
  549. right->addCallback(boost::bind(&CSlider::moveRight,this));
  550. slider->addCallback(boost::bind(&CSlider::sliderClicked,this));
  551. if(horizontal)
  552. {
  553. pos.h = slider->pos.h;
  554. pos.w = totalw;
  555. }
  556. else
  557. {
  558. pos.w = slider->pos.w;
  559. pos.h = totalw;
  560. }
  561. value = -1;
  562. moveTo(Value);
  563. }
  564. void CSlider::block( bool on )
  565. {
  566. left->block(on);
  567. right->block(on);
  568. slider->block(on);
  569. }
  570. void CSlider::setAmount( int to )
  571. {
  572. amount = to;
  573. positions = to - capacity;
  574. vstd::amax(positions, 0);
  575. }
  576. void CSlider::showAll(SDL_Surface * to)
  577. {
  578. CSDL_Ext::fillRectBlack(to, &pos);
  579. CIntObject::showAll(to);
  580. }
  581. void CSlider::wheelScrolled(bool down, bool in)
  582. {
  583. moveTo(value + 3 * (down ? +scrollStep : -scrollStep));
  584. }
  585. void CSlider::keyPressed(const SDL_KeyboardEvent & key)
  586. {
  587. if(key.state != SDL_PRESSED) return;
  588. int moveDest = 0;
  589. switch(key.keysym.sym)
  590. {
  591. case SDLK_UP:
  592. case SDLK_LEFT:
  593. moveDest = value - scrollStep;
  594. break;
  595. case SDLK_DOWN:
  596. case SDLK_RIGHT:
  597. moveDest = value + scrollStep;
  598. break;
  599. case SDLK_PAGEUP:
  600. moveDest = value - capacity + scrollStep;
  601. break;
  602. case SDLK_PAGEDOWN:
  603. moveDest = value + capacity - scrollStep;
  604. break;
  605. case SDLK_HOME:
  606. moveDest = 0;
  607. break;
  608. case SDLK_END:
  609. moveDest = amount - capacity;
  610. break;
  611. default:
  612. return;
  613. }
  614. moveTo(moveDest);
  615. }
  616. void CSlider::moveToMax()
  617. {
  618. moveTo(amount);
  619. }