diff --git a/doc/classes/ColorRect.xml b/doc/classes/ColorRect.xml
index 35f2d50d048b..e2bd3576920b 100644
--- a/doc/classes/ColorRect.xml
+++ b/doc/classes/ColorRect.xml
@@ -5,13 +5,19 @@
Displays a rectangle filled with a solid [member color]. If you need to display the border alone, consider using [ReferenceRect] instead.
+ Displays a rectangle filled with a solid [member color] by default, but can be used as a reference box which displays only a border around its rectangle.
+ If you need more advanced functionality, [ColorRect] uses [method CanvasItem.draw_rect] for drawing rectangles, so you can extend [ColorRect] or create a new class via script.
https://godotengine.org/asset-library/asset/515
+
+ Sets the border width of the [ColorRect]. The border grows both inwards and outwards with respect to the rectangle box.
+ [b]Note:[/b] This property has no effect when [member filled] is [code]true[/code].
+
- The fill color.
+ Sets the rectangle's fill and border color.
[codeblocks]
[gdscript]
$ColorRect.color = Color(1, 0, 0, 1) # Set ColorRect's color to red.
@@ -21,5 +27,11 @@
[/csharp]
[/codeblocks]
+
+ If [code]true[/code], the [ColorRect] will only be visible while in the editor.
+
+
+ If [code]true[/code], the [ColorRect]'s area is filled with a solid [member color], otherwise only the rectangle's border is displayed.
+
diff --git a/scene/gui/color_rect.cpp b/scene/gui/color_rect.cpp
index 143662efc6f4..0ae10ca38656 100644
--- a/scene/gui/color_rect.cpp
+++ b/scene/gui/color_rect.cpp
@@ -42,17 +42,67 @@ Color ColorRect::get_color() const {
return color;
}
+void ColorRect::set_filled(bool p_filled) {
+ filled = p_filled;
+ queue_redraw();
+ notify_property_list_changed();
+}
+
+bool ColorRect::is_filled() const {
+ return filled;
+}
+
+void ColorRect::set_border_width(float p_width) {
+ border_width = p_width;
+ queue_redraw();
+}
+
+float ColorRect::get_border_width() const {
+ return border_width;
+}
+
+void ColorRect::set_editor_only(bool p_enabled) {
+ editor_only = p_enabled;
+ queue_redraw();
+}
+
+bool ColorRect::is_editor_only() const {
+ return editor_only;
+}
+
void ColorRect::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_DRAW: {
- draw_rect(Rect2(Point2(), get_size()), color);
+ if (Engine::get_singleton()->is_editor_hint() || !editor_only) {
+ draw_rect(Rect2(Point2(), get_size()), color, filled, filled ? 1.0 : border_width);
+ }
} break;
}
}
+void ColorRect::_validate_property(PropertyInfo &property) const {
+ if (property.name == "border_width") {
+ if (filled) {
+ property.usage = PROPERTY_USAGE_NO_EDITOR;
+ }
+ }
+}
+
void ColorRect::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_color", "color"), &ColorRect::set_color);
ClassDB::bind_method(D_METHOD("get_color"), &ColorRect::get_color);
+ ClassDB::bind_method(D_METHOD("set_filled", "filled"), &ColorRect::set_filled);
+ ClassDB::bind_method(D_METHOD("is_filled"), &ColorRect::is_filled);
+
+ ClassDB::bind_method(D_METHOD("set_border_width", "width"), &ColorRect::set_border_width);
+ ClassDB::bind_method(D_METHOD("get_border_width"), &ColorRect::get_border_width);
+
+ ClassDB::bind_method(D_METHOD("is_editor_only"), &ColorRect::is_editor_only);
+ ClassDB::bind_method(D_METHOD("set_editor_only", "enabled"), &ColorRect::set_editor_only);
+
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_color", "get_color");
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filled"), "set_filled", "is_filled");
+ ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "border_width", PROPERTY_HINT_RANGE, "0.0,5.0,0.1,or_greater"), "set_border_width", "get_border_width"); // Should be after `filled`.
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor_only"), "set_editor_only", "is_editor_only");
}
diff --git a/scene/gui/color_rect.h b/scene/gui/color_rect.h
index 35c8ebcaf89c..89dc2aaea03d 100644
--- a/scene/gui/color_rect.h
+++ b/scene/gui/color_rect.h
@@ -37,14 +37,27 @@ class ColorRect : public Control {
GDCLASS(ColorRect, Control);
Color color = Color(1, 1, 1);
+ bool filled = true;
+ float border_width = 1.0; // Has no effect when `filled` is `true`.
+ bool editor_only = false;
protected:
void _notification(int p_what);
+ void _validate_property(PropertyInfo &property) const;
static void _bind_methods();
public:
void set_color(const Color &p_color);
Color get_color() const;
+
+ void set_filled(bool p_filled);
+ bool is_filled() const;
+
+ void set_border_width(float p_width);
+ float get_border_width() const;
+
+ void set_editor_only(bool p_enabled);
+ bool is_editor_only() const;
};
#endif // COLOR_RECT_H