Slider.cpp 7.7 KB

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