Buttons.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * Buttons.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "Buttons.h"
  12. #include "Images.h"
  13. #include "TextControls.h"
  14. #include "../CMusicHandler.h"
  15. #include "../CGameInfo.h"
  16. #include "../CPlayerInterface.h"
  17. #include "../battle/BattleInterface.h"
  18. #include "../battle/BattleInterfaceClasses.h"
  19. #include "../gui/CAnimation.h"
  20. #include "../gui/CGuiHandler.h"
  21. #include "../windows/InfoWindows.h"
  22. #include "../../lib/CConfigHandler.h"
  23. #include "../../lib/CGeneralTextHandler.h"
  24. void CButton::update()
  25. {
  26. if (overlay)
  27. {
  28. Point targetPos = Rect::createCentered( pos, overlay->pos.dimensions()).topLeft();
  29. if (state == PRESSED)
  30. overlay->moveTo(targetPos + Point(1,1));
  31. else
  32. overlay->moveTo(targetPos);
  33. }
  34. int newPos = stateToIndex[int(state)];
  35. if(animateLonelyFrame)
  36. {
  37. if(state == PRESSED)
  38. image->moveBy(Point(1,1));
  39. else
  40. image->moveBy(Point(-1,-1));
  41. }
  42. if (newPos < 0)
  43. newPos = 0;
  44. if (state == HIGHLIGHTED && image->size() < 4)
  45. newPos = (int)image->size()-1;
  46. image->setFrame(newPos);
  47. if (active)
  48. redraw();
  49. }
  50. void CButton::setBorderColor(boost::optional<SDL_Color> borderColor)
  51. {
  52. setBorderColor(borderColor, borderColor, borderColor, borderColor);
  53. }
  54. void CButton::setBorderColor(boost::optional<SDL_Color> normalBorderColor,
  55. boost::optional<SDL_Color> pressedBorderColor,
  56. boost::optional<SDL_Color> blockedBorderColor,
  57. boost::optional<SDL_Color> highlightedBorderColor)
  58. {
  59. stateToBorderColor[NORMAL] = normalBorderColor;
  60. stateToBorderColor[PRESSED] = pressedBorderColor;
  61. stateToBorderColor[BLOCKED] = blockedBorderColor;
  62. stateToBorderColor[HIGHLIGHTED] = highlightedBorderColor;
  63. update();
  64. }
  65. void CButton::addCallback(std::function<void()> callback)
  66. {
  67. this->callback += callback;
  68. }
  69. void CButton::addTextOverlay(const std::string & Text, EFonts font, SDL_Color color)
  70. {
  71. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  72. addOverlay(std::make_shared<CLabel>(pos.w/2, pos.h/2, font, ETextAlignment::CENTER, color, Text));
  73. update();
  74. }
  75. void CButton::addOverlay(std::shared_ptr<CIntObject> newOverlay)
  76. {
  77. overlay = newOverlay;
  78. if(overlay)
  79. {
  80. addChild(newOverlay.get());
  81. Point targetPos = Rect::createCentered( pos, overlay->pos.dimensions()).topLeft();
  82. overlay->moveTo(targetPos);
  83. }
  84. update();
  85. }
  86. void CButton::addImage(std::string filename)
  87. {
  88. imageNames.push_back(filename);
  89. }
  90. void CButton::addHoverText(ButtonState state, std::string text)
  91. {
  92. hoverTexts[state] = text;
  93. }
  94. void CButton::setImageOrder(int state1, int state2, int state3, int state4)
  95. {
  96. stateToIndex[0] = state1;
  97. stateToIndex[1] = state2;
  98. stateToIndex[2] = state3;
  99. stateToIndex[3] = state4;
  100. update();
  101. }
  102. void CButton::setAnimateLonelyFrame(bool agreement)
  103. {
  104. animateLonelyFrame = agreement;
  105. }
  106. void CButton::setState(ButtonState newState)
  107. {
  108. if (state == newState)
  109. return;
  110. state = newState;
  111. update();
  112. }
  113. CButton::ButtonState CButton::getState()
  114. {
  115. return state;
  116. }
  117. bool CButton::isBlocked()
  118. {
  119. return state == BLOCKED;
  120. }
  121. bool CButton::isHighlighted()
  122. {
  123. return state == HIGHLIGHTED;
  124. }
  125. void CButton::block(bool on)
  126. {
  127. if(on || state == BLOCKED) //dont change button state if unblock requested, but it's not blocked
  128. setState(on ? BLOCKED : NORMAL);
  129. }
  130. void CButton::onButtonClicked()
  131. {
  132. // debug logging to figure out pressed button (and as result - player actions) in case of crash
  133. logAnim->trace("Button clicked at %dx%d", pos.x, pos.y);
  134. CIntObject * parent = this->parent;
  135. std::string prefix = "Parent is";
  136. while (parent)
  137. {
  138. logAnim->trace("%s%s at %dx%d", prefix, typeid(*parent).name(), parent->pos.x, parent->pos.y);
  139. parent = parent->parent;
  140. prefix = '\t' + prefix;
  141. }
  142. callback();
  143. }
  144. void CButton::clickLeft(tribool down, bool previousState)
  145. {
  146. if(isBlocked())
  147. return;
  148. if (down)
  149. {
  150. if (!soundDisabled)
  151. CCS->soundh->playSound(soundBase::button);
  152. setState(PRESSED);
  153. }
  154. else if(hoverable && hovered)
  155. setState(HIGHLIGHTED);
  156. else
  157. setState(NORMAL);
  158. if (actOnDown && down)
  159. {
  160. onButtonClicked();
  161. }
  162. else if (!actOnDown && previousState && (down==false))
  163. {
  164. onButtonClicked();
  165. }
  166. }
  167. void CButton::clickRight(tribool down, bool previousState)
  168. {
  169. if(down && helpBox.size()) //there is no point to show window with nothing inside...
  170. CRClickPopup::createAndPush(helpBox);
  171. }
  172. void CButton::hover (bool on)
  173. {
  174. if(hoverable && !isBlocked())
  175. {
  176. if(on)
  177. setState(HIGHLIGHTED);
  178. else
  179. setState(NORMAL);
  180. }
  181. /*if(pressedL && on) // WTF is this? When this is used?
  182. setState(PRESSED);*/
  183. std::string name = hoverTexts[getState()].empty()
  184. ? hoverTexts[0]
  185. : hoverTexts[getState()];
  186. if(!name.empty() && !isBlocked()) //if there is no name, there is nothing to display also
  187. {
  188. if (on)
  189. GH.statusbar->write(name);
  190. else
  191. GH.statusbar->clearIfMatching(name);
  192. }
  193. }
  194. CButton::CButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help, CFunctionList<void()> Callback, int key, bool playerColoredButton):
  195. CKeyShortcut(key),
  196. callback(Callback)
  197. {
  198. defActions = 255-DISPOSE;
  199. addUsedEvents(LCLICK | RCLICK | HOVER | KEYBOARD);
  200. stateToIndex[0] = 0;
  201. stateToIndex[1] = 1;
  202. stateToIndex[2] = 2;
  203. stateToIndex[3] = 3;
  204. state=NORMAL;
  205. currentImage = -1;
  206. hoverable = actOnDown = soundDisabled = false;
  207. hoverTexts[0] = help.first;
  208. helpBox=help.second;
  209. pos.x += position.x;
  210. pos.y += position.y;
  211. if (!defName.empty())
  212. {
  213. imageNames.push_back(defName);
  214. setIndex(0, playerColoredButton);
  215. }
  216. }
  217. void CButton::setIndex(size_t index, bool playerColoredButton)
  218. {
  219. if (index == currentImage || index>=imageNames.size())
  220. return;
  221. currentImage = index;
  222. auto anim = std::make_shared<CAnimation>(imageNames[index]);
  223. setImage(anim, playerColoredButton);
  224. }
  225. void CButton::setImage(std::shared_ptr<CAnimation> anim, bool playerColoredButton, int animFlags)
  226. {
  227. OBJECT_CONSTRUCTION_CUSTOM_CAPTURING(255-DISPOSE);
  228. image = std::make_shared<CAnimImage>(anim, getState(), 0, 0, 0, animFlags);
  229. if (playerColoredButton)
  230. image->playerColored(LOCPLINT->playerID);
  231. pos = image->pos;
  232. }
  233. void CButton::setPlayerColor(PlayerColor player)
  234. {
  235. if (image)
  236. image->playerColored(player);
  237. }
  238. void CButton::showAll(SDL_Surface * to)
  239. {
  240. CIntObject::showAll(to);
  241. auto borderColor = stateToBorderColor[getState()];
  242. if (borderColor)
  243. CSDL_Ext::drawBorder(to, pos.x-1, pos.y-1, pos.w+2, pos.h+2, *borderColor);
  244. }
  245. std::pair<std::string, std::string> CButton::tooltip()
  246. {
  247. return std::pair<std::string, std::string>();
  248. }
  249. std::pair<std::string, std::string> CButton::tooltipLocalized(const std::string & key)
  250. {
  251. return std::make_pair(
  252. CGI->generaltexth->translate(key, "hover"),
  253. CGI->generaltexth->translate(key, "help")
  254. );
  255. }
  256. std::pair<std::string, std::string> CButton::tooltip(const std::string & hover, const std::string & help)
  257. {
  258. return std::make_pair(hover, help);
  259. }
  260. CToggleBase::CToggleBase(CFunctionList<void (bool)> callback):
  261. callback(callback),
  262. selected(false),
  263. allowDeselection(true)
  264. {
  265. }
  266. CToggleBase::~CToggleBase() = default;
  267. void CToggleBase::doSelect(bool on)
  268. {
  269. // for overrides
  270. }
  271. void CToggleBase::setEnabled(bool enabled)
  272. {
  273. // for overrides
  274. }
  275. void CToggleBase::setSelected(bool on)
  276. {
  277. bool changed = (on != selected);
  278. selected = on;
  279. doSelect(on);
  280. if (changed)
  281. callback(on);
  282. }
  283. bool CToggleBase::canActivate()
  284. {
  285. if (selected && !allowDeselection)
  286. return false;
  287. return true;
  288. }
  289. void CToggleBase::addCallback(std::function<void(bool)> function)
  290. {
  291. callback += function;
  292. }
  293. CToggleButton::CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  294. CFunctionList<void(bool)> callback, int key, bool playerColoredButton):
  295. CButton(position, defName, help, 0, key, playerColoredButton),
  296. CToggleBase(callback)
  297. {
  298. allowDeselection = true;
  299. }
  300. void CToggleButton::doSelect(bool on)
  301. {
  302. if (on)
  303. {
  304. setState(HIGHLIGHTED);
  305. }
  306. else
  307. {
  308. setState(NORMAL);
  309. }
  310. }
  311. void CToggleButton::setEnabled(bool enabled)
  312. {
  313. setState(enabled ? NORMAL : BLOCKED);
  314. }
  315. void CToggleButton::clickLeft(tribool down, bool previousState)
  316. {
  317. // force refresh
  318. hover(false);
  319. hover(true);
  320. if(isBlocked())
  321. return;
  322. if (down && canActivate())
  323. {
  324. CCS->soundh->playSound(soundBase::button);
  325. setState(PRESSED);
  326. }
  327. if(previousState)//mouse up
  328. {
  329. if(down == false && getState() == PRESSED && canActivate())
  330. {
  331. onButtonClicked();
  332. setSelected(!selected);
  333. }
  334. else
  335. doSelect(selected); // restore
  336. }
  337. }
  338. void CToggleGroup::addCallback(std::function<void(int)> callback)
  339. {
  340. onChange += callback;
  341. }
  342. void CToggleGroup::resetCallback()
  343. {
  344. onChange.clear();
  345. }
  346. void CToggleGroup::addToggle(int identifier, std::shared_ptr<CToggleBase> button)
  347. {
  348. if(auto intObj = std::dynamic_pointer_cast<CIntObject>(button)) // hack-ish workagound to avoid diamond problem with inheritance
  349. {
  350. addChild(intObj.get());
  351. }
  352. button->addCallback([=] (bool on) { if (on) selectionChanged(identifier);});
  353. button->allowDeselection = false;
  354. if(buttons.count(identifier)>0)
  355. logAnim->error("Duplicated toggle button id %d", identifier);
  356. buttons[identifier] = button;
  357. }
  358. CToggleGroup::CToggleGroup(const CFunctionList<void(int)> &OnChange)
  359. : onChange(OnChange), selectedID(-2)
  360. {
  361. }
  362. void CToggleGroup::setSelected(int id)
  363. {
  364. selectionChanged(id);
  365. }
  366. void CToggleGroup::setSelectedOnly(int id)
  367. {
  368. for(auto it = buttons.begin(); it != buttons.end(); it++)
  369. {
  370. int buttonId = it->first;
  371. buttons[buttonId]->setEnabled(buttonId == id);
  372. }
  373. selectionChanged(id);
  374. }
  375. void CToggleGroup::selectionChanged(int to)
  376. {
  377. if (to == selectedID)
  378. return;
  379. int oldSelection = selectedID;
  380. selectedID = to;
  381. if (buttons.count(oldSelection))
  382. buttons[oldSelection]->setSelected(false);
  383. if (buttons.count(to))
  384. buttons[to]->setSelected(true);
  385. onChange(to);
  386. if (parent)
  387. parent->redraw();
  388. }
  389. int CToggleGroup::getSelected() const
  390. {
  391. return selectedID;
  392. }
  393. CVolumeSlider::CVolumeSlider(const Point & position, const std::string & defName, const int value, ETooltipMode mode)
  394. : CIntObject(LCLICK | RCLICK | WHEEL),
  395. value(value),
  396. mode(mode)
  397. {
  398. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  399. animImage = std::make_shared<CAnimImage>(std::make_shared<CAnimation>(defName), 0, 0, position.x, position.y),
  400. pos.x += position.x;
  401. pos.y += position.y;
  402. pos.w = (animImage->pos.w + 1) * (int)animImage->size();
  403. pos.h = animImage->pos.h;
  404. type |= REDRAW_PARENT;
  405. setVolume(value);
  406. }
  407. void CVolumeSlider::setVolume(int value_)
  408. {
  409. value = value_;
  410. moveTo((int)(value * static_cast<double>(animImage->size()) / 100.0));
  411. }
  412. void CVolumeSlider::moveTo(int id)
  413. {
  414. vstd::abetween(id, 0, animImage->size() - 1);
  415. animImage->setFrame(id);
  416. animImage->moveTo(Point(pos.x + (animImage->pos.w + 1) * id, pos.y));
  417. if (active)
  418. redraw();
  419. }
  420. void CVolumeSlider::addCallback(std::function<void(int)> callback)
  421. {
  422. onChange += callback;
  423. }
  424. void CVolumeSlider::clickLeft(tribool down, bool previousState)
  425. {
  426. if (down)
  427. {
  428. double px = GH.getCursorPosition().x - pos.x;
  429. double rx = px / static_cast<double>(pos.w);
  430. // setVolume is out of 100
  431. setVolume((int)(rx * 100));
  432. // Volume config is out of 100, set to increments of 5ish roughly based on the half point of the indicator
  433. // 0.0 -> 0, 0.05 -> 5, 0.09 -> 5,...,
  434. // 0.1 -> 10, ..., 0.19 -> 15, 0.2 -> 20, ...,
  435. // 0.28 -> 25, 0.29 -> 30, 0.3 -> 30, ...,
  436. // 0.85 -> 85, 0.86 -> 90, ..., 0.87 -> 90,...,
  437. // 0.95 -> 95, 0.96 -> 100, 0.99 -> 100
  438. int volume = 5 * int(rx * (2 * animImage->size() + 1));
  439. onChange(volume);
  440. }
  441. }
  442. void CVolumeSlider::clickRight(tribool down, bool previousState)
  443. {
  444. if (down)
  445. {
  446. double px = GH.getCursorPosition().x - pos.x;
  447. int index = static_cast<int>(px / static_cast<double>(pos.w) * animImage->size());
  448. size_t helpIndex = index + (mode == MUSIC ? 326 : 336);
  449. std::string helpBox = CGI->generaltexth->translate("core.help", helpIndex, "help" );
  450. if(!helpBox.empty())
  451. CRClickPopup::createAndPush(helpBox);
  452. if(GH.statusbar)
  453. GH.statusbar->write(helpBox);
  454. }
  455. }
  456. void CVolumeSlider::wheelScrolled(bool down, bool in)
  457. {
  458. if (in)
  459. {
  460. int volume = value + 3 * (down ? 1 : -1);
  461. vstd::abetween(volume, 0, 100);
  462. setVolume(volume);
  463. onChange(volume);
  464. }
  465. }
  466. void CSlider::sliderClicked()
  467. {
  468. if(!(active & MOVE))
  469. addUsedEvents(MOVE);
  470. }
  471. void CSlider::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  472. {
  473. double v = 0;
  474. if(horizontal)
  475. {
  476. 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 )
  477. return;
  478. v = sEvent.x - pos.x - 24;
  479. v *= positions;
  480. v /= (pos.w - 48);
  481. }
  482. else
  483. {
  484. 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 )
  485. return;
  486. v = sEvent.y - pos.y - 24;
  487. v *= positions;
  488. v /= (pos.h - 48);
  489. }
  490. v += 0.5;
  491. if(v!=value)
  492. {
  493. moveTo((int)v);
  494. }
  495. }
  496. void CSlider::setScrollStep(int to)
  497. {
  498. scrollStep = to;
  499. }
  500. int CSlider::getAmount() const
  501. {
  502. return amount;
  503. }
  504. int CSlider::getValue() const
  505. {
  506. return value;
  507. }
  508. int CSlider::getCapacity() const
  509. {
  510. return capacity;
  511. }
  512. void CSlider::moveLeft()
  513. {
  514. moveTo(value-1);
  515. }
  516. void CSlider::moveRight()
  517. {
  518. moveTo(value+1);
  519. }
  520. void CSlider::moveBy(int amount)
  521. {
  522. moveTo(value + amount);
  523. }
  524. void CSlider::updateSliderPos()
  525. {
  526. if(horizontal)
  527. {
  528. if(positions)
  529. {
  530. double part = static_cast<double>(value) / positions;
  531. part*=(pos.w-48);
  532. int newPos = static_cast<int>(part + pos.x + 16 - slider->pos.x);
  533. slider->moveBy(Point(newPos, 0));
  534. }
  535. else
  536. slider->moveTo(Point(pos.x+16, pos.y));
  537. }
  538. else
  539. {
  540. if(positions)
  541. {
  542. double part = static_cast<double>(value) / positions;
  543. part*=(pos.h-48);
  544. int newPos = static_cast<int>(part + pos.y + 16 - slider->pos.y);
  545. slider->moveBy(Point(0, newPos));
  546. }
  547. else
  548. slider->moveTo(Point(pos.x, pos.y+16));
  549. }
  550. }
  551. void CSlider::moveTo(int to)
  552. {
  553. vstd::amax(to, 0);
  554. vstd::amin(to, positions);
  555. //same, old position?
  556. if(value == to)
  557. return;
  558. value = to;
  559. updateSliderPos();
  560. moved(to);
  561. }
  562. void CSlider::clickLeft(tribool down, bool previousState)
  563. {
  564. if(down && !slider->isBlocked())
  565. {
  566. double pw = 0;
  567. double rw = 0;
  568. if(horizontal)
  569. {
  570. pw = GH.getCursorPosition().x-pos.x-25;
  571. rw = pw / static_cast<double>(pos.w - 48);
  572. }
  573. else
  574. {
  575. pw = GH.getCursorPosition().y-pos.y-24;
  576. rw = pw / (pos.h-48);
  577. }
  578. if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
  579. return;
  580. // if (rw>1) return;
  581. // if (rw<0) return;
  582. slider->clickLeft(true, slider->mouseState(EIntObjMouseBtnType::LEFT));
  583. moveTo((int)(rw * positions + 0.5));
  584. return;
  585. }
  586. if(active & MOVE)
  587. removeUsedEvents(MOVE);
  588. }
  589. CSlider::CSlider(Point position, int totalw, std::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, CSlider::EStyle style)
  590. : CIntObject(LCLICK | RCLICK | WHEEL),
  591. capacity(Capacity),
  592. horizontal(Horizontal),
  593. amount(Amount),
  594. value(Value),
  595. scrollStep(1),
  596. moved(Moved)
  597. {
  598. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  599. setAmount(amount);
  600. vstd::amax(value, 0);
  601. vstd::amin(value, positions);
  602. strongInterest = true;
  603. pos.x += position.x;
  604. pos.y += position.y;
  605. if(style == BROWN)
  606. {
  607. std::string name = horizontal ? "IGPCRDIV.DEF" : "OVBUTN2.DEF";
  608. //NOTE: this images do not have "blocked" frames. They should be implemented somehow (e.g. palette transform or something...)
  609. left = std::make_shared<CButton>(Point(), name, CButton::tooltip());
  610. right = std::make_shared<CButton>(Point(), name, CButton::tooltip());
  611. slider = std::make_shared<CButton>(Point(), name, CButton::tooltip());
  612. left->setImageOrder(0, 1, 1, 1);
  613. right->setImageOrder(2, 3, 3, 3);
  614. slider->setImageOrder(4, 4, 4, 4);
  615. }
  616. else
  617. {
  618. left = std::make_shared<CButton>(Point(), horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF", CButton::tooltip());
  619. right = std::make_shared<CButton>(Point(), horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF", CButton::tooltip());
  620. slider = std::make_shared<CButton>(Point(), "SCNRBSL.DEF", CButton::tooltip());
  621. }
  622. slider->actOnDown = true;
  623. slider->soundDisabled = true;
  624. left->soundDisabled = true;
  625. right->soundDisabled = true;
  626. if (horizontal)
  627. right->moveBy(Point(totalw - right->pos.w, 0));
  628. else
  629. right->moveBy(Point(0, totalw - right->pos.h));
  630. left->addCallback(std::bind(&CSlider::moveLeft,this));
  631. right->addCallback(std::bind(&CSlider::moveRight,this));
  632. slider->addCallback(std::bind(&CSlider::sliderClicked,this));
  633. if(horizontal)
  634. {
  635. pos.h = slider->pos.h;
  636. pos.w = totalw;
  637. }
  638. else
  639. {
  640. pos.w = slider->pos.w;
  641. pos.h = totalw;
  642. }
  643. updateSliderPos();
  644. }
  645. CSlider::~CSlider() = default;
  646. void CSlider::block( bool on )
  647. {
  648. left->block(on);
  649. right->block(on);
  650. slider->block(on);
  651. }
  652. void CSlider::setAmount( int to )
  653. {
  654. amount = to;
  655. positions = to - capacity;
  656. vstd::amax(positions, 0);
  657. }
  658. void CSlider::showAll(SDL_Surface * to)
  659. {
  660. CSDL_Ext::fillRect(to, pos, Colors::BLACK);
  661. CIntObject::showAll(to);
  662. }
  663. void CSlider::wheelScrolled(bool down, bool in)
  664. {
  665. moveTo(value + 3 * (down ? +scrollStep : -scrollStep));
  666. }
  667. void CSlider::keyPressed(const SDL_KeyboardEvent & key)
  668. {
  669. if(key.state != SDL_PRESSED) return;
  670. int moveDest = value;
  671. switch(key.keysym.sym)
  672. {
  673. case SDLK_UP:
  674. if (!horizontal)
  675. moveDest = value - scrollStep;
  676. break;
  677. case SDLK_LEFT:
  678. if (horizontal)
  679. moveDest = value - scrollStep;
  680. break;
  681. case SDLK_DOWN:
  682. if (!horizontal)
  683. moveDest = value + scrollStep;
  684. break;
  685. case SDLK_RIGHT:
  686. if (horizontal)
  687. moveDest = value + scrollStep;
  688. break;
  689. case SDLK_PAGEUP:
  690. moveDest = value - capacity + scrollStep;
  691. break;
  692. case SDLK_PAGEDOWN:
  693. moveDest = value + capacity - scrollStep;
  694. break;
  695. case SDLK_HOME:
  696. moveDest = 0;
  697. break;
  698. case SDLK_END:
  699. moveDest = amount - capacity;
  700. break;
  701. default:
  702. return;
  703. }
  704. moveTo(moveDest);
  705. }
  706. void CSlider::moveToMin()
  707. {
  708. moveTo(0);
  709. }
  710. void CSlider::moveToMax()
  711. {
  712. moveTo(amount);
  713. }