scroll_container.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*************************************************************************/
  2. /* scroll_container.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_container.h"
  31. #include "core/os/os.h"
  32. bool ScrollContainer::clips_input() const {
  33. return true;
  34. }
  35. Size2 ScrollContainer::get_minimum_size() const {
  36. Ref<StyleBox> sb = get_stylebox("bg");
  37. Size2 min_size;
  38. for (int i = 0; i < get_child_count(); i++) {
  39. Control *c = Object::cast_to<Control>(get_child(i));
  40. if (!c)
  41. continue;
  42. if (c->is_set_as_toplevel())
  43. continue;
  44. if (c == h_scroll || c == v_scroll)
  45. continue;
  46. Size2 minsize = c->get_combined_minimum_size();
  47. if (!scroll_h) {
  48. min_size.x = MAX(min_size.x, minsize.x);
  49. }
  50. if (!scroll_v) {
  51. min_size.y = MAX(min_size.y, minsize.y);
  52. }
  53. }
  54. if (h_scroll->is_visible_in_tree()) {
  55. min_size.y += h_scroll->get_minimum_size().y;
  56. }
  57. if (v_scroll->is_visible_in_tree()) {
  58. min_size.x += v_scroll->get_minimum_size().x;
  59. }
  60. min_size += sb->get_minimum_size();
  61. return min_size;
  62. }
  63. void ScrollContainer::_cancel_drag() {
  64. set_physics_process_internal(false);
  65. drag_touching_deaccel = false;
  66. drag_touching = false;
  67. drag_speed = Vector2();
  68. drag_accum = Vector2();
  69. last_drag_accum = Vector2();
  70. drag_from = Vector2();
  71. if (beyond_deadzone) {
  72. emit_signal("scroll_ended");
  73. propagate_notification(NOTIFICATION_SCROLL_END);
  74. beyond_deadzone = false;
  75. }
  76. }
  77. void ScrollContainer::_gui_input(const Ref<InputEvent> &p_gui_input) {
  78. Ref<InputEventMouseButton> mb = p_gui_input;
  79. if (mb.is_valid()) {
  80. if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) {
  81. // only horizontal is enabled, scroll horizontally
  82. if (h_scroll->is_visible() && !v_scroll->is_visible()) {
  83. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 8 * mb->get_factor());
  84. } else if (v_scroll->is_visible_in_tree()) {
  85. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 8 * mb->get_factor());
  86. }
  87. }
  88. if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) {
  89. // only horizontal is enabled, scroll horizontally
  90. if (h_scroll->is_visible() && !v_scroll->is_visible()) {
  91. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 8 * mb->get_factor());
  92. } else if (v_scroll->is_visible()) {
  93. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 8 * mb->get_factor());
  94. }
  95. }
  96. if (mb->get_button_index() == BUTTON_WHEEL_LEFT && mb->is_pressed()) {
  97. if (h_scroll->is_visible_in_tree()) {
  98. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * mb->get_factor() / 8);
  99. }
  100. }
  101. if (mb->get_button_index() == BUTTON_WHEEL_RIGHT && mb->is_pressed()) {
  102. if (h_scroll->is_visible_in_tree()) {
  103. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * mb->get_factor() / 8);
  104. }
  105. }
  106. if (!OS::get_singleton()->has_touchscreen_ui_hint())
  107. return;
  108. if (mb->get_button_index() != BUTTON_LEFT)
  109. return;
  110. if (mb->is_pressed()) {
  111. if (drag_touching) {
  112. _cancel_drag();
  113. }
  114. if (true) {
  115. drag_speed = Vector2();
  116. drag_accum = Vector2();
  117. last_drag_accum = Vector2();
  118. drag_from = Vector2(h_scroll->get_value(), v_scroll->get_value());
  119. drag_touching = OS::get_singleton()->has_touchscreen_ui_hint();
  120. drag_touching_deaccel = false;
  121. beyond_deadzone = false;
  122. time_since_motion = 0;
  123. if (drag_touching) {
  124. set_physics_process_internal(true);
  125. time_since_motion = 0;
  126. }
  127. }
  128. } else {
  129. if (drag_touching) {
  130. if (drag_speed == Vector2()) {
  131. _cancel_drag();
  132. } else {
  133. drag_touching_deaccel = true;
  134. }
  135. }
  136. }
  137. }
  138. Ref<InputEventMouseMotion> mm = p_gui_input;
  139. if (mm.is_valid()) {
  140. if (drag_touching && !drag_touching_deaccel) {
  141. Vector2 motion = Vector2(mm->get_relative().x, mm->get_relative().y);
  142. drag_accum -= motion;
  143. if (beyond_deadzone || (scroll_h && Math::abs(drag_accum.x) > deadzone) || (scroll_v && Math::abs(drag_accum.y) > deadzone)) {
  144. if (!beyond_deadzone) {
  145. propagate_notification(NOTIFICATION_SCROLL_BEGIN);
  146. emit_signal("scroll_started");
  147. beyond_deadzone = true;
  148. // resetting drag_accum here ensures smooth scrolling after reaching deadzone
  149. drag_accum = -motion;
  150. }
  151. Vector2 diff = drag_from + drag_accum;
  152. if (scroll_h)
  153. h_scroll->set_value(diff.x);
  154. else
  155. drag_accum.x = 0;
  156. if (scroll_v)
  157. v_scroll->set_value(diff.y);
  158. else
  159. drag_accum.y = 0;
  160. time_since_motion = 0;
  161. }
  162. }
  163. }
  164. Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  165. if (pan_gesture.is_valid()) {
  166. if (h_scroll->is_visible_in_tree()) {
  167. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  168. }
  169. if (v_scroll->is_visible_in_tree()) {
  170. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  171. }
  172. }
  173. }
  174. void ScrollContainer::_update_scrollbar_position() {
  175. Size2 hmin = h_scroll->get_combined_minimum_size();
  176. Size2 vmin = v_scroll->get_combined_minimum_size();
  177. v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width);
  178. v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  179. v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  180. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  181. h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0);
  182. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  183. h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height);
  184. h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  185. h_scroll->raise();
  186. v_scroll->raise();
  187. }
  188. void ScrollContainer::_notification(int p_what) {
  189. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  190. call_deferred("_update_scrollbar_position");
  191. };
  192. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  193. child_max_size = Size2(0, 0);
  194. Size2 size = get_size();
  195. Point2 ofs;
  196. Ref<StyleBox> sb = get_stylebox("bg");
  197. size -= sb->get_minimum_size();
  198. ofs += sb->get_offset();
  199. if (h_scroll->is_visible_in_tree() && h_scroll->get_parent() == this) //scrolls may have been moved out for reasons
  200. size.y -= h_scroll->get_minimum_size().y;
  201. if (v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) //scrolls may have been moved out for reasons
  202. size.x -= v_scroll->get_minimum_size().x;
  203. for (int i = 0; i < get_child_count(); i++) {
  204. Control *c = Object::cast_to<Control>(get_child(i));
  205. if (!c)
  206. continue;
  207. if (c->is_set_as_toplevel())
  208. continue;
  209. if (c == h_scroll || c == v_scroll)
  210. continue;
  211. Size2 minsize = c->get_combined_minimum_size();
  212. child_max_size.x = MAX(child_max_size.x, minsize.x);
  213. child_max_size.y = MAX(child_max_size.y, minsize.y);
  214. Rect2 r = Rect2(-scroll, minsize);
  215. if (!scroll_h || (!h_scroll->is_visible_in_tree() && c->get_h_size_flags() & SIZE_EXPAND)) {
  216. r.position.x = 0;
  217. if (c->get_h_size_flags() & SIZE_EXPAND)
  218. r.size.width = MAX(size.width, minsize.width);
  219. else
  220. r.size.width = minsize.width;
  221. }
  222. if (!scroll_v || (!v_scroll->is_visible_in_tree() && c->get_v_size_flags() & SIZE_EXPAND)) {
  223. r.position.y = 0;
  224. if (c->get_v_size_flags() & SIZE_EXPAND)
  225. r.size.height = MAX(size.height, minsize.height);
  226. else
  227. r.size.height = minsize.height;
  228. }
  229. r.position += ofs;
  230. fit_child_in_rect(c, r);
  231. }
  232. update();
  233. };
  234. if (p_what == NOTIFICATION_DRAW) {
  235. Ref<StyleBox> sb = get_stylebox("bg");
  236. draw_style_box(sb, Rect2(Vector2(), get_size()));
  237. update_scrollbars();
  238. }
  239. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  240. if (drag_touching) {
  241. if (drag_touching_deaccel) {
  242. Vector2 pos = Vector2(h_scroll->get_value(), v_scroll->get_value());
  243. pos += drag_speed * get_physics_process_delta_time();
  244. bool turnoff_h = false;
  245. bool turnoff_v = false;
  246. if (pos.x < 0) {
  247. pos.x = 0;
  248. turnoff_h = true;
  249. }
  250. if (pos.x > (h_scroll->get_max() - h_scroll->get_page())) {
  251. pos.x = h_scroll->get_max() - h_scroll->get_page();
  252. turnoff_h = true;
  253. }
  254. if (pos.y < 0) {
  255. pos.y = 0;
  256. turnoff_v = true;
  257. }
  258. if (pos.y > (v_scroll->get_max() - v_scroll->get_page())) {
  259. pos.y = v_scroll->get_max() - v_scroll->get_page();
  260. turnoff_v = true;
  261. }
  262. if (scroll_h)
  263. h_scroll->set_value(pos.x);
  264. if (scroll_v)
  265. v_scroll->set_value(pos.y);
  266. float sgn_x = drag_speed.x < 0 ? -1 : 1;
  267. float val_x = Math::abs(drag_speed.x);
  268. val_x -= 1000 * get_physics_process_delta_time();
  269. if (val_x < 0) {
  270. turnoff_h = true;
  271. }
  272. float sgn_y = drag_speed.y < 0 ? -1 : 1;
  273. float val_y = Math::abs(drag_speed.y);
  274. val_y -= 1000 * get_physics_process_delta_time();
  275. if (val_y < 0) {
  276. turnoff_v = true;
  277. }
  278. drag_speed = Vector2(sgn_x * val_x, sgn_y * val_y);
  279. if (turnoff_h && turnoff_v) {
  280. _cancel_drag();
  281. }
  282. } else {
  283. if (time_since_motion == 0 || time_since_motion > 0.1) {
  284. Vector2 diff = drag_accum - last_drag_accum;
  285. last_drag_accum = drag_accum;
  286. drag_speed = diff / get_physics_process_delta_time();
  287. }
  288. time_since_motion += get_physics_process_delta_time();
  289. }
  290. }
  291. }
  292. };
  293. void ScrollContainer::update_scrollbars() {
  294. Size2 size = get_size();
  295. Ref<StyleBox> sb = get_stylebox("bg");
  296. size -= sb->get_minimum_size();
  297. Size2 hmin = h_scroll->get_combined_minimum_size();
  298. Size2 vmin = v_scroll->get_combined_minimum_size();
  299. Size2 min = child_max_size;
  300. if (!scroll_v || min.height <= size.height - hmin.height) {
  301. v_scroll->hide();
  302. v_scroll->set_max(0);
  303. scroll.y = 0;
  304. } else {
  305. v_scroll->show();
  306. v_scroll->set_max(min.height);
  307. v_scroll->set_page(size.height - hmin.height);
  308. scroll.y = v_scroll->get_value();
  309. }
  310. if (!scroll_h || min.width <= size.width - vmin.width) {
  311. h_scroll->hide();
  312. h_scroll->set_max(0);
  313. scroll.x = 0;
  314. } else {
  315. h_scroll->show();
  316. h_scroll->set_max(min.width);
  317. h_scroll->set_page(size.width - vmin.width);
  318. scroll.x = h_scroll->get_value();
  319. }
  320. }
  321. void ScrollContainer::_scroll_moved(float) {
  322. scroll.x = h_scroll->get_value();
  323. scroll.y = v_scroll->get_value();
  324. queue_sort();
  325. update();
  326. };
  327. void ScrollContainer::set_enable_h_scroll(bool p_enable) {
  328. scroll_h = p_enable;
  329. queue_sort();
  330. }
  331. bool ScrollContainer::is_h_scroll_enabled() const {
  332. return scroll_h;
  333. }
  334. void ScrollContainer::set_enable_v_scroll(bool p_enable) {
  335. scroll_v = p_enable;
  336. queue_sort();
  337. }
  338. bool ScrollContainer::is_v_scroll_enabled() const {
  339. return scroll_v;
  340. }
  341. int ScrollContainer::get_v_scroll() const {
  342. return v_scroll->get_value();
  343. }
  344. void ScrollContainer::set_v_scroll(int p_pos) {
  345. v_scroll->set_value(p_pos);
  346. _cancel_drag();
  347. }
  348. int ScrollContainer::get_h_scroll() const {
  349. return h_scroll->get_value();
  350. }
  351. void ScrollContainer::set_h_scroll(int p_pos) {
  352. h_scroll->set_value(p_pos);
  353. _cancel_drag();
  354. }
  355. int ScrollContainer::get_deadzone() const {
  356. return deadzone;
  357. }
  358. void ScrollContainer::set_deadzone(int p_deadzone) {
  359. deadzone = p_deadzone;
  360. }
  361. String ScrollContainer::get_configuration_warning() const {
  362. int found = 0;
  363. for (int i = 0; i < get_child_count(); i++) {
  364. Control *c = Object::cast_to<Control>(get_child(i));
  365. if (!c)
  366. continue;
  367. if (c->is_set_as_toplevel())
  368. continue;
  369. if (c == h_scroll || c == v_scroll)
  370. continue;
  371. found++;
  372. }
  373. if (found != 1)
  374. return TTR("ScrollContainer is intended to work with a single child control.\nUse a container as child (VBox,HBox,etc), or a Control and set the custom minimum size manually.");
  375. else
  376. return "";
  377. }
  378. HScrollBar *ScrollContainer::get_h_scrollbar() {
  379. return h_scroll;
  380. }
  381. VScrollBar *ScrollContainer::get_v_scrollbar() {
  382. return v_scroll;
  383. }
  384. void ScrollContainer::_bind_methods() {
  385. ClassDB::bind_method(D_METHOD("_scroll_moved"), &ScrollContainer::_scroll_moved);
  386. ClassDB::bind_method(D_METHOD("_gui_input"), &ScrollContainer::_gui_input);
  387. ClassDB::bind_method(D_METHOD("set_enable_h_scroll", "enable"), &ScrollContainer::set_enable_h_scroll);
  388. ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &ScrollContainer::is_h_scroll_enabled);
  389. ClassDB::bind_method(D_METHOD("set_enable_v_scroll", "enable"), &ScrollContainer::set_enable_v_scroll);
  390. ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"), &ScrollContainer::is_v_scroll_enabled);
  391. ClassDB::bind_method(D_METHOD("_update_scrollbar_position"), &ScrollContainer::_update_scrollbar_position);
  392. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &ScrollContainer::set_h_scroll);
  393. ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
  394. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &ScrollContainer::set_v_scroll);
  395. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll);
  396. ClassDB::bind_method(D_METHOD("set_deadzone", "deadzone"), &ScrollContainer::set_deadzone);
  397. ClassDB::bind_method(D_METHOD("get_deadzone"), &ScrollContainer::get_deadzone);
  398. ClassDB::bind_method(D_METHOD("get_h_scrollbar"), &ScrollContainer::get_h_scrollbar);
  399. ClassDB::bind_method(D_METHOD("get_v_scrollbar"), &ScrollContainer::get_v_scrollbar);
  400. ADD_SIGNAL(MethodInfo("scroll_started"));
  401. ADD_SIGNAL(MethodInfo("scroll_ended"));
  402. ADD_GROUP("Scroll", "scroll_");
  403. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_horizontal_enabled"), "set_enable_h_scroll", "is_h_scroll_enabled");
  404. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal"), "set_h_scroll", "get_h_scroll");
  405. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_vertical_enabled"), "set_enable_v_scroll", "is_v_scroll_enabled");
  406. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_vertical"), "set_v_scroll", "get_v_scroll");
  407. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_deadzone"), "set_deadzone", "get_deadzone");
  408. GLOBAL_DEF("gui/common/default_scroll_deadzone", 0);
  409. };
  410. ScrollContainer::ScrollContainer() {
  411. h_scroll = memnew(HScrollBar);
  412. h_scroll->set_name("_h_scroll");
  413. add_child(h_scroll);
  414. v_scroll = memnew(VScrollBar);
  415. v_scroll->set_name("_v_scroll");
  416. add_child(v_scroll);
  417. h_scroll->connect("value_changed", this, "_scroll_moved");
  418. v_scroll->connect("value_changed", this, "_scroll_moved");
  419. drag_speed = Vector2();
  420. drag_touching = false;
  421. drag_touching_deaccel = false;
  422. beyond_deadzone = false;
  423. scroll_h = true;
  424. scroll_v = true;
  425. deadzone = GLOBAL_GET("gui/common/default_scroll_deadzone");
  426. set_clip_contents(true);
  427. };