Support for shadow offset in box style

This commit is contained in:
PouleyKetchoupp 2019-03-04 21:35:29 +01:00
parent a53645e726
commit cb097283db
2 changed files with 57 additions and 25 deletions

View File

@ -511,6 +511,16 @@ int StyleBoxFlat::get_shadow_size() const {
return shadow_size;
}
void StyleBoxFlat::set_shadow_offset(const Point2 &p_offset) {
shadow_offset = p_offset;
emit_changed();
}
Point2 StyleBoxFlat::get_shadow_offset() const {
return shadow_offset;
}
void StyleBoxFlat::set_anti_aliased(const bool &p_anti_aliased) {
anti_aliased = p_anti_aliased;
emit_changed();
@ -565,12 +575,13 @@ inline void set_inner_corner_radius(const Rect2 style_rect, const Rect2 inner_re
}
inline void draw_ring(Vector<Vector2> &verts, Vector<int> &indices, Vector<Color> &colors, const Rect2 style_rect, const int corner_radius[4],
const Rect2 ring_rect, const int border_width[4], const Color inner_color[4], const Color outer_color[4], const int corner_detail) {
const Rect2 ring_rect, const int border_width[4], const Color inner_color[4], const Color outer_color[4], const int corner_detail, const bool fill_center = false) {
int vert_offset = verts.size();
if (!vert_offset) {
vert_offset = 0;
}
int adapted_corner_detail = (corner_radius[0] == 0 && corner_radius[1] == 0 && corner_radius[2] == 0 && corner_radius[3] == 0) ? 1 : corner_detail;
int ring_corner_radius[4];
@ -585,10 +596,11 @@ inline void draw_ring(Vector<Vector2> &verts, Vector<int> &indices, Vector<Color
Rect2 inner_rect;
inner_rect = ring_rect.grow_individual(-border_width[MARGIN_LEFT], -border_width[MARGIN_TOP], -border_width[MARGIN_RIGHT], -border_width[MARGIN_BOTTOM]);
int inner_corner_radius[4];
set_inner_corner_radius(style_rect, inner_rect, corner_radius, inner_corner_radius);
Vector<Point2> inner_points;
set_inner_corner_radius(style_rect, inner_rect, corner_radius, inner_corner_radius);
inner_points.push_back(inner_rect.position + Vector2(inner_corner_radius[0], inner_corner_radius[0])); //tl
inner_points.push_back(Point2(inner_rect.position.x + inner_rect.size.x - inner_corner_radius[1], inner_rect.position.y + inner_corner_radius[1])); //tr
inner_points.push_back(inner_rect.position + inner_rect.size - Vector2(inner_corner_radius[2], inner_corner_radius[2])); //br
@ -618,17 +630,28 @@ inline void draw_ring(Vector<Vector2> &verts, Vector<int> &indices, Vector<Color
}
}
int vert_count = (adapted_corner_detail + 1) * 4 * 2;
int vert_count = verts.size() - vert_offset;
//fill the indices and the colors for the border
for (int i = 0; i < vert_count; i++) {
//poly 1
indices.push_back(vert_offset + ((i + 0) % vert_count));
indices.push_back(vert_offset + ((i + 2) % vert_count));
indices.push_back(vert_offset + ((i + 1) % vert_count));
//poly 2
indices.push_back(vert_offset + ((i + 1) % vert_count));
indices.push_back(vert_offset + ((i + 2) % vert_count));
indices.push_back(vert_offset + ((i + 3) % vert_count));
}
if (fill_center) {
//fill the indices and the colors for the center
for (int index = 0; index < vert_count / 2; index += 2) {
int i = index;
//poly 1
indices.push_back(vert_offset + i);
indices.push_back(vert_offset + vert_count - 4 - i);
indices.push_back(vert_offset + i + 2);
//poly 2
indices.push_back(vert_offset + i);
indices.push_back(vert_offset + vert_count - 2 - i);
indices.push_back(vert_offset + vert_count - 4 - i);
}
}
}
@ -692,13 +715,27 @@ void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
//DRAW SHADOW
if (shadow_size > 0) {
int shadow_width[4] = { shadow_size, shadow_size, shadow_size, shadow_size };
Rect2 shadow_inner_rect = style_rect;
shadow_inner_rect.position += shadow_offset;
Rect2 shadow_rect = style_rect.grow(shadow_size);
shadow_rect.position += shadow_offset;
Color shadow_colors[4] = { shadow_color, shadow_color, shadow_color, shadow_color };
Color shadow_colors_transparent[4];
for (int i = 0; i < 4; i++) {
shadow_colors_transparent[i] = Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
}
draw_ring(verts, indices, colors, style_rect, adapted_corner,
style_rect.grow(shadow_size), shadow_width, shadow_colors, shadow_colors_transparent, corner_detail);
draw_ring(verts, indices, colors, shadow_inner_rect, adapted_corner,
shadow_rect, shadow_width, shadow_colors, shadow_colors_transparent, corner_detail);
if (draw_center) {
int no_border[4] = { 0, 0, 0, 0 };
draw_ring(verts, indices, colors, shadow_inner_rect, adapted_corner,
shadow_inner_rect, no_border, shadow_colors, shadow_colors, corner_detail, true);
}
}
//DRAW border
@ -709,23 +746,9 @@ void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
//DRAW INFILL
if (draw_center) {
int temp_vert_offset = verts.size();
int no_border[4] = { 0, 0, 0, 0 };
draw_ring(verts, indices, colors, style_rect, adapted_corner,
infill_rect, no_border, &bg_color, &bg_color, corner_detail);
int added_vert_count = verts.size() - temp_vert_offset;
//fill the indices and the colors for the center
for (int index = 0; index <= added_vert_count / 2; index += 2) {
int i = index;
//poly 1
indices.push_back(temp_vert_offset + i);
indices.push_back(temp_vert_offset + added_vert_count - 4 - i);
indices.push_back(temp_vert_offset + i + 2);
//poly 1
indices.push_back(temp_vert_offset + i);
indices.push_back(temp_vert_offset + added_vert_count - 2 - i);
indices.push_back(temp_vert_offset + added_vert_count - 4 - i);
}
infill_rect, no_border, &bg_color, &bg_color, corner_detail, true);
}
if (aa_on) {
@ -802,6 +825,9 @@ void StyleBoxFlat::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_shadow_size", "size"), &StyleBoxFlat::set_shadow_size);
ClassDB::bind_method(D_METHOD("get_shadow_size"), &StyleBoxFlat::get_shadow_size);
ClassDB::bind_method(D_METHOD("set_shadow_offset", "offset"), &StyleBoxFlat::set_shadow_offset);
ClassDB::bind_method(D_METHOD("get_shadow_offset"), &StyleBoxFlat::get_shadow_offset);
ClassDB::bind_method(D_METHOD("set_anti_aliased", "anti_aliased"), &StyleBoxFlat::set_anti_aliased);
ClassDB::bind_method(D_METHOD("is_anti_aliased"), &StyleBoxFlat::is_anti_aliased);
@ -843,6 +869,7 @@ void StyleBoxFlat::_bind_methods() {
ADD_GROUP("Shadow", "shadow_");
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color");
ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_size"), "set_shadow_size", "get_shadow_size");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "shadow_offset"), "set_shadow_offset", "get_shadow_offset");
ADD_GROUP("Anti Aliasing", "anti_aliasing_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "anti_aliasing"), "set_anti_aliased", "is_anti_aliased");
@ -864,6 +891,7 @@ StyleBoxFlat::StyleBoxFlat() {
anti_aliased = true;
shadow_size = 0;
shadow_offset = Point2(0, 0);
corner_detail = 8;
aa_size = 1;

View File

@ -161,6 +161,7 @@ class StyleBoxFlat : public StyleBox {
int corner_detail;
int shadow_size;
Point2 shadow_offset;
int aa_size;
protected:
@ -218,6 +219,9 @@ public:
void set_shadow_size(const int &p_size);
int get_shadow_size() const;
void set_shadow_offset(const Point2 &p_offset);
Point2 get_shadow_offset() const;
//ANTI_ALIASING
void set_anti_aliased(const bool &p_anti_aliased);
bool is_anti_aliased() const;