Skip to content

Commit 5a556c6

Browse files
committed
Merge pull request #109118 from bruvzg/svg_nine
Add `SVGTexture`support to `NinePatchRect`, `TextureProgressBar` and `StyleBoxTexture`.
2 parents 253043d + 610c98c commit 5a556c6

File tree

7 files changed

+19
-28
lines changed

7 files changed

+19
-28
lines changed

doc/classes/SVGTexture.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
Creates a new [SVGTexture] and initializes it by allocating and setting the SVG data from string.
2020
</description>
2121
</method>
22+
<method name="get_scaled_rid" qualifiers="const">
23+
<return type="RID" />
24+
<description>
25+
Returns the [RID] of the texture rasterized to match the oversampling of the currently drawn canvas item.
26+
</description>
27+
</method>
2228
<method name="get_source" qualifiers="const">
2329
<return type="String" />
2430
<description>

scene/gui/nine_patch_rect.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void NinePatchRect::_notification(int p_what) {
4545
texture->get_rect_region(rect, src_rect, rect, src_rect);
4646

4747
RID ci = get_canvas_item();
48-
RS::get_singleton()->canvas_item_add_nine_patch(ci, rect, src_rect, texture->get_rid(), Vector2(margin[SIDE_LEFT], margin[SIDE_TOP]), Vector2(margin[SIDE_RIGHT], margin[SIDE_BOTTOM]), RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center);
48+
RS::get_singleton()->canvas_item_add_nine_patch(ci, rect, src_rect, texture->get_scaled_rid(), Vector2(margin[SIDE_LEFT], margin[SIDE_TOP]), Vector2(margin[SIDE_RIGHT], margin[SIDE_BOTTOM]), RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center);
4949
} break;
5050
}
5151
}

scene/gui/texture_progress_bar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ void TextureProgressBar::draw_nine_patch_stretched(const Ref<Texture2D> &p_textu
424424
p_texture->get_rect_region(dst_rect, src_rect, dst_rect, src_rect);
425425

426426
RID ci = get_canvas_item();
427-
RS::get_singleton()->canvas_item_add_nine_patch(ci, dst_rect, src_rect, p_texture->get_rid(), topleft, bottomright, RS::NINE_PATCH_STRETCH, RS::NINE_PATCH_STRETCH, true, p_modulate);
427+
RS::get_singleton()->canvas_item_add_nine_patch(ci, dst_rect, src_rect, p_texture->get_scaled_rid(), topleft, bottomright, RS::NINE_PATCH_STRETCH, RS::NINE_PATCH_STRETCH, true, p_modulate);
428428
}
429429

430430
void TextureProgressBar::_notification(int p_what) {

scene/resources/style_box_texture.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ void StyleBoxTexture::draw(RID p_canvas_item, const Rect2 &p_rect) const {
178178
Vector2 start_offset = Vector2(texture_margin[SIDE_LEFT], texture_margin[SIDE_TOP]);
179179
Vector2 end_offset = Vector2(texture_margin[SIDE_RIGHT], texture_margin[SIDE_BOTTOM]);
180180

181-
RenderingServer::get_singleton()->canvas_item_add_nine_patch(p_canvas_item, rect, src_rect, texture->get_rid(), start_offset, end_offset, RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center, modulate);
181+
RenderingServer::get_singleton()->canvas_item_add_nine_patch(p_canvas_item, rect, src_rect, texture->get_scaled_rid(), start_offset, end_offset, RS::NinePatchAxisMode(axis_h), RS::NinePatchAxisMode(axis_v), draw_center, modulate);
182182
}
183183

184184
void StyleBoxTexture::_bind_methods() {

scene/resources/svg_texture.cpp

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ bool SVGTexture::has_alpha() const {
273273
return true;
274274
}
275275

276-
void SVGTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
276+
RID SVGTexture::get_scaled_rid() const {
277277
double scale = 1.0;
278278
CanvasItem *ci = CanvasItem::get_current_item_drawn();
279279
if (ci) {
@@ -282,37 +282,19 @@ void SVGTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_mod
282282
scale = vp->get_oversampling();
283283
}
284284
}
285-
RID rid = _ensure_scale(scale);
285+
return _ensure_scale(scale);
286+
}
286287

287-
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, size), rid, false, p_modulate, p_transpose);
288+
void SVGTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
289+
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, size), get_scaled_rid(), false, p_modulate, p_transpose);
288290
}
289291

290292
void SVGTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
291-
double scale = 1.0;
292-
CanvasItem *ci = CanvasItem::get_current_item_drawn();
293-
if (ci) {
294-
Viewport *vp = ci->get_viewport();
295-
if (vp) {
296-
scale = vp->get_oversampling();
297-
}
298-
}
299-
RID rid = _ensure_scale(scale);
300-
301-
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, rid, p_tile, p_modulate, p_transpose);
293+
RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, get_scaled_rid(), p_tile, p_modulate, p_transpose);
302294
}
303295

304296
void SVGTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const {
305-
double scale = 1.0;
306-
CanvasItem *ci = CanvasItem::get_current_item_drawn();
307-
if (ci) {
308-
Viewport *vp = ci->get_viewport();
309-
if (vp) {
310-
scale = vp->get_oversampling();
311-
}
312-
}
313-
RID rid = _ensure_scale(scale);
314-
315-
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, rid, p_src_rect, p_modulate, p_transpose, p_clip_uv);
297+
RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, get_scaled_rid(), p_src_rect, p_modulate, p_transpose, p_clip_uv);
316298
}
317299

318300
bool SVGTexture::is_pixel_opaque(int p_x, int p_y) const {
@@ -382,6 +364,7 @@ void SVGTexture::_bind_methods() {
382364
ClassDB::bind_method(D_METHOD("set_color_map", "color_map"), &SVGTexture::set_color_map);
383365
ClassDB::bind_method(D_METHOD("get_color_map"), &SVGTexture::get_color_map);
384366
ClassDB::bind_method(D_METHOD("set_size_override", "size"), &SVGTexture::set_size_override);
367+
ClassDB::bind_method(D_METHOD("get_scaled_rid"), &SVGTexture::get_scaled_rid);
385368

386369
ADD_PROPERTY(PropertyInfo(Variant::STRING, "_source", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_STORAGE), "set_source", "get_source");
387370
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "base_scale", PROPERTY_HINT_RANGE, "0.01,10.0,0.01"), "set_base_scale", "get_base_scale");

scene/resources/svg_texture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class SVGTexture : public Texture2D {
9191
virtual RID get_rid() const override;
9292

9393
bool has_alpha() const override;
94+
virtual RID get_scaled_rid() const override;
9495
virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
9596
virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const override;
9697
virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, bool p_clip_uv = true) const override;

scene/resources/texture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class Texture2D : public Texture {
6666

6767
virtual bool has_alpha() const;
6868

69+
virtual RID get_scaled_rid() const { return get_rid(); }
6970
virtual void draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const;
7071
virtual void draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile = false, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false) const;
7172
virtual void draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate = Color(1, 1, 1), bool p_transpose = false, bool p_clip_uv = true) const;

0 commit comments

Comments
 (0)