StyleBox preview adjusted to fit all drawn content

This change allows StyleBox preview to take shadows and content margins into account to display how a whole panel would be rendered.

The preview control clips contents so that in any case it doesn't bleed on controls around.

Fixes #33801
This commit is contained in:
PouleyKetchoupp 2019-11-24 16:26:30 +01:00
parent 636bc5c32f
commit 1120de862d
3 changed files with 33 additions and 1 deletions

View File

@ -68,7 +68,14 @@ void StyleBoxPreview::_sb_changed() {
void StyleBoxPreview::_redraw() { void StyleBoxPreview::_redraw() {
if (stylebox.is_valid()) { if (stylebox.is_valid()) {
preview->draw_style_box(stylebox, preview->get_rect()); Rect2 preview_rect = preview->get_rect();
// Re-adjust preview panel to fit all drawn content
Rect2 draw_rect = stylebox->get_draw_rect(preview_rect);
preview_rect.size -= draw_rect.size - preview_rect.size;
preview_rect.position -= draw_rect.position - preview_rect.position;
preview->draw_style_box(stylebox, preview_rect);
} }
} }
@ -81,6 +88,7 @@ void StyleBoxPreview::_bind_methods() {
StyleBoxPreview::StyleBoxPreview() { StyleBoxPreview::StyleBoxPreview() {
preview = memnew(Control); preview = memnew(Control);
preview->set_custom_minimum_size(Size2(0, 150 * EDSCALE)); preview->set_custom_minimum_size(Size2(0, 150 * EDSCALE));
preview->set_clip_contents(true);
preview->connect("draw", this, "_redraw"); preview->connect("draw", this, "_redraw");
add_margin_child(TTR("Preview:"), preview); add_margin_child(TTR("Preview:"), preview);
} }

View File

@ -81,6 +81,10 @@ Size2 StyleBox::get_center_size() const {
return Size2(); return Size2();
} }
Rect2 StyleBox::get_draw_rect(const Rect2 &p_rect) const {
return p_rect;
}
void StyleBox::_bind_methods() { void StyleBox::_bind_methods() {
ClassDB::bind_method(D_METHOD("test_mask", "point", "rect"), &StyleBox::test_mask); ClassDB::bind_method(D_METHOD("test_mask", "point", "rect"), &StyleBox::test_mask);
@ -175,6 +179,10 @@ float StyleBoxTexture::get_style_margin(Margin p_margin) const {
return margin[p_margin]; 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 { void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const {
if (texture.is_null()) if (texture.is_null())
return; return;
@ -685,6 +693,19 @@ 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_a] = MIN(p_max_a, adapted_values[p_index_a]);
adapted_values[p_index_b] = MIN(p_max_b, adapted_values[p_index_b]); 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 { void StyleBoxFlat::draw(RID p_canvas_item, const Rect2 &p_rect) const {
//PREPARATIONS //PREPARATIONS

View File

@ -56,6 +56,7 @@ public:
float get_margin(Margin p_margin) const; float get_margin(Margin p_margin) const;
virtual Size2 get_center_size() const; virtual Size2 get_center_size() const;
virtual Rect2 get_draw_rect(const Rect2 &p_rect) const;
virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const = 0; virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const = 0;
CanvasItem *get_current_item_drawn() const; CanvasItem *get_current_item_drawn() const;
@ -133,6 +134,7 @@ public:
void set_modulate(const Color &p_modulate); void set_modulate(const Color &p_modulate);
Color get_modulate() const; Color get_modulate() const;
virtual Rect2 get_draw_rect(const Rect2 &p_rect) const;
virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const; virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const;
StyleBoxTexture(); StyleBoxTexture();
@ -227,6 +229,7 @@ public:
virtual Size2 get_center_size() const; virtual Size2 get_center_size() const;
virtual Rect2 get_draw_rect(const Rect2 &p_rect) const;
virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const; virtual void draw(RID p_canvas_item, const Rect2 &p_rect) const;
StyleBoxFlat(); StyleBoxFlat();