Skip to content

Commit aee88a7

Browse files
authored
Merge pull request #46208 from floppyhammer/AddFillModeToProgressBar
2 parents afc4973 + f76d417 commit aee88a7

File tree

3 files changed

+86
-8
lines changed

3 files changed

+86
-8
lines changed

doc/classes/ProgressBar.xml

+17
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,27 @@
99
<tutorials>
1010
</tutorials>
1111
<members>
12+
<member name="fill_mode" type="int" setter="set_fill_mode" getter="get_fill_mode" default="0">
13+
The fill direction. See [enum FillMode] for possible values.
14+
</member>
1215
<member name="percent_visible" type="bool" setter="set_percent_visible" getter="is_percent_visible" default="true">
1316
If [code]true[/code], the fill percentage is displayed on the bar.
1417
</member>
1518
</members>
19+
<constants>
20+
<constant name="FILL_BEGIN_TO_END" value="0" enum="FillMode">
21+
The progress bar fills from begin to end horizontally, according to the language direction. If [method Control.is_layout_rtl] returns [code]false[/code], it fills from left to right, and if it returns [code]true[/code], it fills from right to left.
22+
</constant>
23+
<constant name="FILL_END_TO_BEGIN" value="1" enum="FillMode">
24+
The progress bar fills from end to begin horizontally, according to the language direction. If [method Control.is_layout_rtl] returns [code]false[/code], it fills from right to left, and if it returns [code]true[/code], it fills from left to right.
25+
</constant>
26+
<constant name="FILL_TOP_TO_BOTTOM" value="2" enum="FillMode">
27+
The progress fills from top to bottom.
28+
</constant>
29+
<constant name="FILL_BOTTOM_TO_TOP" value="3" enum="FillMode">
30+
The progress fills from bottom to top.
31+
</constant>
32+
</constants>
1633
<theme_items>
1734
<theme_item name="font_color" data_type="color" type="Color" default="Color(0.95, 0.95, 0.95, 1)">
1835
The color of the text.

scene/gui/progress_bar.cpp

+53-8
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,42 @@ void ProgressBar::_notification(int p_what) {
6262
Color font_color = get_theme_color(SNAME("font_color"));
6363

6464
draw_style_box(bg, Rect2(Point2(), get_size()));
65+
6566
float r = get_as_ratio();
66-
int mp = fg->get_minimum_size().width;
67-
int p = r * (get_size().width - mp);
68-
if (p > 0) {
69-
if (is_layout_rtl()) {
70-
draw_style_box(fg, Rect2(Point2(p, 0), Size2(fg->get_minimum_size().width, get_size().height)));
71-
} else {
72-
draw_style_box(fg, Rect2(Point2(0, 0), Size2(p + fg->get_minimum_size().width, get_size().height)));
73-
}
67+
68+
switch (mode) {
69+
case FILL_BEGIN_TO_END:
70+
case FILL_END_TO_BEGIN: {
71+
int mp = fg->get_minimum_size().width;
72+
int p = round(r * (get_size().width - mp));
73+
// We want FILL_BEGIN_TO_END to map to right to left when UI layout is RTL,
74+
// and left to right otherwise. And likewise for FILL_END_TO_BEGIN.
75+
bool right_to_left = is_layout_rtl() ? (mode == FILL_BEGIN_TO_END) : (mode == FILL_END_TO_BEGIN);
76+
if (p > 0) {
77+
if (right_to_left) {
78+
int p_remaining = round((1.0 - r) * (get_size().width - mp));
79+
draw_style_box(fg, Rect2(Point2(p_remaining, 0), Size2(p + fg->get_minimum_size().width, get_size().height)));
80+
} else {
81+
draw_style_box(fg, Rect2(Point2(0, 0), Size2(p + fg->get_minimum_size().width, get_size().height)));
82+
}
83+
}
84+
} break;
85+
case FILL_TOP_TO_BOTTOM:
86+
case FILL_BOTTOM_TO_TOP: {
87+
int mp = fg->get_minimum_size().height;
88+
int p = round(r * (get_size().height - mp));
89+
90+
if (p > 0) {
91+
if (mode == FILL_TOP_TO_BOTTOM) {
92+
draw_style_box(fg, Rect2(Point2(0, 0), Size2(get_size().width, p + fg->get_minimum_size().height)));
93+
} else {
94+
int p_remaining = round((1.0 - r) * (get_size().height - mp));
95+
draw_style_box(fg, Rect2(Point2(0, p_remaining), Size2(get_size().width, p + fg->get_minimum_size().height)));
96+
}
97+
}
98+
} break;
99+
case FILL_MODE_MAX:
100+
break;
74101
}
75102

76103
if (percent_visible) {
@@ -88,6 +115,16 @@ void ProgressBar::_notification(int p_what) {
88115
}
89116
}
90117

118+
void ProgressBar::set_fill_mode(int p_fill) {
119+
ERR_FAIL_INDEX(p_fill, FILL_MODE_MAX);
120+
mode = (FillMode)p_fill;
121+
update();
122+
}
123+
124+
int ProgressBar::get_fill_mode() {
125+
return mode;
126+
}
127+
91128
void ProgressBar::set_percent_visible(bool p_visible) {
92129
percent_visible = p_visible;
93130
update();
@@ -98,10 +135,18 @@ bool ProgressBar::is_percent_visible() const {
98135
}
99136

100137
void ProgressBar::_bind_methods() {
138+
ClassDB::bind_method(D_METHOD("set_fill_mode", "mode"), &ProgressBar::set_fill_mode);
139+
ClassDB::bind_method(D_METHOD("get_fill_mode"), &ProgressBar::get_fill_mode);
101140
ClassDB::bind_method(D_METHOD("set_percent_visible", "visible"), &ProgressBar::set_percent_visible);
102141
ClassDB::bind_method(D_METHOD("is_percent_visible"), &ProgressBar::is_percent_visible);
103142

143+
ADD_PROPERTY(PropertyInfo(Variant::INT, "fill_mode", PROPERTY_HINT_ENUM, "Begin to End,End to Begin,Top to Bottom,Bottom to Top"), "set_fill_mode", "get_fill_mode");
104144
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "percent_visible"), "set_percent_visible", "is_percent_visible");
145+
146+
BIND_ENUM_CONSTANT(FILL_BEGIN_TO_END);
147+
BIND_ENUM_CONSTANT(FILL_END_TO_BEGIN);
148+
BIND_ENUM_CONSTANT(FILL_TOP_TO_BOTTOM);
149+
BIND_ENUM_CONSTANT(FILL_BOTTOM_TO_TOP);
105150
}
106151

107152
ProgressBar::ProgressBar() {

scene/gui/progress_bar.h

+16
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,27 @@ class ProgressBar : public Range {
4343
static void _bind_methods();
4444

4545
public:
46+
enum FillMode {
47+
FILL_BEGIN_TO_END,
48+
FILL_END_TO_BEGIN,
49+
FILL_TOP_TO_BOTTOM,
50+
FILL_BOTTOM_TO_TOP,
51+
FILL_MODE_MAX
52+
};
53+
54+
void set_fill_mode(int p_fill);
55+
int get_fill_mode();
56+
4657
void set_percent_visible(bool p_visible);
4758
bool is_percent_visible() const;
4859

4960
Size2 get_minimum_size() const override;
5061
ProgressBar();
62+
63+
private:
64+
FillMode mode = FILL_BEGIN_TO_END;
5165
};
5266

67+
VARIANT_ENUM_CAST(ProgressBar::FillMode);
68+
5369
#endif // PROGRESS_BAR_H

0 commit comments

Comments
 (0)