summaryrefslogtreecommitdiffstats
path: root/scene/main/canvas_item.cpp
diff options
context:
space:
mode:
authorYaohua Xiong <xiongyaohua@gmail.com>2023-11-05 12:25:29 +0800
committerRémi Verschelde <rverschelde@gmail.com>2024-05-02 10:42:24 +0200
commit1f2aa17d143da5ad39863bf87682fd3757bf33b0 (patch)
tree884dc7855bd5545df296afc97fa8e4bfd39bbb4d /scene/main/canvas_item.cpp
parentf91db3dc58f1d6a0cb63d591515183b5a45cd3ba (diff)
downloadredot-engine-1f2aa17d143da5ad39863bf87682fd3757bf33b0.tar.gz
Extend `CanvasItem::draw_circle()` with filled, width, and antialiased options
Make it possible to draw unfilled circle, like draw_rect(). Antialising is only implemented for unfilled version.
Diffstat (limited to 'scene/main/canvas_item.cpp')
-rw-r--r--scene/main/canvas_item.cpp36
1 files changed, 33 insertions, 3 deletions
diff --git a/scene/main/canvas_item.cpp b/scene/main/canvas_item.cpp
index 56aa453407..cabba0f2ed 100644
--- a/scene/main/canvas_item.cpp
+++ b/scene/main/canvas_item.cpp
@@ -29,6 +29,7 @@
/**************************************************************************/
#include "canvas_item.h"
+#include "canvas_item.compat.inc"
#include "scene/2d/canvas_group.h"
#include "scene/main/canvas_layer.h"
@@ -726,11 +727,40 @@ void CanvasItem::draw_rect(const Rect2 &p_rect, const Color &p_color, bool p_fil
}
}
-void CanvasItem::draw_circle(const Point2 &p_pos, real_t p_radius, const Color &p_color) {
+void CanvasItem::draw_circle(const Point2 &p_pos, real_t p_radius, const Color &p_color, bool p_filled, real_t p_width, bool p_antialiased) {
ERR_THREAD_GUARD;
ERR_DRAW_GUARD;
- RenderingServer::get_singleton()->canvas_item_add_circle(canvas_item, p_pos, p_radius, p_color);
+ if (p_filled) {
+ if (p_width != -1.0) {
+ WARN_PRINT("The draw_circle() \"width\" argument has no effect when \"filled\" is \"true\".");
+ }
+
+ RenderingServer::get_singleton()->canvas_item_add_circle(canvas_item, p_pos, p_radius, p_color);
+ } else if (p_width >= 2.0 * p_radius) {
+ RenderingServer::get_singleton()->canvas_item_add_circle(canvas_item, p_pos, p_radius + 0.5 * p_width, p_color);
+ } else {
+ // Tessellation count is hardcoded. Keep in sync with the same variable in `RendererCanvasCull::canvas_item_add_circle()`.
+ const int circle_segments = 64;
+
+ Vector<Vector2> points;
+ points.resize(circle_segments + 1);
+
+ Vector2 *points_ptr = points.ptrw();
+ const real_t circle_point_step = Math_TAU / circle_segments;
+
+ for (int i = 0; i < circle_segments; i++) {
+ float angle = i * circle_point_step;
+ points_ptr[i].x = Math::cos(angle) * p_radius;
+ points_ptr[i].y = Math::sin(angle) * p_radius;
+ points_ptr[i] += p_pos;
+ }
+ points_ptr[circle_segments] = points_ptr[0];
+
+ Vector<Color> colors = { p_color };
+
+ RenderingServer::get_singleton()->canvas_item_add_polyline(canvas_item, points, colors, p_width, p_antialiased);
+ }
}
void CanvasItem::draw_texture(const Ref<Texture2D> &p_texture, const Point2 &p_pos, const Color &p_modulate) {
@@ -1163,7 +1193,7 @@ void CanvasItem::_bind_methods() {
ClassDB::bind_method(D_METHOD("draw_multiline", "points", "color", "width"), &CanvasItem::draw_multiline, DEFVAL(-1.0));
ClassDB::bind_method(D_METHOD("draw_multiline_colors", "points", "colors", "width"), &CanvasItem::draw_multiline_colors, DEFVAL(-1.0));
ClassDB::bind_method(D_METHOD("draw_rect", "rect", "color", "filled", "width"), &CanvasItem::draw_rect, DEFVAL(true), DEFVAL(-1.0));
- ClassDB::bind_method(D_METHOD("draw_circle", "position", "radius", "color"), &CanvasItem::draw_circle);
+ ClassDB::bind_method(D_METHOD("draw_circle", "position", "radius", "color", "filled", "width", "antialiased"), &CanvasItem::draw_circle, DEFVAL(true), DEFVAL(-1.0), DEFVAL(false));
ClassDB::bind_method(D_METHOD("draw_texture", "texture", "position", "modulate"), &CanvasItem::draw_texture, DEFVAL(Color(1, 1, 1, 1)));
ClassDB::bind_method(D_METHOD("draw_texture_rect", "texture", "rect", "tile", "modulate", "transpose"), &CanvasItem::draw_texture_rect, DEFVAL(Color(1, 1, 1, 1)), DEFVAL(false));
ClassDB::bind_method(D_METHOD("draw_texture_rect_region", "texture", "rect", "src_rect", "modulate", "transpose", "clip_uv"), &CanvasItem::draw_texture_rect_region, DEFVAL(Color(1, 1, 1, 1)), DEFVAL(false), DEFVAL(true));