Buttons.cpp 18 KB

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