Buttons.cpp 18 KB

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