summaryrefslogtreecommitdiff
path: root/scene/resources/style_box.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/resources/style_box.cpp')
-rw-r--r--scene/resources/style_box.cpp72
1 files changed, 65 insertions, 7 deletions
diff --git a/scene/resources/style_box.cpp b/scene/resources/style_box.cpp
index c313d9f9fd..f26b57b572 100644
--- a/scene/resources/style_box.cpp
+++ b/scene/resources/style_box.cpp
@@ -40,16 +40,22 @@ bool StyleBox::test_mask(const Point2 &p_point, const Rect2 &p_rect) const {
void StyleBox::set_default_margin(Margin p_margin, float p_value) {
+ ERR_FAIL_INDEX((int)p_margin, 4);
+
margin[p_margin] = p_value;
emit_changed();
}
float StyleBox::get_default_margin(Margin p_margin) const {
+ ERR_FAIL_INDEX_V((int)p_margin, 4, 0.0);
+
return margin[p_margin];
}
float StyleBox::get_margin(Margin p_margin) const {
+ ERR_FAIL_INDEX_V((int)p_margin, 4, 0.0);
+
if (margin[p_margin] < 0)
return get_style_margin(p_margin);
else
@@ -75,6 +81,10 @@ Size2 StyleBox::get_center_size() const {
return Size2();
}
+Rect2 StyleBox::get_draw_rect(const Rect2 &p_rect) const {
+ return p_rect;
+}
+
void StyleBox::_bind_methods() {
ClassDB::bind_method(D_METHOD("test_mask", "point", "rect"), &StyleBox::test_mask);
@@ -157,14 +167,22 @@ void StyleBoxTexture::set_margin_size(Margin p_margin, float p_size) {
}
float StyleBoxTexture::get_margin_size(Margin p_margin) const {
+ ERR_FAIL_INDEX_V((int)p_margin, 4, 0.0);
+
return margin[p_margin];
}
float StyleBoxTexture::get_style_margin(Margin p_margin) const {
+ ERR_FAIL_INDEX_V((int)p_margin, 4, 0.0);
+
return margin[p_margin];
}
+Rect2 StyleBoxTexture::get_draw_rect(const Rect2 &p_rect) const {
+ return p_rect.grow_individual(expand_margin[MARGIN_LEFT], expand_margin[MARGIN_TOP], expand_margin[MARGIN_RIGHT], expand_margin[MARGIN_BOTTOM]);
+}
+
void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const {
if (texture.is_null())
return;
@@ -250,6 +268,7 @@ Rect2 StyleBoxTexture::get_region_rect() const {
void StyleBoxTexture::set_h_axis_stretch_mode(AxisStretchMode p_mode) {
+ ERR_FAIL_INDEX((int)p_mode, 3);
axis_h = p_mode;
emit_changed();
}
@@ -261,6 +280,7 @@ StyleBoxTexture::AxisStretchMode StyleBoxTexture::get_h_axis_stretch_mode() cons
void StyleBoxTexture::set_v_axis_stretch_mode(AxisStretchMode p_mode) {
+ ERR_FAIL_INDEX((int)p_mode, 3);
axis_v = p_mode;
emit_changed();
}
@@ -391,11 +411,13 @@ int StyleBoxFlat::get_border_width_min() const {
}
void StyleBoxFlat::set_border_width(Margin p_margin, int p_width) {
+ ERR_FAIL_INDEX((int)p_margin, 4);
border_width[p_margin] = p_width;
emit_changed();
}
int StyleBoxFlat::get_border_width(Margin p_margin) const {
+ ERR_FAIL_INDEX_V((int)p_margin, 4, 0);
return border_width[p_margin];
}
@@ -437,15 +459,19 @@ int StyleBoxFlat::get_corner_radius_min() const {
void StyleBoxFlat::set_corner_radius(const Corner p_corner, const int radius) {
+ ERR_FAIL_INDEX((int)p_corner, 4);
corner_radius[p_corner] = radius;
emit_changed();
}
int StyleBoxFlat::get_corner_radius(const Corner p_corner) const {
+
+ ERR_FAIL_INDEX_V((int)p_corner, 4, 0);
return corner_radius[p_corner];
}
void StyleBoxFlat::set_expand_margin_size(Margin p_expand_margin, float p_size) {
+ ERR_FAIL_INDEX((int)p_expand_margin, 4);
expand_margin[p_expand_margin] = p_size;
emit_changed();
}
@@ -468,6 +494,7 @@ void StyleBoxFlat::set_expand_margin_size_all(float p_expand_margin_size) {
float StyleBoxFlat::get_expand_margin_size(Margin p_expand_margin) const {
+ ERR_FAIL_INDEX_V((int)p_expand_margin, 4, 0.0);
return expand_margin[p_expand_margin];
}
void StyleBoxFlat::set_draw_center(bool p_enabled) {
@@ -666,19 +693,40 @@ inline void adapt_values(int p_index_a, int p_index_b, int *adapted_values, cons
adapted_values[p_index_a] = MIN(p_max_a, adapted_values[p_index_a]);
adapted_values[p_index_b] = MIN(p_max_b, adapted_values[p_index_b]);
}
+
+Rect2 StyleBoxFlat::get_draw_rect(const Rect2 &p_rect) const {
+ Rect2 draw_rect = p_rect.grow_individual(expand_margin[MARGIN_LEFT], expand_margin[MARGIN_TOP], expand_margin[MARGIN_RIGHT], expand_margin[MARGIN_BOTTOM]);
+
+ if (shadow_size > 0) {
+ Rect2 shadow_rect = draw_rect.grow(shadow_size);
+ shadow_rect.position += shadow_offset;
+ draw_rect = draw_rect.merge(shadow_rect);
+ }
+
+ return draw_rect;
+}
+
void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
//PREPARATIONS
+ bool draw_border = (border_width[0] > 0) || (border_width[1] > 0) || (border_width[2] > 0) || (border_width[3] > 0);
+ bool draw_shadow = (shadow_size > 0);
+ if (!draw_border && !draw_center && !draw_shadow) {
+ return;
+ }
+
+ Rect2 style_rect = p_rect.grow_individual(expand_margin[MARGIN_LEFT], expand_margin[MARGIN_TOP], expand_margin[MARGIN_RIGHT], expand_margin[MARGIN_BOTTOM]);
+ if (Math::is_zero_approx(style_rect.size.width) || Math::is_zero_approx(style_rect.size.height)) {
+ return;
+ }
bool rounded_corners = (corner_radius[0] > 0) || (corner_radius[1] > 0) || (corner_radius[2] > 0) || (corner_radius[3] > 0);
bool aa_on = rounded_corners && anti_aliased;
- bool draw_border = (border_width[0] > 0) || (border_width[1] > 0) || (border_width[2] > 0) || (border_width[3] > 0);
Color border_color_alpha = Color(border_color.r, border_color.g, border_color.b, 0);
bool blend_on = blend_border && draw_border;
- Rect2 style_rect = p_rect.grow_individual(expand_margin[MARGIN_LEFT], expand_margin[MARGIN_TOP], expand_margin[MARGIN_RIGHT], expand_margin[MARGIN_BOTTOM]);
Rect2 border_style_rect = style_rect;
if (aa_on && !blend_on) {
float aa_size_grow = 0.5 * ((aa_size + 1) / 2);
@@ -709,12 +757,10 @@ void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
Vector<Point2> verts;
Vector<int> indices;
Vector<Color> colors;
-
- //DRAWING
- VisualServer *vs = VisualServer::get_singleton();
+ Vector<Point2> uvs;
//DRAW SHADOW
- if (shadow_size > 0) {
+ if (draw_shadow) {
int shadow_width[4] = { shadow_size, shadow_size, shadow_size, shadow_size };
Rect2 shadow_inner_rect = style_rect;
@@ -798,10 +844,21 @@ void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
}
}
- vs->canvas_item_add_triangle_array(p_canvas_item, indices, verts, colors);
+ //COMPUTE UV COORDINATES
+ Rect2 uv_rect = style_rect.grow(aa_on ? aa_size : 0);
+ uvs.resize(verts.size());
+ for (int i = 0; i < verts.size(); i++) {
+ uvs.write[i].x = (verts[i].x - uv_rect.position.x) / uv_rect.size.width;
+ uvs.write[i].y = (verts[i].y - uv_rect.position.y) / uv_rect.size.height;
+ }
+
+ //DRAWING
+ VisualServer *vs = VisualServer::get_singleton();
+ vs->canvas_item_add_triangle_array(p_canvas_item, indices, verts, colors, uvs);
}
float StyleBoxFlat::get_style_margin(Margin p_margin) const {
+ ERR_FAIL_INDEX_V((int)p_margin, 4, 0.0);
return border_width[p_margin];
}
void StyleBoxFlat::_bind_methods() {
@@ -985,6 +1042,7 @@ void StyleBoxLine::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "vertical"), "set_vertical", "is_vertical");
}
float StyleBoxLine::get_style_margin(Margin p_margin) const {
+ ERR_FAIL_INDEX_V((int)p_margin, 4, thickness);
return thickness;
}
Size2 StyleBoxLine::get_center_size() const {