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