Buttons.cpp 17 KB

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