Buttons.cpp 17 KB

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