scroll_container.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*************************************************************************/
  2. /* scroll_container.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "scroll_container.h"
  30. #include "os/os.h"
  31. bool ScrollContainer::clips_input() const {
  32. return true;
  33. }
  34. Size2 ScrollContainer::get_minimum_size() const {
  35. return Size2(1, 1);
  36. };
  37. void ScrollContainer::_cancel_drag() {
  38. set_fixed_process(false);
  39. drag_touching_deaccel=false;
  40. drag_touching=false;
  41. drag_speed=Vector2();
  42. drag_accum=Vector2();
  43. last_drag_accum=Vector2();
  44. drag_from=Vector2();
  45. }
  46. void ScrollContainer::_input_event(const InputEvent& p_input_event) {
  47. switch(p_input_event.type) {
  48. case InputEvent::MOUSE_BUTTON: {
  49. const InputEventMouseButton &mb=p_input_event.mouse_button;
  50. if (mb.button_index==BUTTON_WHEEL_UP && mb.pressed && v_scroll->is_visible()) {
  51. v_scroll->set_val( v_scroll->get_val()-v_scroll->get_page()/8 );
  52. }
  53. if (mb.button_index==BUTTON_WHEEL_DOWN && mb.pressed && v_scroll->is_visible()) {
  54. v_scroll->set_val( v_scroll->get_val()+v_scroll->get_page()/8 );
  55. }
  56. if(!OS::get_singleton()->has_touchscreen_ui_hint())
  57. return;
  58. if (mb.button_index!=BUTTON_LEFT)
  59. break;
  60. if (mb.pressed) {
  61. if (drag_touching) {
  62. set_fixed_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. if (true) {
  71. drag_speed=Vector2();
  72. drag_accum=Vector2();
  73. last_drag_accum=Vector2();
  74. drag_from=Vector2(h_scroll->get_val(),v_scroll->get_val());
  75. drag_touching=OS::get_singleton()->has_touchscreen_ui_hint();
  76. drag_touching_deaccel=false;
  77. time_since_motion=0;
  78. if (drag_touching) {
  79. set_fixed_process(true);
  80. time_since_motion=0;
  81. }
  82. }
  83. } else {
  84. if (drag_touching) {
  85. if (drag_speed==Vector2()) {
  86. drag_touching_deaccel=false;
  87. drag_touching=false;
  88. set_fixed_process(false);
  89. } else {
  90. drag_touching_deaccel=true;
  91. }
  92. }
  93. }
  94. } break;
  95. case InputEvent::MOUSE_MOTION: {
  96. const InputEventMouseMotion &mm=p_input_event.mouse_motion;
  97. if (drag_touching && ! drag_touching_deaccel) {
  98. Vector2 motion = Vector2(mm.relative_x,mm.relative_y);
  99. drag_accum-=motion;
  100. Vector2 diff = drag_from+drag_accum;
  101. if (scroll_h)
  102. h_scroll->set_val(diff.x);
  103. else
  104. drag_accum.x=0;
  105. if (scroll_v)
  106. v_scroll->set_val(diff.y);
  107. else
  108. drag_accum.y=0;
  109. time_since_motion=0;
  110. }
  111. } break;
  112. }
  113. }
  114. void ScrollContainer::_update_scrollbar_pos() {
  115. Size2 size = get_size();
  116. Size2 hmin = h_scroll->get_combined_minimum_size();
  117. Size2 vmin = v_scroll->get_combined_minimum_size();
  118. v_scroll->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,vmin.width);
  119. v_scroll->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0);
  120. v_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,0);
  121. v_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0);
  122. h_scroll->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_BEGIN,0);
  123. h_scroll->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,0);
  124. h_scroll->set_anchor_and_margin(MARGIN_TOP,ANCHOR_END,hmin.height);
  125. h_scroll->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_END,0);
  126. h_scroll->raise();
  127. v_scroll->raise();
  128. }
  129. void ScrollContainer::_notification(int p_what) {
  130. if (p_what == NOTIFICATION_ENTER_SCENE || p_what == NOTIFICATION_THEME_CHANGED) {
  131. call_deferred("_update_scrollbar_pos");
  132. };
  133. if (p_what==NOTIFICATION_SORT_CHILDREN) {
  134. child_max_size = Size2(0, 0);
  135. Size2 size = get_size();
  136. for(int i=0;i<get_child_count();i++) {
  137. Control *c = get_child(i)->cast_to<Control>();
  138. if (!c)
  139. continue;
  140. if (c->is_set_as_toplevel())
  141. continue;
  142. if (c == h_scroll || c == v_scroll)
  143. continue;
  144. Size2 minsize = c->get_combined_minimum_size();
  145. child_max_size.x = MAX(child_max_size.x, minsize.x);
  146. child_max_size.y = MAX(child_max_size.y, minsize.y);
  147. Rect2 r = Rect2(-scroll,minsize);
  148. if (!scroll_h) {
  149. r.pos.x=0;
  150. r.size.width=size.width;
  151. }
  152. if (!scroll_v) {
  153. r.pos.y=0;
  154. r.size.height=size.height;
  155. }
  156. fit_child_in_rect(c,r);
  157. }
  158. update();
  159. };
  160. if (p_what == NOTIFICATION_DRAW) {
  161. update_scrollbars();
  162. VisualServer::get_singleton()->canvas_item_set_clip(get_canvas_item(),true);
  163. }
  164. if (p_what==NOTIFICATION_FIXED_PROCESS) {
  165. if (drag_touching) {
  166. if (drag_touching_deaccel) {
  167. Vector2 pos = Vector2(h_scroll->get_val(),v_scroll->get_val());
  168. pos+=drag_speed*get_fixed_process_delta_time();
  169. bool turnoff_h=false;
  170. bool turnoff_v=false;
  171. if (pos.x<0) {
  172. pos.x=0;
  173. turnoff_h=true;
  174. }
  175. if (pos.x > (h_scroll->get_max()-h_scroll->get_page())) {
  176. pos.x=h_scroll->get_max()-h_scroll->get_page();
  177. turnoff_h=true;
  178. }
  179. if (pos.y<0) {
  180. pos.y=0;
  181. turnoff_v=true;
  182. }
  183. if (pos.y > (v_scroll->get_max()-v_scroll->get_page())) {
  184. pos.y=v_scroll->get_max()-v_scroll->get_page();
  185. turnoff_v=true;
  186. }
  187. if (scroll_h)
  188. h_scroll->set_val(pos.x);
  189. if (scroll_v)
  190. v_scroll->set_val(pos.y);
  191. float sgn_x = drag_speed.x<0? -1 : 1;
  192. float val_x = Math::abs(drag_speed.x);
  193. val_x-=1000*get_fixed_process_delta_time();
  194. if (val_x<0) {
  195. turnoff_h=true;
  196. }
  197. float sgn_y = drag_speed.y<0? -1 : 1;
  198. float val_y = Math::abs(drag_speed.y);
  199. val_y-=1000*get_fixed_process_delta_time();
  200. if (val_y<0) {
  201. turnoff_v=true;
  202. }
  203. drag_speed=Vector2(sgn_x*val_x,sgn_y*val_y);
  204. if (turnoff_h && turnoff_v) {
  205. set_fixed_process(false);
  206. drag_touching=false;
  207. drag_touching_deaccel=false;
  208. }
  209. } else {
  210. if (time_since_motion==0 || time_since_motion>0.1) {
  211. Vector2 diff = drag_accum - last_drag_accum;
  212. last_drag_accum=drag_accum;
  213. drag_speed=diff/get_fixed_process_delta_time();
  214. }
  215. time_since_motion+=get_fixed_process_delta_time();
  216. }
  217. }
  218. }
  219. };
  220. void ScrollContainer::update_scrollbars() {
  221. Size2 size = get_size();
  222. Size2 hmin = h_scroll->get_combined_minimum_size();
  223. Size2 vmin = v_scroll->get_combined_minimum_size();
  224. Size2 min = child_max_size;
  225. if (!scroll_v || min.height <= size.height - hmin.height) {
  226. v_scroll->hide();
  227. scroll.y=0;
  228. } else {
  229. v_scroll->show();
  230. scroll.y=v_scroll->get_val();
  231. }
  232. v_scroll->set_max(min.height);
  233. v_scroll->set_page(size.height - hmin.height);
  234. if (!scroll_h || min.width <= size.width - vmin.width) {
  235. h_scroll->hide();
  236. scroll.x=0;
  237. } else {
  238. h_scroll->show();
  239. h_scroll->set_max(min.width);
  240. h_scroll->set_page(size.width - vmin.width);
  241. scroll.x=h_scroll->get_val();
  242. }
  243. }
  244. void ScrollContainer::_scroll_moved(float) {
  245. scroll.x=h_scroll->get_val();
  246. scroll.y=v_scroll->get_val();
  247. queue_sort();
  248. update();
  249. };
  250. void ScrollContainer::set_enable_h_scroll(bool p_enable) {
  251. scroll_h=p_enable;
  252. queue_sort();
  253. }
  254. bool ScrollContainer::is_h_scroll_enabled() const{
  255. return scroll_h;
  256. }
  257. void ScrollContainer::set_enable_v_scroll(bool p_enable) {
  258. scroll_v=p_enable;
  259. queue_sort();
  260. }
  261. bool ScrollContainer::is_v_scroll_enabled() const{
  262. return scroll_v;
  263. }
  264. int ScrollContainer::get_v_scroll() const {
  265. return v_scroll->get_val();
  266. }
  267. void ScrollContainer::set_v_scroll(int p_pos) {
  268. v_scroll->set_val(p_pos);
  269. _cancel_drag();
  270. }
  271. int ScrollContainer::get_h_scroll() const {
  272. return h_scroll->get_val();
  273. }
  274. void ScrollContainer::set_h_scroll(int p_pos) {
  275. h_scroll->set_val(p_pos);
  276. _cancel_drag();
  277. }
  278. void ScrollContainer::_bind_methods() {
  279. ObjectTypeDB::bind_method(_MD("_scroll_moved"),&ScrollContainer::_scroll_moved);
  280. ObjectTypeDB::bind_method(_MD("_input_event"),&ScrollContainer::_input_event);
  281. ObjectTypeDB::bind_method(_MD("set_enable_h_scroll","enable"),&ScrollContainer::set_enable_h_scroll);
  282. ObjectTypeDB::bind_method(_MD("is_h_scroll_enabled"),&ScrollContainer::is_h_scroll_enabled);
  283. ObjectTypeDB::bind_method(_MD("set_enable_v_scroll","enable"),&ScrollContainer::set_enable_v_scroll);
  284. ObjectTypeDB::bind_method(_MD("is_v_scroll_enabled"),&ScrollContainer::is_v_scroll_enabled);
  285. ObjectTypeDB::bind_method(_MD("_update_scrollbar_pos"),&ScrollContainer::_update_scrollbar_pos);
  286. ObjectTypeDB::bind_method(_MD("set_h_scroll","val"),&ScrollContainer::set_h_scroll);
  287. ObjectTypeDB::bind_method(_MD("get_h_scroll"),&ScrollContainer::get_h_scroll);
  288. ObjectTypeDB::bind_method(_MD("set_v_scroll","val"),&ScrollContainer::set_v_scroll);
  289. ObjectTypeDB::bind_method(_MD("get_v_scroll"),&ScrollContainer::get_v_scroll);
  290. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "scroll/horizontal"), _SCS("set_enable_h_scroll"),_SCS("is_h_scroll_enabled"));
  291. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "scroll/vertical"), _SCS("set_enable_v_scroll"),_SCS("is_v_scroll_enabled"));
  292. };
  293. ScrollContainer::ScrollContainer() {
  294. h_scroll = memnew(HScrollBar);
  295. h_scroll->set_name("_h_scroll");
  296. add_child(h_scroll);
  297. v_scroll = memnew(VScrollBar);
  298. v_scroll->set_name("_v_scroll");
  299. add_child(v_scroll);
  300. h_scroll->connect("value_changed", this,"_scroll_moved");
  301. v_scroll->connect("value_changed", this,"_scroll_moved");
  302. drag_speed=Vector2();
  303. drag_touching=false;
  304. drag_touching_deaccel=false;
  305. scroll_h=true;
  306. scroll_v=true;
  307. };