scroll_container.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. }
  156. void ScrollContainer::_update_scrollbar_position() {
  157. Size2 hmin = h_scroll->get_combined_minimum_size();
  158. Size2 vmin = v_scroll->get_combined_minimum_size();
  159. v_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -vmin.width);
  160. v_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  161. v_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 0);
  162. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  163. h_scroll->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 0);
  164. h_scroll->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 0);
  165. h_scroll->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -hmin.height);
  166. h_scroll->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, 0);
  167. h_scroll->raise();
  168. v_scroll->raise();
  169. }
  170. void ScrollContainer::_notification(int p_what) {
  171. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  172. call_deferred("_update_scrollbar_position");
  173. };
  174. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  175. child_max_size = Size2(0, 0);
  176. Size2 size = get_size();
  177. if (h_scroll->is_visible_in_tree())
  178. size.y -= h_scroll->get_minimum_size().y;
  179. if (v_scroll->is_visible_in_tree())
  180. size.x -= h_scroll->get_minimum_size().x;
  181. for (int i = 0; i < get_child_count(); i++) {
  182. Control *c = Object::cast_to<Control>(get_child(i));
  183. if (!c)
  184. continue;
  185. if (c->is_set_as_toplevel())
  186. continue;
  187. if (c == h_scroll || c == v_scroll)
  188. continue;
  189. Size2 minsize = c->get_combined_minimum_size();
  190. child_max_size.x = MAX(child_max_size.x, minsize.x);
  191. child_max_size.y = MAX(child_max_size.y, minsize.y);
  192. Rect2 r = Rect2(-scroll, minsize);
  193. if (!scroll_h || (!h_scroll->is_visible_in_tree() && c->get_h_size_flags() & SIZE_EXPAND)) {
  194. r.position.x = 0;
  195. if (c->get_h_size_flags() & SIZE_EXPAND)
  196. r.size.width = MAX(size.width, minsize.width);
  197. else
  198. r.size.width = minsize.width;
  199. }
  200. if (!scroll_v || (!v_scroll->is_visible_in_tree() && c->get_v_size_flags() & SIZE_EXPAND)) {
  201. r.position.y = 0;
  202. r.size.height = size.height;
  203. if (c->get_v_size_flags() & SIZE_EXPAND)
  204. r.size.height = MAX(size.height, minsize.height);
  205. else
  206. r.size.height = minsize.height;
  207. }
  208. fit_child_in_rect(c, r);
  209. }
  210. update();
  211. };
  212. if (p_what == NOTIFICATION_DRAW) {
  213. update_scrollbars();
  214. }
  215. if (p_what == NOTIFICATION_PHYSICS_PROCESS) {
  216. if (drag_touching) {
  217. if (drag_touching_deaccel) {
  218. Vector2 pos = Vector2(h_scroll->get_value(), v_scroll->get_value());
  219. pos += drag_speed * get_physics_process_delta_time();
  220. bool turnoff_h = false;
  221. bool turnoff_v = false;
  222. if (pos.x < 0) {
  223. pos.x = 0;
  224. turnoff_h = true;
  225. }
  226. if (pos.x > (h_scroll->get_max() - h_scroll->get_page())) {
  227. pos.x = h_scroll->get_max() - h_scroll->get_page();
  228. turnoff_h = true;
  229. }
  230. if (pos.y < 0) {
  231. pos.y = 0;
  232. turnoff_v = true;
  233. }
  234. if (pos.y > (v_scroll->get_max() - v_scroll->get_page())) {
  235. pos.y = v_scroll->get_max() - v_scroll->get_page();
  236. turnoff_v = true;
  237. }
  238. if (scroll_h)
  239. h_scroll->set_value(pos.x);
  240. if (scroll_v)
  241. v_scroll->set_value(pos.y);
  242. float sgn_x = drag_speed.x < 0 ? -1 : 1;
  243. float val_x = Math::abs(drag_speed.x);
  244. val_x -= 1000 * get_physics_process_delta_time();
  245. if (val_x < 0) {
  246. turnoff_h = true;
  247. }
  248. float sgn_y = drag_speed.y < 0 ? -1 : 1;
  249. float val_y = Math::abs(drag_speed.y);
  250. val_y -= 1000 * get_physics_process_delta_time();
  251. if (val_y < 0) {
  252. turnoff_v = true;
  253. }
  254. drag_speed = Vector2(sgn_x * val_x, sgn_y * val_y);
  255. if (turnoff_h && turnoff_v) {
  256. set_physics_process(false);
  257. drag_touching = false;
  258. drag_touching_deaccel = false;
  259. }
  260. } else {
  261. if (time_since_motion == 0 || time_since_motion > 0.1) {
  262. Vector2 diff = drag_accum - last_drag_accum;
  263. last_drag_accum = drag_accum;
  264. drag_speed = diff / get_physics_process_delta_time();
  265. }
  266. time_since_motion += get_physics_process_delta_time();
  267. }
  268. }
  269. }
  270. };
  271. void ScrollContainer::update_scrollbars() {
  272. Size2 size = get_size();
  273. Size2 hmin = h_scroll->get_combined_minimum_size();
  274. Size2 vmin = v_scroll->get_combined_minimum_size();
  275. Size2 min = child_max_size;
  276. if (!scroll_v || min.height <= size.height - hmin.height) {
  277. v_scroll->hide();
  278. scroll.y = 0;
  279. } else {
  280. v_scroll->show();
  281. scroll.y = v_scroll->get_value();
  282. }
  283. v_scroll->set_max(min.height);
  284. v_scroll->set_page(size.height - hmin.height);
  285. if (!scroll_h || min.width <= size.width - vmin.width) {
  286. h_scroll->hide();
  287. scroll.x = 0;
  288. } else {
  289. h_scroll->show();
  290. h_scroll->set_max(min.width);
  291. h_scroll->set_page(size.width - vmin.width);
  292. scroll.x = h_scroll->get_value();
  293. }
  294. }
  295. void ScrollContainer::_scroll_moved(float) {
  296. scroll.x = h_scroll->get_value();
  297. scroll.y = v_scroll->get_value();
  298. queue_sort();
  299. update();
  300. };
  301. void ScrollContainer::set_enable_h_scroll(bool p_enable) {
  302. scroll_h = p_enable;
  303. queue_sort();
  304. }
  305. bool ScrollContainer::is_h_scroll_enabled() const {
  306. return scroll_h;
  307. }
  308. void ScrollContainer::set_enable_v_scroll(bool p_enable) {
  309. scroll_v = p_enable;
  310. queue_sort();
  311. }
  312. bool ScrollContainer::is_v_scroll_enabled() const {
  313. return scroll_v;
  314. }
  315. int ScrollContainer::get_v_scroll() const {
  316. return v_scroll->get_value();
  317. }
  318. void ScrollContainer::set_v_scroll(int p_pos) {
  319. v_scroll->set_value(p_pos);
  320. _cancel_drag();
  321. }
  322. int ScrollContainer::get_h_scroll() const {
  323. return h_scroll->get_value();
  324. }
  325. void ScrollContainer::set_h_scroll(int p_pos) {
  326. h_scroll->set_value(p_pos);
  327. _cancel_drag();
  328. }
  329. String ScrollContainer::get_configuration_warning() const {
  330. int found = 0;
  331. for (int i = 0; i < get_child_count(); i++) {
  332. Control *c = Object::cast_to<Control>(get_child(i));
  333. if (!c)
  334. continue;
  335. if (c->is_set_as_toplevel())
  336. continue;
  337. if (c == h_scroll || c == v_scroll)
  338. continue;
  339. found++;
  340. }
  341. if (found != 1)
  342. 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.");
  343. else
  344. return "";
  345. }
  346. void ScrollContainer::_bind_methods() {
  347. ClassDB::bind_method(D_METHOD("_scroll_moved"), &ScrollContainer::_scroll_moved);
  348. ClassDB::bind_method(D_METHOD("_gui_input"), &ScrollContainer::_gui_input);
  349. ClassDB::bind_method(D_METHOD("set_enable_h_scroll", "enable"), &ScrollContainer::set_enable_h_scroll);
  350. ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &ScrollContainer::is_h_scroll_enabled);
  351. ClassDB::bind_method(D_METHOD("set_enable_v_scroll", "enable"), &ScrollContainer::set_enable_v_scroll);
  352. ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"), &ScrollContainer::is_v_scroll_enabled);
  353. ClassDB::bind_method(D_METHOD("_update_scrollbar_position"), &ScrollContainer::_update_scrollbar_position);
  354. ClassDB::bind_method(D_METHOD("set_h_scroll", "val"), &ScrollContainer::set_h_scroll);
  355. ClassDB::bind_method(D_METHOD("get_h_scroll"), &ScrollContainer::get_h_scroll);
  356. ClassDB::bind_method(D_METHOD("set_v_scroll", "val"), &ScrollContainer::set_v_scroll);
  357. ClassDB::bind_method(D_METHOD("get_v_scroll"), &ScrollContainer::get_v_scroll);
  358. ADD_GROUP("Scroll", "scroll_");
  359. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_horizontal"), "set_enable_h_scroll", "is_h_scroll_enabled");
  360. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_vertical"), "set_enable_v_scroll", "is_v_scroll_enabled");
  361. };
  362. ScrollContainer::ScrollContainer() {
  363. h_scroll = memnew(HScrollBar);
  364. h_scroll->set_name("_h_scroll");
  365. add_child(h_scroll);
  366. v_scroll = memnew(VScrollBar);
  367. v_scroll->set_name("_v_scroll");
  368. add_child(v_scroll);
  369. h_scroll->connect("value_changed", this, "_scroll_moved");
  370. v_scroll->connect("value_changed", this, "_scroll_moved");
  371. drag_speed = Vector2();
  372. drag_touching = false;
  373. drag_touching_deaccel = false;
  374. scroll_h = true;
  375. scroll_v = true;
  376. set_clip_contents(true);
  377. };