scroll_container.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. r.size.height = size.height;
  225. if (c->get_v_size_flags() & SIZE_EXPAND)
  226. r.size.height = MAX(size.height, minsize.height);
  227. else
  228. r.size.height = minsize.height;
  229. }
  230. r.position += ofs;
  231. fit_child_in_rect(c, r);
  232. }
  233. update();
  234. };
  235. if (p_what == NOTIFICATION_DRAW) {
  236. Ref<StyleBox> sb = get_stylebox("bg");
  237. draw_style_box(sb, Rect2(Vector2(), get_size()));
  238. update_scrollbars();
  239. }
  240. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  241. if (drag_touching) {
  242. if (drag_touching_deaccel) {
  243. Vector2 pos = Vector2(h_scroll->get_value(), v_scroll->get_value());
  244. pos += drag_speed * get_physics_process_delta_time();
  245. bool turnoff_h = false;
  246. bool turnoff_v = false;
  247. if (pos.x < 0) {
  248. pos.x = 0;
  249. turnoff_h = true;
  250. }
  251. if (pos.x > (h_scroll->get_max() - h_scroll->get_page())) {
  252. pos.x = h_scroll->get_max() - h_scroll->get_page();
  253. turnoff_h = true;
  254. }
  255. if (pos.y < 0) {
  256. pos.y = 0;
  257. turnoff_v = true;
  258. }
  259. if (pos.y > (v_scroll->get_max() - v_scroll->get_page())) {
  260. pos.y = v_scroll->get_max() - v_scroll->get_page();
  261. turnoff_v = true;
  262. }
  263. if (scroll_h)
  264. h_scroll->set_value(pos.x);
  265. if (scroll_v)
  266. v_scroll->set_value(pos.y);
  267. float sgn_x = drag_speed.x < 0 ? -1 : 1;
  268. float val_x = Math::abs(drag_speed.x);
  269. val_x -= 1000 * get_physics_process_delta_time();
  270. if (val_x < 0) {
  271. turnoff_h = true;
  272. }
  273. float sgn_y = drag_speed.y < 0 ? -1 : 1;
  274. float val_y = Math::abs(drag_speed.y);
  275. val_y -= 1000 * get_physics_process_delta_time();
  276. if (val_y < 0) {
  277. turnoff_v = true;
  278. }
  279. drag_speed = Vector2(sgn_x * val_x, sgn_y * val_y);
  280. if (turnoff_h && turnoff_v) {
  281. _cancel_drag();
  282. }
  283. } else {
  284. if (time_since_motion == 0 || time_since_motion > 0.1) {
  285. Vector2 diff = drag_accum - last_drag_accum;
  286. last_drag_accum = drag_accum;
  287. drag_speed = diff / get_physics_process_delta_time();
  288. }
  289. time_since_motion += get_physics_process_delta_time();
  290. }
  291. }
  292. }
  293. };
  294. void ScrollContainer::update_scrollbars() {
  295. Size2 size = get_size();
  296. Ref<StyleBox> sb = get_stylebox("bg");
  297. size -= sb->get_minimum_size();
  298. Size2 hmin = h_scroll->get_combined_minimum_size();
  299. Size2 vmin = v_scroll->get_combined_minimum_size();
  300. Size2 min = child_max_size;
  301. if (!scroll_v || min.height <= size.height - hmin.height) {
  302. v_scroll->hide();
  303. v_scroll->set_max(0);
  304. scroll.y = 0;
  305. } else {
  306. v_scroll->show();
  307. v_scroll->set_max(min.height);
  308. v_scroll->set_page(size.height - hmin.height);
  309. scroll.y = v_scroll->get_value();
  310. }
  311. if (!scroll_h || min.width <= size.width - vmin.width) {
  312. h_scroll->hide();
  313. h_scroll->set_max(0);
  314. scroll.x = 0;
  315. } else {
  316. h_scroll->show();
  317. h_scroll->set_max(min.width);
  318. h_scroll->set_page(size.width - vmin.width);
  319. scroll.x = h_scroll->get_value();
  320. }
  321. }
  322. void ScrollContainer::_scroll_moved(float) {
  323. scroll.x = h_scroll->get_value();
  324. scroll.y = v_scroll->get_value();
  325. queue_sort();
  326. update();
  327. };
  328. void ScrollContainer::set_enable_h_scroll(bool p_enable) {
  329. scroll_h = p_enable;
  330. queue_sort();
  331. }
  332. bool ScrollContainer::is_h_scroll_enabled() const {
  333. return scroll_h;
  334. }
  335. void ScrollContainer::set_enable_v_scroll(bool p_enable) {
  336. scroll_v = p_enable;
  337. queue_sort();
  338. }
  339. bool ScrollContainer::is_v_scroll_enabled() const {
  340. return scroll_v;
  341. }
  342. int ScrollContainer::get_v_scroll() const {
  343. return v_scroll->get_value();
  344. }
  345. void ScrollContainer::set_v_scroll(int p_pos) {
  346. v_scroll->set_value(p_pos);
  347. _cancel_drag();
  348. }
  349. int ScrollContainer::get_h_scroll() const {
  350. return h_scroll->get_value();
  351. }
  352. void ScrollContainer::set_h_scroll(int p_pos) {
  353. h_scroll->set_value(p_pos);
  354. _cancel_drag();
  355. }
  356. int ScrollContainer::get_deadzone() const {
  357. return deadzone;
  358. }
  359. void ScrollContainer::set_deadzone(int p_deadzone) {
  360. deadzone = p_deadzone;
  361. }
  362. String ScrollContainer::get_configuration_warning() const {
  363. int found = 0;
  364. for (int i = 0; i < get_child_count(); i++) {
  365. Control *c = Object::cast_to<Control>(get_child(i));
  366. if (!c)
  367. continue;
  368. if (c->is_set_as_toplevel())
  369. continue;
  370. if (c == h_scroll || c == v_scroll)
  371. continue;
  372. found++;
  373. }
  374. if (found != 1)
  375. 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.");
  376. else
  377. return "";
  378. }
  379. HScrollBar *ScrollContainer::get_h_scrollbar() {
  380. return h_scroll;
  381. }
  382. VScrollBar *ScrollContainer::get_v_scrollbar() {
  383. return v_scroll;
  384. }
  385. void ScrollContainer::_bind_methods() {
  386. ClassDB::bind_method(D_METHOD("_scroll_moved"), &ScrollContainer::_scroll_moved);
  387. ClassDB::bind_method(D_METHOD("_gui_input"), &ScrollContainer::_gui_input);
  388. ClassDB::bind_method(D_METHOD("set_enable_h_scroll", "enable"), &ScrollContainer::set_enable_h_scroll);
  389. ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &ScrollContainer::is_h_scroll_enabled);
  390. ClassDB::bind_method(D_METHOD("set_enable_v_scroll", "enable"), &ScrollContainer::set_enable_v_scroll);
  391. ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"), &ScrollContainer::is_v_scroll_enabled);
  392. ClassDB::bind_method(D_METHOD("_update_scrollbar_position"), &ScrollContainer::_update_scrollbar_position);
  393. ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &ScrollContainer::set_h_scroll);
  394. ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
  395. ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &ScrollContainer::set_v_scroll);
  396. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll);
  397. ClassDB::bind_method(D_METHOD("set_deadzone", "deadzone"), &ScrollContainer::set_deadzone);
  398. ClassDB::bind_method(D_METHOD("get_deadzone"), &ScrollContainer::get_deadzone);
  399. ClassDB::bind_method(D_METHOD("get_h_scrollbar"), &ScrollContainer::get_h_scrollbar);
  400. ClassDB::bind_method(D_METHOD("get_v_scrollbar"), &ScrollContainer::get_v_scrollbar);
  401. ADD_SIGNAL(MethodInfo("scroll_started"));
  402. ADD_SIGNAL(MethodInfo("scroll_ended"));
  403. ADD_GROUP("Scroll", "scroll_");
  404. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_horizontal_enabled"), "set_enable_h_scroll", "is_h_scroll_enabled");
  405. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal"), "set_h_scroll", "get_h_scroll");
  406. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_vertical_enabled"), "set_enable_v_scroll", "is_v_scroll_enabled");
  407. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_vertical"), "set_v_scroll", "get_v_scroll");
  408. ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_deadzone"), "set_deadzone", "get_deadzone");
  409. GLOBAL_DEF("gui/common/default_scroll_deadzone", 0);
  410. };
  411. ScrollContainer::ScrollContainer() {
  412. h_scroll = memnew(HScrollBar);
  413. h_scroll->set_name("_h_scroll");
  414. add_child(h_scroll);
  415. v_scroll = memnew(VScrollBar);
  416. v_scroll->set_name("_v_scroll");
  417. add_child(v_scroll);
  418. h_scroll->connect("value_changed", this, "_scroll_moved");
  419. v_scroll->connect("value_changed", this, "_scroll_moved");
  420. drag_speed = Vector2();
  421. drag_touching = false;
  422. drag_touching_deaccel = false;
  423. beyond_deadzone = false;
  424. scroll_h = true;
  425. scroll_v = true;
  426. deadzone = GLOBAL_GET("gui/common/default_scroll_deadzone");
  427. set_clip_contents(true);
  428. };