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. 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(EShortcut key)
  685. {
  686. int moveDest = value;
  687. switch(key)
  688. {
  689. case EShortcut::MOVE_UP:
  690. if (!horizontal)
  691. moveDest = value - scrollStep;
  692. break;
  693. case EShortcut::MOVE_LEFT:
  694. if (horizontal)
  695. moveDest = value - scrollStep;
  696. break;
  697. case EShortcut::MOVE_DOWN:
  698. if (!horizontal)
  699. moveDest = value + scrollStep;
  700. break;
  701. case EShortcut::MOVE_RIGHT:
  702. if (horizontal)
  703. moveDest = value + scrollStep;
  704. break;
  705. case EShortcut::MOVE_PAGE_UP:
  706. moveDest = value - capacity + scrollStep;
  707. break;
  708. case EShortcut::MOVE_PAGE_DOWN:
  709. moveDest = value + capacity - scrollStep;
  710. break;
  711. case EShortcut::MOVE_FIRST:
  712. moveDest = 0;
  713. break;
  714. case EShortcut::MOVE_LAST:
  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. }