diff options
author | Danil Alexeev <danil@alexeev.xyz> | 2023-01-16 15:55:24 +0300 |
---|---|---|
committer | Danil Alexeev <danil@alexeev.xyz> | 2023-01-17 09:15:47 +0300 |
commit | c046bb8389da4c16aa99a3969d95026f0eda2177 (patch) | |
tree | 6906d734ec5f4e1ab4ffb9d091a2e7770445a86f /scene/main | |
parent | 629796c333bcc46f2aeb4399c1a5786d6b013289 (diff) |
Fix `CanvasItem.draw_rect` function with `filled = false`
Diffstat (limited to 'scene/main')
-rw-r--r-- | scene/main/canvas_item.cpp | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/scene/main/canvas_item.cpp b/scene/main/canvas_item.cpp index 9be1a6431b..73a16b2488 100644 --- a/scene/main/canvas_item.cpp +++ b/scene/main/canvas_item.cpp @@ -557,44 +557,47 @@ void CanvasItem::draw_multiline_colors(const Vector<Point2> &p_points, const Vec void CanvasItem::draw_rect(const Rect2 &p_rect, const Color &p_color, bool p_filled, real_t p_width) { ERR_FAIL_COND_MSG(!drawing, "Drawing is only allowed inside NOTIFICATION_DRAW, _draw() function or 'draw' signal."); + Rect2 rect = p_rect.abs(); + if (p_filled) { if (p_width != -1.0) { WARN_PRINT("The draw_rect() \"width\" argument has no effect when \"filled\" is \"true\"."); } - RenderingServer::get_singleton()->canvas_item_add_rect(canvas_item, p_rect, p_color); + RenderingServer::get_singleton()->canvas_item_add_rect(canvas_item, rect, p_color); + } else if (p_width >= rect.size.width || p_width >= rect.size.height) { + RenderingServer::get_singleton()->canvas_item_add_rect(canvas_item, rect.grow(0.5f * p_width), p_color); } else { // Thick lines are offset depending on their width to avoid partial overlapping. - // Thin lines don't require an offset, so don't apply one in this case - real_t offset; - if (p_width >= 0) { - offset = p_width / 2.0; - } else { - offset = 0.0; - } + // Thin lines are drawn without offset. The result may not be perfect. + real_t offset = (p_width >= 0) ? 0.5f * p_width : 0.0f; + // Top line. RenderingServer::get_singleton()->canvas_item_add_line( canvas_item, - p_rect.position + Size2(-offset, 0), - p_rect.position + Size2(p_rect.size.width + offset, 0), + rect.position + Size2(-offset, 0), + rect.position + Size2(-offset + rect.size.width, 0), p_color, p_width); + // Right line. RenderingServer::get_singleton()->canvas_item_add_line( canvas_item, - p_rect.position + Size2(p_rect.size.width, offset), - p_rect.position + Size2(p_rect.size.width, p_rect.size.height - offset), + rect.position + Size2(rect.size.width, -offset), + rect.position + Size2(rect.size.width, -offset + rect.size.height), p_color, p_width); + // Bottom line. RenderingServer::get_singleton()->canvas_item_add_line( canvas_item, - p_rect.position + Size2(p_rect.size.width + offset, p_rect.size.height), - p_rect.position + Size2(-offset, p_rect.size.height), + rect.position + Size2(offset + rect.size.width, rect.size.height), + rect.position + Size2(offset, rect.size.height), p_color, p_width); + // Left line. RenderingServer::get_singleton()->canvas_item_add_line( canvas_item, - p_rect.position + Size2(0, p_rect.size.height - offset), - p_rect.position + Size2(0, offset), + rect.position + Size2(0, offset + rect.size.height), + rect.position + Size2(0, offset), p_color, p_width); } |