Buttons.cpp 18 KB

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