scroll_bar.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /**************************************************************************/
  2. /* scroll_bar.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "scroll_bar.h"
  31. #include "scene/main/window.h"
  32. #include "scene/theme/theme_db.h"
  33. bool ScrollBar::focus_by_default = false;
  34. void ScrollBar::set_can_focus_by_default(bool p_can_focus) {
  35. focus_by_default = p_can_focus;
  36. }
  37. void ScrollBar::gui_input(const Ref<InputEvent> &p_event) {
  38. ERR_FAIL_COND(p_event.is_null());
  39. Ref<InputEventMouseMotion> m = p_event;
  40. Ref<InputEventMouseButton> b = p_event;
  41. Ref<InputEventPanGesture> pg = p_event;
  42. if (pg.is_valid()) {
  43. accept_event();
  44. if (orientation == HORIZONTAL) {
  45. if (pg->get_delta().x != 0) {
  46. if (pg->get_delta().x < 0) {
  47. scroll(-MAX(fabsf(pg->get_delta().x), get_step()));
  48. }
  49. if (pg->get_delta().x > 0) {
  50. scroll(MAX(pg->get_delta().x, get_step()));
  51. }
  52. } else if (pg->get_delta().y != 0) {
  53. if (pg->get_delta().y < 0) {
  54. scroll(-MAX(fabsf(pg->get_delta().y), get_step()));
  55. }
  56. if (pg->get_delta().y > 0) {
  57. scroll(MAX(pg->get_delta().y, get_step()));
  58. }
  59. }
  60. } else {
  61. if (pg->get_delta().y < 0) {
  62. scroll(-MAX(fabsf(pg->get_delta().y), get_step()));
  63. }
  64. if (pg->get_delta().y > 0) {
  65. scroll(MAX(pg->get_delta().y, get_step()));
  66. }
  67. }
  68. }
  69. if (b.is_valid()) {
  70. accept_event();
  71. if (b->get_button_index() == MouseButton::WHEEL_DOWN && b->is_pressed()) {
  72. double change = ((get_page() != 0.0) ? get_page() / PAGE_DIVISOR : (get_max() - get_min()) / 16.0) * b->get_factor();
  73. scroll(MAX(change, get_step()));
  74. accept_event();
  75. }
  76. if (b->get_button_index() == MouseButton::WHEEL_UP && b->is_pressed()) {
  77. double change = ((get_page() != 0.0) ? get_page() / PAGE_DIVISOR : (get_max() - get_min()) / 16.0) * b->get_factor();
  78. scroll(-MAX(change, get_step()));
  79. accept_event();
  80. }
  81. if (b->get_button_index() != MouseButton::LEFT) {
  82. return;
  83. }
  84. if (b->is_pressed()) {
  85. double ofs = orientation == VERTICAL ? b->get_position().y : b->get_position().x;
  86. Ref<Texture2D> decr = theme_cache.decrement_icon;
  87. Ref<Texture2D> incr = theme_cache.increment_icon;
  88. double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width();
  89. double incr_size = orientation == VERTICAL ? incr->get_height() : incr->get_width();
  90. double grabber_ofs = get_grabber_offset();
  91. double grabber_size = get_grabber_size();
  92. double total = orientation == VERTICAL ? get_size().height : get_size().width;
  93. if (ofs < decr_size) {
  94. decr_active = true;
  95. scroll(-(custom_step >= 0 ? custom_step : get_step()));
  96. queue_redraw();
  97. return;
  98. }
  99. if (ofs > total - incr_size) {
  100. incr_active = true;
  101. scroll(custom_step >= 0 ? custom_step : get_step());
  102. queue_redraw();
  103. return;
  104. }
  105. ofs -= decr_size + theme_cache.scroll_style->get_margin(orientation == VERTICAL ? SIDE_TOP : SIDE_LEFT);
  106. if (ofs < grabber_ofs) {
  107. if (scrolling) {
  108. target_scroll = CLAMP(target_scroll - get_page(), get_min(), get_max() - get_page());
  109. } else {
  110. double change = get_page() != 0.0 ? get_page() : (get_max() - get_min()) / 16.0;
  111. target_scroll = CLAMP(get_value() - change, get_min(), get_max() - get_page());
  112. }
  113. if (smooth_scroll_enabled) {
  114. scrolling = true;
  115. set_process_internal(true);
  116. } else {
  117. scroll_to(target_scroll);
  118. }
  119. return;
  120. }
  121. ofs -= grabber_ofs;
  122. if (ofs < grabber_size) {
  123. drag.active = true;
  124. drag.pos_at_click = grabber_ofs + ofs;
  125. drag.value_at_click = get_as_ratio();
  126. queue_redraw();
  127. } else {
  128. if (scrolling) {
  129. target_scroll = CLAMP(target_scroll + get_page(), get_min(), get_max() - get_page());
  130. } else {
  131. double change = get_page() != 0.0 ? get_page() : (get_max() - get_min()) / 16.0;
  132. target_scroll = CLAMP(get_value() + change, get_min(), get_max() - get_page());
  133. }
  134. if (smooth_scroll_enabled) {
  135. scrolling = true;
  136. set_process_internal(true);
  137. } else {
  138. scroll_to(target_scroll);
  139. }
  140. }
  141. } else {
  142. incr_active = false;
  143. decr_active = false;
  144. drag.active = false;
  145. queue_redraw();
  146. }
  147. }
  148. if (m.is_valid()) {
  149. accept_event();
  150. if (drag.active) {
  151. double ofs = orientation == VERTICAL ? m->get_position().y : m->get_position().x;
  152. Ref<Texture2D> decr = theme_cache.decrement_icon;
  153. double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width();
  154. ofs -= decr_size + theme_cache.scroll_style->get_margin(orientation == VERTICAL ? SIDE_TOP : SIDE_LEFT);
  155. double diff = (ofs - drag.pos_at_click) / get_area_size();
  156. double prev_scroll = get_value();
  157. set_as_ratio(drag.value_at_click + diff);
  158. if (!Math::is_equal_approx(prev_scroll, get_value())) {
  159. emit_signal(SNAME("scrolling"));
  160. }
  161. } else {
  162. double ofs = orientation == VERTICAL ? m->get_position().y : m->get_position().x;
  163. Ref<Texture2D> decr = theme_cache.decrement_icon;
  164. Ref<Texture2D> incr = theme_cache.increment_icon;
  165. double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width();
  166. double incr_size = orientation == VERTICAL ? incr->get_height() : incr->get_width();
  167. double total = orientation == VERTICAL ? get_size().height : get_size().width;
  168. HighlightStatus new_hilite;
  169. if (ofs < decr_size) {
  170. new_hilite = HIGHLIGHT_DECR;
  171. } else if (ofs > total - incr_size) {
  172. new_hilite = HIGHLIGHT_INCR;
  173. } else {
  174. new_hilite = HIGHLIGHT_RANGE;
  175. }
  176. if (new_hilite != highlight) {
  177. highlight = new_hilite;
  178. queue_redraw();
  179. }
  180. }
  181. }
  182. if (p_event->is_pressed()) {
  183. if (p_event->is_action("ui_left", true)) {
  184. if (orientation != HORIZONTAL) {
  185. return;
  186. }
  187. scroll(-(custom_step >= 0 ? custom_step : get_step()));
  188. } else if (p_event->is_action("ui_right", true)) {
  189. if (orientation != HORIZONTAL) {
  190. return;
  191. }
  192. scroll(custom_step >= 0 ? custom_step : get_step());
  193. } else if (p_event->is_action("ui_up", true)) {
  194. if (orientation != VERTICAL) {
  195. return;
  196. }
  197. scroll(-(custom_step >= 0 ? custom_step : get_step()));
  198. } else if (p_event->is_action("ui_down", true)) {
  199. if (orientation != VERTICAL) {
  200. return;
  201. }
  202. scroll(custom_step >= 0 ? custom_step : get_step());
  203. } else if (p_event->is_action("ui_home", true)) {
  204. scroll_to(get_min());
  205. } else if (p_event->is_action("ui_end", true)) {
  206. scroll_to(get_max());
  207. }
  208. }
  209. }
  210. void ScrollBar::_notification(int p_what) {
  211. switch (p_what) {
  212. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  213. RID ae = get_accessibility_element();
  214. ERR_FAIL_COND(ae.is_null());
  215. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_SCROLL_BAR);
  216. } break;
  217. case NOTIFICATION_DRAW: {
  218. RID ci = get_canvas_item();
  219. Ref<Texture2D> decr, incr;
  220. if (decr_active) {
  221. decr = theme_cache.decrement_pressed_icon;
  222. } else if (highlight == HIGHLIGHT_DECR) {
  223. decr = theme_cache.decrement_hl_icon;
  224. } else {
  225. decr = theme_cache.decrement_icon;
  226. }
  227. if (incr_active) {
  228. incr = theme_cache.increment_pressed_icon;
  229. } else if (highlight == HIGHLIGHT_INCR) {
  230. incr = theme_cache.increment_hl_icon;
  231. } else {
  232. incr = theme_cache.increment_icon;
  233. }
  234. Ref<StyleBox> grabber;
  235. if (drag.active) {
  236. grabber = theme_cache.grabber_pressed_style;
  237. } else if (highlight == HIGHLIGHT_RANGE) {
  238. grabber = theme_cache.grabber_hl_style;
  239. } else {
  240. grabber = theme_cache.grabber_style;
  241. }
  242. Point2 ofs;
  243. decr->draw(ci, Point2());
  244. if (orientation == HORIZONTAL) {
  245. ofs.x += decr->get_width();
  246. } else {
  247. ofs.y += decr->get_height();
  248. }
  249. Size2 area = get_size();
  250. if (orientation == HORIZONTAL) {
  251. area.width -= incr->get_width() + decr->get_width();
  252. } else {
  253. area.height -= incr->get_height() + decr->get_height();
  254. }
  255. if (has_focus(true)) {
  256. theme_cache.scroll_focus_style->draw(ci, Rect2(ofs, area));
  257. } else {
  258. theme_cache.scroll_style->draw(ci, Rect2(ofs, area));
  259. }
  260. if (orientation == HORIZONTAL) {
  261. ofs.width += area.width;
  262. } else {
  263. ofs.height += area.height;
  264. }
  265. incr->draw(ci, ofs);
  266. Rect2 grabber_rect;
  267. if (orientation == HORIZONTAL) {
  268. int padding_top = MAX(theme_cache.padding_top, 0);
  269. int padding_bottom = MAX(theme_cache.padding_bottom, 0);
  270. grabber_rect.size.width = get_grabber_size();
  271. grabber_rect.size.height = get_size().height - padding_top - padding_bottom;
  272. grabber_rect.position.y = padding_top;
  273. grabber_rect.position.x = get_grabber_offset() + decr->get_width() + theme_cache.scroll_style->get_margin(SIDE_LEFT);
  274. } else {
  275. int padding_left = MAX(theme_cache.padding_left, 0);
  276. int padding_right = MAX(theme_cache.padding_right, 0);
  277. grabber_rect.size.width = get_size().width - padding_left - padding_right;
  278. grabber_rect.size.height = get_grabber_size();
  279. grabber_rect.position.y = get_grabber_offset() + decr->get_height() + theme_cache.scroll_style->get_margin(SIDE_TOP);
  280. grabber_rect.position.x = padding_left;
  281. }
  282. grabber->draw(ci, grabber_rect);
  283. } break;
  284. case NOTIFICATION_ENTER_TREE: {
  285. if (has_node(drag_node_path)) {
  286. Node *n = get_node(drag_node_path);
  287. drag_node = Object::cast_to<Control>(n);
  288. }
  289. if (drag_node) {
  290. drag_node->connect(SceneStringName(gui_input), callable_mp(this, &ScrollBar::_drag_node_input));
  291. drag_node->connect(SceneStringName(tree_exiting), callable_mp(this, &ScrollBar::_drag_node_exit), CONNECT_ONE_SHOT);
  292. }
  293. } break;
  294. case NOTIFICATION_EXIT_TREE: {
  295. if (drag_node) {
  296. drag_node->disconnect(SceneStringName(gui_input), callable_mp(this, &ScrollBar::_drag_node_input));
  297. drag_node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &ScrollBar::_drag_node_exit));
  298. }
  299. drag_node = nullptr;
  300. } break;
  301. case NOTIFICATION_INTERNAL_PROCESS: {
  302. if (scrolling) {
  303. if (get_value() != target_scroll) {
  304. double target = target_scroll - get_value();
  305. double dist = std::abs(target);
  306. double vel = ((target / dist) * 500) * get_process_delta_time();
  307. if (Math::abs(vel) >= dist) {
  308. scroll_to(target_scroll);
  309. scrolling = false;
  310. set_process_internal(false);
  311. } else {
  312. scroll(vel);
  313. }
  314. } else {
  315. scrolling = false;
  316. set_process_internal(false);
  317. }
  318. } else if (drag_node_touching) {
  319. if (drag_node_touching_deaccel) {
  320. Vector2 pos = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0);
  321. pos += drag_node_speed * get_process_delta_time();
  322. bool turnoff = false;
  323. if (orientation == HORIZONTAL) {
  324. if (pos.x < 0) {
  325. pos.x = 0;
  326. turnoff = true;
  327. }
  328. if (pos.x > (get_max() - get_page())) {
  329. pos.x = get_max() - get_page();
  330. turnoff = true;
  331. }
  332. scroll_to(pos.x);
  333. float sgn_x = drag_node_speed.x < 0 ? -1 : 1;
  334. float val_x = Math::abs(drag_node_speed.x);
  335. val_x -= 1000 * get_process_delta_time();
  336. if (val_x < 0) {
  337. turnoff = true;
  338. }
  339. drag_node_speed.x = sgn_x * val_x;
  340. } else {
  341. if (pos.y < 0) {
  342. pos.y = 0;
  343. turnoff = true;
  344. }
  345. if (pos.y > (get_max() - get_page())) {
  346. pos.y = get_max() - get_page();
  347. turnoff = true;
  348. }
  349. scroll_to(pos.y);
  350. float sgn_y = drag_node_speed.y < 0 ? -1 : 1;
  351. float val_y = Math::abs(drag_node_speed.y);
  352. val_y -= 1000 * get_process_delta_time();
  353. if (val_y < 0) {
  354. turnoff = true;
  355. }
  356. drag_node_speed.y = sgn_y * val_y;
  357. }
  358. if (turnoff) {
  359. set_process_internal(false);
  360. drag_node_touching = false;
  361. drag_node_touching_deaccel = false;
  362. }
  363. } else {
  364. if (time_since_motion == 0 || time_since_motion > 0.1) {
  365. Vector2 diff = drag_node_accum - last_drag_node_accum;
  366. last_drag_node_accum = drag_node_accum;
  367. drag_node_speed = diff / get_process_delta_time();
  368. }
  369. time_since_motion += get_process_delta_time();
  370. }
  371. }
  372. } break;
  373. case NOTIFICATION_VISIBILITY_CHANGED: {
  374. if (!is_visible()) {
  375. incr_active = false;
  376. decr_active = false;
  377. drag.active = false;
  378. }
  379. } break;
  380. case NOTIFICATION_MOUSE_EXIT: {
  381. highlight = HIGHLIGHT_NONE;
  382. queue_redraw();
  383. } break;
  384. }
  385. }
  386. double ScrollBar::get_grabber_min_size() const {
  387. Ref<StyleBox> grabber = theme_cache.grabber_style;
  388. Size2 gminsize = grabber->get_minimum_size();
  389. return (orientation == VERTICAL) ? gminsize.height : gminsize.width;
  390. }
  391. double ScrollBar::get_grabber_size() const {
  392. float range = get_max() - get_min();
  393. if (range <= 0) {
  394. return 0;
  395. }
  396. float page = (get_page() > 0) ? get_page() : 0;
  397. double area_size = get_area_size();
  398. double grabber_size = page / range * area_size;
  399. return grabber_size + get_grabber_min_size();
  400. }
  401. double ScrollBar::get_area_size() const {
  402. switch (orientation) {
  403. case VERTICAL: {
  404. double area = get_size().height;
  405. area -= theme_cache.scroll_style->get_minimum_size().height;
  406. area -= theme_cache.increment_icon->get_height();
  407. area -= theme_cache.decrement_icon->get_height();
  408. area -= get_grabber_min_size();
  409. return area;
  410. } break;
  411. case HORIZONTAL: {
  412. double area = get_size().width;
  413. area -= theme_cache.scroll_style->get_minimum_size().width;
  414. area -= theme_cache.increment_icon->get_width();
  415. area -= theme_cache.decrement_icon->get_width();
  416. area -= get_grabber_min_size();
  417. return area;
  418. } break;
  419. default: {
  420. return 0.0;
  421. }
  422. }
  423. }
  424. double ScrollBar::get_grabber_offset() const {
  425. return get_area_size() * get_as_ratio();
  426. }
  427. Size2 ScrollBar::get_minimum_size() const {
  428. Ref<Texture2D> incr = theme_cache.increment_icon;
  429. Ref<Texture2D> decr = theme_cache.decrement_icon;
  430. Ref<StyleBox> bg = theme_cache.scroll_style;
  431. Size2 minsize;
  432. if (orientation == VERTICAL) {
  433. int padding_left = MAX(theme_cache.padding_left, 0);
  434. int padding_right = MAX(theme_cache.padding_right, 0);
  435. minsize.width = MAX(incr->get_size().width, bg->get_minimum_size().width);
  436. minsize.width += padding_left + padding_right;
  437. minsize.height += incr->get_size().height;
  438. minsize.height += decr->get_size().height;
  439. minsize.height += bg->get_minimum_size().height;
  440. minsize.height += get_grabber_min_size();
  441. }
  442. if (orientation == HORIZONTAL) {
  443. int padding_top = MAX(theme_cache.padding_top, 0);
  444. int padding_bottom = MAX(theme_cache.padding_bottom, 0);
  445. minsize.height = MAX(incr->get_size().height, bg->get_minimum_size().height);
  446. minsize.height += padding_top + padding_bottom;
  447. minsize.width += incr->get_size().width;
  448. minsize.width += decr->get_size().width;
  449. minsize.width += bg->get_minimum_size().width;
  450. minsize.width += get_grabber_min_size();
  451. }
  452. return minsize;
  453. }
  454. void ScrollBar::scroll(double p_amount) {
  455. scroll_to(get_value() + p_amount);
  456. }
  457. void ScrollBar::scroll_to(double p_position) {
  458. double prev_scroll = get_value();
  459. set_value(p_position);
  460. if (!Math::is_equal_approx(prev_scroll, get_value())) {
  461. emit_signal(SNAME("scrolling"));
  462. }
  463. }
  464. void ScrollBar::set_custom_step(float p_custom_step) {
  465. custom_step = p_custom_step;
  466. }
  467. float ScrollBar::get_custom_step() const {
  468. return custom_step;
  469. }
  470. void ScrollBar::_drag_node_exit() {
  471. if (drag_node) {
  472. drag_node->disconnect(SceneStringName(gui_input), callable_mp(this, &ScrollBar::_drag_node_input));
  473. }
  474. drag_node = nullptr;
  475. }
  476. void ScrollBar::_drag_node_input(const Ref<InputEvent> &p_input) {
  477. if (!drag_node_enabled) {
  478. return;
  479. }
  480. Ref<InputEventMouseButton> mb = p_input;
  481. if (mb.is_valid()) {
  482. if (mb->get_button_index() != MouseButton::LEFT) {
  483. return;
  484. }
  485. if (mb->is_pressed()) {
  486. drag_node_speed = Vector2();
  487. drag_node_accum = Vector2();
  488. last_drag_node_accum = Vector2();
  489. drag_node_from = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0);
  490. drag_node_touching = DisplayServer::get_singleton()->is_touchscreen_available();
  491. drag_node_touching_deaccel = false;
  492. time_since_motion = 0;
  493. if (drag_node_touching) {
  494. set_process_internal(true);
  495. time_since_motion = 0;
  496. }
  497. } else {
  498. if (drag_node_touching) {
  499. if (drag_node_speed == Vector2()) {
  500. drag_node_touching_deaccel = false;
  501. drag_node_touching = false;
  502. set_process_internal(false);
  503. } else {
  504. drag_node_touching_deaccel = true;
  505. }
  506. }
  507. }
  508. }
  509. Ref<InputEventMouseMotion> mm = p_input;
  510. if (mm.is_valid()) {
  511. if (drag_node_touching && !drag_node_touching_deaccel) {
  512. Vector2 motion = mm->get_relative();
  513. drag_node_accum -= motion;
  514. Vector2 diff = drag_node_from + drag_node_accum;
  515. if (orientation == HORIZONTAL) {
  516. scroll_to(diff.x);
  517. }
  518. if (orientation == VERTICAL) {
  519. scroll_to(diff.y);
  520. }
  521. time_since_motion = 0;
  522. }
  523. }
  524. }
  525. void ScrollBar::set_drag_node(const NodePath &p_path) {
  526. if (is_inside_tree()) {
  527. if (drag_node) {
  528. drag_node->disconnect(SceneStringName(gui_input), callable_mp(this, &ScrollBar::_drag_node_input));
  529. drag_node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &ScrollBar::_drag_node_exit));
  530. }
  531. }
  532. drag_node = nullptr;
  533. drag_node_path = p_path;
  534. if (is_inside_tree()) {
  535. if (has_node(p_path)) {
  536. Node *n = get_node(p_path);
  537. drag_node = Object::cast_to<Control>(n);
  538. }
  539. if (drag_node) {
  540. drag_node->connect(SceneStringName(gui_input), callable_mp(this, &ScrollBar::_drag_node_input));
  541. drag_node->connect(SceneStringName(tree_exiting), callable_mp(this, &ScrollBar::_drag_node_exit), CONNECT_ONE_SHOT);
  542. }
  543. }
  544. }
  545. NodePath ScrollBar::get_drag_node() const {
  546. return drag_node_path;
  547. }
  548. void ScrollBar::set_drag_node_enabled(bool p_enable) {
  549. drag_node_enabled = p_enable;
  550. }
  551. void ScrollBar::set_smooth_scroll_enabled(bool p_enable) {
  552. smooth_scroll_enabled = p_enable;
  553. }
  554. bool ScrollBar::is_smooth_scroll_enabled() const {
  555. return smooth_scroll_enabled;
  556. }
  557. void ScrollBar::_bind_methods() {
  558. ClassDB::bind_method(D_METHOD("set_custom_step", "step"), &ScrollBar::set_custom_step);
  559. ClassDB::bind_method(D_METHOD("get_custom_step"), &ScrollBar::get_custom_step);
  560. ADD_SIGNAL(MethodInfo("scrolling"));
  561. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_step", PROPERTY_HINT_RANGE, "-1,4096,suffix:px"), "set_custom_step", "get_custom_step");
  562. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollBar, scroll_style, "scroll");
  563. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollBar, scroll_focus_style, "scroll_focus");
  564. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollBar, grabber_style, "grabber");
  565. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollBar, grabber_hl_style, "grabber_highlight");
  566. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ScrollBar, grabber_pressed_style, "grabber_pressed");
  567. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ScrollBar, increment_icon, "increment");
  568. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ScrollBar, increment_hl_icon, "increment_highlight");
  569. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ScrollBar, increment_pressed_icon, "increment_pressed");
  570. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ScrollBar, decrement_icon, "decrement");
  571. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ScrollBar, decrement_hl_icon, "decrement_highlight");
  572. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, ScrollBar, decrement_pressed_icon, "decrement_pressed");
  573. }
  574. ScrollBar::ScrollBar(Orientation p_orientation) {
  575. orientation = p_orientation;
  576. if (focus_by_default) {
  577. set_focus_mode(FOCUS_ALL);
  578. } else {
  579. set_focus_mode(FOCUS_ACCESSIBILITY);
  580. }
  581. set_step(0);
  582. }
  583. ScrollBar::~ScrollBar() {
  584. }
  585. void VScrollBar::_bind_methods() {
  586. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, VScrollBar, padding_left);
  587. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, VScrollBar, padding_right);
  588. }
  589. void HScrollBar::_bind_methods() {
  590. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, HScrollBar, padding_top);
  591. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, HScrollBar, padding_bottom);
  592. }