Buttons.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. if(on || state == BLOCKED) //dont change button state if unblock requested, but it's not blocked
  125. setState(on ? BLOCKED : NORMAL);
  126. }
  127. void CButton::onButtonClicked()
  128. {
  129. // debug logging to figure out pressed button (and as result - player actions) in case of crash
  130. logAnim->traceStream() << "Button clicked at " << pos.x << "x" << pos.y;
  131. CIntObject * parent = this->parent;
  132. std::string prefix = "Parent is";
  133. while (parent)
  134. {
  135. logAnim->traceStream() << prefix << typeid(*parent).name() << " at " << parent->pos.x << "x" << parent->pos.y;
  136. parent = parent->parent;
  137. prefix = '\t' + prefix;
  138. }
  139. callback();
  140. }
  141. void CButton::clickLeft(tribool down, bool previousState)
  142. {
  143. if(isBlocked())
  144. return;
  145. if (down)
  146. {
  147. if (!soundDisabled)
  148. CCS->soundh->playSound(soundBase::button);
  149. setState(PRESSED);
  150. }
  151. else if(hoverable && hovered)
  152. setState(HIGHLIGHTED);
  153. else
  154. setState(NORMAL);
  155. if (actOnDown && down)
  156. {
  157. onButtonClicked();
  158. }
  159. else if (!actOnDown && previousState && (down==false))
  160. {
  161. onButtonClicked();
  162. }
  163. }
  164. void CButton::clickRight(tribool down, bool previousState)
  165. {
  166. if(down && helpBox.size()) //there is no point to show window with nothing inside...
  167. CRClickPopup::createAndPush(helpBox);
  168. }
  169. void CButton::hover (bool on)
  170. {
  171. if(hoverable && !isBlocked())
  172. {
  173. if(on)
  174. setState(HIGHLIGHTED);
  175. else
  176. setState(NORMAL);
  177. }
  178. /*if(pressedL && on) // WTF is this? When this is used?
  179. setState(PRESSED);*/
  180. std::string name = hoverTexts[getState()].empty()
  181. ? hoverTexts[getState()]
  182. : hoverTexts[0];
  183. if(!name.empty() && !isBlocked()) //if there is no name, there is nohing to display also
  184. {
  185. if (LOCPLINT && LOCPLINT->battleInt) //for battle buttons
  186. {
  187. if(on && LOCPLINT->battleInt->console->alterTxt == "")
  188. {
  189. LOCPLINT->battleInt->console->alterTxt = name;
  190. LOCPLINT->battleInt->console->whoSetAlter = 1;
  191. }
  192. else if (LOCPLINT->battleInt->console->alterTxt == name)
  193. {
  194. LOCPLINT->battleInt->console->alterTxt = "";
  195. LOCPLINT->battleInt->console->whoSetAlter = 0;
  196. }
  197. }
  198. else if(GH.statusbar) //for other buttons
  199. {
  200. if (on)
  201. GH.statusbar->setText(name);
  202. else if ( GH.statusbar->getText()==(name) )
  203. GH.statusbar->clear();
  204. }
  205. }
  206. }
  207. CButton::CButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help, CFunctionList<void()> Callback, int key, bool playerColoredButton):
  208. CKeyShortcut(key),
  209. callback(Callback)
  210. {
  211. addUsedEvents(LCLICK | RCLICK | HOVER | KEYBOARD);
  212. stateToIndex[0] = 0;
  213. stateToIndex[1] = 1;
  214. stateToIndex[2] = 2;
  215. stateToIndex[3] = 3;
  216. state=NORMAL;
  217. image = nullptr;
  218. overlay = nullptr;
  219. currentImage = -1;
  220. hoverable = actOnDown = soundDisabled = false;
  221. hoverTexts[0] = help.first;
  222. helpBox=help.second;
  223. pos.x += position.x;
  224. pos.y += position.y;
  225. if (!defName.empty())
  226. {
  227. imageNames.push_back(defName);
  228. setIndex(0, playerColoredButton);
  229. }
  230. }
  231. void CButton::setIndex(size_t index, bool playerColoredButton)
  232. {
  233. if (index == currentImage || index>=imageNames.size())
  234. return;
  235. currentImage = index;
  236. setImage(new CAnimation(imageNames[index]), playerColoredButton);
  237. }
  238. void CButton::setImage(CAnimation* anim, bool playerColoredButton, int animFlags)
  239. {
  240. OBJ_CONSTRUCTION_CAPTURING_ALL;
  241. image = new CAnimImage(anim, getState(), 0, 0, 0, animFlags);
  242. if (playerColoredButton)
  243. image->playerColored(LOCPLINT->playerID);
  244. pos = image->pos;
  245. }
  246. void CButton::setPlayerColor(PlayerColor player)
  247. {
  248. if (image)
  249. image->playerColored(player);
  250. }
  251. void CButton::showAll(SDL_Surface * to)
  252. {
  253. CIntObject::showAll(to);
  254. #ifdef VCMI_SDL1
  255. if (borderColor && borderColor->unused == 0)
  256. CSDL_Ext::drawBorder(to, pos.x-1, pos.y-1, pos.w+2, pos.h+2, int3(borderColor->r, borderColor->g, borderColor->b));
  257. #else
  258. if (borderColor && borderColor->a == 0)
  259. CSDL_Ext::drawBorder(to, pos.x-1, pos.y-1, pos.w+2, pos.h+2, int3(borderColor->r, borderColor->g, borderColor->b));
  260. #endif // 0
  261. }
  262. std::pair<std::string, std::string> CButton::tooltip()
  263. {
  264. return std::pair<std::string, std::string>();
  265. }
  266. std::pair<std::string, std::string> CButton::tooltip(const JsonNode & localizedTexts)
  267. {
  268. return std::make_pair(localizedTexts["label"].String(), localizedTexts["help"].String());
  269. }
  270. std::pair<std::string, std::string> CButton::tooltip(const std::string & hover, const std::string & help)
  271. {
  272. return std::make_pair(hover, help);
  273. }
  274. CToggleBase::CToggleBase(CFunctionList<void (bool)> callback):
  275. callback(callback),
  276. selected(false),
  277. allowDeselection(true)
  278. {
  279. }
  280. CToggleBase::~CToggleBase()
  281. {
  282. }
  283. void CToggleBase::doSelect(bool on)
  284. {
  285. // for overrides
  286. }
  287. void CToggleBase::setSelected(bool on)
  288. {
  289. bool changed = (on != selected);
  290. selected = on;
  291. doSelect(on);
  292. if (changed)
  293. callback(on);
  294. }
  295. bool CToggleBase::canActivate()
  296. {
  297. if (selected && !allowDeselection)
  298. return false;
  299. return true;
  300. }
  301. void CToggleBase::addCallback(std::function<void(bool)> function)
  302. {
  303. callback += function;
  304. }
  305. CToggleButton::CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  306. CFunctionList<void(bool)> callback, int key, bool playerColoredButton):
  307. CButton(position, defName, help, 0, key, playerColoredButton),
  308. CToggleBase(callback)
  309. {
  310. allowDeselection = true;
  311. }
  312. void CToggleButton::doSelect(bool on)
  313. {
  314. if (on)
  315. {
  316. setState(HIGHLIGHTED);
  317. }
  318. else
  319. {
  320. setState(NORMAL);
  321. }
  322. }
  323. void CToggleButton::clickLeft(tribool down, bool previousState)
  324. {
  325. // force refresh
  326. hover(false);
  327. hover(true);
  328. if(isBlocked())
  329. return;
  330. if (down && canActivate())
  331. {
  332. CCS->soundh->playSound(soundBase::button);
  333. setState(PRESSED);
  334. }
  335. if(previousState)//mouse up
  336. {
  337. if(down == false && getState() == PRESSED && canActivate())
  338. {
  339. onButtonClicked();
  340. setSelected(!selected);
  341. }
  342. else
  343. doSelect(selected); // restore
  344. }
  345. }
  346. void CToggleGroup::addCallback(std::function<void(int)> callback)
  347. {
  348. onChange += callback;
  349. }
  350. void CToggleGroup::addToggle(int identifier, CToggleBase* bt)
  351. {
  352. if (auto intObj = dynamic_cast<CIntObject*>(bt)) // hack-ish workagound to avoid diamond problem with inheritance
  353. {
  354. if (intObj->parent)
  355. intObj->parent->removeChild(intObj);
  356. addChild(intObj);
  357. }
  358. bt->addCallback([=] (bool on) { if (on) selectionChanged(identifier);});
  359. bt->allowDeselection = false;
  360. assert(buttons[identifier] == nullptr);
  361. buttons[identifier] = bt;
  362. }
  363. CToggleGroup::CToggleGroup(const CFunctionList<void(int)> &OnChange, bool musicLikeButtons)
  364. : onChange(OnChange), selectedID(-2), musicLike(musicLikeButtons)
  365. {}
  366. void CToggleGroup::setSelected(int id)
  367. {
  368. selectionChanged(id);
  369. }
  370. void CToggleGroup::selectionChanged(int to)
  371. {
  372. if (to == selectedID)
  373. return;
  374. int oldSelection = selectedID;
  375. selectedID = to;
  376. if (buttons.count(oldSelection))
  377. buttons[oldSelection]->setSelected(false);
  378. if (buttons.count(to))
  379. buttons[to]->setSelected(true);
  380. onChange(to);
  381. if (parent)
  382. parent->redraw();
  383. }
  384. void CToggleGroup::show(SDL_Surface * to)
  385. {
  386. if (musicLike)
  387. {
  388. if (auto intObj = dynamic_cast<CIntObject*>(buttons[selectedID])) // hack-ish workagound to avoid diamond problem with inheritance
  389. intObj->show(to);
  390. }
  391. else
  392. CIntObject::show(to);
  393. }
  394. void CToggleGroup::showAll(SDL_Surface * to)
  395. {
  396. if (musicLike)
  397. {
  398. if (auto intObj = dynamic_cast<CIntObject*>(buttons[selectedID])) // hack-ish workagound to avoid diamond problem with inheritance
  399. intObj->showAll(to);
  400. }
  401. else
  402. CIntObject::showAll(to);
  403. }
  404. void CSlider::sliderClicked()
  405. {
  406. if(!(active & MOVE))
  407. addUsedEvents(MOVE);
  408. }
  409. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  410. {
  411. double v = 0;
  412. if(horizontal)
  413. {
  414. 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 )
  415. return;
  416. v = sEvent.x - pos.x - 24;
  417. v *= positions;
  418. v /= (pos.w - 48);
  419. }
  420. else
  421. {
  422. 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 )
  423. return;
  424. v = sEvent.y - pos.y - 24;
  425. v *= positions;
  426. v /= (pos.h - 48);
  427. }
  428. v += 0.5;
  429. if(v!=value)
  430. {
  431. moveTo(v);
  432. }
  433. }
  434. void CSlider::setScrollStep(int to)
  435. {
  436. scrollStep = to;
  437. }
  438. int CSlider::getAmount()
  439. {
  440. return amount;
  441. }
  442. int CSlider::getValue()
  443. {
  444. return value;
  445. }
  446. void CSlider::moveLeft()
  447. {
  448. moveTo(value-1);
  449. }
  450. void CSlider::moveRight()
  451. {
  452. moveTo(value+1);
  453. }
  454. void CSlider::moveBy(int amount)
  455. {
  456. moveTo(value + amount);
  457. }
  458. void CSlider::updateSliderPos()
  459. {
  460. if(horizontal)
  461. {
  462. if(positions)
  463. {
  464. double part = static_cast<double>(value) / positions;
  465. part*=(pos.w-48);
  466. int newPos = part + pos.x + 16 - slider->pos.x;
  467. slider->moveBy(Point(newPos, 0));
  468. }
  469. else
  470. slider->moveTo(Point(pos.x+16, pos.y));
  471. }
  472. else
  473. {
  474. if(positions)
  475. {
  476. double part = static_cast<double>(value) / positions;
  477. part*=(pos.h-48);
  478. int newPos = part + pos.y + 16 - slider->pos.y;
  479. slider->moveBy(Point(0, newPos));
  480. }
  481. else
  482. slider->moveTo(Point(pos.x, pos.y+16));
  483. }
  484. }
  485. void CSlider::moveTo(int to)
  486. {
  487. vstd::amax(to, 0);
  488. vstd::amin(to, positions);
  489. //same, old position?
  490. if(value == to)
  491. return;
  492. value = to;
  493. updateSliderPos();
  494. moved(to);
  495. }
  496. void CSlider::clickLeft(tribool down, bool previousState)
  497. {
  498. if(down && !slider->isBlocked())
  499. {
  500. double pw = 0;
  501. double rw = 0;
  502. if(horizontal)
  503. {
  504. pw = GH.current->motion.x-pos.x-25;
  505. rw = pw / static_cast<double>(pos.w - 48);
  506. }
  507. else
  508. {
  509. pw = GH.current->motion.y-pos.y-24;
  510. rw = pw / (pos.h-48);
  511. }
  512. if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
  513. return;
  514. // if (rw>1) return;
  515. // if (rw<0) return;
  516. slider->clickLeft(true, slider->pressedL);
  517. moveTo(rw * positions + 0.5);
  518. return;
  519. }
  520. if(active & MOVE)
  521. removeUsedEvents(MOVE);
  522. }
  523. CSlider::~CSlider()
  524. {
  525. }
  526. CSlider::CSlider(Point position, int totalw, std::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, CSlider::EStyle style):
  527. capacity(Capacity),
  528. horizontal(Horizontal),
  529. amount(Amount),
  530. value(Value),
  531. scrollStep(1),
  532. moved(Moved)
  533. {
  534. OBJ_CONSTRUCTION_CAPTURING_ALL;
  535. setAmount(amount);
  536. vstd::amax(value, 0);
  537. vstd::amin(value, positions);
  538. addUsedEvents(LCLICK | KEYBOARD | WHEEL);
  539. strongInterest = true;
  540. pos.x += position.x;
  541. pos.y += position.y;
  542. if(style == BROWN)
  543. {
  544. std::string name = horizontal?"IGPCRDIV.DEF":"OVBUTN2.DEF";
  545. //NOTE: this images do not have "blocked" frames. They should be implemented somehow (e.g. palette transform or something...)
  546. left = new CButton(Point(), name, CButton::tooltip());
  547. right = new CButton(Point(), name, CButton::tooltip());
  548. slider = new CButton(Point(), name, CButton::tooltip());
  549. left->setImageOrder(0, 1, 1, 1);
  550. right->setImageOrder(2, 3, 3, 3);
  551. slider->setImageOrder(4, 4, 4, 4);
  552. }
  553. else
  554. {
  555. left = new CButton(Point(), horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF", CButton::tooltip());
  556. right = new CButton(Point(), horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF", CButton::tooltip());
  557. slider = new CButton(Point(), "SCNRBSL.DEF", CButton::tooltip());
  558. }
  559. slider->actOnDown = true;
  560. slider->soundDisabled = true;
  561. left->soundDisabled = true;
  562. right->soundDisabled = true;
  563. if (horizontal)
  564. right->moveBy(Point(totalw - right->pos.w, 0));
  565. else
  566. right->moveBy(Point(0, totalw - right->pos.h));
  567. left->addCallback(std::bind(&CSlider::moveLeft,this));
  568. right->addCallback(std::bind(&CSlider::moveRight,this));
  569. slider->addCallback(std::bind(&CSlider::sliderClicked,this));
  570. if(horizontal)
  571. {
  572. pos.h = slider->pos.h;
  573. pos.w = totalw;
  574. }
  575. else
  576. {
  577. pos.w = slider->pos.w;
  578. pos.h = totalw;
  579. }
  580. updateSliderPos();
  581. }
  582. void CSlider::block( bool on )
  583. {
  584. left->block(on);
  585. right->block(on);
  586. slider->block(on);
  587. }
  588. void CSlider::setAmount( int to )
  589. {
  590. amount = to;
  591. positions = to - capacity;
  592. vstd::amax(positions, 0);
  593. }
  594. void CSlider::showAll(SDL_Surface * to)
  595. {
  596. CSDL_Ext::fillRectBlack(to, &pos);
  597. CIntObject::showAll(to);
  598. }
  599. void CSlider::wheelScrolled(bool down, bool in)
  600. {
  601. moveTo(value + 3 * (down ? +scrollStep : -scrollStep));
  602. }
  603. void CSlider::keyPressed(const SDL_KeyboardEvent & key)
  604. {
  605. if(key.state != SDL_PRESSED) return;
  606. int moveDest = value;
  607. switch(key.keysym.sym)
  608. {
  609. case SDLK_UP:
  610. if (!horizontal)
  611. moveDest = value - scrollStep;
  612. break;
  613. case SDLK_LEFT:
  614. if (horizontal)
  615. moveDest = value - scrollStep;
  616. break;
  617. case SDLK_DOWN:
  618. if (!horizontal)
  619. moveDest = value + scrollStep;
  620. break;
  621. case SDLK_RIGHT:
  622. if (horizontal)
  623. moveDest = value + scrollStep;
  624. break;
  625. case SDLK_PAGEUP:
  626. moveDest = value - capacity + scrollStep;
  627. break;
  628. case SDLK_PAGEDOWN:
  629. moveDest = value + capacity - scrollStep;
  630. break;
  631. case SDLK_HOME:
  632. moveDest = 0;
  633. break;
  634. case SDLK_END:
  635. moveDest = amount - capacity;
  636. break;
  637. default:
  638. return;
  639. }
  640. moveTo(moveDest);
  641. }
  642. void CSlider::moveToMin()
  643. {
  644. moveTo(0);
  645. }
  646. void CSlider::moveToMax()
  647. {
  648. moveTo(amount);
  649. }