scroll_bar.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*************************************************************************/
  2. /* scroll_bar.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 "core/os/keyboard.h"
  32. #include "core/os/os.h"
  33. #include "core/print_string.h"
  34. bool ScrollBar::focus_by_default = false;
  35. void ScrollBar::set_can_focus_by_default(bool p_can_focus) {
  36. focus_by_default = p_can_focus;
  37. }
  38. void ScrollBar::_gui_input(Ref<InputEvent> p_event) {
  39. Ref<InputEventMouseMotion> m = p_event;
  40. if (!m.is_valid() || drag.active) {
  41. emit_signal("scrolling");
  42. }
  43. Ref<InputEventMouseButton> b = p_event;
  44. if (b.is_valid()) {
  45. accept_event();
  46. if (b->get_button_index() == 5 && b->is_pressed()) {
  47. /*
  48. if (orientation==VERTICAL)
  49. set_val( get_val() + get_page() / 4.0 );
  50. else
  51. */
  52. set_value(get_value() + get_page() / 4.0);
  53. accept_event();
  54. }
  55. if (b->get_button_index() == 4 && b->is_pressed()) {
  56. /*
  57. if (orientation==HORIZONTAL)
  58. set_val( get_val() - get_page() / 4.0 );
  59. else
  60. */
  61. set_value(get_value() - get_page() / 4.0);
  62. accept_event();
  63. }
  64. if (b->get_button_index() != 1)
  65. return;
  66. if (b->is_pressed()) {
  67. double ofs = orientation == VERTICAL ? b->get_position().y : b->get_position().x;
  68. Ref<Texture> decr = get_icon("decrement");
  69. Ref<Texture> incr = get_icon("increment");
  70. double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width();
  71. double incr_size = orientation == VERTICAL ? incr->get_height() : incr->get_width();
  72. double grabber_ofs = get_grabber_offset();
  73. double grabber_size = get_grabber_size();
  74. double total = orientation == VERTICAL ? get_size().height : get_size().width;
  75. if (ofs < decr_size) {
  76. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  77. return;
  78. }
  79. if (ofs > total - incr_size) {
  80. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  81. return;
  82. }
  83. ofs -= decr_size;
  84. if (ofs < grabber_ofs) {
  85. if (scrolling) {
  86. target_scroll = CLAMP(target_scroll - get_page(), get_min(), get_max() - get_page());
  87. } else {
  88. target_scroll = CLAMP(get_value() - get_page(), get_min(), get_max() - get_page());
  89. }
  90. if (smooth_scroll_enabled) {
  91. scrolling = true;
  92. set_physics_process_internal(true);
  93. } else {
  94. set_value(target_scroll);
  95. }
  96. return;
  97. }
  98. ofs -= grabber_ofs;
  99. if (ofs < grabber_size) {
  100. drag.active = true;
  101. drag.pos_at_click = grabber_ofs + ofs;
  102. drag.value_at_click = get_as_ratio();
  103. update();
  104. } else {
  105. if (scrolling) {
  106. target_scroll = CLAMP(target_scroll + get_page(), get_min(), get_max() - get_page());
  107. } else {
  108. target_scroll = CLAMP(get_value() + get_page(), get_min(), get_max() - get_page());
  109. }
  110. if (smooth_scroll_enabled) {
  111. scrolling = true;
  112. set_physics_process_internal(true);
  113. } else {
  114. set_value(target_scroll);
  115. }
  116. }
  117. } else {
  118. drag.active = false;
  119. update();
  120. }
  121. }
  122. if (m.is_valid()) {
  123. accept_event();
  124. if (drag.active) {
  125. double ofs = orientation == VERTICAL ? m->get_position().y : m->get_position().x;
  126. Ref<Texture> decr = get_icon("decrement");
  127. double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width();
  128. ofs -= decr_size;
  129. double diff = (ofs - drag.pos_at_click) / get_area_size();
  130. set_as_ratio(drag.value_at_click + diff);
  131. } else {
  132. double ofs = orientation == VERTICAL ? m->get_position().y : m->get_position().x;
  133. Ref<Texture> decr = get_icon("decrement");
  134. Ref<Texture> incr = get_icon("increment");
  135. double decr_size = orientation == VERTICAL ? decr->get_height() : decr->get_width();
  136. double incr_size = orientation == VERTICAL ? incr->get_height() : incr->get_width();
  137. double total = orientation == VERTICAL ? get_size().height : get_size().width;
  138. HighlightStatus new_hilite;
  139. if (ofs < decr_size) {
  140. new_hilite = HIGHLIGHT_DECR;
  141. } else if (ofs > total - incr_size) {
  142. new_hilite = HIGHLIGHT_INCR;
  143. } else {
  144. new_hilite = HIGHLIGHT_RANGE;
  145. }
  146. if (new_hilite != highlight) {
  147. highlight = new_hilite;
  148. update();
  149. }
  150. }
  151. }
  152. if (p_event->is_pressed()) {
  153. if (p_event->is_action("ui_left")) {
  154. if (orientation != HORIZONTAL)
  155. return;
  156. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  157. } else if (p_event->is_action("ui_right")) {
  158. if (orientation != HORIZONTAL)
  159. return;
  160. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  161. } else if (p_event->is_action("ui_up")) {
  162. if (orientation != VERTICAL)
  163. return;
  164. set_value(get_value() - (custom_step >= 0 ? custom_step : get_step()));
  165. } else if (p_event->is_action("ui_down")) {
  166. if (orientation != VERTICAL)
  167. return;
  168. set_value(get_value() + (custom_step >= 0 ? custom_step : get_step()));
  169. } else if (p_event->is_action("ui_home")) {
  170. set_value(get_min());
  171. } else if (p_event->is_action("ui_end")) {
  172. set_value(get_max());
  173. }
  174. }
  175. }
  176. void ScrollBar::_notification(int p_what) {
  177. if (p_what == NOTIFICATION_DRAW) {
  178. RID ci = get_canvas_item();
  179. Ref<Texture> decr = highlight == HIGHLIGHT_DECR ? get_icon("decrement_highlight") : get_icon("decrement");
  180. Ref<Texture> incr = highlight == HIGHLIGHT_INCR ? get_icon("increment_highlight") : get_icon("increment");
  181. Ref<StyleBox> bg = has_focus() ? get_stylebox("scroll_focus") : get_stylebox("scroll");
  182. Ref<StyleBox> grabber;
  183. if (drag.active)
  184. grabber = get_stylebox("grabber_pressed");
  185. else if (highlight == HIGHLIGHT_RANGE)
  186. grabber = get_stylebox("grabber_highlight");
  187. else
  188. grabber = get_stylebox("grabber");
  189. Point2 ofs;
  190. decr->draw(ci, Point2());
  191. if (orientation == HORIZONTAL)
  192. ofs.x += decr->get_width();
  193. else
  194. ofs.y += decr->get_height();
  195. Size2 area = get_size();
  196. if (orientation == HORIZONTAL)
  197. area.width -= incr->get_width() + decr->get_width();
  198. else
  199. area.height -= incr->get_height() + decr->get_height();
  200. bg->draw(ci, Rect2(ofs, area));
  201. if (orientation == HORIZONTAL)
  202. ofs.width += area.width;
  203. else
  204. ofs.height += area.height;
  205. incr->draw(ci, ofs);
  206. Rect2 grabber_rect;
  207. if (orientation == HORIZONTAL) {
  208. grabber_rect.size.width = get_grabber_size();
  209. grabber_rect.size.height = get_size().height;
  210. grabber_rect.position.y = 0;
  211. grabber_rect.position.x = get_grabber_offset() + decr->get_width() + bg->get_margin(MARGIN_LEFT);
  212. } else {
  213. grabber_rect.size.width = get_size().width;
  214. grabber_rect.size.height = get_grabber_size();
  215. grabber_rect.position.y = get_grabber_offset() + decr->get_height() + bg->get_margin(MARGIN_TOP);
  216. grabber_rect.position.x = 0;
  217. }
  218. grabber->draw(ci, grabber_rect);
  219. }
  220. if (p_what == NOTIFICATION_ENTER_TREE) {
  221. if (has_node(drag_node_path)) {
  222. Node *n = get_node(drag_node_path);
  223. drag_node = Object::cast_to<Control>(n);
  224. }
  225. if (drag_node) {
  226. drag_node->connect("gui_input", this, "_drag_node_input");
  227. drag_node->connect("tree_exiting", this, "_drag_node_exit", varray(), CONNECT_ONESHOT);
  228. }
  229. }
  230. if (p_what == NOTIFICATION_EXIT_TREE) {
  231. if (drag_node) {
  232. drag_node->disconnect("gui_input", this, "_drag_node_input");
  233. drag_node->disconnect("tree_exiting", this, "_drag_node_exit");
  234. }
  235. drag_node = NULL;
  236. }
  237. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  238. if (scrolling) {
  239. if (get_value() != target_scroll) {
  240. double target = target_scroll - get_value();
  241. double dist = sqrt(target * target);
  242. double vel = ((target / dist) * 500) * get_physics_process_delta_time();
  243. if (Math::abs(vel) >= dist) {
  244. set_value(target_scroll);
  245. scrolling = false;
  246. set_physics_process_internal(false);
  247. } else {
  248. set_value(get_value() + vel);
  249. }
  250. } else {
  251. scrolling = false;
  252. set_physics_process_internal(false);
  253. }
  254. } else if (drag_node_touching) {
  255. if (drag_node_touching_deaccel) {
  256. Vector2 pos = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0);
  257. pos += drag_node_speed * get_physics_process_delta_time();
  258. bool turnoff = false;
  259. if (orientation == HORIZONTAL) {
  260. if (pos.x < 0) {
  261. pos.x = 0;
  262. turnoff = true;
  263. }
  264. if (pos.x > (get_max() - get_page())) {
  265. pos.x = get_max() - get_page();
  266. turnoff = true;
  267. }
  268. set_value(pos.x);
  269. float sgn_x = drag_node_speed.x < 0 ? -1 : 1;
  270. float val_x = Math::abs(drag_node_speed.x);
  271. val_x -= 1000 * get_physics_process_delta_time();
  272. if (val_x < 0) {
  273. turnoff = true;
  274. }
  275. drag_node_speed.x = sgn_x * val_x;
  276. } else {
  277. if (pos.y < 0) {
  278. pos.y = 0;
  279. turnoff = true;
  280. }
  281. if (pos.y > (get_max() - get_page())) {
  282. pos.y = get_max() - get_page();
  283. turnoff = true;
  284. }
  285. set_value(pos.y);
  286. float sgn_y = drag_node_speed.y < 0 ? -1 : 1;
  287. float val_y = Math::abs(drag_node_speed.y);
  288. val_y -= 1000 * get_physics_process_delta_time();
  289. if (val_y < 0) {
  290. turnoff = true;
  291. }
  292. drag_node_speed.y = sgn_y * val_y;
  293. }
  294. if (turnoff) {
  295. set_physics_process_internal(false);
  296. drag_node_touching = false;
  297. drag_node_touching_deaccel = false;
  298. }
  299. } else {
  300. if (time_since_motion == 0 || time_since_motion > 0.1) {
  301. Vector2 diff = drag_node_accum - last_drag_node_accum;
  302. last_drag_node_accum = drag_node_accum;
  303. drag_node_speed = diff / get_physics_process_delta_time();
  304. }
  305. time_since_motion += get_physics_process_delta_time();
  306. }
  307. }
  308. }
  309. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  310. highlight = HIGHLIGHT_NONE;
  311. update();
  312. }
  313. }
  314. double ScrollBar::get_grabber_min_size() const {
  315. Ref<StyleBox> grabber = get_stylebox("grabber");
  316. Size2 gminsize = grabber->get_minimum_size() + grabber->get_center_size();
  317. return (orientation == VERTICAL) ? gminsize.height : gminsize.width;
  318. }
  319. double ScrollBar::get_grabber_size() const {
  320. float range = get_max() - get_min();
  321. if (range <= 0)
  322. return 0;
  323. float page = (get_page() > 0) ? get_page() : 0;
  324. /*
  325. if (grabber_range < get_step())
  326. grabber_range=get_step();
  327. */
  328. double area_size = get_area_size();
  329. double grabber_size = page / range * area_size;
  330. return grabber_size + get_grabber_min_size();
  331. }
  332. double ScrollBar::get_area_size() const {
  333. if (orientation == VERTICAL) {
  334. double area = get_size().height;
  335. area -= get_stylebox("scroll")->get_minimum_size().height;
  336. area -= get_icon("increment")->get_height();
  337. area -= get_icon("decrement")->get_height();
  338. area -= get_grabber_min_size();
  339. return area;
  340. } else if (orientation == HORIZONTAL) {
  341. double area = get_size().width;
  342. area -= get_stylebox("scroll")->get_minimum_size().width;
  343. area -= get_icon("increment")->get_width();
  344. area -= get_icon("decrement")->get_width();
  345. area -= get_grabber_min_size();
  346. return area;
  347. } else {
  348. return 0;
  349. }
  350. }
  351. double ScrollBar::get_area_offset() const {
  352. double ofs = 0;
  353. if (orientation == VERTICAL) {
  354. ofs += get_stylebox("hscroll")->get_margin(MARGIN_TOP);
  355. ofs += get_icon("decrement")->get_height();
  356. }
  357. if (orientation == HORIZONTAL) {
  358. ofs += get_stylebox("hscroll")->get_margin(MARGIN_LEFT);
  359. ofs += get_icon("decrement")->get_width();
  360. }
  361. return ofs;
  362. }
  363. double ScrollBar::get_click_pos(const Point2 &p_pos) const {
  364. float pos = (orientation == VERTICAL) ? p_pos.y : p_pos.x;
  365. pos -= get_area_offset();
  366. float area = get_area_size();
  367. if (area == 0)
  368. return 0;
  369. else
  370. return pos / area;
  371. }
  372. double ScrollBar::get_grabber_offset() const {
  373. return (get_area_size()) * get_as_ratio();
  374. }
  375. Size2 ScrollBar::get_minimum_size() const {
  376. Ref<Texture> incr = get_icon("increment");
  377. Ref<Texture> decr = get_icon("decrement");
  378. Ref<StyleBox> bg = get_stylebox("scroll");
  379. Size2 minsize;
  380. if (orientation == VERTICAL) {
  381. minsize.width = MAX(incr->get_size().width, (bg->get_minimum_size() + bg->get_center_size()).width);
  382. minsize.height += incr->get_size().height;
  383. minsize.height += decr->get_size().height;
  384. minsize.height += bg->get_minimum_size().height;
  385. minsize.height += get_grabber_min_size();
  386. }
  387. if (orientation == HORIZONTAL) {
  388. minsize.height = MAX(incr->get_size().height, (bg->get_center_size() + bg->get_minimum_size()).height);
  389. minsize.width += incr->get_size().width;
  390. minsize.width += decr->get_size().width;
  391. minsize.width += bg->get_minimum_size().width;
  392. minsize.width += get_grabber_min_size();
  393. }
  394. return minsize;
  395. }
  396. void ScrollBar::set_custom_step(float p_custom_step) {
  397. custom_step = p_custom_step;
  398. }
  399. float ScrollBar::get_custom_step() const {
  400. return custom_step;
  401. }
  402. void ScrollBar::_drag_node_exit() {
  403. if (drag_node) {
  404. drag_node->disconnect("gui_input", this, "_drag_node_input");
  405. }
  406. drag_node = NULL;
  407. }
  408. void ScrollBar::_drag_node_input(const Ref<InputEvent> &p_input) {
  409. Ref<InputEventMouseButton> mb = p_input;
  410. if (mb.is_valid()) {
  411. if (mb->get_button_index() != 1)
  412. return;
  413. if (mb->is_pressed()) {
  414. drag_node_speed = Vector2();
  415. drag_node_accum = Vector2();
  416. last_drag_node_accum = Vector2();
  417. drag_node_from = Vector2(orientation == HORIZONTAL ? get_value() : 0, orientation == VERTICAL ? get_value() : 0);
  418. drag_node_touching = OS::get_singleton()->has_touchscreen_ui_hint();
  419. drag_node_touching_deaccel = false;
  420. time_since_motion = 0;
  421. if (drag_node_touching) {
  422. set_physics_process_internal(true);
  423. time_since_motion = 0;
  424. }
  425. } else {
  426. if (drag_node_touching) {
  427. if (drag_node_speed == Vector2()) {
  428. drag_node_touching_deaccel = false;
  429. drag_node_touching = false;
  430. set_physics_process_internal(false);
  431. } else {
  432. drag_node_touching_deaccel = true;
  433. }
  434. }
  435. }
  436. }
  437. Ref<InputEventMouseMotion> mm = p_input;
  438. if (mm.is_valid()) {
  439. if (drag_node_touching && !drag_node_touching_deaccel) {
  440. Vector2 motion = Vector2(mm->get_relative().x, mm->get_relative().y);
  441. drag_node_accum -= motion;
  442. Vector2 diff = drag_node_from + drag_node_accum;
  443. if (orientation == HORIZONTAL)
  444. set_value(diff.x);
  445. if (orientation == VERTICAL)
  446. set_value(diff.y);
  447. time_since_motion = 0;
  448. }
  449. }
  450. }
  451. void ScrollBar::set_drag_node(const NodePath &p_path) {
  452. if (is_inside_tree()) {
  453. if (drag_node) {
  454. drag_node->disconnect("gui_input", this, "_drag_node_input");
  455. drag_node->disconnect("tree_exiting", this, "_drag_node_exit");
  456. }
  457. }
  458. drag_node = NULL;
  459. drag_node_path = p_path;
  460. if (is_inside_tree()) {
  461. if (has_node(p_path)) {
  462. Node *n = get_node(p_path);
  463. drag_node = Object::cast_to<Control>(n);
  464. }
  465. if (drag_node) {
  466. drag_node->connect("gui_input", this, "_drag_node_input");
  467. drag_node->connect("tree_exiting", this, "_drag_node_exit", varray(), CONNECT_ONESHOT);
  468. }
  469. }
  470. }
  471. NodePath ScrollBar::get_drag_node() const {
  472. return drag_node_path;
  473. }
  474. void ScrollBar::set_smooth_scroll_enabled(bool p_enable) {
  475. smooth_scroll_enabled = p_enable;
  476. }
  477. bool ScrollBar::is_smooth_scroll_enabled() const {
  478. return smooth_scroll_enabled;
  479. }
  480. void ScrollBar::_bind_methods() {
  481. ClassDB::bind_method(D_METHOD("_gui_input"), &ScrollBar::_gui_input);
  482. ClassDB::bind_method(D_METHOD("set_custom_step", "step"), &ScrollBar::set_custom_step);
  483. ClassDB::bind_method(D_METHOD("get_custom_step"), &ScrollBar::get_custom_step);
  484. ClassDB::bind_method(D_METHOD("_drag_node_input"), &ScrollBar::_drag_node_input);
  485. ClassDB::bind_method(D_METHOD("_drag_node_exit"), &ScrollBar::_drag_node_exit);
  486. ADD_SIGNAL(MethodInfo("scrolling"));
  487. ADD_PROPERTY(PropertyInfo(Variant::REAL, "custom_step", PROPERTY_HINT_RANGE, "-1,4096"), "set_custom_step", "get_custom_step");
  488. }
  489. ScrollBar::ScrollBar(Orientation p_orientation) {
  490. orientation = p_orientation;
  491. highlight = HIGHLIGHT_NONE;
  492. custom_step = -1;
  493. drag_node = NULL;
  494. drag.active = false;
  495. drag_node_speed = Vector2();
  496. drag_node_touching = false;
  497. drag_node_touching_deaccel = false;
  498. scrolling = false;
  499. target_scroll = 0;
  500. smooth_scroll_enabled = false;
  501. if (focus_by_default)
  502. set_focus_mode(FOCUS_ALL);
  503. set_step(0);
  504. }
  505. ScrollBar::~ScrollBar() {
  506. }