Buttons.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. if(!isActive(MOVE))
  475. addUsedEvents(MOVE);
  476. }
  477. void CSlider::mouseMoved (const Point & cursorPosition)
  478. {
  479. double v = 0;
  480. if(horizontal)
  481. {
  482. 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 )
  483. return;
  484. v = cursorPosition.x - pos.x - 24;
  485. v *= positions;
  486. v /= (pos.w - 48);
  487. }
  488. else
  489. {
  490. 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 )
  491. return;
  492. v = cursorPosition.y - pos.y - 24;
  493. v *= positions;
  494. v /= (pos.h - 48);
  495. }
  496. v += 0.5;
  497. if(v!=value)
  498. {
  499. moveTo(static_cast<int>(v));
  500. }
  501. }
  502. void CSlider::setScrollStep(int to)
  503. {
  504. scrollStep = to;
  505. }
  506. void CSlider::setScrollBounds(const Rect & bounds )
  507. {
  508. scrollBounds = bounds;
  509. }
  510. void CSlider::clearScrollBounds()
  511. {
  512. scrollBounds = std::nullopt;
  513. }
  514. int CSlider::getAmount() const
  515. {
  516. return amount;
  517. }
  518. int CSlider::getValue() const
  519. {
  520. return value;
  521. }
  522. int CSlider::getCapacity() const
  523. {
  524. return capacity;
  525. }
  526. void CSlider::moveLeft()
  527. {
  528. moveTo(value-1);
  529. }
  530. void CSlider::moveRight()
  531. {
  532. moveTo(value+1);
  533. }
  534. void CSlider::moveBy(int amount)
  535. {
  536. moveTo(value + amount);
  537. }
  538. void CSlider::updateSliderPos()
  539. {
  540. if(horizontal)
  541. {
  542. if(positions)
  543. {
  544. double part = static_cast<double>(value) / positions;
  545. part*=(pos.w-48);
  546. int newPos = static_cast<int>(part + pos.x + 16 - slider->pos.x);
  547. slider->moveBy(Point(newPos, 0));
  548. }
  549. else
  550. slider->moveTo(Point(pos.x+16, pos.y));
  551. }
  552. else
  553. {
  554. if(positions)
  555. {
  556. double part = static_cast<double>(value) / positions;
  557. part*=(pos.h-48);
  558. int newPos = static_cast<int>(part + pos.y + 16 - slider->pos.y);
  559. slider->moveBy(Point(0, newPos));
  560. }
  561. else
  562. slider->moveTo(Point(pos.x, pos.y+16));
  563. }
  564. }
  565. void CSlider::moveTo(int to)
  566. {
  567. vstd::amax(to, 0);
  568. vstd::amin(to, positions);
  569. //same, old position?
  570. if(value == to)
  571. return;
  572. value = to;
  573. updateSliderPos();
  574. moved(to);
  575. }
  576. void CSlider::clickLeft(tribool down, bool previousState)
  577. {
  578. if(down && !slider->isBlocked())
  579. {
  580. double pw = 0;
  581. double rw = 0;
  582. if(horizontal)
  583. {
  584. pw = GH.getCursorPosition().x-pos.x-25;
  585. rw = pw / static_cast<double>(pos.w - 48);
  586. }
  587. else
  588. {
  589. pw = GH.getCursorPosition().y-pos.y-24;
  590. rw = pw / (pos.h-48);
  591. }
  592. if(pw < -8 || pw > (horizontal ? pos.w : pos.h) - 40)
  593. return;
  594. // if (rw>1) return;
  595. // if (rw<0) return;
  596. slider->clickLeft(true, slider->isMouseButtonPressed(MouseButton::LEFT));
  597. moveTo((int)(rw * positions + 0.5));
  598. return;
  599. }
  600. if(isActive(MOVE))
  601. removeUsedEvents(MOVE);
  602. }
  603. CSlider::CSlider(Point position, int totalw, std::function<void(int)> Moved, int Capacity, int Amount, int Value, bool Horizontal, CSlider::EStyle style)
  604. : CIntObject(LCLICK | RCLICK | WHEEL),
  605. capacity(Capacity),
  606. horizontal(Horizontal),
  607. amount(Amount),
  608. value(Value),
  609. scrollStep(1),
  610. moved(Moved)
  611. {
  612. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  613. setAmount(amount);
  614. vstd::amax(value, 0);
  615. vstd::amin(value, positions);
  616. setMoveEventStrongInterest(true);
  617. pos.x += position.x;
  618. pos.y += position.y;
  619. if(style == BROWN)
  620. {
  621. std::string name = horizontal ? "IGPCRDIV.DEF" : "OVBUTN2.DEF";
  622. //NOTE: this images do not have "blocked" frames. They should be implemented somehow (e.g. palette transform or something...)
  623. left = std::make_shared<CButton>(Point(), name, CButton::tooltip());
  624. right = std::make_shared<CButton>(Point(), name, CButton::tooltip());
  625. slider = std::make_shared<CButton>(Point(), name, CButton::tooltip());
  626. left->setImageOrder(0, 1, 1, 1);
  627. right->setImageOrder(2, 3, 3, 3);
  628. slider->setImageOrder(4, 4, 4, 4);
  629. }
  630. else
  631. {
  632. left = std::make_shared<CButton>(Point(), horizontal ? "SCNRBLF.DEF" : "SCNRBUP.DEF", CButton::tooltip());
  633. right = std::make_shared<CButton>(Point(), horizontal ? "SCNRBRT.DEF" : "SCNRBDN.DEF", CButton::tooltip());
  634. slider = std::make_shared<CButton>(Point(), "SCNRBSL.DEF", CButton::tooltip());
  635. }
  636. slider->actOnDown = true;
  637. slider->soundDisabled = true;
  638. left->soundDisabled = true;
  639. right->soundDisabled = true;
  640. if (horizontal)
  641. right->moveBy(Point(totalw - right->pos.w, 0));
  642. else
  643. right->moveBy(Point(0, totalw - right->pos.h));
  644. left->addCallback(std::bind(&CSlider::moveLeft,this));
  645. right->addCallback(std::bind(&CSlider::moveRight,this));
  646. slider->addCallback(std::bind(&CSlider::sliderClicked,this));
  647. if(horizontal)
  648. {
  649. pos.h = slider->pos.h;
  650. pos.w = totalw;
  651. }
  652. else
  653. {
  654. pos.w = slider->pos.w;
  655. pos.h = totalw;
  656. }
  657. updateSliderPos();
  658. }
  659. CSlider::~CSlider() = default;
  660. void CSlider::block( bool on )
  661. {
  662. left->block(on);
  663. right->block(on);
  664. slider->block(on);
  665. }
  666. void CSlider::setAmount( int to )
  667. {
  668. amount = to;
  669. positions = to - capacity;
  670. vstd::amax(positions, 0);
  671. }
  672. void CSlider::showAll(SDL_Surface * to)
  673. {
  674. CSDL_Ext::fillRect(to, pos, Colors::BLACK);
  675. CIntObject::showAll(to);
  676. }
  677. void CSlider::wheelScrolled(bool down, bool in)
  678. {
  679. if (scrollBounds)
  680. {
  681. Rect testTarget = *scrollBounds + pos.topLeft();
  682. if (!testTarget.isInside(GH.getCursorPosition()))
  683. return;
  684. }
  685. // vertical slider -> scrolling up move slider upwards
  686. // horizontal slider -> scrolling up moves slider towards right
  687. bool positive = (down != horizontal);
  688. moveTo(value + 3 * (positive ? +scrollStep : -scrollStep));
  689. }
  690. void CSlider::keyPressed(EShortcut key)
  691. {
  692. int moveDest = value;
  693. switch(key)
  694. {
  695. case EShortcut::MOVE_UP:
  696. if (!horizontal)
  697. moveDest = value - scrollStep;
  698. break;
  699. case EShortcut::MOVE_LEFT:
  700. if (horizontal)
  701. moveDest = value - scrollStep;
  702. break;
  703. case EShortcut::MOVE_DOWN:
  704. if (!horizontal)
  705. moveDest = value + scrollStep;
  706. break;
  707. case EShortcut::MOVE_RIGHT:
  708. if (horizontal)
  709. moveDest = value + scrollStep;
  710. break;
  711. case EShortcut::MOVE_PAGE_UP:
  712. moveDest = value - capacity + scrollStep;
  713. break;
  714. case EShortcut::MOVE_PAGE_DOWN:
  715. moveDest = value + capacity - scrollStep;
  716. break;
  717. case EShortcut::MOVE_FIRST:
  718. moveDest = 0;
  719. break;
  720. case EShortcut::MOVE_LAST:
  721. moveDest = amount - capacity;
  722. break;
  723. default:
  724. return;
  725. }
  726. moveTo(moveDest);
  727. }
  728. void CSlider::moveToMin()
  729. {
  730. moveTo(0);
  731. }
  732. void CSlider::moveToMax()
  733. {
  734. moveTo(amount);
  735. }