Buttons.cpp 18 KB

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