Slider.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Slider.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 "Slider.h"
  12. #include "Buttons.h"
  13. #include "../gui/MouseButton.h"
  14. #include "../gui/Shortcut.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../render/Canvas.h"
  17. #include "../render/Colors.h"
  18. void CSlider::mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance)
  19. {
  20. double v = 0;
  21. if(getOrientation() == Orientation::HORIZONTAL)
  22. {
  23. v = cursorPosition.x - pos.x - 24;
  24. v *= positions;
  25. v /= (pos.w - 48);
  26. }
  27. else
  28. {
  29. v = cursorPosition.y - pos.y - 24;
  30. v *= positions;
  31. v /= (pos.h - 48);
  32. }
  33. v += 0.5;
  34. if(v!=value)
  35. {
  36. scrollTo(static_cast<int>(v));
  37. }
  38. }
  39. void CSlider::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  40. {
  41. if (getOrientation() == Orientation::VERTICAL)
  42. Scrollable::gesturePanning(initialPosition, currentPosition, lastUpdateDistance);
  43. else
  44. mouseDragged(currentPosition, lastUpdateDistance);
  45. }
  46. void CSlider::setScrollBounds(const Rect & bounds )
  47. {
  48. scrollBounds = bounds;
  49. }
  50. void CSlider::clearScrollBounds()
  51. {
  52. scrollBounds = std::nullopt;
  53. }
  54. int CSlider::getAmount() const
  55. {
  56. return amount;
  57. }
  58. int CSlider::getValue() const
  59. {
  60. return value;
  61. }
  62. int CSlider::getCapacity() const
  63. {
  64. return capacity;
  65. }
  66. void CSlider::scrollBy(int amount)
  67. {
  68. scrollTo(value + amount);
  69. }
  70. void CSlider::updateSliderPos()
  71. {
  72. if(getOrientation() == Orientation::HORIZONTAL)
  73. {
  74. if(positions)
  75. {
  76. double part = static_cast<double>(value) / positions;
  77. part*=(pos.w-48);
  78. int newPos = static_cast<int>(part + pos.x + 16 - slider->pos.x);
  79. slider->moveBy(Point(newPos, 0));
  80. }
  81. else
  82. slider->moveTo(Point(pos.x+16, pos.y));
  83. }
  84. else
  85. {
  86. if(positions)
  87. {
  88. double part = static_cast<double>(value) / positions;
  89. part*=(pos.h-48);
  90. int newPos = static_cast<int>(part + pos.y + 16 - slider->pos.y);
  91. slider->moveBy(Point(0, newPos));
  92. }
  93. else
  94. slider->moveTo(Point(pos.x, pos.y+16));
  95. }
  96. }
  97. void CSlider::scrollTo(int to)
  98. {
  99. vstd::amax(to, 0);
  100. vstd::amin(to, positions);
  101. //same, old position?
  102. if(value == to)
  103. return;
  104. value = to;
  105. updateSliderPos();
  106. moved(to);
  107. }
  108. void CSlider::clickPressed(const Point & cursorPosition)
  109. {
  110. if(!slider->isBlocked())
  111. {
  112. double pw = 0;
  113. double rw = 0;
  114. if(getOrientation() == Orientation::HORIZONTAL)
  115. {
  116. pw = cursorPosition.x-pos.x-25;
  117. rw = pw / static_cast<double>(pos.w - 48);
  118. }
  119. else
  120. {
  121. pw = cursorPosition.y-pos.y-24;
  122. rw = pw / (pos.h-48);
  123. }
  124. // click on area covered by buttons -> ignore, will be handled by left/right buttons
  125. if (!vstd::iswithin(rw, 0, 1))
  126. return;
  127. slider->clickPressed(cursorPosition);
  128. scrollTo((int)(rw * positions + 0.5));
  129. return;
  130. }
  131. }
  132. bool CSlider::receiveEvent(const Point &position, int eventType) const
  133. {
  134. if(eventType != WHEEL && eventType != GESTURE)
  135. {
  136. return CIntObject::receiveEvent(position, eventType);
  137. }
  138. if (!scrollBounds)
  139. return true;
  140. Rect testTarget = *scrollBounds + pos.topLeft();
  141. return testTarget.isInside(position);
  142. }
  143. CSlider::CSlider(Point position, int totalw, std::function<void(int)> Moved, int Capacity, int Amount, int Value, Orientation orientation, CSlider::EStyle style)
  144. : Scrollable(LCLICK | DRAG, position, orientation ),
  145. capacity(Capacity),
  146. amount(Amount),
  147. value(Value),
  148. moved(Moved)
  149. {
  150. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  151. setAmount(amount);
  152. vstd::amax(value, 0);
  153. vstd::amin(value, positions);
  154. if(style == BROWN)
  155. {
  156. std::string name = getOrientation() == Orientation::HORIZONTAL ? "IGPCRDIV.DEF" : "OVBUTN2.DEF";
  157. //NOTE: this images do not have "blocked" frames. They should be implemented somehow (e.g. palette transform or something...)
  158. left = std::make_shared<CButton>(Point(), name, CButton::tooltip());
  159. right = std::make_shared<CButton>(Point(), name, CButton::tooltip());
  160. slider = std::make_shared<CButton>(Point(), name, CButton::tooltip());
  161. left->setImageOrder(0, 1, 1, 1);
  162. right->setImageOrder(2, 3, 3, 3);
  163. slider->setImageOrder(4, 4, 4, 4);
  164. }
  165. else
  166. {
  167. left = std::make_shared<CButton>(Point(), getOrientation() == Orientation::HORIZONTAL ? "SCNRBLF.DEF" : "SCNRBUP.DEF", CButton::tooltip());
  168. right = std::make_shared<CButton>(Point(), getOrientation() == Orientation::HORIZONTAL ? "SCNRBRT.DEF" : "SCNRBDN.DEF", CButton::tooltip());
  169. slider = std::make_shared<CButton>(Point(), "SCNRBSL.DEF", CButton::tooltip());
  170. }
  171. slider->actOnDown = true;
  172. slider->soundDisabled = true;
  173. left->soundDisabled = true;
  174. right->soundDisabled = true;
  175. if (getOrientation() == Orientation::HORIZONTAL)
  176. right->moveBy(Point(totalw - right->pos.w, 0));
  177. else
  178. right->moveBy(Point(0, totalw - right->pos.h));
  179. left->addCallback(std::bind(&CSlider::scrollPrev,this));
  180. right->addCallback(std::bind(&CSlider::scrollNext,this));
  181. if(getOrientation() == Orientation::HORIZONTAL)
  182. {
  183. pos.h = slider->pos.h;
  184. pos.w = totalw;
  185. }
  186. else
  187. {
  188. pos.w = slider->pos.w;
  189. pos.h = totalw;
  190. }
  191. // for horizontal sliders that act as values selection - add keyboard event to receive left/right click
  192. if (getOrientation() == Orientation::HORIZONTAL)
  193. addUsedEvents(KEYBOARD);
  194. updateSliderPos();
  195. }
  196. CSlider::~CSlider() = default;
  197. void CSlider::block( bool on )
  198. {
  199. left->block(on);
  200. right->block(on);
  201. slider->block(on);
  202. }
  203. void CSlider::setAmount( int to )
  204. {
  205. amount = to;
  206. positions = to - capacity;
  207. vstd::amax(positions, 0);
  208. }
  209. void CSlider::showAll(Canvas & to)
  210. {
  211. to.drawColor(pos, Colors::BLACK);
  212. CIntObject::showAll(to);
  213. }
  214. void CSlider::keyPressed(EShortcut key)
  215. {
  216. int moveDest = value;
  217. switch(key)
  218. {
  219. case EShortcut::MOVE_UP:
  220. if (getOrientation() == Orientation::VERTICAL)
  221. moveDest = value - getScrollStep();
  222. break;
  223. case EShortcut::MOVE_LEFT:
  224. if (getOrientation() == Orientation::HORIZONTAL)
  225. moveDest = value - getScrollStep();
  226. break;
  227. case EShortcut::MOVE_DOWN:
  228. if (getOrientation() == Orientation::VERTICAL)
  229. moveDest = value + getScrollStep();
  230. break;
  231. case EShortcut::MOVE_RIGHT:
  232. if (getOrientation() == Orientation::HORIZONTAL)
  233. moveDest = value + getScrollStep();
  234. break;
  235. case EShortcut::MOVE_PAGE_UP:
  236. moveDest = value - capacity + getScrollStep();
  237. break;
  238. case EShortcut::MOVE_PAGE_DOWN:
  239. moveDest = value + capacity - getScrollStep();
  240. break;
  241. case EShortcut::MOVE_FIRST:
  242. moveDest = 0;
  243. break;
  244. case EShortcut::MOVE_LAST:
  245. moveDest = amount - capacity;
  246. break;
  247. default:
  248. return;
  249. }
  250. scrollTo(moveDest);
  251. }
  252. void CSlider::scrollToMin()
  253. {
  254. scrollTo(0);
  255. }
  256. void CSlider::scrollToMax()
  257. {
  258. scrollTo(amount);
  259. }