Slider.cpp 6.0 KB

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