Buttons.cpp 18 KB

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