Slider.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 newPosition = 0;
  21. if(getOrientation() == Orientation::HORIZONTAL)
  22. {
  23. newPosition = cursorPosition.x - pos.x - 24;
  24. newPosition *= positions;
  25. newPosition /= (pos.w - 48);
  26. }
  27. else
  28. {
  29. newPosition = cursorPosition.y - pos.y - 24;
  30. newPosition *= positions;
  31. newPosition /= (pos.h - 48);
  32. }
  33. int positionInteger = std::round(newPosition);
  34. if(positionInteger != value)
  35. {
  36. scrollTo(static_cast<int>(newPosition));
  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. void CSlider::setValue(int to)
  63. {
  64. scrollTo(value);
  65. }
  66. int CSlider::getCapacity() const
  67. {
  68. return capacity;
  69. }
  70. void CSlider::scrollBy(int amount)
  71. {
  72. scrollTo(value + amount);
  73. }
  74. void CSlider::updateSliderPos()
  75. {
  76. if(getOrientation() == Orientation::HORIZONTAL)
  77. {
  78. if(positions)
  79. {
  80. double part = static_cast<double>(value) / positions;
  81. part*=(pos.w-48);
  82. int newPos = static_cast<int>(part + pos.x + 16 - slider->pos.x);
  83. slider->moveBy(Point(newPos, 0));
  84. }
  85. else
  86. slider->moveTo(Point(pos.x+16, pos.y));
  87. }
  88. else
  89. {
  90. if(positions)
  91. {
  92. double part = static_cast<double>(value) / positions;
  93. part*=(pos.h-48);
  94. int newPos = static_cast<int>(part + pos.y + 16 - slider->pos.y);
  95. slider->moveBy(Point(0, newPos));
  96. }
  97. else
  98. slider->moveTo(Point(pos.x, pos.y+16));
  99. }
  100. }
  101. void CSlider::scrollTo(int to)
  102. {
  103. vstd::amax(to, 0);
  104. vstd::amin(to, positions);
  105. //same, old position?
  106. if(value == to)
  107. return;
  108. value = to;
  109. updateSliderPos();
  110. moved(getValue());
  111. }
  112. void CSlider::clickPressed(const Point & cursorPosition)
  113. {
  114. if(!slider->isBlocked())
  115. {
  116. double pw = 0;
  117. double rw = 0;
  118. if(getOrientation() == Orientation::HORIZONTAL)
  119. {
  120. pw = cursorPosition.x-pos.x-25;
  121. rw = pw / static_cast<double>(pos.w - 48);
  122. }
  123. else
  124. {
  125. pw = cursorPosition.y-pos.y-24;
  126. rw = pw / (pos.h-48);
  127. }
  128. // click on area covered by buttons -> ignore, will be handled by left/right buttons
  129. if (!vstd::iswithin(rw, 0, 1))
  130. return;
  131. slider->clickPressed(cursorPosition);
  132. scrollTo((int)(rw * positions + 0.5));
  133. return;
  134. }
  135. }
  136. bool CSlider::receiveEvent(const Point &position, int eventType) const
  137. {
  138. if (eventType == LCLICK)
  139. {
  140. return pos.isInside(position) && !left->pos.isInside(position) && !right->pos.isInside(position);
  141. }
  142. if(eventType != WHEEL && eventType != GESTURE)
  143. {
  144. return CIntObject::receiveEvent(position, eventType);
  145. }
  146. if (!scrollBounds)
  147. return true;
  148. Rect testTarget = *scrollBounds + pos.topLeft();
  149. return testTarget.isInside(position);
  150. }
  151. CSlider::CSlider(Point position, int totalw, const SliderMovingFunctor & Moved, int Capacity, int Amount, int Value, Orientation orientation, CSlider::EStyle style)
  152. : Scrollable(LCLICK | DRAG, position, orientation ),
  153. capacity(Capacity),
  154. amount(Amount),
  155. value(Value),
  156. moved(Moved)
  157. {
  158. OBJECT_CONSTRUCTION_CAPTURING(255-DISPOSE);
  159. setAmount(amount);
  160. vstd::amax(value, 0);
  161. vstd::amin(value, positions);
  162. if(style == BROWN)
  163. {
  164. AnimationPath name = AnimationPath::builtin(getOrientation() == Orientation::HORIZONTAL ? "IGPCRDIV.DEF" : "OVBUTN2.DEF");
  165. //NOTE: this images do not have "blocked" frames. They should be implemented somehow (e.g. palette transform or something...)
  166. left = std::make_shared<CButton>(Point(), name, CButton::tooltip());
  167. right = std::make_shared<CButton>(Point(), name, CButton::tooltip());
  168. slider = std::make_shared<CButton>(Point(), name, CButton::tooltip());
  169. left->setImageOrder(0, 1, 1, 1);
  170. right->setImageOrder(2, 3, 3, 3);
  171. slider->setImageOrder(4, 4, 4, 4);
  172. }
  173. else
  174. {
  175. left = std::make_shared<CButton>(Point(), AnimationPath::builtin(getOrientation() == Orientation::HORIZONTAL ? "SCNRBLF.DEF" : "SCNRBUP.DEF"), CButton::tooltip());
  176. right = std::make_shared<CButton>(Point(), AnimationPath::builtin(getOrientation() == Orientation::HORIZONTAL ? "SCNRBRT.DEF" : "SCNRBDN.DEF"), CButton::tooltip());
  177. slider = std::make_shared<CButton>(Point(), AnimationPath::builtin("SCNRBSL.DEF"), CButton::tooltip());
  178. }
  179. slider->setActOnDown(true);
  180. slider->setSoundDisabled(true);
  181. left->setSoundDisabled(true);
  182. right->setSoundDisabled(true);
  183. if (getOrientation() == Orientation::HORIZONTAL)
  184. right->moveBy(Point(totalw - right->pos.w, 0));
  185. else
  186. right->moveBy(Point(0, totalw - right->pos.h));
  187. left->addCallback(std::bind(&CSlider::scrollPrev,this));
  188. right->addCallback(std::bind(&CSlider::scrollNext,this));
  189. if(getOrientation() == Orientation::HORIZONTAL)
  190. {
  191. pos.h = slider->pos.h;
  192. pos.w = totalw;
  193. }
  194. else
  195. {
  196. pos.w = slider->pos.w;
  197. pos.h = totalw;
  198. }
  199. // for horizontal sliders that act as values selection - add keyboard event to receive left/right click
  200. if (getOrientation() == Orientation::HORIZONTAL)
  201. addUsedEvents(KEYBOARD);
  202. updateSliderPos();
  203. }
  204. CSlider::~CSlider() = default;
  205. void CSlider::block( bool on )
  206. {
  207. left->block(on);
  208. right->block(on);
  209. slider->block(on);
  210. }
  211. void CSlider::setAmount( int to )
  212. {
  213. amount = to;
  214. positions = to - capacity;
  215. vstd::amax(positions, 0);
  216. }
  217. void CSlider::showAll(Canvas & to)
  218. {
  219. to.drawColor(pos, Colors::BLACK);
  220. CIntObject::showAll(to);
  221. }
  222. void CSlider::keyPressed(EShortcut key)
  223. {
  224. int moveDest = value;
  225. switch(key)
  226. {
  227. case EShortcut::MOVE_UP:
  228. if (getOrientation() == Orientation::VERTICAL)
  229. moveDest = value - getScrollStep();
  230. break;
  231. case EShortcut::MOVE_LEFT:
  232. if (getOrientation() == Orientation::HORIZONTAL)
  233. moveDest = value - getScrollStep();
  234. break;
  235. case EShortcut::MOVE_DOWN:
  236. if (getOrientation() == Orientation::VERTICAL)
  237. moveDest = value + getScrollStep();
  238. break;
  239. case EShortcut::MOVE_RIGHT:
  240. if (getOrientation() == Orientation::HORIZONTAL)
  241. moveDest = value + getScrollStep();
  242. break;
  243. case EShortcut::MOVE_PAGE_UP:
  244. moveDest = value - capacity + getScrollStep();
  245. break;
  246. case EShortcut::MOVE_PAGE_DOWN:
  247. moveDest = value + capacity - getScrollStep();
  248. break;
  249. case EShortcut::MOVE_FIRST:
  250. moveDest = 0;
  251. break;
  252. case EShortcut::MOVE_LAST:
  253. moveDest = amount - capacity;
  254. break;
  255. default:
  256. return;
  257. }
  258. scrollTo(moveDest);
  259. }
  260. void CSlider::scrollToMin()
  261. {
  262. scrollTo(0);
  263. }
  264. void CSlider::scrollToMax()
  265. {
  266. scrollTo(amount);
  267. }
  268. SliderNonlinear::SliderNonlinear(Point position, int length, const std::function<void(int)> & Moved, const std::vector<int> & values, int Value, Orientation orientation, EStyle style)
  269. : CSlider(position, length, Moved, 1, values.size(), Value, orientation, style)
  270. , scaledValues(values)
  271. {
  272. }
  273. int SliderNonlinear::getValue() const
  274. {
  275. return scaledValues.at(CSlider::getValue());
  276. }
  277. void SliderNonlinear::setValue(int to)
  278. {
  279. size_t nearest = 0;
  280. for(size_t i = 0; i < scaledValues.size(); ++i)
  281. {
  282. int nearestDistance = std::abs(to - scaledValues[nearest]);
  283. int currentDistance = std::abs(to - scaledValues[i]);
  284. if(currentDistance < nearestDistance)
  285. nearest = i;
  286. }
  287. scrollTo(nearest);
  288. }