Skip to content

Commit 2cd268f

Browse files
committed
Merge pull request godotengine#112441 from YeldhamDev/scrollbar_padding
Allow to add padding to `ScrollBar`s
2 parents ba44671 + 23a4cff commit 2cd268f

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

doc/classes/HScrollBar.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,14 @@
88
</description>
99
<tutorials>
1010
</tutorials>
11+
<theme_items>
12+
<theme_item name="padding_bottom" data_type="constant" type="int" default="0">
13+
Padding between the bottom of the [theme_item ScrollBar.scroll] element and the [theme_item ScrollBar.grabber].
14+
[b]Note:[/b] To apply horizontal padding, modify the left/right content margins of [theme_item ScrollBar.scroll] instead.
15+
</theme_item>
16+
<theme_item name="padding_top" data_type="constant" type="int" default="0">
17+
Padding between the top of the [theme_item ScrollBar.scroll] element and the [theme_item ScrollBar.grabber].
18+
[b]Note:[/b] To apply horizontal padding, modify the left/right content margins of [theme_item ScrollBar.scroll] instead.
19+
</theme_item>
20+
</theme_items>
1121
</class>

doc/classes/VScrollBar.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@
1212
<member name="size_flags_horizontal" type="int" setter="set_h_size_flags" getter="get_h_size_flags" overrides="Control" enum="Control.SizeFlags" is_bitfield="true" default="0" />
1313
<member name="size_flags_vertical" type="int" setter="set_v_size_flags" getter="get_v_size_flags" overrides="Control" enum="Control.SizeFlags" is_bitfield="true" default="1" />
1414
</members>
15+
<theme_items>
16+
<theme_item name="padding_left" data_type="constant" type="int" default="0">
17+
Padding between the left of the [theme_item ScrollBar.scroll] element and the [theme_item ScrollBar.grabber].
18+
[b]Note:[/b] To apply vertical padding, modify the top/bottom content margins of [theme_item ScrollBar.scroll] instead.
19+
</theme_item>
20+
<theme_item name="padding_right" data_type="constant" type="int" default="0">
21+
Padding between the right of the [theme_item ScrollBar.scroll] element and the [theme_item ScrollBar.grabber].
22+
[b]Note:[/b] To apply vertical padding, modify the top/bottom content margins of [theme_item ScrollBar.scroll] instead.
23+
</theme_item>
24+
</theme_items>
1525
</class>

scene/gui/scroll_bar.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,15 +295,19 @@ void ScrollBar::_notification(int p_what) {
295295
Rect2 grabber_rect;
296296

297297
if (orientation == HORIZONTAL) {
298+
int padding_top = MAX(theme_cache.padding_top, 0);
299+
int padding_bottom = MAX(theme_cache.padding_bottom, 0);
298300
grabber_rect.size.width = get_grabber_size();
299-
grabber_rect.size.height = get_size().height;
300-
grabber_rect.position.y = 0;
301+
grabber_rect.size.height = get_size().height - padding_top - padding_bottom;
302+
grabber_rect.position.y = padding_top;
301303
grabber_rect.position.x = get_grabber_offset() + decr->get_width() + theme_cache.scroll_style->get_margin(SIDE_LEFT);
302304
} else {
303-
grabber_rect.size.width = get_size().width;
305+
int padding_left = MAX(theme_cache.padding_left, 0);
306+
int padding_right = MAX(theme_cache.padding_right, 0);
307+
grabber_rect.size.width = get_size().width - padding_left - padding_right;
304308
grabber_rect.size.height = get_grabber_size();
305309
grabber_rect.position.y = get_grabber_offset() + decr->get_height() + theme_cache.scroll_style->get_margin(SIDE_TOP);
306-
grabber_rect.position.x = 0;
310+
grabber_rect.position.x = padding_left;
307311
}
308312

309313
grabber->draw(ci, grabber_rect);
@@ -488,15 +492,21 @@ Size2 ScrollBar::get_minimum_size() const {
488492
Size2 minsize;
489493

490494
if (orientation == VERTICAL) {
495+
int padding_left = MAX(theme_cache.padding_left, 0);
496+
int padding_right = MAX(theme_cache.padding_right, 0);
491497
minsize.width = MAX(incr->get_size().width, bg->get_minimum_size().width);
498+
minsize.width += padding_left + padding_right;
492499
minsize.height += incr->get_size().height;
493500
minsize.height += decr->get_size().height;
494501
minsize.height += bg->get_minimum_size().height;
495502
minsize.height += get_grabber_min_size();
496503
}
497504

498505
if (orientation == HORIZONTAL) {
506+
int padding_top = MAX(theme_cache.padding_top, 0);
507+
int padding_bottom = MAX(theme_cache.padding_bottom, 0);
499508
minsize.height = MAX(incr->get_size().height, bg->get_minimum_size().height);
509+
minsize.height += padding_top + padding_bottom;
500510
minsize.width += incr->get_size().width;
501511
minsize.width += decr->get_size().width;
502512
minsize.width += bg->get_minimum_size().width;
@@ -669,3 +679,13 @@ ScrollBar::ScrollBar(Orientation p_orientation) {
669679

670680
ScrollBar::~ScrollBar() {
671681
}
682+
683+
void VScrollBar::_bind_methods() {
684+
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, VScrollBar, padding_left);
685+
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, VScrollBar, padding_right);
686+
}
687+
688+
void HScrollBar::_bind_methods() {
689+
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, HScrollBar, padding_top);
690+
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, HScrollBar, padding_bottom);
691+
}

scene/gui/scroll_bar.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ class ScrollBar : public Range {
8484
double target_scroll = 0.0;
8585
bool smooth_scroll_enabled = false;
8686

87+
void _drag_node_exit();
88+
void _drag_node_input(const Ref<InputEvent> &p_input);
89+
90+
virtual void gui_input(const Ref<InputEvent> &p_event) override;
91+
92+
protected:
8793
struct ThemeCache {
8894
Ref<StyleBox> scroll_style;
8995
Ref<StyleBox> scroll_focus_style;
@@ -98,14 +104,13 @@ class ScrollBar : public Range {
98104
Ref<Texture2D> decrement_icon;
99105
Ref<Texture2D> decrement_hl_icon;
100106
Ref<Texture2D> decrement_pressed_icon;
101-
} theme_cache;
102-
103-
void _drag_node_exit();
104-
void _drag_node_input(const Ref<InputEvent> &p_input);
105107

106-
virtual void gui_input(const Ref<InputEvent> &p_event) override;
108+
int padding_left = 0;
109+
int padding_top = 0;
110+
int padding_right = 0;
111+
int padding_bottom = 0;
112+
} theme_cache;
107113

108-
protected:
109114
void _notification(int p_what);
110115
static void _bind_methods();
111116

@@ -133,6 +138,9 @@ class ScrollBar : public Range {
133138
class HScrollBar : public ScrollBar {
134139
GDCLASS(HScrollBar, ScrollBar);
135140

141+
protected:
142+
static void _bind_methods();
143+
136144
public:
137145
HScrollBar() :
138146
ScrollBar(HORIZONTAL) { set_v_size_flags(0); }
@@ -141,6 +149,9 @@ class HScrollBar : public ScrollBar {
141149
class VScrollBar : public ScrollBar {
142150
GDCLASS(VScrollBar, ScrollBar);
143151

152+
protected:
153+
static void _bind_methods();
154+
144155
public:
145156
VScrollBar() :
146157
ScrollBar(VERTICAL) { set_h_size_flags(0); }

0 commit comments

Comments
 (0)