scroll_container.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*************************************************************************/
  2. /* scroll_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "os/os.h"
  32. bool ScrollContainer::clips_input() const {
  33. return true;
  34. }
  35. Size2 ScrollContainer::get_minimum_size() const {
  36. Size2 min_size;
  37. for (int i = 0; i < get_child_count(); i++) {
  38. Control *c = Object::cast_to<Control>(get_child(i));
  39. if (!c)
  40. continue;
  41. if (c->is_set_as_toplevel())
  42. continue;
  43. if (c == h_scroll || c == v_scroll)
  44. continue;
  45. Size2 minsize = c->get_combined_minimum_size();
  46. if (!scroll_h) {
  47. min_size.x = MAX(min_size.x, minsize.x);
  48. }
  49. if (!scroll_v) {
  50. min_size.y = MAX(min_size.y, minsize.y);
  51. }
  52. }
  53. if (h_scroll->is_visible_in_tree()) {
  54. min_size.y += h_scroll->get_minimum_size().y;
  55. }
  56. if (v_scroll->is_visible_in_tree()) {
  57. min_size.x += v_scroll->get_minimum_size().x;
  58. }
  59. return min_size;
  60. };
  61. void ScrollContainer::_cancel_drag() {
  62. set_physics_process(false);
  63. drag_touching_deaccel = false;
  64. drag_touching = false;
  65. drag_speed = Vector2();
  66. drag_accum = Vector2();
  67. last_drag_accum = Vector2();
  68. drag_from = Vector2();
  69. }
  70. void ScrollContainer::_gui_input(const Ref<InputEvent> &p_gui_input) {
  71. Ref<InputEventMouseButton> mb = p_gui_input;
  72. if (mb.is_valid()) {
  73. if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) {
  74. // only horizontal is enabled, scroll horizontally
  75. if (h_scroll->is_visible() && !v_scroll->is_visible()) {
  76. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 8 * mb->get_factor());
  77. } else if (v_scroll->is_visible_in_tree()) {
  78. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 8 * mb->get_factor());
  79. }
  80. }
  81. if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) {
  82. // only horizontal is enabled, scroll horizontally
  83. if (h_scroll->is_visible() && !v_scroll->is_visible()) {
  84. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 8 * mb->get_factor());
  85. } else if (v_scroll->is_visible()) {
  86. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 8 * mb->get_factor());
  87. }
  88. }
  89. if (mb->get_button_index() == BUTTON_WHEEL_LEFT && mb->is_pressed()) {
  90. if (h_scroll->is_visible_in_tree()) {
  91. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() * mb->get_factor() / 8);
  92. }
  93. }
  94. if (mb->get_button_index() == BUTTON_WHEEL_RIGHT && mb->is_pressed()) {
  95. if (h_scroll->is_visible_in_tree()) {
  96. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * mb->get_factor() / 8);
  97. }
  98. }
  99. if (!OS::get_singleton()->has_touchscreen_ui_hint())
  100. return;
  101. if (mb->get_button_index() != BUTTON_LEFT)
  102. return;
  103. if (mb->is_pressed()) {
  104. if (drag_touching) {
  105. set_physics_process(false);
  106. drag_touching_deaccel = false;
  107. drag_touching = false;
  108. drag_speed = Vector2();
  109. drag_accum = Vector2();
  110. last_drag_accum = Vector2();
  111. drag_from = Vector2();
  112. }
  113. if (true) {
  114. drag_speed = Vector2();
  115. drag_accum = Vector2();
  116. last_drag_accum = Vector2();
  117. drag_from = Vector2(h_scroll->get_value(), v_scroll->get_value());
  118. drag_touching = OS::get_singleton()->has_touchscreen_ui_hint();
  119. drag_touching_deaccel = false;
  120. time_since_motion = 0;
  121. if (drag_touching) {
  122. set_physics_process(true);
  123. time_since_motion = 0;
  124. }
  125. }
  126. } else {
  127. if (drag_touching) {
  128. if (drag_speed == Vector2()) {
  129. drag_touching_deaccel = false;
  130. drag_touching = false;
  131. set_physics_process(false);
  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. Vector2 diff = drag_from + drag_accum;
  144. if (scroll_h)
  145. h_scroll->set_value(diff.x);
  146. else
  147. drag_accum.x = 0;
  148. if (scroll_v)
  149. v_scroll->set_value(diff.y);
  150. else
  151. drag_accum.y = 0;
  152. time_since_motion = 0;
  153. }
  154. }
  155. Ref<InputEventPanGesture> pan_gesture = p_gui_input;
  156. if (pan_gesture.is_valid()) {
  157. if (h_scroll->is_visible_in_tree()) {
  158. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  159. }
  160. if (v_scroll->is_visible_in_tree()) {
  161. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  162. }
  163. }
  164. }
  165. void ScrollContainer::_update_scrollbar_position() {
  166. Size2 hmin = h_scroll->get_combined_minimum_size();
  167. Size2 vmin = v_scroll->get_combined_minimum_size();
  168. v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width);
  169. v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  170. v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  171. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  172. h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0);
  173. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  174. h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height);
  175. h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  176. h_scroll->raise();
  177. v_scroll->raise();
  178. }
  179. void ScrollContainer::_notification(int p_what) {
  180. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  181. call_deferred("_update_scrollbar_position");
  182. };
  183. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  184. child_max_size = Size2(0, 0);
  185. Size2 size = get_size();
  186. if (h_scroll->is_visible_in_tree())
  187. size.y -= h_scroll->get_minimum_size().y;
  188. if (v_scroll->is_visible_in_tree())
  189. size.x -= h_scroll->get_minimum_size().x;
  190. for (int i = 0; i < get_child_count(); i++) {
  191. Control *c = Object::cast_to<Control>(get_child(i));
  192. if (!c)
  193. continue;
  194. if (c->is_set_as_toplevel())
  195. continue;
  196. if (c == h_scroll || c == v_scroll)
  197. continue;
  198. Size2 minsize = c->get_combined_minimum_size();
  199. child_max_size.x = MAX(child_max_size.x, minsize.x);
  200. child_max_size.y = MAX(child_max_size.y, minsize.y);
  201. Rect2 r = Rect2(-scroll, minsize);
  202. if (!scroll_h || (!h_scroll->is_visible_in_tree() && c->get_h_size_flags() & SIZE_EXPAND)) {
  203. r.position.x = 0;
  204. if (c->get_h_size_flags() & SIZE_EXPAND)
  205. r.size.width = MAX(size.width, minsize.width);
  206. else
  207. r.size.width = minsize.width;
  208. }
  209. if (!scroll_v || (!v_scroll->is_visible_in_tree() && c->get_v_size_flags() & SIZE_EXPAND)) {
  210. r.position.y = 0;
  211. r.size.height = size.height;
  212. if (c->get_v_size_flags() & SIZE_EXPAND)
  213. r.size.height = MAX(size.height, minsize.height);
  214. else
  215. r.size.height = minsize.height;
  216. }
  217. fit_child_in_rect(c, r);
  218. }
  219. update();
  220. };
  221. if (p_what == NOTIFICATION_DRAW) {
  222. update_scrollbars();
  223. }
  224. if (p_what == NOTIFICATION_PHYSICS_PROCESS) {
  225. if (drag_touching) {
  226. if (drag_touching_deaccel) {
  227. Vector2 pos = Vector2(h_scroll->get_value(), v_scroll->get_value());
  228. pos += drag_speed * get_physics_process_delta_time();
  229. bool turnoff_h = false;
  230. bool turnoff_v = false;
  231. if (pos.x < 0) {
  232. pos.x = 0;
  233. turnoff_h = true;
  234. }
  235. if (pos.x > (h_scroll->get_max() - h_scroll->get_page())) {
  236. pos.x = h_scroll->get_max() - h_scroll->get_page();
  237. turnoff_h = true;
  238. }
  239. if (pos.y < 0) {
  240. pos.y = 0;
  241. turnoff_v = true;
  242. }
  243. if (pos.y > (v_scroll->get_max() - v_scroll->get_page())) {
  244. pos.y = v_scroll->get_max() - v_scroll->get_page();
  245. turnoff_v = true;
  246. }
  247. if (scroll_h)
  248. h_scroll->set_value(pos.x);
  249. if (scroll_v)
  250. v_scroll->set_value(pos.y);
  251. float sgn_x = drag_speed.x < 0 ? -1 : 1;
  252. float val_x = Math::abs(drag_speed.x);
  253. val_x -= 1000 * get_physics_process_delta_time();
  254. if (val_x < 0) {
  255. turnoff_h = true;
  256. }
  257. float sgn_y = drag_speed.y < 0 ? -1 : 1;
  258. float val_y = Math::abs(drag_speed.y);
  259. val_y -= 1000 * get_physics_process_delta_time();
  260. if (val_y < 0) {
  261. turnoff_v = true;
  262. }
  263. drag_speed = Vector2(sgn_x * val_x, sgn_y * val_y);
  264. if (turnoff_h && turnoff_v) {
  265. set_physics_process(false);
  266. drag_touching = false;
  267. drag_touching_deaccel = false;
  268. }
  269. } else {
  270. if (time_since_motion == 0 || time_since_motion > 0.1) {
  271. Vector2 diff = drag_accum - last_drag_accum;
  272. last_drag_accum = drag_accum;
  273. drag_speed = diff / get_physics_process_delta_time();
  274. }
  275. time_since_motion += get_physics_process_delta_time();
  276. }
  277. }
  278. }
  279. };
  280. void ScrollContainer::update_scrollbars() {
  281. Size2 size = get_size();
  282. Size2 hmin = h_scroll->get_combined_minimum_size();
  283. Size2 vmin = v_scroll->get_combined_minimum_size();
  284. Size2 min = child_max_size;
  285. if (!scroll_v || min.height <= size.height - hmin.height) {
  286. v_scroll->hide();
  287. v_scroll->set_max(0);
  288. scroll.y = 0;
  289. } else {
  290. v_scroll->show();
  291. v_scroll->set_max(min.height);
  292. v_scroll->set_page(size.height - hmin.height);
  293. scroll.y = v_scroll->get_value();
  294. }
  295. if (!scroll_h || min.width <= size.width - vmin.width) {
  296. h_scroll->hide();
  297. h_scroll->set_max(0);
  298. scroll.x = 0;
  299. } else {
  300. h_scroll->show();
  301. h_scroll->set_max(min.width);
  302. h_scroll->set_page(size.width - vmin.width);
  303. scroll.x = h_scroll->get_value();
  304. }
  305. }
  306. void ScrollContainer::_scroll_moved(float) {
  307. scroll.x = h_scroll->get_value();
  308. scroll.y = v_scroll->get_value();
  309. queue_sort();
  310. update();
  311. };
  312. void ScrollContainer::set_enable_h_scroll(bool p_enable) {
  313. scroll_h = p_enable;
  314. queue_sort();
  315. }
  316. bool ScrollContainer::is_h_scroll_enabled() const {
  317. return scroll_h;
  318. }
  319. void ScrollContainer::set_enable_v_scroll(bool p_enable) {
  320. scroll_v = p_enable;
  321. queue_sort();
  322. }
  323. bool ScrollContainer::is_v_scroll_enabled() const {
  324. return scroll_v;
  325. }
  326. int ScrollContainer::get_v_scroll() const {
  327. return v_scroll->get_value();
  328. }
  329. void ScrollContainer::set_v_scroll(int p_pos) {
  330. v_scroll->set_value(p_pos);
  331. _cancel_drag();
  332. }
  333. int ScrollContainer::get_h_scroll() const {
  334. return h_scroll->get_value();
  335. }
  336. void ScrollContainer::set_h_scroll(int p_pos) {
  337. h_scroll->set_value(p_pos);
  338. _cancel_drag();
  339. }
  340. String ScrollContainer::get_configuration_warning() const {
  341. int found = 0;
  342. for (int i = 0; i < get_child_count(); i++) {
  343. Control *c = Object::cast_to<Control>(get_child(i));
  344. if (!c)
  345. continue;
  346. if (c->is_set_as_toplevel())
  347. continue;
  348. if (c == h_scroll || c == v_scroll)
  349. continue;
  350. found++;
  351. }
  352. if (found != 1)
  353. 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.");
  354. else
  355. return "";
  356. }
  357. void ScrollContainer::_bind_methods() {
  358. ClassDB::bind_method(D_METHOD("_scroll_moved"), &ScrollContainer::_scroll_moved);
  359. ClassDB::bind_method(D_METHOD("_gui_input"), &ScrollContainer::_gui_input);
  360. ClassDB::bind_method(D_METHOD("set_enable_h_scroll", "enable"), &ScrollContainer::set_enable_h_scroll);
  361. ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &ScrollContainer::is_h_scroll_enabled);
  362. ClassDB::bind_method(D_METHOD("set_enable_v_scroll", "enable"), &ScrollContainer::set_enable_v_scroll);
  363. ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"), &ScrollContainer::is_v_scroll_enabled);
  364. ClassDB::bind_method(D_METHOD("_update_scrollbar_position"), &ScrollContainer::_update_scrollbar_position);
  365. ClassDB::bind_method(D_METHOD("set_h_scroll", "val"), &ScrollContainer::set_h_scroll);
  366. ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
  367. ClassDB::bind_method(D_METHOD("set_v_scroll", "val"), &ScrollContainer::set_v_scroll);
  368. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll);
  369. ADD_GROUP("Scroll", "scroll_");
  370. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_horizontal"), "set_enable_h_scroll", "is_h_scroll_enabled");
  371. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_vertical"), "set_enable_v_scroll", "is_v_scroll_enabled");
  372. };
  373. ScrollContainer::ScrollContainer() {
  374. h_scroll = memnew(HScrollBar);
  375. h_scroll->set_name("_h_scroll");
  376. add_child(h_scroll);
  377. v_scroll = memnew(VScrollBar);
  378. v_scroll->set_name("_v_scroll");
  379. add_child(v_scroll);
  380. h_scroll->connect("value_changed", this, "_scroll_moved");
  381. v_scroll->connect("value_changed", this, "_scroll_moved");
  382. drag_speed = Vector2();
  383. drag_touching = false;
  384. drag_touching_deaccel = false;
  385. scroll_h = true;
  386. scroll_v = true;
  387. set_clip_contents(true);
  388. };