2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* scroll_container.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2014-02-10 02:10:30 +01:00
/*************************************************************************/
2019-01-01 12:53:14 +01:00
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 02:10:30 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-05 00:50:27 +01:00
2014-02-10 02:10:30 +01:00
# include "scroll_container.h"
2018-09-11 18:13:45 +02:00
# include "core/os/os.h"
2018-09-26 16:35:32 +02:00
2014-02-10 02:10:30 +01:00
bool ScrollContainer : : clips_input ( ) const {
return true ;
}
Size2 ScrollContainer : : get_minimum_size ( ) const {
2018-05-15 22:12:35 +02:00
Ref < StyleBox > sb = get_stylebox ( " bg " ) ;
2016-03-12 14:44:12 +01:00
Size2 min_size ;
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < get_child_count ( ) ; i + + ) {
2016-03-12 14:44:12 +01:00
2017-08-24 22:58:51 +02:00
Control * c = Object : : cast_to < Control > ( get_child ( i ) ) ;
2016-03-12 14:44:12 +01:00
if ( ! c )
continue ;
if ( c - > is_set_as_toplevel ( ) )
continue ;
if ( c = = h_scroll | | c = = v_scroll )
continue ;
Size2 minsize = c - > get_combined_minimum_size ( ) ;
if ( ! scroll_h ) {
min_size . x = MAX ( min_size . x , minsize . x ) ;
}
if ( ! scroll_v ) {
min_size . y = MAX ( min_size . y , minsize . y ) ;
}
}
2017-01-13 14:45:50 +01:00
if ( h_scroll - > is_visible_in_tree ( ) ) {
2017-03-05 16:44:50 +01:00
min_size . y + = h_scroll - > get_minimum_size ( ) . y ;
2016-03-12 14:44:12 +01:00
}
2017-01-13 14:45:50 +01:00
if ( v_scroll - > is_visible_in_tree ( ) ) {
2017-03-05 16:44:50 +01:00
min_size . x + = v_scroll - > get_minimum_size ( ) . x ;
2016-03-12 14:44:12 +01:00
}
2018-05-15 22:12:35 +02:00
min_size + = sb - > get_minimum_size ( ) ;
2016-03-12 14:44:12 +01:00
return min_size ;
2018-05-15 22:12:35 +02:00
}
2014-02-10 02:10:30 +01:00
void ScrollContainer : : _cancel_drag ( ) {
2018-04-11 09:28:14 +02:00
set_physics_process_internal ( false ) ;
2017-03-05 16:44:50 +01:00
drag_touching_deaccel = false ;
drag_touching = false ;
drag_speed = Vector2 ( ) ;
drag_accum = Vector2 ( ) ;
last_drag_accum = Vector2 ( ) ;
drag_from = Vector2 ( ) ;
2018-02-27 16:37:20 +01:00
if ( beyond_deadzone ) {
emit_signal ( " scroll_ended " ) ;
propagate_notification ( NOTIFICATION_SCROLL_END ) ;
beyond_deadzone = false ;
}
2014-02-10 02:10:30 +01:00
}
2017-05-20 17:38:03 +02:00
void ScrollContainer : : _gui_input ( const Ref < InputEvent > & p_gui_input ) {
2014-02-10 02:10:30 +01:00
2019-05-21 18:53:29 +02:00
double prev_v_scroll = v_scroll - > get_value ( ) ;
double prev_h_scroll = h_scroll - > get_value ( ) ;
2017-05-20 17:38:03 +02:00
Ref < InputEventMouseButton > mb = p_gui_input ;
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
if ( mb . is_valid ( ) ) {
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
if ( mb - > get_button_index ( ) = = BUTTON_WHEEL_UP & & mb - > is_pressed ( ) ) {
// only horizontal is enabled, scroll horizontally
2019-05-21 18:53:29 +02:00
if ( h_scroll - > is_visible ( ) & & ( ! v_scroll - > is_visible ( ) | | mb - > get_shift ( ) ) ) {
2017-05-20 17:38:03 +02:00
h_scroll - > set_value ( h_scroll - > get_value ( ) - h_scroll - > get_page ( ) / 8 * mb - > get_factor ( ) ) ;
} else if ( v_scroll - > is_visible_in_tree ( ) ) {
v_scroll - > set_value ( v_scroll - > get_value ( ) - v_scroll - > get_page ( ) / 8 * mb - > get_factor ( ) ) ;
2014-02-10 02:10:30 +01:00
}
2017-05-20 17:38:03 +02:00
}
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
if ( mb - > get_button_index ( ) = = BUTTON_WHEEL_DOWN & & mb - > is_pressed ( ) ) {
// only horizontal is enabled, scroll horizontally
2019-05-21 18:53:29 +02:00
if ( h_scroll - > is_visible ( ) & & ( ! v_scroll - > is_visible ( ) | | mb - > get_shift ( ) ) ) {
2017-05-20 17:38:03 +02:00
h_scroll - > set_value ( h_scroll - > get_value ( ) + h_scroll - > get_page ( ) / 8 * mb - > get_factor ( ) ) ;
} else if ( v_scroll - > is_visible ( ) ) {
v_scroll - > set_value ( v_scroll - > get_value ( ) + v_scroll - > get_page ( ) / 8 * mb - > get_factor ( ) ) ;
2017-02-21 23:45:31 +01:00
}
2017-05-20 17:38:03 +02:00
}
2017-02-21 23:45:31 +01:00
2017-05-20 17:38:03 +02:00
if ( mb - > get_button_index ( ) = = BUTTON_WHEEL_LEFT & & mb - > is_pressed ( ) ) {
if ( h_scroll - > is_visible_in_tree ( ) ) {
h_scroll - > set_value ( h_scroll - > get_value ( ) - h_scroll - > get_page ( ) * mb - > get_factor ( ) / 8 ) ;
2017-02-21 23:45:31 +01:00
}
2017-05-20 17:38:03 +02:00
}
2017-02-21 23:45:31 +01:00
2017-05-20 17:38:03 +02:00
if ( mb - > get_button_index ( ) = = BUTTON_WHEEL_RIGHT & & mb - > is_pressed ( ) ) {
if ( h_scroll - > is_visible_in_tree ( ) ) {
h_scroll - > set_value ( h_scroll - > get_value ( ) + h_scroll - > get_page ( ) * mb - > get_factor ( ) / 8 ) ;
2014-02-10 02:10:30 +01:00
}
2017-05-20 17:38:03 +02:00
}
2014-02-10 02:10:30 +01:00
2019-05-21 18:53:29 +02:00
if ( v_scroll - > get_value ( ) ! = prev_v_scroll | | h_scroll - > get_value ( ) ! = prev_h_scroll )
accept_event ( ) ; //accept event if scroll changed
2017-05-20 17:38:03 +02:00
if ( ! OS : : get_singleton ( ) - > has_touchscreen_ui_hint ( ) )
return ;
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
if ( mb - > get_button_index ( ) ! = BUTTON_LEFT )
return ;
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
if ( mb - > is_pressed ( ) ) {
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
if ( drag_touching ) {
2018-02-27 16:37:20 +01:00
_cancel_drag ( ) ;
2017-05-20 17:38:03 +02:00
}
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
if ( true ) {
drag_speed = Vector2 ( ) ;
drag_accum = Vector2 ( ) ;
last_drag_accum = Vector2 ( ) ;
drag_from = Vector2 ( h_scroll - > get_value ( ) , v_scroll - > get_value ( ) ) ;
drag_touching = OS : : get_singleton ( ) - > has_touchscreen_ui_hint ( ) ;
drag_touching_deaccel = false ;
2018-02-27 16:37:20 +01:00
beyond_deadzone = false ;
2017-05-20 17:38:03 +02:00
time_since_motion = 0 ;
if ( drag_touching ) {
2018-04-11 09:28:14 +02:00
set_physics_process_internal ( true ) ;
2017-03-05 16:44:50 +01:00
time_since_motion = 0 ;
2014-02-10 02:10:30 +01:00
}
2017-05-20 17:38:03 +02:00
}
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
} else {
if ( drag_touching ) {
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
if ( drag_speed = = Vector2 ( ) ) {
2018-02-27 16:37:20 +01:00
_cancel_drag ( ) ;
2017-05-20 17:38:03 +02:00
} else {
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
drag_touching_deaccel = true ;
2014-02-10 02:10:30 +01:00
}
}
2017-05-20 17:38:03 +02:00
}
}
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
Ref < InputEventMouseMotion > mm = p_gui_input ;
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
if ( mm . is_valid ( ) ) {
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
if ( drag_touching & & ! drag_touching_deaccel ) {
2014-02-10 02:10:30 +01:00
2017-05-20 17:38:03 +02:00
Vector2 motion = Vector2 ( mm - > get_relative ( ) . x , mm - > get_relative ( ) . y ) ;
drag_accum - = motion ;
2018-02-27 16:37:20 +01:00
2018-09-26 16:35:32 +02:00
if ( beyond_deadzone | | ( scroll_h & & Math : : abs ( drag_accum . x ) > deadzone ) | | ( scroll_v & & Math : : abs ( drag_accum . y ) > deadzone ) ) {
2018-02-27 16:37:20 +01:00
if ( ! beyond_deadzone ) {
propagate_notification ( NOTIFICATION_SCROLL_BEGIN ) ;
emit_signal ( " scroll_started " ) ;
beyond_deadzone = true ;
// resetting drag_accum here ensures smooth scrolling after reaching deadzone
drag_accum = - motion ;
}
Vector2 diff = drag_from + drag_accum ;
if ( scroll_h )
h_scroll - > set_value ( diff . x ) ;
else
drag_accum . x = 0 ;
if ( scroll_v )
v_scroll - > set_value ( diff . y ) ;
else
drag_accum . y = 0 ;
time_since_motion = 0 ;
}
2017-05-20 17:38:03 +02:00
}
2014-02-10 02:10:30 +01:00
}
2017-11-01 21:49:39 +01:00
Ref < InputEventPanGesture > pan_gesture = p_gui_input ;
if ( pan_gesture . is_valid ( ) ) {
if ( h_scroll - > is_visible_in_tree ( ) ) {
h_scroll - > set_value ( h_scroll - > get_value ( ) + h_scroll - > get_page ( ) * pan_gesture - > get_delta ( ) . x / 8 ) ;
}
if ( v_scroll - > is_visible_in_tree ( ) ) {
v_scroll - > set_value ( v_scroll - > get_value ( ) + v_scroll - > get_page ( ) * pan_gesture - > get_delta ( ) . y / 8 ) ;
}
}
2019-05-21 18:53:29 +02:00
if ( v_scroll - > get_value ( ) ! = prev_v_scroll | | h_scroll - > get_value ( ) ! = prev_h_scroll )
accept_event ( ) ; //accept event if scroll changed
2014-02-10 02:10:30 +01:00
}
2017-09-10 15:37:49 +02:00
void ScrollContainer : : _update_scrollbar_position ( ) {
2014-02-10 02:10:30 +01:00
Size2 hmin = h_scroll - > get_combined_minimum_size ( ) ;
Size2 vmin = v_scroll - > get_combined_minimum_size ( ) ;
2017-07-06 09:16:27 +02:00
v_scroll - > set_anchor_and_margin ( MARGIN_LEFT , ANCHOR_END , - vmin . width ) ;
2017-03-05 16:44:50 +01:00
v_scroll - > set_anchor_and_margin ( MARGIN_RIGHT , ANCHOR_END , 0 ) ;
v_scroll - > set_anchor_and_margin ( MARGIN_TOP , ANCHOR_BEGIN , 0 ) ;
v_scroll - > set_anchor_and_margin ( MARGIN_BOTTOM , ANCHOR_END , 0 ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
h_scroll - > set_anchor_and_margin ( MARGIN_LEFT , ANCHOR_BEGIN , 0 ) ;
h_scroll - > set_anchor_and_margin ( MARGIN_RIGHT , ANCHOR_END , 0 ) ;
2017-07-06 09:16:27 +02:00
h_scroll - > set_anchor_and_margin ( MARGIN_TOP , ANCHOR_END , - hmin . height ) ;
2017-03-05 16:44:50 +01:00
h_scroll - > set_anchor_and_margin ( MARGIN_BOTTOM , ANCHOR_END , 0 ) ;
2014-02-10 02:10:30 +01:00
h_scroll - > raise ( ) ;
v_scroll - > raise ( ) ;
}
void ScrollContainer : : _notification ( int p_what ) {
2014-11-06 01:20:42 +01:00
if ( p_what = = NOTIFICATION_ENTER_TREE | | p_what = = NOTIFICATION_THEME_CHANGED ) {
2014-02-10 02:10:30 +01:00
2017-09-10 15:37:49 +02:00
call_deferred ( " _update_scrollbar_position " ) ;
2014-02-10 02:10:30 +01:00
} ;
2017-03-05 16:44:50 +01:00
if ( p_what = = NOTIFICATION_SORT_CHILDREN ) {
2014-02-10 02:10:30 +01:00
child_max_size = Size2 ( 0 , 0 ) ;
Size2 size = get_size ( ) ;
2018-05-15 22:12:35 +02:00
Point2 ofs ;
Ref < StyleBox > sb = get_stylebox ( " bg " ) ;
size - = sb - > get_minimum_size ( ) ;
ofs + = sb - > get_offset ( ) ;
2018-06-07 17:46:14 +02:00
if ( h_scroll - > is_visible_in_tree ( ) & & h_scroll - > get_parent ( ) = = this ) //scrolls may have been moved out for reasons
2017-03-05 16:44:50 +01:00
size . y - = h_scroll - > get_minimum_size ( ) . y ;
2016-03-12 14:44:12 +01:00
2018-06-07 17:46:14 +02:00
if ( v_scroll - > is_visible_in_tree ( ) & & v_scroll - > get_parent ( ) = = this ) //scrolls may have been moved out for reasons
2018-11-16 02:47:43 +01:00
size . x - = v_scroll - > get_minimum_size ( ) . x ;
2016-03-12 14:44:12 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < get_child_count ( ) ; i + + ) {
2014-02-10 02:10:30 +01:00
2017-08-24 22:58:51 +02:00
Control * c = Object : : cast_to < Control > ( get_child ( i ) ) ;
2014-02-10 02:10:30 +01:00
if ( ! c )
continue ;
if ( c - > is_set_as_toplevel ( ) )
continue ;
if ( c = = h_scroll | | c = = v_scroll )
continue ;
Size2 minsize = c - > get_combined_minimum_size ( ) ;
child_max_size . x = MAX ( child_max_size . x , minsize . x ) ;
child_max_size . y = MAX ( child_max_size . y , minsize . y ) ;
2017-03-05 16:44:50 +01:00
Rect2 r = Rect2 ( - scroll , minsize ) ;
if ( ! scroll_h | | ( ! h_scroll - > is_visible_in_tree ( ) & & c - > get_h_size_flags ( ) & SIZE_EXPAND ) ) {
2017-06-04 00:25:13 +02:00
r . position . x = 0 ;
2017-03-05 16:44:50 +01:00
if ( c - > get_h_size_flags ( ) & SIZE_EXPAND )
r . size . width = MAX ( size . width , minsize . width ) ;
2015-10-26 00:08:18 +01:00
else
2017-03-05 16:44:50 +01:00
r . size . width = minsize . width ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
if ( ! scroll_v | | ( ! v_scroll - > is_visible_in_tree ( ) & & c - > get_v_size_flags ( ) & SIZE_EXPAND ) ) {
2017-06-04 00:25:13 +02:00
r . position . y = 0 ;
2017-03-05 16:44:50 +01:00
if ( c - > get_v_size_flags ( ) & SIZE_EXPAND )
r . size . height = MAX ( size . height , minsize . height ) ;
2015-10-26 00:08:18 +01:00
else
2017-03-05 16:44:50 +01:00
r . size . height = minsize . height ;
2014-02-10 02:10:30 +01:00
}
2018-05-15 22:12:35 +02:00
r . position + = ofs ;
2017-03-05 16:44:50 +01:00
fit_child_in_rect ( c , r ) ;
2014-02-10 02:10:30 +01:00
}
update ( ) ;
} ;
if ( p_what = = NOTIFICATION_DRAW ) {
2018-05-15 22:12:35 +02:00
Ref < StyleBox > sb = get_stylebox ( " bg " ) ;
draw_style_box ( sb , Rect2 ( Vector2 ( ) , get_size ( ) ) ) ;
2014-02-10 02:10:30 +01:00
update_scrollbars ( ) ;
}
2018-04-11 09:28:14 +02:00
if ( p_what = = NOTIFICATION_INTERNAL_PHYSICS_PROCESS ) {
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( drag_touching ) {
2014-02-10 02:10:30 +01:00
if ( drag_touching_deaccel ) {
2017-03-05 16:44:50 +01:00
Vector2 pos = Vector2 ( h_scroll - > get_value ( ) , v_scroll - > get_value ( ) ) ;
2017-09-30 16:19:07 +02:00
pos + = drag_speed * get_physics_process_delta_time ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
bool turnoff_h = false ;
bool turnoff_v = false ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( pos . x < 0 ) {
pos . x = 0 ;
turnoff_h = true ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
if ( pos . x > ( h_scroll - > get_max ( ) - h_scroll - > get_page ( ) ) ) {
pos . x = h_scroll - > get_max ( ) - h_scroll - > get_page ( ) ;
turnoff_h = true ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
if ( pos . y < 0 ) {
pos . y = 0 ;
turnoff_v = true ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
if ( pos . y > ( v_scroll - > get_max ( ) - v_scroll - > get_page ( ) ) ) {
pos . y = v_scroll - > get_max ( ) - v_scroll - > get_page ( ) ;
turnoff_v = true ;
2014-02-10 02:10:30 +01:00
}
if ( scroll_h )
2017-01-04 05:16:14 +01:00
h_scroll - > set_value ( pos . x ) ;
2014-02-10 02:10:30 +01:00
if ( scroll_v )
2017-01-04 05:16:14 +01:00
v_scroll - > set_value ( pos . y ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
float sgn_x = drag_speed . x < 0 ? - 1 : 1 ;
2014-02-10 02:10:30 +01:00
float val_x = Math : : abs ( drag_speed . x ) ;
2017-09-30 16:19:07 +02:00
val_x - = 1000 * get_physics_process_delta_time ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( val_x < 0 ) {
turnoff_h = true ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
float sgn_y = drag_speed . y < 0 ? - 1 : 1 ;
2014-02-10 02:10:30 +01:00
float val_y = Math : : abs ( drag_speed . y ) ;
2017-09-30 16:19:07 +02:00
val_y - = 1000 * get_physics_process_delta_time ( ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
if ( val_y < 0 ) {
turnoff_v = true ;
2014-02-10 02:10:30 +01:00
}
2017-03-05 16:44:50 +01:00
drag_speed = Vector2 ( sgn_x * val_x , sgn_y * val_y ) ;
2014-02-10 02:10:30 +01:00
if ( turnoff_h & & turnoff_v ) {
2018-02-27 16:37:20 +01:00
_cancel_drag ( ) ;
2014-02-10 02:10:30 +01:00
}
} else {
2017-03-05 16:44:50 +01:00
if ( time_since_motion = = 0 | | time_since_motion > 0.1 ) {
2014-02-10 02:10:30 +01:00
Vector2 diff = drag_accum - last_drag_accum ;
2017-03-05 16:44:50 +01:00
last_drag_accum = drag_accum ;
2017-09-30 16:19:07 +02:00
drag_speed = diff / get_physics_process_delta_time ( ) ;
2014-02-10 02:10:30 +01:00
}
2017-09-30 16:19:07 +02:00
time_since_motion + = get_physics_process_delta_time ( ) ;
2014-02-10 02:10:30 +01:00
}
}
}
} ;
void ScrollContainer : : update_scrollbars ( ) {
Size2 size = get_size ( ) ;
2018-05-15 22:12:35 +02:00
Ref < StyleBox > sb = get_stylebox ( " bg " ) ;
size - = sb - > get_minimum_size ( ) ;
2014-02-10 02:10:30 +01:00
2018-08-28 15:41:33 +02:00
Size2 hmin ;
Size2 vmin ;
if ( scroll_h ) hmin = h_scroll - > get_combined_minimum_size ( ) ;
if ( scroll_v ) vmin = v_scroll - > get_combined_minimum_size ( ) ;
2014-02-10 02:10:30 +01:00
Size2 min = child_max_size ;
if ( ! scroll_v | | min . height < = size . height - hmin . height ) {
v_scroll - > hide ( ) ;
2017-12-06 05:00:46 +01:00
v_scroll - > set_max ( 0 ) ;
2017-03-05 16:44:50 +01:00
scroll . y = 0 ;
2014-02-10 02:10:30 +01:00
} else {
v_scroll - > show ( ) ;
2017-11-13 15:11:08 +01:00
v_scroll - > set_max ( min . height ) ;
v_scroll - > set_page ( size . height - hmin . height ) ;
2017-03-05 16:44:50 +01:00
scroll . y = v_scroll - > get_value ( ) ;
2014-02-10 02:10:30 +01:00
}
if ( ! scroll_h | | min . width < = size . width - vmin . width ) {
h_scroll - > hide ( ) ;
2017-12-06 05:00:46 +01:00
h_scroll - > set_max ( 0 ) ;
2017-03-05 16:44:50 +01:00
scroll . x = 0 ;
2014-02-10 02:10:30 +01:00
} else {
h_scroll - > show ( ) ;
h_scroll - > set_max ( min . width ) ;
h_scroll - > set_page ( size . width - vmin . width ) ;
2017-03-05 16:44:50 +01:00
scroll . x = h_scroll - > get_value ( ) ;
2014-02-10 02:10:30 +01:00
}
}
void ScrollContainer : : _scroll_moved ( float ) {
2017-03-05 16:44:50 +01:00
scroll . x = h_scroll - > get_value ( ) ;
scroll . y = v_scroll - > get_value ( ) ;
2014-02-10 02:10:30 +01:00
queue_sort ( ) ;
update ( ) ;
} ;
void ScrollContainer : : set_enable_h_scroll ( bool p_enable ) {
2017-03-05 16:44:50 +01:00
scroll_h = p_enable ;
2014-02-10 02:10:30 +01:00
queue_sort ( ) ;
}
2017-03-05 16:44:50 +01:00
bool ScrollContainer : : is_h_scroll_enabled ( ) const {
2014-02-10 02:10:30 +01:00
return scroll_h ;
}
void ScrollContainer : : set_enable_v_scroll ( bool p_enable ) {
2017-03-05 16:44:50 +01:00
scroll_v = p_enable ;
2014-02-10 02:10:30 +01:00
queue_sort ( ) ;
}
2017-03-05 16:44:50 +01:00
bool ScrollContainer : : is_v_scroll_enabled ( ) const {
2014-02-10 02:10:30 +01:00
return scroll_v ;
}
int ScrollContainer : : get_v_scroll ( ) const {
2017-01-04 05:16:14 +01:00
return v_scroll - > get_value ( ) ;
2014-02-10 02:10:30 +01:00
}
void ScrollContainer : : set_v_scroll ( int p_pos ) {
2017-01-04 05:16:14 +01:00
v_scroll - > set_value ( p_pos ) ;
2014-02-10 02:10:30 +01:00
_cancel_drag ( ) ;
}
int ScrollContainer : : get_h_scroll ( ) const {
2017-01-04 05:16:14 +01:00
return h_scroll - > get_value ( ) ;
2014-02-10 02:10:30 +01:00
}
void ScrollContainer : : set_h_scroll ( int p_pos ) {
2017-01-04 05:16:14 +01:00
h_scroll - > set_value ( p_pos ) ;
2014-02-10 02:10:30 +01:00
_cancel_drag ( ) ;
}
2018-02-27 16:37:20 +01:00
int ScrollContainer : : get_deadzone ( ) const {
return deadzone ;
}
void ScrollContainer : : set_deadzone ( int p_deadzone ) {
deadzone = p_deadzone ;
}
2017-01-09 19:50:08 +01:00
String ScrollContainer : : get_configuration_warning ( ) const {
2017-03-05 16:44:50 +01:00
int found = 0 ;
2017-01-09 19:50:08 +01:00
2017-03-05 16:44:50 +01:00
for ( int i = 0 ; i < get_child_count ( ) ; i + + ) {
2017-01-09 19:50:08 +01:00
2017-08-24 22:58:51 +02:00
Control * c = Object : : cast_to < Control > ( get_child ( i ) ) ;
2017-01-09 19:50:08 +01:00
if ( ! c )
continue ;
if ( c - > is_set_as_toplevel ( ) )
continue ;
if ( c = = h_scroll | | c = = v_scroll )
continue ;
found + + ;
}
2017-03-05 16:44:50 +01:00
if ( found ! = 1 )
2017-01-09 19:50:08 +01:00
return TTR ( " ScrollContainer is intended to work with a single child control. \n Use a container as child (VBox,HBox,etc) , or a Control and set the custom minimum size manually . " ) ;
else
return " " ;
}
2018-06-07 17:46:14 +02:00
HScrollBar * ScrollContainer : : get_h_scrollbar ( ) {
return h_scroll ;
}
VScrollBar * ScrollContainer : : get_v_scrollbar ( ) {
return v_scroll ;
}
2014-02-10 02:10:30 +01:00
void ScrollContainer : : _bind_methods ( ) {
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " _scroll_moved " ) , & ScrollContainer : : _scroll_moved ) ;
ClassDB : : bind_method ( D_METHOD ( " _gui_input " ) , & ScrollContainer : : _gui_input ) ;
ClassDB : : bind_method ( D_METHOD ( " set_enable_h_scroll " , " enable " ) , & ScrollContainer : : set_enable_h_scroll ) ;
ClassDB : : bind_method ( D_METHOD ( " is_h_scroll_enabled " ) , & ScrollContainer : : is_h_scroll_enabled ) ;
ClassDB : : bind_method ( D_METHOD ( " set_enable_v_scroll " , " enable " ) , & ScrollContainer : : set_enable_v_scroll ) ;
ClassDB : : bind_method ( D_METHOD ( " is_v_scroll_enabled " ) , & ScrollContainer : : is_v_scroll_enabled ) ;
2017-09-10 15:37:49 +02:00
ClassDB : : bind_method ( D_METHOD ( " _update_scrollbar_position " ) , & ScrollContainer : : _update_scrollbar_position ) ;
2018-01-17 10:43:23 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_h_scroll " , " value " ) , & ScrollContainer : : set_h_scroll ) ;
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " get_h_scroll " ) , & ScrollContainer : : get_h_scroll ) ;
2018-01-17 10:43:23 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_v_scroll " , " value " ) , & ScrollContainer : : set_v_scroll ) ;
2017-03-05 16:44:50 +01:00
ClassDB : : bind_method ( D_METHOD ( " get_v_scroll " ) , & ScrollContainer : : get_v_scroll ) ;
2018-02-27 16:37:20 +01:00
ClassDB : : bind_method ( D_METHOD ( " set_deadzone " , " deadzone " ) , & ScrollContainer : : set_deadzone ) ;
ClassDB : : bind_method ( D_METHOD ( " get_deadzone " ) , & ScrollContainer : : get_deadzone ) ;
2018-06-07 17:46:14 +02:00
ClassDB : : bind_method ( D_METHOD ( " get_h_scrollbar " ) , & ScrollContainer : : get_h_scrollbar ) ;
ClassDB : : bind_method ( D_METHOD ( " get_v_scrollbar " ) , & ScrollContainer : : get_v_scrollbar ) ;
2018-02-27 16:37:20 +01:00
ADD_SIGNAL ( MethodInfo ( " scroll_started " ) ) ;
ADD_SIGNAL ( MethodInfo ( " scroll_ended " ) ) ;
2017-03-05 16:44:50 +01:00
ADD_GROUP ( " Scroll " , " scroll_ " ) ;
2018-01-11 23:35:12 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " scroll_horizontal_enabled " ) , " set_enable_h_scroll " , " is_h_scroll_enabled " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " scroll_horizontal " ) , " set_h_scroll " , " get_h_scroll " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : BOOL , " scroll_vertical_enabled " ) , " set_enable_v_scroll " , " is_v_scroll_enabled " ) ;
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " scroll_vertical " ) , " set_v_scroll " , " get_v_scroll " ) ;
2018-02-27 16:37:20 +01:00
ADD_PROPERTY ( PropertyInfo ( Variant : : INT , " scroll_deadzone " ) , " set_deadzone " , " get_deadzone " ) ;
GLOBAL_DEF ( " gui/common/default_scroll_deadzone " , 0 ) ;
2014-02-10 02:10:30 +01:00
} ;
ScrollContainer : : ScrollContainer ( ) {
h_scroll = memnew ( HScrollBar ) ;
h_scroll - > set_name ( " _h_scroll " ) ;
add_child ( h_scroll ) ;
v_scroll = memnew ( VScrollBar ) ;
v_scroll - > set_name ( " _v_scroll " ) ;
add_child ( v_scroll ) ;
2017-03-05 16:44:50 +01:00
h_scroll - > connect ( " value_changed " , this , " _scroll_moved " ) ;
v_scroll - > connect ( " value_changed " , this , " _scroll_moved " ) ;
2014-02-10 02:10:30 +01:00
2017-03-05 16:44:50 +01:00
drag_speed = Vector2 ( ) ;
drag_touching = false ;
drag_touching_deaccel = false ;
2018-02-27 16:37:20 +01:00
beyond_deadzone = false ;
2017-03-05 16:44:50 +01:00
scroll_h = true ;
scroll_v = true ;
2014-02-10 02:10:30 +01:00
2018-02-27 16:37:20 +01:00
deadzone = GLOBAL_GET ( " gui/common/default_scroll_deadzone " ) ;
2017-01-09 19:50:08 +01:00
set_clip_contents ( true ) ;
2014-02-10 02:10:30 +01:00
} ;