Slider.cpp 9.5 KB

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