Buttons.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 && !isBlocked())
  171. {
  172. if(on)
  173. setState(HIGHLIGHTED);
  174. else
  175. setState(NORMAL);
  176. }
  177. /*if(pressedL && on) // WTF is this? When this is used?
  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. selected(false),
  276. allowDeselection(true)
  277. {
  278. }
  279. CToggleBase::~CToggleBase()
  280. {
  281. }
  282. void CToggleBase::doSelect(bool on)
  283. {
  284. // for overrides
  285. }
  286. void CToggleBase::setSelected(bool on)
  287. {
  288. bool changed = (on != selected);
  289. selected = on;
  290. doSelect(on);
  291. if (changed)
  292. callback(on);
  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. allowDeselection = true;
  310. }
  311. void CToggleButton::doSelect(bool on)
  312. {
  313. if (on)
  314. {
  315. setState(HIGHLIGHTED);
  316. }
  317. else
  318. {
  319. setState(NORMAL);
  320. }
  321. }
  322. void CToggleButton::clickLeft(tribool down, bool previousState)
  323. {
  324. // force refresh
  325. hover(false);
  326. hover(true);
  327. if(isBlocked())
  328. return;
  329. if (down && canActivate())
  330. {
  331. CCS->soundh->playSound(soundBase::button);
  332. setState(PRESSED);
  333. }
  334. if(previousState)//mouse up
  335. {
  336. if(down == false && getState() == PRESSED && canActivate())
  337. {
  338. onButtonClicked();
  339. setSelected(!selected);
  340. }
  341. else
  342. doSelect(selected); // restore
  343. }
  344. }
  345. void CToggleGroup::addCallback(std::function<void(int)> callback)
  346. {
  347. onChange += callback;
  348. }
  349. void CToggleGroup::addToggle(int identifier, CToggleBase* bt)
  350. {
  351. if (auto intObj = dynamic_cast<CIntObject*>(bt)) // hack-ish workagound to avoid diamond problem with inheritance
  352. {
  353. if (intObj->parent)
  354. intObj->parent->removeChild(intObj);
  355. addChild(intObj);
  356. }
  357. bt->addCallback([=] (bool on) { if (on) selectionChanged(identifier);});
  358. bt->allowDeselection = false;
  359. assert(buttons[identifier] == nullptr);
  360. buttons[identifier] = bt;
  361. }
  362. CToggleGroup::CToggleGroup(const CFunctionList<void(int)> &OnChange, bool musicLikeButtons)
  363. : onChange(OnChange), musicLike(musicLikeButtons)
  364. {}
  365. void CToggleGroup::setSelected(int id)
  366. {
  367. selectionChanged(id);
  368. }
  369. void CToggleGroup::selectionChanged(int to)
  370. {
  371. if (to == selectedID)
  372. return;
  373. int oldSelection = selectedID;
  374. selectedID = to;
  375. if (buttons.count(oldSelection))
  376. buttons[oldSelection]->setSelected(false);
  377. if (buttons.count(to))
  378. buttons[to]->setSelected(true);
  379. onChange(to);
  380. if (parent)
  381. parent->redraw();
  382. }
  383. void CToggleGroup::show(SDL_Surface * to)
  384. {
  385. if (musicLike)
  386. {
  387. if (auto intObj = dynamic_cast<CIntObject*>(buttons[selectedID])) // hack-ish workagound to avoid diamond problem with inheritance
  388. intObj->show(to);
  389. }
  390. else
  391. CIntObject::show(to);
  392. }
  393. void CToggleGroup::showAll(SDL_Surface * to)
  394. {
  395. if (musicLike)
  396. {
  397. if (auto intObj = dynamic_cast<CIntObject*>(buttons[selectedID])) // hack-ish workagound to avoid diamond problem with inheritance
  398. intObj->showAll(to);
  399. }
  400. else
  401. CIntObject::showAll(to);
  402. }
  403. void CSlider::sliderClicked()
  404. {
  405. if(!(active & MOVE))
  406. addUsedEvents(MOVE);
  407. }
  408. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  409. {
  410. double v = 0;
  411. if(horizontal)
  412. {
  413. 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 )
  414. return;
  415. v = sEvent.x - pos.x - 24;
  416. v *= positions;
  417. v /= (pos.w - 48);
  418. }
  419. else
  420. {
  421. 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 )
  422. return;
  423. v = sEvent.y - pos.y - 24;
  424. v *= positions;
  425. v /= (pos.h - 48);
  426. }
  427. v += 0.5;
  428. if(v!=value)
  429. {
  430. moveTo(v);
  431. }
  432. }
  433. void CSlider::setScrollStep(int to)
  434. {
  435. scrollStep = to;
  436. }
  437. int CSlider::getAmount()
  438. {
  439. return amount;
  440. }
  441. int CSlider::getValue()
  442. {
  443. return value;
  444. }
  445. void CSlider::moveLeft()
  446. {
  447. moveTo(value-1);
  448. }
  449. void CSlider::moveRight()
  450. {
  451. moveTo(value+1);
  452. }
  453. void CSlider::moveBy(int amount)
  454. {
  455. moveTo(value + amount);
  456. }
  457. void CSlider::updateSliderPos()
  458. {
  459. if(horizontal)
  460. {
  461. if(positions)
  462. {
  463. double part = static_cast<double>(value) / positions;
  464. part*=(pos.w-48);
  465. int newPos = part + pos.x + 16 - slider->pos.x;
  466. slider->moveBy(Point(newPos, 0));
  467. }
  468. else
  469. slider->moveTo(Point(pos.x+16, pos.y));
  470. }
  471. else
  472. {
  473. if(positions)
  474. {
  475. double part = static_cast<double>(value) / positions;
  476. part*=(pos.h-48);
  477. int newPos = part + pos.y + 16 - slider->pos.y;
  478. slider->moveBy(Point(0, newPos));
  479. }
  480. else
  481. slider->moveTo(Point(pos.x, pos.y+16));
  482. }
  483. }
  484. void CSlider::moveTo(int to)
  485. {
  486. vstd::amax(to, 0);
  487. vstd::amin(to, positions);
  488. //same, old position?
  489. if(value == to)
  490. return;
  491. value = to;
  492. updateSliderPos();
  493. moved(to);
  494. }
  495. void CSlider::clickLeft(tribool down, bool previousState)
  496. {
  497. if(down && !slider->isBlocked())
  498. {
  499. double pw = 0;
  500. double rw = 0;
  501. if(horizontal)
  502. {
  503. pw = GH.current->motion.x-pos.x-25;
  504. rw = pw / static_cast<double>(pos.w - 48);
  505. }
  506. else
  507. {
  508. pw = GH.current->motion.y-pos.y-24;
  509. rw = pw / (pos.h-48);
  510. }
  511. if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
  512. return;
  513. // if (rw>1) return;
  514. // if (rw<0) return;
  515. slider->clickLeft(true, slider->pressedL);
  516. moveTo(rw * positions + 0.5);
  517. return;
  518. }
  519. if(active & MOVE)
  520. removeUsedEvents(MOVE);
  521. }
  522. CSlider::~CSlider()
  523. {
  524. }
  525. CSlider::CSlider(Point position, int totalw, std::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, CSlider::EStyle style):
  526. capacity(Capacity),
  527. horizontal(Horizontal),
  528. amount(Amount),
  529. value(Value),
  530. scrollStep(1),
  531. moved(Moved)
  532. {
  533. OBJ_CONSTRUCTION_CAPTURING_ALL;
  534. setAmount(amount);
  535. vstd::amax(value, 0);
  536. vstd::amin(value, positions);
  537. addUsedEvents(LCLICK | KEYBOARD | WHEEL);
  538. strongInterest = true;
  539. pos.x += position.x;
  540. pos.y += position.y;
  541. if(style == BROWN)
  542. {
  543. std::string name = horizontal?"IGPCRDIV.DEF":"OVBUTN2.DEF";
  544. //NOTE: this images do not have "blocked" frames. They should be implemented somehow (e.g. palette transform or something...)
  545. left = new CButton(Point(), name, CButton::tooltip());
  546. right = new CButton(Point(), name, CButton::tooltip());
  547. slider = new CButton(Point(), name, CButton::tooltip());
  548. left->setImageOrder(0, 1, 1, 1);
  549. right->setImageOrder(2, 3, 3, 3);
  550. slider->setImageOrder(4, 4, 4, 4);
  551. }
  552. else
  553. {
  554. left = new CButton(Point(), horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF", CButton::tooltip());
  555. right = new CButton(Point(), horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF", CButton::tooltip());
  556. slider = new CButton(Point(), "SCNRBSL.DEF", CButton::tooltip());
  557. }
  558. slider->actOnDown = true;
  559. slider->soundDisabled = true;
  560. left->soundDisabled = true;
  561. right->soundDisabled = true;
  562. if (horizontal)
  563. right->moveBy(Point(totalw - right->pos.w, 0));
  564. else
  565. right->moveBy(Point(0, totalw - right->pos.h));
  566. left->addCallback(std::bind(&CSlider::moveLeft,this));
  567. right->addCallback(std::bind(&CSlider::moveRight,this));
  568. slider->addCallback(std::bind(&CSlider::sliderClicked,this));
  569. if(horizontal)
  570. {
  571. pos.h = slider->pos.h;
  572. pos.w = totalw;
  573. }
  574. else
  575. {
  576. pos.w = slider->pos.w;
  577. pos.h = totalw;
  578. }
  579. updateSliderPos();
  580. }
  581. void CSlider::block( bool on )
  582. {
  583. left->block(on);
  584. right->block(on);
  585. slider->block(on);
  586. }
  587. void CSlider::setAmount( int to )
  588. {
  589. amount = to;
  590. positions = to - capacity;
  591. vstd::amax(positions, 0);
  592. }
  593. void CSlider::showAll(SDL_Surface * to)
  594. {
  595. CSDL_Ext::fillRectBlack(to, &pos);
  596. CIntObject::showAll(to);
  597. }
  598. void CSlider::wheelScrolled(bool down, bool in)
  599. {
  600. moveTo(value + 3 * (down ? +scrollStep : -scrollStep));
  601. }
  602. void CSlider::keyPressed(const SDL_KeyboardEvent & key)
  603. {
  604. if(key.state != SDL_PRESSED) return;
  605. int moveDest = value;
  606. switch(key.keysym.sym)
  607. {
  608. case SDLK_UP:
  609. if (!horizontal)
  610. moveDest = value - scrollStep;
  611. break;
  612. case SDLK_LEFT:
  613. if (horizontal)
  614. moveDest = value - scrollStep;
  615. break;
  616. case SDLK_DOWN:
  617. if (!horizontal)
  618. moveDest = value + scrollStep;
  619. break;
  620. case SDLK_RIGHT:
  621. if (horizontal)
  622. moveDest = value + scrollStep;
  623. break;
  624. case SDLK_PAGEUP:
  625. moveDest = value - capacity + scrollStep;
  626. break;
  627. case SDLK_PAGEDOWN:
  628. moveDest = value + capacity - scrollStep;
  629. break;
  630. case SDLK_HOME:
  631. moveDest = 0;
  632. break;
  633. case SDLK_END:
  634. moveDest = amount - capacity;
  635. break;
  636. default:
  637. return;
  638. }
  639. moveTo(moveDest);
  640. }
  641. void CSlider::moveToMax()
  642. {
  643. moveTo(amount);
  644. }