Slider.cpp 8.3 KB

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