Skip to content

Commit

Permalink
Add debug_canvas_item_get_local_bound() function to VisualServer
Browse files Browse the repository at this point in the history
Useful for debugging hierarchical culling.
  • Loading branch information
lawnjelly committed Jul 31, 2023
1 parent ac5d7dc commit 4812992
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 4 deletions.
8 changes: 8 additions & 0 deletions doc/classes/VisualServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,14 @@
Modulates all colors in the given canvas.
</description>
</method>
<method name="debug_canvas_item_get_local_bound">
<return type="Rect2" />
<argument index="0" name="item" type="RID" />
<description>
Returns the bounding rectangle for a canvas item and its descendants in local space, as calculated by the renderer. This bound is used internally for culling.
[b]Warning:[/b] This function is intended for debugging in the editor, and will pass through and return a zero [Rect2] in exported projects.
</description>
</method>
<method name="debug_canvas_item_get_rect">
<return type="Rect2" />
<argument index="0" name="item" type="RID" />
Expand Down
6 changes: 6 additions & 0 deletions servers/visual/visual_server_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,12 @@ Rect2 VisualServerCanvas::_debug_canvas_item_get_rect(RID p_item) {
return canvas_item->get_rect();
}

Rect2 VisualServerCanvas::_debug_canvas_item_get_local_bound(RID p_item) {
Item *canvas_item = canvas_item_owner.getornull(p_item);
ERR_FAIL_COND_V(!canvas_item, Rect2());
return canvas_item->local_bound;
}

void VisualServerCanvas::canvas_item_set_skeleton_relative_xform(RID p_item, Transform2D p_relative_xform) {
Item *canvas_item = canvas_item_owner.getornull(p_item);
ERR_FAIL_COND(!canvas_item);
Expand Down
1 change: 1 addition & 0 deletions servers/visual/visual_server_canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ class VisualServerCanvas {
void canvas_item_attach_skeleton(RID p_item, RID p_skeleton);
void canvas_item_set_skeleton_relative_xform(RID p_item, Transform2D p_relative_xform);
Rect2 _debug_canvas_item_get_rect(RID p_item);
Rect2 _debug_canvas_item_get_local_bound(RID p_item);
void _canvas_item_skeleton_moved(RID p_item);

RID canvas_light_create();
Expand Down
1 change: 1 addition & 0 deletions servers/visual/visual_server_raster.h
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ class VisualServerRaster : public VisualServer {
BIND2(canvas_item_attach_skeleton, RID, RID)
BIND2(canvas_item_set_skeleton_relative_xform, RID, Transform2D)
BIND1R(Rect2, _debug_canvas_item_get_rect, RID)
BIND1R(Rect2, _debug_canvas_item_get_local_bound, RID)

BIND1(canvas_item_clear, RID)
BIND2(canvas_item_set_draw_index, RID, int)
Expand Down
1 change: 1 addition & 0 deletions servers/visual/visual_server_wrap_mt.h
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ class VisualServerWrapMT : public VisualServer {
FUNC2(canvas_item_attach_skeleton, RID, RID)
FUNC2(canvas_item_set_skeleton_relative_xform, RID, Transform2D)
FUNC1R(Rect2, _debug_canvas_item_get_rect, RID)
FUNC1R(Rect2, _debug_canvas_item_get_local_bound, RID)

FUNC1(canvas_item_clear, RID)
FUNC2(canvas_item_set_draw_index, RID, int)
Expand Down
1 change: 1 addition & 0 deletions servers/visual_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2203,6 +2203,7 @@ void VisualServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("canvas_item_set_material", "item", "material"), &VisualServer::canvas_item_set_material);
ClassDB::bind_method(D_METHOD("canvas_item_set_use_parent_material", "item", "enabled"), &VisualServer::canvas_item_set_use_parent_material);
ClassDB::bind_method(D_METHOD("debug_canvas_item_get_rect", "item"), &VisualServer::debug_canvas_item_get_rect);
ClassDB::bind_method(D_METHOD("debug_canvas_item_get_local_bound", "item"), &VisualServer::debug_canvas_item_get_local_bound);
ClassDB::bind_method(D_METHOD("canvas_light_create"), &VisualServer::canvas_light_create);
ClassDB::bind_method(D_METHOD("canvas_light_attach_to_canvas", "light", "canvas"), &VisualServer::canvas_light_attach_to_canvas);
ClassDB::bind_method(D_METHOD("canvas_light_set_enabled", "light", "enabled"), &VisualServer::canvas_light_set_enabled);
Expand Down
9 changes: 5 additions & 4 deletions servers/visual_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -1056,14 +1056,15 @@ class VisualServer : public Object {
virtual void canvas_item_attach_skeleton(RID p_item, RID p_skeleton) = 0;
virtual void canvas_item_set_skeleton_relative_xform(RID p_item, Transform2D p_relative_xform) = 0;

Rect2 debug_canvas_item_get_rect(RID p_item) {
#ifdef TOOLS_ENABLED
return _debug_canvas_item_get_rect(p_item);
Rect2 debug_canvas_item_get_rect(RID p_item) { return _debug_canvas_item_get_rect(p_item); }
Rect2 debug_canvas_item_get_local_bound(RID p_item) { return _debug_canvas_item_get_local_bound(p_item); }
#else
return Rect2();
Rect2 debug_canvas_item_get_rect(RID p_item) { return Rect2(); }
Rect2 debug_canvas_item_get_local_bound(RID p_item) { return Rect2(); }
#endif
}
virtual Rect2 _debug_canvas_item_get_rect(RID p_item) = 0;
virtual Rect2 _debug_canvas_item_get_local_bound(RID p_item) = 0;

virtual void canvas_item_clear(RID p_item) = 0;
virtual void canvas_item_set_draw_index(RID p_item, int p_index) = 0;
Expand Down

0 comments on commit 4812992

Please sign in to comment.