Slider.cpp 6.3 KB

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