Buttons.cpp 17 KB

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