Buttons.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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 = 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. OBJ_CONSTRUCTION_CAPTURING_ALL;
  70. addOverlay(new CLabel(pos.w/2, pos.h/2, font, CENTER, color, Text));
  71. update();
  72. }
  73. void CButton::addOverlay(CIntObject *newOverlay)
  74. {
  75. delete overlay;
  76. overlay = newOverlay;
  77. addChild(newOverlay);
  78. overlay->moveTo(overlay->pos.centerIn(pos).topLeft());
  79. update();
  80. }
  81. void CButton::addImage(std::string filename)
  82. {
  83. imageNames.push_back(filename);
  84. }
  85. void CButton::addHoverText(ButtonState state, std::string text)
  86. {
  87. hoverTexts[state] = text;
  88. }
  89. void CButton::setImageOrder(int state1, int state2, int state3, int state4)
  90. {
  91. stateToIndex[0] = state1;
  92. stateToIndex[1] = state2;
  93. stateToIndex[2] = state3;
  94. stateToIndex[3] = state4;
  95. update();
  96. }
  97. void CButton::setAnimateLonelyFrame(bool agreement)
  98. {
  99. animateLonelyFrame = agreement;
  100. }
  101. void CButton::setState(ButtonState newState)
  102. {
  103. if (state == newState)
  104. return;
  105. state = newState;
  106. update();
  107. }
  108. CButton::ButtonState CButton::getState()
  109. {
  110. return state;
  111. }
  112. bool CButton::isBlocked()
  113. {
  114. return state == BLOCKED;
  115. }
  116. bool CButton::isHighlighted()
  117. {
  118. return state == HIGHLIGHTED;
  119. }
  120. void CButton::block(bool on)
  121. {
  122. if(on || state == BLOCKED) //dont change button state if unblock requested, but it's not blocked
  123. setState(on ? BLOCKED : NORMAL);
  124. }
  125. void CButton::onButtonClicked()
  126. {
  127. // debug logging to figure out pressed button (and as result - player actions) in case of crash
  128. logAnim->trace("Button clicked at %dx%d", pos.x, pos.y);
  129. CIntObject * parent = this->parent;
  130. std::string prefix = "Parent is";
  131. while (parent)
  132. {
  133. logAnim->trace("%s%s at %dx%d", prefix, typeid(*parent).name(), parent->pos.x, parent->pos.y);
  134. parent = parent->parent;
  135. prefix = '\t' + prefix;
  136. }
  137. callback();
  138. }
  139. void CButton::clickLeft(tribool down, bool previousState)
  140. {
  141. if(isBlocked())
  142. return;
  143. if (down)
  144. {
  145. if (!soundDisabled)
  146. CCS->soundh->playSound(soundBase::button);
  147. setState(PRESSED);
  148. }
  149. else if(hoverable && hovered)
  150. setState(HIGHLIGHTED);
  151. else
  152. setState(NORMAL);
  153. if (actOnDown && down)
  154. {
  155. onButtonClicked();
  156. }
  157. else if (!actOnDown && previousState && (down==false))
  158. {
  159. onButtonClicked();
  160. }
  161. }
  162. void CButton::clickRight(tribool down, bool previousState)
  163. {
  164. if(down && helpBox.size()) //there is no point to show window with nothing inside...
  165. CRClickPopup::createAndPush(helpBox);
  166. }
  167. void CButton::hover (bool on)
  168. {
  169. if(hoverable && !isBlocked())
  170. {
  171. if(on)
  172. setState(HIGHLIGHTED);
  173. else
  174. setState(NORMAL);
  175. }
  176. /*if(pressedL && on) // WTF is this? When this is used?
  177. setState(PRESSED);*/
  178. std::string name = hoverTexts[getState()].empty()
  179. ? hoverTexts[0]
  180. : hoverTexts[getState()];
  181. if(!name.empty() && !isBlocked()) //if there is no name, there is nothing to display also
  182. {
  183. if (LOCPLINT && LOCPLINT->battleInt) //for battle buttons
  184. {
  185. if(on && LOCPLINT->battleInt->console->alterTxt == "")
  186. {
  187. LOCPLINT->battleInt->console->alterTxt = name;
  188. LOCPLINT->battleInt->console->whoSetAlter = 1;
  189. }
  190. else if (LOCPLINT->battleInt->console->alterTxt == name)
  191. {
  192. LOCPLINT->battleInt->console->alterTxt = "";
  193. LOCPLINT->battleInt->console->whoSetAlter = 0;
  194. }
  195. }
  196. else if(GH.statusbar) //for other buttons
  197. {
  198. if (on)
  199. GH.statusbar->setText(name);
  200. else if ( GH.statusbar->getText()==(name) )
  201. GH.statusbar->clear();
  202. }
  203. }
  204. }
  205. CButton::CButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help, CFunctionList<void()> Callback, int key, bool playerColoredButton):
  206. CKeyShortcut(key),
  207. callback(Callback)
  208. {
  209. addUsedEvents(LCLICK | RCLICK | HOVER | KEYBOARD);
  210. stateToIndex[0] = 0;
  211. stateToIndex[1] = 1;
  212. stateToIndex[2] = 2;
  213. stateToIndex[3] = 3;
  214. state=NORMAL;
  215. image = nullptr;
  216. overlay = nullptr;
  217. currentImage = -1;
  218. hoverable = actOnDown = soundDisabled = false;
  219. hoverTexts[0] = help.first;
  220. helpBox=help.second;
  221. pos.x += position.x;
  222. pos.y += position.y;
  223. if (!defName.empty())
  224. {
  225. imageNames.push_back(defName);
  226. setIndex(0, playerColoredButton);
  227. }
  228. }
  229. void CButton::setIndex(size_t index, bool playerColoredButton)
  230. {
  231. if (index == currentImage || index>=imageNames.size())
  232. return;
  233. currentImage = index;
  234. auto anim = std::make_shared<CAnimation>(imageNames[index]);
  235. setImage(anim, playerColoredButton);
  236. }
  237. void CButton::setImage(std::shared_ptr<CAnimation> anim, bool playerColoredButton, int animFlags)
  238. {
  239. OBJ_CONSTRUCTION_CAPTURING_ALL;
  240. image = new CAnimImage(anim, getState(), 0, 0, 0, animFlags);
  241. if (playerColoredButton)
  242. image->playerColored(LOCPLINT->playerID);
  243. pos = image->pos;
  244. }
  245. void CButton::setPlayerColor(PlayerColor player)
  246. {
  247. if (image)
  248. image->playerColored(player);
  249. }
  250. void CButton::showAll(SDL_Surface * to)
  251. {
  252. CIntObject::showAll(to);
  253. auto borderColor = stateToBorderColor[getState()];
  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(std::make_shared<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->mouseState(EIntObjMouseBtnType::LEFT));
  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. }