Buttons.cpp 17 KB

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