Slider.cpp 6.5 KB

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